]> git.stg.codes - stg.git/blob - tests/test_reconnect_on_tariff_change.cpp
b2e2aa022ba9c4f561704a7efd32d3aa1e00dacc
[stg.git] / tests / test_reconnect_on_tariff_change.cpp
1 #include "tut/tut.hpp"
2
3 #include "stg/admin.h"
4 #include "stg/user_property.h"
5 #include "user_impl.h"
6
7 #include "testsettings.h"
8 #include "testtariffs.h"
9 #include "teststore.h"
10 #include "testauth.h"
11 #include "testusers.h"
12 #include "testservices.h"
13
14 namespace
15 {
16
17 class AFTER_CONNECTED_NOTIFIER : public STG::PropertyNotifierBase<bool> {
18 public:
19     AFTER_CONNECTED_NOTIFIER()
20         : connects(0),
21           disconnects(0)
22     {}
23     void Notify(const bool & oldValue, const bool & newValue);
24
25     size_t GetConnects() const { return connects; }
26     size_t GetDisconnects() const { return disconnects; }
27
28 private:
29     size_t connects;
30     size_t disconnects;
31 };
32
33 class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
34     public:
35         TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
36             : TEST_SETTINGS(),
37               reconnectOnTariffChange(_reconnectOnTariffChange)
38         {}
39
40         bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
41
42     private:
43         bool reconnectOnTariffChange;
44 };
45
46 }
47
48 namespace tut
49 {
50     struct reconnect_on_tariff_change_data {
51     };
52
53     typedef test_group<reconnect_on_tariff_change_data> tg;
54     tg reconnect_on_tariff_change_test_group("Reconnect on tariff change tests group");
55
56     typedef tg::object testobject;
57
58     template<>
59     template<>
60     void testobject::test<1>()
61     {
62         set_test_name("Check normal behaviour");
63
64         TEST_SETTINGS_LOCAL settings(false);
65         TEST_TARIFFS tariffs;
66         STG::Admin admin(STG::Priv(0xFFFF), {}, {});
67         TEST_STORE store;
68         TEST_AUTH auth;
69         TEST_USERS users;
70         TEST_SERVICES services;
71         STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
72
73         AFTER_CONNECTED_NOTIFIER connectionNotifier;
74
75         user.AddConnectedAfterNotifier(&connectionNotifier);
76
77         STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
78         STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
79
80         ips = STG::UserIPs::parse("*");
81
82         ensure_equals("user.connected = false", user.GetConnected(), false);
83         ensure_equals("connects = 0", connectionNotifier.GetConnects(), static_cast<size_t>(0));
84         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
85
86         ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
87
88         user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
89         user.Run();
90
91         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
92
93         ensure_equals("user.connected = true", user.GetConnected(), true);
94         ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
95         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
96
97         tariffName = "test";
98         ensure_equals("user.tariffName == 'test'", user.GetProperties().tariffName.ConstData(), "test");
99
100         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
101
102         ensure_equals("user.connected = true", user.GetConnected(), true);
103         ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
104         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
105     }
106
107
108     template<>
109     template<>
110     void testobject::test<2>()
111     {
112         set_test_name("Check reconnect on tariff change");
113
114         TEST_SETTINGS_LOCAL settings(true);
115
116         TEST_SETTINGS * s1 = &settings;
117         STG::Settings * s2 = &settings;
118
119         ensure("settings.GetReconnectOnTariffChange() == true", settings.GetReconnectOnTariffChange());
120         ensure("s1->GetReconnectOnTariffChange() == true", s1->GetReconnectOnTariffChange());
121         ensure("s2->GetReconnectOnTariffChange() == true", s2->GetReconnectOnTariffChange());
122
123         TEST_TARIFFS tariffs;
124         STG::Admin admin(STG::Priv(0xFFFF), {}, {});
125         TEST_STORE store;
126         TEST_AUTH auth;
127         TEST_USERS users;
128         TEST_SERVICES services;
129         STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
130
131         AFTER_CONNECTED_NOTIFIER connectionNotifier;
132
133         user.AddConnectedAfterNotifier(&connectionNotifier);
134
135         STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
136         STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
137
138         ips = STG::UserIPs::parse("*");
139
140         ensure_equals("user.connected = false", user.GetConnected(), false);
141         ensure_equals("connects = 0", connectionNotifier.GetConnects(), static_cast<size_t>(0));
142         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
143
144         ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
145
146         user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
147         user.Run();
148
149         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
150
151         ensure_equals("user.connected = true", user.GetConnected(), true);
152         ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
153         ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
154
155         tariffName = "test";
156         ensure_equals("user.tariffName == 'test'", user.GetProperties().tariffName.ConstData(), "test");
157
158         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
159
160         ensure_equals("user.connected = true", user.GetConnected(), true);
161         ensure_equals("connects = 2", connectionNotifier.GetConnects(), static_cast<size_t>(2));
162         ensure_equals("disconnects = 1", connectionNotifier.GetDisconnects(), static_cast<size_t>(1));
163     }
164 }
165
166 void AFTER_CONNECTED_NOTIFIER::Notify(const bool & oldValue, const bool & newValue)
167 {
168     if (!oldValue && newValue)
169         ++connects;
170     if (oldValue && !newValue)
171         ++disconnects;
172 }