4 #include "stg/user_property.h"
7 #include "testsettings.h"
8 #include "testtariffs.h"
11 #include "testusers.h"
12 #include "testservices.h"
17 class AFTER_CONNECTED_NOTIFIER : public STG::PropertyNotifierBase<bool> {
19 AFTER_CONNECTED_NOTIFIER()
23 void Notify(const bool & oldValue, const bool & newValue);
25 size_t GetConnects() const { return connects; }
26 size_t GetDisconnects() const { return disconnects; }
33 class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
35 TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
37 reconnectOnTariffChange(_reconnectOnTariffChange)
40 bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
43 bool reconnectOnTariffChange;
50 struct reconnect_on_tariff_change_data {
53 typedef test_group<reconnect_on_tariff_change_data> tg;
54 tg reconnect_on_tariff_change_test_group("Reconnect on tariff change tests group");
56 typedef tg::object testobject;
60 void testobject::test<1>()
62 set_test_name("Check normal behaviour");
64 TEST_SETTINGS_LOCAL settings(false);
66 tariffs.ReadTariffs();
67 STG::Admin admin(STG::Priv(0xFFFF), {}, {});
71 TEST_SERVICES services;
72 STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
74 AFTER_CONNECTED_NOTIFIER connectionNotifier;
76 user.AddConnectedAfterNotifier(&connectionNotifier);
78 STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
79 STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
81 ips = STG::UserIPs::parse("*");
83 ensure_equals("user.connected = false", user.GetConnected(), false);
84 ensure_equals("connects = 0", connectionNotifier.GetConnects(), static_cast<size_t>(0));
85 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
87 ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
89 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
92 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
94 ensure_equals("user.connected = true", user.GetConnected(), true);
95 ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
96 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
99 ensure_equals("user.tariffName == 'test'", user.GetProperties().tariffName.ConstData(), "test");
101 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
103 ensure_equals("user.connected = true", user.GetConnected(), true);
104 ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
105 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
111 void testobject::test<2>()
113 set_test_name("Check reconnect on tariff change");
115 TEST_SETTINGS_LOCAL settings(true);
117 TEST_SETTINGS * s1 = &settings;
118 STG::Settings * s2 = &settings;
120 ensure("settings.GetReconnectOnTariffChange() == true", settings.GetReconnectOnTariffChange());
121 ensure("s1->GetReconnectOnTariffChange() == true", s1->GetReconnectOnTariffChange());
122 ensure("s2->GetReconnectOnTariffChange() == true", s2->GetReconnectOnTariffChange());
124 TEST_TARIFFS tariffs;
125 STG::Admin admin(STG::Priv(0xFFFF), {}, {});
129 TEST_SERVICES services;
130 STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
132 AFTER_CONNECTED_NOTIFIER connectionNotifier;
134 user.AddConnectedAfterNotifier(&connectionNotifier);
136 STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
137 STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
139 ips = STG::UserIPs::parse("*");
141 ensure_equals("user.connected = false", user.GetConnected(), false);
142 ensure_equals("connects = 0", connectionNotifier.GetConnects(), static_cast<size_t>(0));
143 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
145 ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
147 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
150 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
152 ensure_equals("user.connected = true", user.GetConnected(), true);
153 ensure_equals("connects = 1", connectionNotifier.GetConnects(), static_cast<size_t>(1));
154 ensure_equals("disconnects = 0", connectionNotifier.GetDisconnects(), static_cast<size_t>(0));
157 ensure_equals("user.tariffName == 'test'", user.GetProperties().tariffName.ConstData(), "test");
159 ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
161 ensure_equals("user.connected = true", user.GetConnected(), true);
162 ensure_equals("connects = 2", connectionNotifier.GetConnects(), static_cast<size_t>(2));
163 ensure_equals("disconnects = 1", connectionNotifier.GetDisconnects(), static_cast<size_t>(1));
167 void AFTER_CONNECTED_NOTIFIER::Notify(const bool & oldValue, const bool & newValue)
169 if (!oldValue && newValue)
171 if (oldValue && !newValue)