]> git.lizzy.rs Git - minetest.git/blob - src/unittest/test_ban.cpp
Remove an unused method and header includes
[minetest.git] / src / unittest / test_ban.cpp
1 /*
2 Minetest
3 Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "test.h"
21
22 #include "ban.h"
23
24 class TestBan : public TestBase
25 {
26 public:
27         TestBan() { TestManager::registerTestModule(this); }
28         const char *getName() { return "TestBan"; }
29
30         void runTests(IGameDef *gamedef);
31
32 private:
33         void testCreate();
34         void testAdd();
35         void testRemove();
36         void testModificationFlag();
37         void testGetBanName();
38         void testGetBanDescription();
39
40         void reinitTestEnv();
41 };
42
43 static TestBan g_test_instance;
44
45 void TestBan::runTests(IGameDef *gamedef)
46 {
47         reinitTestEnv();
48         TEST(testCreate);
49
50         reinitTestEnv();
51         TEST(testAdd);
52
53         reinitTestEnv();
54         TEST(testRemove);
55
56         reinitTestEnv();
57         TEST(testModificationFlag);
58
59         reinitTestEnv();
60         TEST(testGetBanName);
61
62         reinitTestEnv();
63         TEST(testGetBanDescription);
64
65         // Delete leftover files
66         reinitTestEnv();
67 }
68
69 // This module is stateful due to disk writes, add helper to remove files
70 void TestBan::reinitTestEnv()
71 {
72         fs::DeleteSingleFileOrEmptyDirectory("testbm.txt");
73         fs::DeleteSingleFileOrEmptyDirectory("testbm2.txt");
74 }
75
76 void TestBan::testCreate()
77 {
78         // test save on object removal
79         {
80                 BanManager bm("testbm.txt");
81         }
82
83         UASSERT(std::ifstream("testbm.txt", std::ios::binary).is_open());
84
85         // test manual save
86         {
87                 BanManager bm("testbm2.txt");
88                 bm.save();
89                 UASSERT(std::ifstream("testbm2.txt", std::ios::binary).is_open());
90         }
91 }
92
93 void TestBan::testAdd()
94 {
95         std::string bm_test1_entry = "192.168.0.246";
96         std::string bm_test1_result = "test_username";
97
98         BanManager bm("testbm.txt");
99         bm.add(bm_test1_entry, bm_test1_result);
100
101         UASSERT(bm.getBanName(bm_test1_entry) == bm_test1_result);
102 }
103
104 void TestBan::testRemove()
105 {
106         std::string bm_test1_entry = "192.168.0.249";
107         std::string bm_test1_result = "test_username";
108
109         std::string bm_test2_entry = "192.168.0.250";
110         std::string bm_test2_result = "test_username7";
111
112         BanManager bm("testbm.txt");
113
114         // init data
115         bm.add(bm_test1_entry, bm_test1_result);
116         bm.add(bm_test2_entry, bm_test2_result);
117
118         // the test
119         bm.remove(bm_test1_entry);
120         UASSERT(bm.getBanName(bm_test1_entry).empty());
121
122         bm.remove(bm_test2_result);
123         UASSERT(bm.getBanName(bm_test2_result).empty());
124 }
125
126 void TestBan::testModificationFlag()
127 {
128         BanManager bm("testbm.txt");
129         bm.add("192.168.0.247", "test_username");
130         UASSERT(bm.isModified());
131
132         bm.remove("192.168.0.247");
133         UASSERT(bm.isModified());
134
135         // Clear the modification flag
136         bm.save();
137
138         // Test modification flag is entry was not present
139         bm.remove("test_username");
140         UASSERT(!bm.isModified());
141 }
142
143 void TestBan::testGetBanName()
144 {
145         std::string bm_test1_entry = "192.168.0.247";
146         std::string bm_test1_result = "test_username";
147
148         BanManager bm("testbm.txt");
149         bm.add(bm_test1_entry, bm_test1_result);
150
151         // Test with valid entry
152         UASSERT(bm.getBanName(bm_test1_entry) == bm_test1_result);
153
154         // Test with invalid entry
155         UASSERT(bm.getBanName("---invalid---").empty());
156 }
157
158 void TestBan::testGetBanDescription()
159 {
160         std::string bm_test1_entry = "192.168.0.247";
161         std::string bm_test1_entry2 = "test_username";
162
163         std::string bm_test1_result = "192.168.0.247|test_username";
164
165         BanManager bm("testbm.txt");
166         bm.add(bm_test1_entry, bm_test1_entry2);
167
168         UASSERT(bm.getBanDescription(bm_test1_entry) == bm_test1_result);
169         UASSERT(bm.getBanDescription(bm_test1_entry2) == bm_test1_result);
170 }