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