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