diff --git a/src/tests/system/kernel/Jamfile b/src/tests/system/kernel/Jamfile index efb39a000a..ee1602af15 100644 --- a/src/tests/system/kernel/Jamfile +++ b/src/tests/system/kernel/Jamfile @@ -87,6 +87,7 @@ SimpleTest set_area_protection_test1 : set_area_protection_test1.cpp ; SimpleTest sigsuspend_test : sigsuspend_test.cpp ; SubInclude HAIKU_TOP src tests system kernel cache ; +SubInclude HAIKU_TOP src tests system kernel fs ; #SubInclude HAIKU_TOP src tests system kernel disk_device_manager ; SubInclude HAIKU_TOP src tests system kernel device_manager ; SubInclude HAIKU_TOP src tests system kernel file_corruption ; diff --git a/src/tests/system/kernel/fs/Jamfile b/src/tests/system/kernel/fs/Jamfile new file mode 100644 index 0000000000..50b8c3600a --- /dev/null +++ b/src/tests/system/kernel/fs/Jamfile @@ -0,0 +1,19 @@ +SubDir HAIKU_TOP src tests system kernel fs ; + +UsePrivateKernelHeaders ; + +UnitTestLib libkernelfstest.so : + KernelFSTestAddon.cpp + + KPathTest.cpp + + # Kernel sources + KPath.cpp + + : [ TargetLibstdc++ ] [ TargetLibsupc++ ] +; + +# Tell Jam where to find the kernle sources +SEARCH on [ FGristFiles + KPath.cpp + ] = [ FDirName $(HAIKU_TOP) src system kernel fs ] ; diff --git a/src/tests/system/kernel/fs/KPathTest.cpp b/src/tests/system/kernel/fs/KPathTest.cpp new file mode 100644 index 0000000000..dfb788bac8 --- /dev/null +++ b/src/tests/system/kernel/fs/KPathTest.cpp @@ -0,0 +1,248 @@ +/* + * Copyright 2017, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ + + +#include "KPathTest.h" + +#include + +#include +#include + + +// Kernel stubs + + +extern "C" team_id +team_get_kernel_team_id(void) +{ + return 0; +} + + +extern "C" team_id +team_get_current_team_id(void) +{ + return 0; +} + + +extern "C" status_t +vfs_normalize_path(const char* path, char* buffer, size_t bufferSize, + bool traverseLink, bool kernel) +{ + return B_NOT_SUPPORTED; +} + + +// #pragma mark - + + +KPathTest::KPathTest() +{ +} + + +KPathTest::~KPathTest() +{ +} + + +void +KPathTest::TestSetToAndPath() +{ + KPath path; + status_t status = path.InitCheck(); +// CPPUNIT_ASSERT(status == B_NO_INIT); + + status = path.SetTo("a/b/c"); + CPPUNIT_ASSERT(status == B_OK); + CPPUNIT_ASSERT(strcmp(path.Path(), "a/b/c") == 0); + CPPUNIT_ASSERT(path.Length() == 5); + CPPUNIT_ASSERT(path.BufferSize() == B_PATH_NAME_LENGTH); + + status = path.SetPath("abc/def"); + CPPUNIT_ASSERT(status == B_OK); + CPPUNIT_ASSERT(strcmp(path.Path(), "abc/def") == 0); + CPPUNIT_ASSERT(path.Length() == 7); + CPPUNIT_ASSERT(path.BufferSize() == B_PATH_NAME_LENGTH); + + status = path.SetTo("a/b/c", false, 10); + CPPUNIT_ASSERT(status == B_OK); + CPPUNIT_ASSERT(strcmp(path.Path(), "a/b/c") == 0); + CPPUNIT_ASSERT(path.Length() == 5); + CPPUNIT_ASSERT(path.BufferSize() == 10); + + status = path.SetPath("sorry/i'm/too/long"); + CPPUNIT_ASSERT(status == B_BUFFER_OVERFLOW); + CPPUNIT_ASSERT(strcmp(path.Path(), "a/b/c") == 0); + CPPUNIT_ASSERT(path.Length() == 5); +} + + +void +KPathTest::TestLeaf() +{ + KPath path("a"); + CPPUNIT_ASSERT(strcmp(path.Path(), "a") == 0); + CPPUNIT_ASSERT(strcmp(path.Leaf(), "a") == 0); + + path.SetTo(""); + CPPUNIT_ASSERT(strcmp(path.Path(), "") == 0); + CPPUNIT_ASSERT(strcmp(path.Leaf(), "") == 0); + + path.SetTo("/"); + CPPUNIT_ASSERT(strcmp(path.Path(), "/") == 0); +// CPPUNIT_ASSERT(path.Leaf() == NULL); +// TODO: why '/'? + + path.SetTo("a/b"); + CPPUNIT_ASSERT(strcmp(path.Path(), "a/b") == 0); + CPPUNIT_ASSERT(strcmp(path.Leaf(), "b") == 0); + + path.SetTo("a/b/"); + CPPUNIT_ASSERT(strcmp(path.Path(), "a/b") == 0); + CPPUNIT_ASSERT(strcmp(path.Leaf(), "b") == 0); + + path.SetTo("/a/b//c"); + CPPUNIT_ASSERT(strcmp(path.Path(), "/a/b//c") == 0); + CPPUNIT_ASSERT(strcmp(path.Leaf(), "c") == 0); +} + + +void +KPathTest::TestReplaceLeaf() +{ + KPath path; + status_t status = path.ReplaceLeaf("x"); +// CPPUNIT_ASSERT(status == B_NO_INIT); + + path.SetTo("/a/b/c"); + CPPUNIT_ASSERT(path.Length() == 6); + + status = path.ReplaceLeaf(NULL); + CPPUNIT_ASSERT(status == B_OK); + CPPUNIT_ASSERT(path.Length() == 4); + CPPUNIT_ASSERT(strcmp(path.Path(), "/a/b") == 0); + + status = path.ReplaceLeaf(""); + CPPUNIT_ASSERT(status == B_OK); + CPPUNIT_ASSERT(path.Length() == 2); + CPPUNIT_ASSERT(strcmp(path.Path(), "/a") == 0); + + status = path.ReplaceLeaf("c"); + CPPUNIT_ASSERT(status == B_OK); + CPPUNIT_ASSERT(path.Length() == 2); + CPPUNIT_ASSERT(strcmp(path.Path(), "/c") == 0); +} + + +void +KPathTest::TestRemoveLeaf() +{ + KPath path; + bool removed = path.RemoveLeaf(); + CPPUNIT_ASSERT(!removed); + + path.SetTo("a//b/c"); + removed = path.RemoveLeaf(); + CPPUNIT_ASSERT(removed); + CPPUNIT_ASSERT(strcmp(path.Path(), "a//b") == 0); + CPPUNIT_ASSERT(path.Length() == 4); + + removed = path.RemoveLeaf(); + CPPUNIT_ASSERT(removed); + CPPUNIT_ASSERT(strcmp(path.Path(), "a") == 0); + CPPUNIT_ASSERT(path.Length() == 1); + + removed = path.RemoveLeaf(); + CPPUNIT_ASSERT(!removed); + CPPUNIT_ASSERT(strcmp(path.Path(), "a") == 0); + CPPUNIT_ASSERT(path.Length() == 1); +} + + +void +KPathTest::TestAdopt() +{ + KPath one("one", false, 10); + CPPUNIT_ASSERT(one.InitCheck() == B_OK); + CPPUNIT_ASSERT(one.BufferSize() == 10); + CPPUNIT_ASSERT(one.Length() == 3); + KPath two("two", false, 20); + CPPUNIT_ASSERT(two.InitCheck() == B_OK); + CPPUNIT_ASSERT(two.BufferSize() == 20); + + one.Adopt(two); + CPPUNIT_ASSERT(one.InitCheck() == B_OK); + CPPUNIT_ASSERT(one.BufferSize() == 20); + CPPUNIT_ASSERT(one.Length() == 3); + CPPUNIT_ASSERT(strcmp(one.Path(), "two") == 0); + CPPUNIT_ASSERT(two.Length() == 0); + CPPUNIT_ASSERT(two.BufferSize() == 0); +// CPPUNIT_ASSERT(two.InitCheck() == B_NO_INIT); +} + + +void +KPathTest::TestDetachBuffer() +{ + KPath path("test"); + CPPUNIT_ASSERT(path.InitCheck() == B_OK); + + char* buffer = path.DetachBuffer(); + CPPUNIT_ASSERT(buffer != NULL); + CPPUNIT_ASSERT(strcmp(buffer, "test") == 0); + + CPPUNIT_ASSERT(path.Path() == NULL); +// CPPUNIT_ASSERT(path.InitCheck() == B_NO_INIT); +} + + +void +KPathTest::TestNormalize() +{ + // Outside the kernel, we only test the error case. + KPath path("test/../out"); + CPPUNIT_ASSERT(path.InitCheck() == B_OK); + + status_t status = path.Normalize(true); + CPPUNIT_ASSERT(status == B_NOT_SUPPORTED); + CPPUNIT_ASSERT(path.Path() != NULL); + CPPUNIT_ASSERT(path.Path()[0] == '\0'); + CPPUNIT_ASSERT(path.Path() == path.Leaf()); +} + + +void +KPathTest::TestEquals() +{ +} + + +/*static*/ void +KPathTest::AddTests(BTestSuite& parent) +{ + CppUnit::TestSuite& suite = *new CppUnit::TestSuite("KPathTest"); + + suite.addTest(new CppUnit::TestCaller( + "KPathTest::TestSetToAndPath", &KPathTest::TestSetToAndPath)); + suite.addTest(new CppUnit::TestCaller( + "KPathTest::TestLeaf", &KPathTest::TestLeaf)); + suite.addTest(new CppUnit::TestCaller( + "KPathTest::TestReplaceLeaf", &KPathTest::TestReplaceLeaf)); + suite.addTest(new CppUnit::TestCaller( + "KPathTest::TestRemoveLeaf", &KPathTest::TestRemoveLeaf)); + suite.addTest(new CppUnit::TestCaller( + "KPathTest::TestAdopt", &KPathTest::TestAdopt)); + suite.addTest(new CppUnit::TestCaller( + "KPathTest::TestDetachBuffer", &KPathTest::TestDetachBuffer)); + suite.addTest(new CppUnit::TestCaller( + "KPathTest::TestNormalize", &KPathTest::TestNormalize)); + suite.addTest(new CppUnit::TestCaller( + "KPathTest::TestEquals", &KPathTest::TestEquals)); + + parent.addTest("KPathTest", &suite); +} diff --git a/src/tests/system/kernel/fs/KPathTest.h b/src/tests/system/kernel/fs/KPathTest.h new file mode 100644 index 0000000000..dd32bbb17d --- /dev/null +++ b/src/tests/system/kernel/fs/KPathTest.h @@ -0,0 +1,31 @@ +/* + * Copyright 2017, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ +#ifndef KPATH_TEST_H +#define KPATH_TEST_H + + +#include +#include + + +class KPathTest : public CppUnit::TestCase { +public: + KPathTest(); + virtual ~KPathTest(); + + void TestSetToAndPath(); + void TestLeaf(); + void TestReplaceLeaf(); + void TestRemoveLeaf(); + void TestAdopt(); + void TestDetachBuffer(); + void TestNormalize(); + void TestEquals(); + + static void AddTests(BTestSuite& suite); +}; + + +#endif // KPATH_TEST_H diff --git a/src/tests/system/kernel/fs/KernelFSTestAddon.cpp b/src/tests/system/kernel/fs/KernelFSTestAddon.cpp new file mode 100644 index 0000000000..9081d5e18e --- /dev/null +++ b/src/tests/system/kernel/fs/KernelFSTestAddon.cpp @@ -0,0 +1,21 @@ +/* + * Copyright 2017, Axel Dörfler, axeld@pinc-software.de. + * Distributed under the terms of the MIT License. + */ + + +#include +#include + +#include "KPathTest.h" + + +BTestSuite* +getTestSuite() +{ + BTestSuite* suite = new BTestSuite("KernelFS"); + + KPathTest::AddTests(*suite); + + return suite; +}