]> git.stg.codes - stg.git/blob - tests/test_reconnect_on_tariff_change.cpp
convertor -> sgconv in docs.
[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 namespace
15 {
16
17 class AFTER_CONNECTED_NOTIFIER : public PROPERTY_NOTIFIER_BASE<bool>,
18                                  private NONCOPYABLE {
19 public:
20     AFTER_CONNECTED_NOTIFIER()
21         : connects(0),
22           disconnects(0)
23     {}
24     void Notify(const bool & oldValue, const bool & newValue);
25
26     size_t GetConnects() const { return connects; }
27     size_t GetDisconnects() const { return disconnects; }
28
29 private:
30     size_t connects;
31     size_t disconnects;
32 };
33
34 class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
35     public:
36         TEST_SETTINGS_LOCAL(bool _reconnectOnTariffChange)
37             : TEST_SETTINGS(),
38               reconnectOnTariffChange(_reconnectOnTariffChange)
39         {}
40
41         bool GetReconnectOnTariffChange() const { return reconnectOnTariffChange; }
42
43     private:
44         bool reconnectOnTariffChange;
45 };
46
47 }
48
49 namespace tut
50 {
51     struct reconnect_on_tariff_change_data {
52     };
53
54     typedef test_group<reconnect_on_tariff_change_data> tg;
55     tg reconnect_on_tariff_change_test_group("Reconnect on tariff change tests group");
56
57     typedef tg::object testobject;
58
59     template<>
60     template<>
61     void testobject::test<1>()
62     {
63         set_test_name("Check normal behaviour");
64
65         TEST_SETTINGS_LOCAL settings(false);
66         TEST_TARIFFS tariffs;
67         TEST_ADMIN admin;
68         TEST_STORE store;
69         TEST_AUTH auth;
70         TEST_USERS users;
71         TEST_SERVICES services;
72         USER_IMPL user(&settings, &store, &tariffs, &admin, &users, services);
73
74         AFTER_CONNECTED_NOTIFIER connectionNotifier;
75
76         user.AddConnectedAfterNotifier(&connectionNotifier);
77
78         USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
79         USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
80
81         ips = StrToIPS("*");
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.GetProperty().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.GetProperty().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         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         TEST_ADMIN admin;
126         TEST_STORE store;
127         TEST_AUTH auth;
128         TEST_USERS users;
129         TEST_SERVICES services;
130         USER_IMPL user(&settings, &store, &tariffs, &admin, &users, services);
131
132         AFTER_CONNECTED_NOTIFIER connectionNotifier;
133
134         user.AddConnectedAfterNotifier(&connectionNotifier);
135
136         USER_PROPERTY<std::string> & tariffName(user.GetProperty().tariffName);
137         USER_PROPERTY<USER_IPS> & ips(user.GetProperty().ips);
138
139         ips = StrToIPS("*");
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.GetProperty().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.GetProperty().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 }