Added unit tests for KPath.

* There are a few oddities, and at least one bug.
This commit is contained in:
Axel Dörfler 2017-04-29 18:55:49 +02:00
parent 41b0595487
commit 663b800476
5 changed files with 320 additions and 0 deletions

View File

@ -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 ;

View File

@ -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 ] ;

View File

@ -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 <fs/KPath.h>
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
// 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>(
"KPathTest::TestSetToAndPath", &KPathTest::TestSetToAndPath));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestLeaf", &KPathTest::TestLeaf));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestReplaceLeaf", &KPathTest::TestReplaceLeaf));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestRemoveLeaf", &KPathTest::TestRemoveLeaf));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestAdopt", &KPathTest::TestAdopt));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestDetachBuffer", &KPathTest::TestDetachBuffer));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestNormalize", &KPathTest::TestNormalize));
suite.addTest(new CppUnit::TestCaller<KPathTest>(
"KPathTest::TestEquals", &KPathTest::TestEquals));
parent.addTest("KPathTest", &suite);
}

View File

@ -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 <TestCase.h>
#include <TestSuite.h>
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

View File

@ -0,0 +1,21 @@
/*
* Copyright 2017, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/
#include <TestSuite.h>
#include <TestSuiteAddon.h>
#include "KPathTest.h"
BTestSuite*
getTestSuite()
{
BTestSuite* suite = new BTestSuite("KernelFS");
KPathTest::AddTests(*suite);
return suite;
}