]> git.stg.codes - stg.git/blob - tests/test_reconnect_on_tariff_change.cpp
Convert some tests from TUT to Boost.Test
[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         tariffs.ReadTariffs();
67         STG::Admin admin(STG::Priv(0xFFFF), {}, {});
68         TEST_STORE store;
69         TEST_AUTH auth;
70         TEST_USERS users;
71         TEST_SERVICES services;
72         STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
73
74         AFTER_CONNECTED_NOTIFIER connectionNotifier;
75
76         user.AddConnectedAfterNotifier(&connectionNotifier);
77
78         STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
79         STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
80
81         ips = STG::UserIPs::parse("*");
82
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));
86
87         ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
88
89         user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
90         user.Run();
91
92         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
93
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));
97
98         tariffName = "test";
99         ensure_equals("user.tariffName == 'test'", user.GetProperties().tariffName.ConstData(), "test");
100
101         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
102
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));
106     }
107
108
109     template<>
110     template<>
111     void testobject::test<2>()
112     {
113         set_test_name("Check reconnect on tariff change");
114
115         TEST_SETTINGS_LOCAL settings(true);
116
117         TEST_SETTINGS * s1 = &settings;
118         STG::Settings * s2 = &settings;
119
120         ensure("settings.GetReconnectOnTariffChange() == true", settings.GetReconnectOnTariffChange());
121         ensure("s1->GetReconnectOnTariffChange() == true", s1->GetReconnectOnTariffChange());
122         ensure("s2->GetReconnectOnTariffChange() == true", s2->GetReconnectOnTariffChange());
123
124         TEST_TARIFFS tariffs;
125         STG::Admin admin(STG::Priv(0xFFFF), {}, {});
126         TEST_STORE store;
127         TEST_AUTH auth;
128         TEST_USERS users;
129         TEST_SERVICES services;
130         STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
131
132         AFTER_CONNECTED_NOTIFIER connectionNotifier;
133
134         user.AddConnectedAfterNotifier(&connectionNotifier);
135
136         STG::UserProperty<std::string> & tariffName = user.GetProperties().tariffName;
137         STG::UserProperty<STG::UserIPs> & ips = user.GetProperties().ips;
138
139         ips = STG::UserIPs::parse("*");
140
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));
144
145         ensure_equals("user.tariffName == NO_TARIFF_NAME", user.GetProperties().tariffName.ConstData(), NO_TARIFF_NAME);
146
147         user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
148         user.Run();
149
150         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
151
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));
155
156         tariffName = "test";
157         ensure_equals("user.tariffName == 'test'", user.GetProperties().tariffName.ConstData(), "test");
158
159         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
160
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));
164     }
165 }
166
167 void AFTER_CONNECTED_NOTIFIER::Notify(const bool & oldValue, const bool & newValue)
168 {
169     if (!oldValue && newValue)
170         ++connects;
171     if (oldValue && !newValue)
172         ++disconnects;
173 }