3 Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr>
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.
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.
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.
24 class TestBan : public TestBase
27 TestBan() { TestManager::registerTestModule(this); }
28 const char *getName() { return "TestBan"; }
30 void runTests(IGameDef *gamedef);
36 void testModificationFlag();
37 void testGetBanName();
38 void testGetBanDescription();
43 static TestBan g_test_instance;
45 void TestBan::runTests(IGameDef *gamedef)
57 TEST(testModificationFlag);
63 TEST(testGetBanDescription);
65 // Delete leftover files
69 // This module is stateful due to disk writes, add helper to remove files
70 void TestBan::reinitTestEnv()
72 fs::DeleteSingleFileOrEmptyDirectory("testbm.txt");
73 fs::DeleteSingleFileOrEmptyDirectory("testbm2.txt");
76 void TestBan::testCreate()
78 // test save on object removal
80 BanManager bm("testbm.txt");
83 UASSERT(std::ifstream("testbm.txt", std::ios::binary).is_open());
87 BanManager bm("testbm2.txt");
89 UASSERT(std::ifstream("testbm2.txt", std::ios::binary).is_open());
93 void TestBan::testAdd()
95 std::string bm_test1_entry = "192.168.0.246";
96 std::string bm_test1_result = "test_username";
98 BanManager bm("testbm.txt");
99 bm.add(bm_test1_entry, bm_test1_result);
101 UASSERT(bm.getBanName(bm_test1_entry) == bm_test1_result);
104 void TestBan::testRemove()
106 std::string bm_test1_entry = "192.168.0.249";
107 std::string bm_test1_result = "test_username";
109 std::string bm_test2_entry = "192.168.0.250";
110 std::string bm_test2_result = "test_username7";
112 BanManager bm("testbm.txt");
115 bm.add(bm_test1_entry, bm_test1_result);
116 bm.add(bm_test2_entry, bm_test2_result);
119 bm.remove(bm_test1_entry);
120 UASSERT(bm.getBanName(bm_test1_entry).empty());
122 bm.remove(bm_test2_result);
123 UASSERT(bm.getBanName(bm_test2_result).empty());
126 void TestBan::testModificationFlag()
128 BanManager bm("testbm.txt");
129 bm.add("192.168.0.247", "test_username");
130 UASSERT(bm.isModified());
132 bm.remove("192.168.0.247");
133 UASSERT(bm.isModified());
135 // Clear the modification flag
138 // Test modification flag is entry was not present
139 bm.remove("test_username");
140 UASSERT(!bm.isModified());
143 void TestBan::testGetBanName()
145 std::string bm_test1_entry = "192.168.0.247";
146 std::string bm_test1_result = "test_username";
148 BanManager bm("testbm.txt");
149 bm.add(bm_test1_entry, bm_test1_result);
151 // Test with valid entry
152 UASSERT(bm.getBanName(bm_test1_entry) == bm_test1_result);
154 // Test with invalid entry
155 UASSERT(bm.getBanName("---invalid---").empty());
158 void TestBan::testGetBanDescription()
160 std::string bm_test1_entry = "192.168.0.247";
161 std::string bm_test1_entry2 = "test_username";
163 std::string bm_test1_result = "192.168.0.247|test_username";
165 BanManager bm("testbm.txt");
166 bm.add(bm_test1_entry, bm_test1_entry2);
168 UASSERT(bm.getBanDescription(bm_test1_entry) == bm_test1_result);
169 UASSERT(bm.getBanDescription(bm_test1_entry2) == bm_test1_result);