]> git.stg.codes - stg.git/blob - tests/test_disable_session_log.cpp
Update README.
[stg.git] / tests / test_disable_session_log.cpp
1 #define BOOST_TEST_MODULE STGAdminConf
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 #pragma GCC diagnostic push
15 #pragma GCC diagnostic ignored "-Wold-style-cast"
16 #pragma GCC diagnostic ignored "-Wunused-parameter"
17 #pragma GCC diagnostic ignored "-Wsign-compare"
18 #pragma GCC diagnostic ignored "-Wparentheses"
19 #include <boost/test/unit_test.hpp>
20 #pragma GCC diagnostic pop
21
22 volatile time_t stgTime = 0;
23
24 namespace
25 {
26
27 class Store : public TestStore
28 {
29     public:
30         Store()
31             : m_connects(0),
32               m_disconnects(0)
33         {}
34         int WriteUserConnect(const std::string& /*login*/, uint32_t /*ip*/) const override { ++m_connects; return 0; }
35
36         int WriteUserDisconnect(const std::string& /*login*/,
37                                 const STG::DirTraff& /*up*/,
38                                 const STG::DirTraff& /*down*/,
39                                 const STG::DirTraff& /*sessionUp*/,
40                                 const STG::DirTraff& /*sessionDown*/,
41                                 double /*cash*/,
42                                 double /*freeMb*/,
43                                 const std::string& /*reason*/) const override { ++m_disconnects; return 0; }
44
45         size_t connects() const { return m_connects; }
46         size_t disconnects() const { return m_disconnects; }
47
48     private:
49         mutable size_t m_connects;
50         mutable size_t m_disconnects;
51 };
52
53 class Settings : public TestSettings
54 {
55     public:
56         Settings(bool disableSessionLog)
57             : m_disableSessionLog(disableSessionLog)
58         {}
59
60         bool GetDisableSessionLog() const { return m_disableSessionLog; }
61
62     private:
63         bool m_disableSessionLog;
64 };
65
66 }
67
68 BOOST_AUTO_TEST_SUITE(DisableSessionLog)
69
70 BOOST_AUTO_TEST_CASE(NormalBehavior)
71 {
72     Settings settings(false);
73     TestTariffs tariffs;
74     STG::Admin admin(STG::Priv(0xFFFF), {}, {});
75     Store store;
76     TestAuth auth;
77     TestUsers users;
78     TestServices 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     BOOST_CHECK_EQUAL(user.GetConnected(), false);
86     BOOST_CHECK_EQUAL(store.connects(), static_cast<size_t>(0));
87     BOOST_CHECK_EQUAL(store.disconnects(), static_cast<size_t>(0));
88
89     user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
90     user.Run();
91
92     BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), true);
93
94     BOOST_CHECK_EQUAL(user.GetConnected(), true);
95     BOOST_CHECK_EQUAL(store.connects(), static_cast<size_t>(1));
96     BOOST_CHECK_EQUAL(store.disconnects(), static_cast<size_t>(0));
97
98     user.Unauthorize(&auth);
99     user.Run();
100
101     BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), false);
102
103     BOOST_CHECK_EQUAL(user.GetConnected(), false);
104     BOOST_CHECK_EQUAL(store.connects(), static_cast<size_t>(1));
105     BOOST_CHECK_EQUAL(store.disconnects(), static_cast<size_t>(1));
106 }
107
108 BOOST_AUTO_TEST_CASE(DisabledSessionLog)
109 {
110     Settings settings(true);
111     TestTariffs tariffs;
112     STG::Admin admin(STG::Priv(0xFFFF), {}, {});
113     Store store;
114     TestAuth auth;
115     TestUsers users;
116     TestServices services;
117     STG::UserImpl user(&settings, &store, &tariffs, &admin, &users, services);
118
119     STG::UserProperty<STG::UserIPs> & ips(user.GetProperties().ips);
120
121     ips = STG::UserIPs::parse("*");
122
123     BOOST_CHECK_EQUAL(user.GetConnected(), false);
124     BOOST_CHECK_EQUAL(store.connects(), static_cast<size_t>(0));
125     BOOST_CHECK_EQUAL(store.disconnects(), static_cast<size_t>(0));
126
127     user.Authorize(inet_strington("127.0.0.1"), 0, &auth);
128     user.Run();
129
130     BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), true);
131
132     BOOST_CHECK_EQUAL(user.GetConnected(), true);
133     BOOST_CHECK_EQUAL(store.connects(), static_cast<size_t>(0));
134     BOOST_CHECK_EQUAL(store.disconnects(), static_cast<size_t>(0));
135
136     user.Unauthorize(&auth);
137     user.Run();
138
139     BOOST_CHECK_EQUAL(user.IsAuthorizedBy(&auth), false);
140
141     BOOST_CHECK_EQUAL(user.GetConnected(), false);
142     BOOST_CHECK_EQUAL(store.connects(), static_cast<size_t>(0));
143     BOOST_CHECK_EQUAL(store.disconnects(), static_cast<size_t>(0));
144 }
145
146 BOOST_AUTO_TEST_SUITE_END()