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;
35 void update(bool isConnect)
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);
81 STG::ScopedConnection conn = user.afterConnectedChange([&ctr](auto, auto newVal){ ctr.update(newVal); });
83 STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
84 STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
86 ips = STG::UserIPs::parse("*");
88 BOOST_CHECK_EQUAL(user.GetConnected(), false);
89 BOOST_CHECK_EQUAL(ctr.connects(), static_cast<size_t>(0));
90 BOOST_CHECK_EQUAL(ctr.disconnects(), static_cast<size_t>(0));
92 BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
94 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
97 BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), true);
99 BOOST_CHECK_EQUAL(user.GetConnected(), true);
100 BOOST_CHECK_EQUAL(ctr.connects(), static_cast<size_t>(1));
101 BOOST_CHECK_EQUAL(ctr.disconnects(), static_cast<size_t>(0));
104 BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), "test");
106 BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), true);
108 BOOST_CHECK_EQUAL(user.GetConnected(), true);
109 BOOST_CHECK_EQUAL(ctr.connects(), static_cast<size_t>(1));
110 BOOST_CHECK_EQUAL(ctr.disconnects(), static_cast<size_t>(0));
113 BOOST_AUTO_TEST_CASE(Reconnect)
115 Settings settings(true);
117 TestSettings * s1 = &settings;
118 STG::Settings * s2 = &settings;
120 BOOST_CHECK(settings.GetReconnectOnTariffChange());
121 BOOST_CHECK(s1->GetReconnectOnTariffChange());
122 BOOST_CHECK(s2->GetReconnectOnTariffChange());
125 STG::Admin admin(STG::Priv(0xFFFF), {}, {});
129 TestServices services;
130 STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
133 STG::ScopedConnection conn = user.afterConnectedChange([&ctr](auto, auto newVal){ ctr.update(newVal); });
135 STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
136 STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
138 ips = STG::UserIPs::parse("*");
140 BOOST_CHECK_EQUAL(user.GetConnected(), false);
141 BOOST_CHECK_EQUAL(ctr.connects(), static_cast<size_t>(0));
142 BOOST_CHECK_EQUAL(ctr.disconnects(), static_cast<size_t>(0));
144 BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
146 user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
149 BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), true);
151 BOOST_CHECK_EQUAL(user.GetConnected(), true);
152 BOOST_CHECK_EQUAL(ctr.connects(), static_cast<size_t>(1));
153 BOOST_CHECK_EQUAL(ctr.disconnects(), static_cast<size_t>(0));
156 BOOST_CHECK_EQUAL(user.GetProperties().tariffName.ConstData(), "test");
158 BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), true);
160 BOOST_CHECK_EQUAL(user.GetConnected(), true);
161 BOOST_CHECK_EQUAL(ctr.connects(), static_cast<size_t>(2));
162 BOOST_CHECK_EQUAL(ctr.disconnects(), static_cast<size_t>(1));
165 BOOST_AUTO_TEST_SUITE_END()