diff options
author | Loïc Blot <nerzhul@users.noreply.github.com> | 2018-01-01 18:48:52 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-01 18:48:52 +0100 |
commit | 0d6b58a772d515824afd83e8d9cfa9d2e56a1f91 (patch) | |
tree | 4e9920e88be4bb77ebae7a3dab237af231a4314e /src/unittest | |
parent | 9f6d84aee33e1eb14b542fa36f82e0cd7b29e25c (diff) | |
download | minetest-0d6b58a772d515824afd83e8d9cfa9d2e56a1f91.tar.gz minetest-0d6b58a772d515824afd83e8d9cfa9d2e56a1f91.tar.bz2 minetest-0d6b58a772d515824afd83e8d9cfa9d2e56a1f91.zip |
Add unittests on ActiveObject and BanManager class (#6866)
* Add unittests on ActiveObject and BanManager class
This also permit to fix a bug in ban manager setting bans modified when no modification occurs
Diffstat (limited to 'src/unittest')
-rw-r--r-- | src/unittest/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/unittest/test_activeobject.cpp | 60 | ||||
-rw-r--r-- | src/unittest/test_ban.cpp | 167 |
3 files changed, 229 insertions, 0 deletions
diff --git a/src/unittest/CMakeLists.txt b/src/unittest/CMakeLists.txt index 3a4450fac..f56560666 100644 --- a/src/unittest/CMakeLists.txt +++ b/src/unittest/CMakeLists.txt @@ -1,6 +1,8 @@ set (UNITTEST_SRCS ${CMAKE_CURRENT_SOURCE_DIR}/test.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test_activeobject.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_areastore.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test_ban.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_collision.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_compression.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test_connection.cpp diff --git a/src/unittest/test_activeobject.cpp b/src/unittest/test_activeobject.cpp new file mode 100644 index 000000000..84c46fd77 --- /dev/null +++ b/src/unittest/test_activeobject.cpp @@ -0,0 +1,60 @@ +/* +Minetest +Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr> + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "test.h" + +#include "activeobject.h" + +class TestActiveObject : public TestBase +{ +public: + TestActiveObject() { TestManager::registerTestModule(this); } + const char *getName() { return "TestActiveObject"; } + + void runTests(IGameDef *gamedef); + + void testAOAttributes(); +}; + +static TestActiveObject g_test_instance; + +void TestActiveObject::runTests(IGameDef *gamedef) +{ + TEST(testAOAttributes); +} + +class TestAO : public ActiveObject +{ +public: + TestAO(u16 id) : ActiveObject(id) {} + + virtual ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_TEST; } + virtual bool getCollisionBox(aabb3f *toset) const { return false; } + virtual bool getSelectionBox(aabb3f *toset) const { return false; } + virtual bool collideWithObjects() const { return false; } +}; + +void TestActiveObject::testAOAttributes() +{ + TestAO ao(44); + UASSERT(ao.getId() == 44); + + ao.setId(558); + UASSERT(ao.getId() == 558); +} diff --git a/src/unittest/test_ban.cpp b/src/unittest/test_ban.cpp new file mode 100644 index 000000000..2a95b945f --- /dev/null +++ b/src/unittest/test_ban.cpp @@ -0,0 +1,167 @@ +/* +Minetest +Copyright (C) 2018 nerzhul, Loic BLOT <loic.blot@unix-experience.fr> + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation; either version 2.1 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License along +with this program; if not, write to the Free Software Foundation, Inc., +51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ + +#include "test.h" + +#include "ban.h" + +class TestBan : public TestBase +{ +public: + TestBan() { TestManager::registerTestModule(this); } + const char *getName() { return "TestBan"; } + + void runTests(IGameDef *gamedef); + +private: + void testCreate(); + void testAdd(); + void testRemove(); + void testModificationFlag(); + void testGetBanName(); + void testGetBanDescription(); + + void reinitTestEnv(); +}; + +static TestBan g_test_instance; + +void TestBan::runTests(IGameDef *gamedef) +{ + reinitTestEnv(); + TEST(testCreate); + + reinitTestEnv(); + TEST(testAdd); + + reinitTestEnv(); + TEST(testRemove); + + reinitTestEnv(); + TEST(testModificationFlag); + + reinitTestEnv(); + TEST(testGetBanName); + + reinitTestEnv(); + TEST(testGetBanDescription); +} + +// This module is stateful due to disk writes, add helper to remove files +void TestBan::reinitTestEnv() +{ + fs::DeleteSingleFileOrEmptyDirectory("testbm.txt"); + fs::DeleteSingleFileOrEmptyDirectory("testbm2.txt"); +} + +void TestBan::testCreate() +{ + // test save on object removal + { + BanManager bm("testbm.txt"); + } + + UASSERT(std::ifstream("testbm.txt", std::ios::binary).is_open()); + + // test manual save + { + BanManager bm("testbm2.txt"); + bm.save(); + UASSERT(std::ifstream("testbm2.txt", std::ios::binary).is_open()); + } +} + +void TestBan::testAdd() +{ + std::string bm_test1_entry = "192.168.0.246"; + std::string bm_test1_result = "test_username"; + + BanManager bm("testbm.txt"); + bm.add(bm_test1_entry, bm_test1_result); + + UASSERT(bm.getBanName(bm_test1_entry) == bm_test1_result); +} + +void TestBan::testRemove() +{ + std::string bm_test1_entry = "192.168.0.249"; + std::string bm_test1_result = "test_username"; + + std::string bm_test2_entry = "192.168.0.250"; + std::string bm_test2_result = "test_username7"; + + BanManager bm("testbm.txt"); + + // init data + bm.add(bm_test1_entry, bm_test1_result); + bm.add(bm_test2_entry, bm_test2_result); + + // the test + bm.remove(bm_test1_entry); + UASSERT(bm.getBanName(bm_test1_entry).empty()); + + bm.remove(bm_test2_result); + UASSERT(bm.getBanName(bm_test2_result).empty()); +} + +void TestBan::testModificationFlag() +{ + BanManager bm("testbm.txt"); + bm.add("192.168.0.247", "test_username"); + UASSERT(bm.isModified()); + + bm.remove("192.168.0.247"); + UASSERT(bm.isModified()); + + // Clear the modification flag + bm.save(); + + // Test modification flag is entry was not present + bm.remove("test_username"); + UASSERT(!bm.isModified()); +} + +void TestBan::testGetBanName() +{ + std::string bm_test1_entry = "192.168.0.247"; + std::string bm_test1_result = "test_username"; + + BanManager bm("testbm.txt"); + bm.add(bm_test1_entry, bm_test1_result); + + // Test with valid entry + UASSERT(bm.getBanName(bm_test1_entry) == bm_test1_result); + + // Test with invalid entry + UASSERT(bm.getBanName("---invalid---").empty()); +} + +void TestBan::testGetBanDescription() +{ + std::string bm_test1_entry = "192.168.0.247"; + std::string bm_test1_entry2 = "test_username"; + + std::string bm_test1_result = "192.168.0.247|test_username"; + + BanManager bm("testbm.txt"); + bm.add(bm_test1_entry, bm_test1_entry2); + + UASSERT(bm.getBanDescription(bm_test1_entry) == bm_test1_result); + UASSERT(bm.getBanDescription(bm_test1_entry2) == bm_test1_result); +} |