]> git.stg.codes - stg.git/blob - tests/test_disable_session_log.cpp
Cryptography with void* in the interfaces.
[stg.git] / tests / test_disable_session_log.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 TEST_STORE_LOCAL : public TEST_STORE {
18 public:
19     TEST_STORE_LOCAL()
20         : connects(0),
21           disconnects(0)
22     {}
23     int WriteUserConnect(const std::string & /*login*/, uint32_t /*ip*/) const override { ++connects; return 0; }
24
25     int WriteUserDisconnect(const std::string & /*login*/,
26                             const STG::DirTraff & /*up*/,
27                             const STG::DirTraff & /*down*/,
28                             const STG::DirTraff & /*sessionUp*/,
29                             const STG::DirTraff & /*sessionDown*/,
30                             double /*cash*/,
31                             double /*freeMb*/,
32                             const std::string & /*reason*/) const override { ++disconnects; return 0; }
33
34     size_t GetConnects() const { return connects; }
35     size_t GetDisconnects() const { return disconnects; }
36
37 private:
38     mutable size_t connects;
39     mutable size_t disconnects;
40 };
41
42 class TEST_SETTINGS_LOCAL : public TEST_SETTINGS {
43     public:
44         TEST_SETTINGS_LOCAL(bool _disableSessionLog)
45             : disableSessionLog(_disableSessionLog)
46         {}
47
48         bool GetDisableSessionLog() const { return disableSessionLog; }
49
50     private:
51         bool disableSessionLog;
52 };
53
54 }
55
56 namespace tut
57 {
58     struct disable_session_log_data {
59     };
60
61     typedef test_group<disable_session_log_data> tg;
62     tg disable_session_log_test_group("Disable session log tests group");
63
64     typedef tg::object testobject;
65
66     template<>
67     template<>
68     void testobject::test<1>()
69     {
70         set_test_name("Check normal behaviour");
71
72         TEST_SETTINGS_LOCAL settings(false);
73         TEST_TARIFFS tariffs;
74         STG::Admin admin(STG::Priv(0xFFFF), {}, {});
75         TEST_STORE_LOCAL store;
76         TEST_AUTH auth;
77         TEST_USERS users;
78         TEST_SERVICES services;
79         STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
80
81         STG::UserProperty<STG::UserIPs> & ips(user.GetProperties().ips);
82
83         ips = STG::UserIPs::parse("*");
84
85         ensure_equals("user.connected = false", user.GetConnected(), false);
86         ensure_equals("connects = 0", store.GetConnects(), static_cast<size_t>(0));
87         ensure_equals("disconnects = 0", store.GetDisconnects(), static_cast<size_t>(0));
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", store.GetConnects(), static_cast<size_t>(1));
96         ensure_equals("disconnects = 0", store.GetDisconnects(), static_cast<size_t>(0));
97
98         user.Unauthorize(&auth);
99         user.Run();
100
101         ensure_equals("user.authorised_by = false", user.IsAuthorizedBy(&auth), false);
102
103         ensure_equals("user.connected = false", user.GetConnected(), false);
104         ensure_equals("connects = 1", store.GetConnects(), static_cast<size_t>(1));
105         ensure_equals("disconnects = 1", store.GetDisconnects(), static_cast<size_t>(1));
106     }
107
108
109     template<>
110     template<>
111     void testobject::test<2>()
112     {
113         set_test_name("Check disabled session log");
114
115         TEST_SETTINGS_LOCAL settings(true);
116         TEST_TARIFFS tariffs;
117         STG::Admin admin(STG::Priv(0xFFFF), {}, {});
118         TEST_STORE_LOCAL store;
119         TEST_AUTH auth;
120         TEST_USERS users;
121         TEST_SERVICES services;
122         STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
123
124         STG::UserProperty<STG::UserIPs> & ips(user.GetProperties().ips);
125
126         ips = STG::UserIPs::parse("*");
127
128         ensure_equals("user.connected = false", user.GetConnected(), false);
129         ensure_equals("connects = 0", store.GetConnects(), static_cast<size_t>(0));
130         ensure_equals("disconnects = 0", store.GetDisconnects(), static_cast<size_t>(0));
131
132         user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
133         user.Run();
134
135         ensure_equals("user.authorised_by = true", user.IsAuthorizedBy(&auth), true);
136
137         ensure_equals("user.connected = true", user.GetConnected(), true);
138         ensure_equals("connects = 0", store.GetConnects(), static_cast<size_t>(0));
139         ensure_equals("disconnects = 0", store.GetDisconnects(), static_cast<size_t>(0));
140
141         user.Unauthorize(&auth);
142         user.Run();
143
144         ensure_equals("user.authorised_by = false", user.IsAuthorizedBy(&auth), false);
145
146         ensure_equals("user.connected = false", user.GetConnected(), false);
147         ensure_equals("connects = 0", store.GetConnects(), static_cast<size_t>(0));
148         ensure_equals("disconnects = 0", store.GetDisconnects(), static_cast<size_t>(0));
149     }
150 }