1 #define BOOST_TEST_MODULE STGReconnectOnTariffChange
4 #include "stg/user_property.h"
7 #include "testsettings.h"
8 #include "testtariffs.h"
11 #include "testusers.h"
12 #include "testservices.h"
14 #pragma GCC diagnostic push
15 #pragma GCC diagnostic ignored "-Wold-style-cast"
16 #pragma GCC diagnostic ignored "-Wunused-parameter"
17 #pragma GCC diagnostic ignored "-Wsign-compare"
18 #pragma GCC diagnostic ignored "-Wparentheses"
19 #include <boost/test/unit_test.hpp>
20 #pragma GCC diagnostic pop
22 volatile time_t stgTime = 0;
27 class AfterConnectedNotifier : public STG::PropertyNotifierBase<bool>
30 AfterConnectedNotifier()
35 void Notify(const bool& oldValue, const bool& newValue) override
37 if (!oldValue && newValue)
39 if (oldValue && !newValue)
43 size_t connects() const { return m_connects; }
44 size_t disconnects() const { return m_disconnects; }
51 class Settings : public TestSettings
54 Settings(bool reconnectOnTariffChange)
55 : m_reconnectOnTariffChange(reconnectOnTariffChange)
58 bool GetReconnectOnTariffChange() const { return m_reconnectOnTariffChange; }
61 bool m_reconnectOnTariffChange;
66 BOOST_AUTO_TEST_SUITE(ReconnectOnTariffChange)
68 BOOST_AUTO_TEST_CASE(NormalBehavior)
70 Settings settings(false);
72 tariffs.ReadTariffs();
73 STG::Admin admin(STG::Priv(0xFFFF), {}, {});
77 TestServices services;
78 STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
80 AfterConnectedNotifier connectionNotifier;
82 user.AddConnectedAfterNotifier(&connectionNotifier);
84 STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
85 STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
87 ips = STG::UserIPs::parse("*");
89 BOOST_CHECK_EQUAL(user.GetConnected(), false);
90 BOOST_CHECK_EQUAL(connectionNotifier.connects(), static_cast<size_t>(0));
91 BOOST_CHECK_EQUAL(connectionNotifier.disconnects(), static_cast<size_t>(0));
93 BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
95 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
98 BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), true);
100 BOOST_CHECK_EQUAL(user.GetConnected(), true);
101 BOOST_CHECK_EQUAL(connectionNotifier.connects(), static_cast<size_t>(1));
102 BOOST_CHECK_EQUAL(connectionNotifier.disconnects(), static_cast<size_t>(0));
105 BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), "test");
107 BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), true);
109 BOOST_CHECK_EQUAL(user.GetConnected(), true);
110 BOOST_CHECK_EQUAL(connectionNotifier.connects(), static_cast<size_t>(1));
111 BOOST_CHECK_EQUAL(connectionNotifier.disconnects(), static_cast<size_t>(0));
114 BOOST_AUTO_TEST_CASE(Reconnect)
116 Settings settings(true);
118 TestSettings * s1 = &settings;
119 STG::Settings * s2 = &settings;
121 BOOST_CHECK(settings.GetReconnectOnTariffChange());
122 BOOST_CHECK(s1->GetReconnectOnTariffChange());
123 BOOST_CHECK(s2->GetReconnectOnTariffChange());
126 STG::Admin admin(STG::Priv(0xFFFF), {}, {});
130 TestServices services;
131 STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
133 AfterConnectedNotifier connectionNotifier;
135 user.AddConnectedAfterNotifier(&connectionNotifier);
137 STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
138 STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
140 ips = STG::UserIPs::parse("*");
142 BOOST_CHECK_EQUAL(user.GetConnected(), false);
143 BOOST_CHECK_EQUAL(connectionNotifier.connects(), static_cast<size_t>(0));
144 BOOST_CHECK_EQUAL(connectionNotifier.disconnects(), static_cast<size_t>(0));
146 BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
148 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
151 BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), true);
153 BOOST_CHECK_EQUAL(user.GetConnected(), true);
154 BOOST_CHECK_EQUAL(connectionNotifier.connects(), static_cast<size_t>(1));
155 BOOST_CHECK_EQUAL(connectionNotifier.disconnects(), static_cast<size_t>(0));
158 BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), "test");
160 BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), true);
162 BOOST_CHECK_EQUAL(user.GetConnected(), true);
163 BOOST_CHECK_EQUAL(connectionNotifier.connects(), static_cast<size_t>(2));
164 BOOST_CHECK_EQUAL(connectionNotifier.disconnects(), static_cast<size_t>(1));
167 BOOST_AUTO_TEST_SUITE_END()