[haiku-commits] haiku: hrev52234 - in src: tests/system/kernel/vm system/kernel/vm

  • From: waddlesplash <waddlesplash@xxxxxxxxx>
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Wed, 15 Aug 2018 12:14:20 -0400 (EDT)

hrev52234 adds 1 changeset to branch 'master'
old head: 78058cf6d5c26d1a66584d0033c6fff5f3fd60d5
new head: fe8b46fb0d6850a76fa2a2a4775a7534d2f202fb
overview: 
https://git.haiku-os.org/haiku/log/?qt=range&q=fe8b46fb0d68+%5E78058cf6d5c2

----------------------------------------------------------------------------

fe8b46fb0d68: kernel: vm: add unit tests for get_mount_point
  
  Change-Id: I1eb7540ffadb26acf05b695af2e7508c71ac7274
  Reviewed-on: https://review.haiku-os.org/460
  Reviewed-by: waddlesplash <waddlesplash@xxxxxxxxx>

                                  [ Kacper Kasper <kacperkasper@xxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev52234
Commit:      fe8b46fb0d6850a76fa2a2a4775a7534d2f202fb
URL:         https://git.haiku-os.org/haiku/commit/?id=fe8b46fb0d68
Author:      Kacper Kasper <kacperkasper@xxxxxxxxx>
Date:        Mon Aug 13 12:06:37 2018 UTC
Committer:   waddlesplash <waddlesplash@xxxxxxxxx>
Commit-Date: Wed Aug 15 16:14:18 2018 UTC

----------------------------------------------------------------------------

9 files changed, 355 insertions(+), 44 deletions(-)
src/system/kernel/vm/Jamfile                     |   1 +
src/system/kernel/vm/VMAnonymousCache.cpp        |  45 +---
src/system/kernel/vm/VMUtils.cpp                 |  58 +++++
src/system/kernel/vm/VMUtils.h                   |  17 ++
src/tests/system/kernel/Jamfile                  |   1 +
src/tests/system/kernel/vm/Jamfile               |  20 ++
src/tests/system/kernel/vm/KernelVMTestAddon.cpp |  13 ++
.../system/kernel/vm/VMGetMountPointTest.cpp     | 218 +++++++++++++++++++
src/tests/system/kernel/vm/VMGetMountPointTest.h |  26 +++

----------------------------------------------------------------------------

diff --git a/src/system/kernel/vm/Jamfile b/src/system/kernel/vm/Jamfile
index a646e719c4..c28480f6f2 100644
--- a/src/system/kernel/vm/Jamfile
+++ b/src/system/kernel/vm/Jamfile
@@ -24,6 +24,7 @@ KernelMergeObject kernel_vm.o :
        VMTranslationMap.cpp
        VMUserAddressSpace.cpp
        VMUserArea.cpp
+       VMUtils.cpp
 
        : $(TARGET_KERNEL_PIC_CCFLAGS)
 ;
diff --git a/src/system/kernel/vm/VMAnonymousCache.cpp 
b/src/system/kernel/vm/VMAnonymousCache.cpp
index b22cc45690..c4a223a96a 100644
--- a/src/system/kernel/vm/VMAnonymousCache.cpp
+++ b/src/system/kernel/vm/VMAnonymousCache.cpp
@@ -27,7 +27,6 @@
 #include <FindDirectory.h>
 #include <KernelExport.h>
 #include <NodeMonitor.h>
-#include <StackOrHeapArray.h>
 
 #include <arch_config.h>
 #include <boot_device.h>
@@ -57,6 +56,7 @@
 #include <vm/VMAddressSpace.h>
 
 #include "IORequest.h"
+#include "VMUtils.h"
 
 
 #if    ENABLE_SWAP_SUPPORT
@@ -1243,49 +1243,6 @@ private:
 };
 
 
-status_t
-get_mount_point(KPartition* partition, KPath* mountPoint)
-{
-       if (!mountPoint || !partition->ContainsFileSystem())
-               return B_BAD_VALUE;
-
-       int nameLength = 0;
-       const char* volumeName = partition->ContentName();
-       if (volumeName != NULL)
-               nameLength = strlen(volumeName);
-       if (nameLength == 0) {
-               volumeName = partition->Name();
-               if (volumeName != NULL)
-                       nameLength = strlen(volumeName);
-               if (nameLength == 0) {
-                       volumeName = "unnamed volume";
-                       nameLength = strlen(volumeName);
-               }
-       }
-
-       BStackOrHeapArray<char, 128> basePath(nameLength + 2);
-       if (!basePath.IsValid())
-               return B_NO_MEMORY;
-       int32 len = snprintf(basePath, nameLength + 2, "/%s", volumeName);
-       for (int32 i = 1; i < len; i++)
-               if (basePath[i] == '/')
-                       basePath[i] = '-';
-       char* path = mountPoint->LockBuffer();
-       int32 pathLen = mountPoint->BufferSize();
-       strncpy(path, basePath, pathLen);
-
-       struct stat dummy;
-       for (int i = 1; ; i++) {
-               if (stat(path, &dummy) != 0)
-                       break;
-               snprintf(path, pathLen, "%s%d", (char*)basePath, i);
-       }
-
-       mountPoint->UnlockBuffer();
-       return B_OK;
-}
-
-
 status_t
 swap_file_add(const char* path)
 {
diff --git a/src/system/kernel/vm/VMUtils.cpp b/src/system/kernel/vm/VMUtils.cpp
new file mode 100644
index 0000000000..40ccee94cb
--- /dev/null
+++ b/src/system/kernel/vm/VMUtils.cpp
@@ -0,0 +1,58 @@
+/*
+ * Copyright 2011-2012 Haiku, Inc. All rights reserved.
+ * Distributed under the terms of the MIT License.
+ *
+ * Authors:
+ *             Hamish Morrison, hamish@xxxxxxxxxxx
+ *             Alexander von Gluck IV, kallisti5@xxxxxxxxxxx
+ */
+
+
+#include "VMUtils.h"
+
+#include <stdio.h>
+
+#include <StackOrHeapArray.h>
+
+
+status_t
+get_mount_point(KPartition* partition, KPath* mountPoint)
+{
+       if (!mountPoint || !partition->ContainsFileSystem())
+               return B_BAD_VALUE;
+
+       int nameLength = 0;
+       const char* volumeName = partition->ContentName();
+       if (volumeName != NULL)
+               nameLength = strlen(volumeName);
+       if (nameLength == 0) {
+               volumeName = partition->Name();
+               if (volumeName != NULL)
+                       nameLength = strlen(volumeName);
+               if (nameLength == 0) {
+                       volumeName = "unnamed volume";
+                       nameLength = strlen(volumeName);
+               }
+       }
+
+       BStackOrHeapArray<char, 128> basePath(nameLength + 2);
+       if (!basePath.IsValid())
+               return B_NO_MEMORY;
+       int32 len = snprintf(basePath, nameLength + 2, "/%s", volumeName);
+       for (int32 i = 1; i < len; i++)
+               if (basePath[i] == '/')
+                       basePath[i] = '-';
+       char* path = mountPoint->LockBuffer();
+       int32 pathLen = mountPoint->BufferSize();
+       strncpy(path, basePath, pathLen);
+
+       struct stat dummy;
+       for (int i = 1; ; i++) {
+               if (stat(path, &dummy) != 0)
+                       break;
+               snprintf(path, pathLen, "%s%d", (char*)basePath, i);
+       }
+
+       mountPoint->UnlockBuffer();
+       return B_OK;
+}
diff --git a/src/system/kernel/vm/VMUtils.h b/src/system/kernel/vm/VMUtils.h
new file mode 100644
index 0000000000..263ec9a771
--- /dev/null
+++ b/src/system/kernel/vm/VMUtils.h
@@ -0,0 +1,17 @@
+/*
+ * Copyright 2018 Kacper Kasper <kacperkasper@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT license.
+ */
+#ifndef _KERNEL_VM_UTILS_H
+#define _KERNEL_VM_UTILS_H
+
+
+#include <disk_device_manager/KPartition.h>
+#include <fs/KPath.h>
+
+
+status_t
+get_mount_point(KPartition* partition, KPath* mountPoint);
+
+
+#endif // _KERNEL_VM_UTILS_H
diff --git a/src/tests/system/kernel/Jamfile b/src/tests/system/kernel/Jamfile
index 7698f8ab30..5666769c38 100644
--- a/src/tests/system/kernel/Jamfile
+++ b/src/tests/system/kernel/Jamfile
@@ -100,3 +100,4 @@ SubInclude HAIKU_TOP src tests system kernel slab ;
 SubInclude HAIKU_TOP src tests system kernel swap ;
 SubInclude HAIKU_TOP src tests system kernel unit ;
 SubInclude HAIKU_TOP src tests system kernel util ;
+SubInclude HAIKU_TOP src tests system kernel vm ;
diff --git a/src/tests/system/kernel/vm/Jamfile 
b/src/tests/system/kernel/vm/Jamfile
new file mode 100644
index 0000000000..57b2805ffb
--- /dev/null
+++ b/src/tests/system/kernel/vm/Jamfile
@@ -0,0 +1,20 @@
+SubDir HAIKU_TOP src tests system kernel vm ;
+
+SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src system kernel vm ] ;
+SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src system kernel fs ] ;
+
+UsePrivateHeaders [ FDirName kernel util ] ;
+UsePrivateKernelHeaders ;
+
+UnitTestLib libkernelvmtest.so
+       : KernelVMTestAddon.cpp
+         VMGetMountPointTest.cpp
+
+         # vm
+         VMUtils.cpp
+
+         # fs
+         KPath.cpp
+       : [ TargetLibstdc++ ]
+;
+
diff --git a/src/tests/system/kernel/vm/KernelVMTestAddon.cpp 
b/src/tests/system/kernel/vm/KernelVMTestAddon.cpp
new file mode 100644
index 0000000000..03cbffc20a
--- /dev/null
+++ b/src/tests/system/kernel/vm/KernelVMTestAddon.cpp
@@ -0,0 +1,13 @@
+#include <TestSuite.h>
+#include <TestSuiteAddon.h>
+
+#include "VMGetMountPointTest.h"
+
+
+BTestSuite*
+getTestSuite()
+{
+       BTestSuite *suite = new BTestSuite("KernelVM");
+       suite->addTest("VMGetMountPointTest", VMGetMountPointTest::Suite());
+       return suite;
+}
diff --git a/src/tests/system/kernel/vm/VMGetMountPointTest.cpp 
b/src/tests/system/kernel/vm/VMGetMountPointTest.cpp
new file mode 100644
index 0000000000..c0a02a61e0
--- /dev/null
+++ b/src/tests/system/kernel/vm/VMGetMountPointTest.cpp
@@ -0,0 +1,218 @@
+/*
+ * Copyright 2018 Kacper Kasper <kacperkasper@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT License.
+ */
+
+
+#include "VMGetMountPointTest.h"
+
+#include <string.h>
+
+#include <fs/KPath.h>
+
+#include <cppunit/TestCaller.h>
+#include <cppunit/TestSuite.h>
+#include <TestUtils.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;
+}
+
+struct stat;
+
+extern "C" int
+stat(const char* path, struct stat* s)
+{
+       if(strcmp(path, "/testduplicate") == 0)
+               return 0;
+       else
+               return -1;
+}
+
+namespace BPrivate {
+namespace DiskDevice {
+
+class KPartition {
+public:
+       KPartition(std::string name, std::string contentName, bool 
containsFilesystem)
+               : fName(name), fContentName(contentName), 
fContainsFilesystem(containsFilesystem)
+               {}
+
+       const char *Name() const;
+       const char *ContentName() const;
+       bool ContainsFileSystem() const;
+
+private:
+       std::string fName;
+       std::string fContentName;
+       bool fContainsFilesystem;
+};
+
+
+const char *
+KPartition::Name() const
+{
+       return fName.c_str();
+}
+
+
+const char *
+KPartition::ContentName() const
+{
+       return fContentName.c_str();
+}
+
+
+bool
+KPartition::ContainsFileSystem() const
+{
+       return fContainsFilesystem;
+}
+
+}
+}
+
+
+using BPrivate::DiskDevice::KPartition;
+
+
+status_t
+get_mount_point(KPartition* partition, KPath* mountPoint);
+
+
+//     #pragma mark -
+
+
+VMGetMountPointTest::VMGetMountPointTest(std::string name)
+       : BTestCase(name)
+{
+}
+
+#define ADD_TEST(s, cls, m) \
+       s->addTest(new CppUnit::TestCaller<cls>(#cls "::" #m, &cls::m));
+
+
+CppUnit::Test*
+VMGetMountPointTest::Suite()
+{
+       CppUnit::TestSuite *suite = new 
CppUnit::TestSuite("VMGetMountPointTest");
+
+       ADD_TEST(suite, VMGetMountPointTest, TestNullMountPointReturnsBadValue);
+       ADD_TEST(suite, VMGetMountPointTest, 
TestPartitionWithoutFilesystemReturnsBadValue);
+       ADD_TEST(suite, VMGetMountPointTest, TestPartitionContentNameUsedFirst);
+       ADD_TEST(suite, VMGetMountPointTest, TestPartitionNameUsedSecond);
+       ADD_TEST(suite, VMGetMountPointTest, 
TestPartitionWithoutAnyNameIsNotRoot);
+       ADD_TEST(suite, VMGetMountPointTest, 
TestPartitionNameWithSlashesRemoved);
+       ADD_TEST(suite, VMGetMountPointTest, TestPartitionMountPointExists);
+
+       return suite;
+}
+
+
+void
+VMGetMountPointTest::TestNullMountPointReturnsBadValue()
+{
+       status_t status = get_mount_point(NULL, NULL);
+
+       CPPUNIT_ASSERT_EQUAL(status, B_BAD_VALUE);
+}
+
+
+void
+VMGetMountPointTest::TestPartitionWithoutFilesystemReturnsBadValue()
+{
+       KPartition partition("", "", false);
+       KPath path;
+
+       status_t status = get_mount_point(&partition, &path);
+
+       CPPUNIT_ASSERT_EQUAL(status, B_BAD_VALUE);
+}
+
+
+void
+VMGetMountPointTest::TestPartitionContentNameUsedFirst()
+{
+       KPartition partition("test1", "test2", true);
+       KPath path;
+
+       status_t status = get_mount_point(&partition, &path);
+
+       CPPUNIT_ASSERT_EQUAL(status, B_OK);
+       CPPUNIT_ASSERT(strcmp(path.Path(), "/test2") == 0);
+}
+
+
+void
+VMGetMountPointTest::TestPartitionNameUsedSecond()
+{
+       KPartition partition("test1", "", true);
+       KPath path;
+
+       status_t status = get_mount_point(&partition, &path);
+
+       CPPUNIT_ASSERT_EQUAL(status, B_OK);
+       CPPUNIT_ASSERT(strcmp(path.Path(), "/test1") == 0);
+}
+
+
+void
+VMGetMountPointTest::TestPartitionWithoutAnyNameIsNotRoot()
+{
+       KPartition partition("", "", true);
+       KPath path;
+
+       status_t status = get_mount_point(&partition, &path);
+
+       CPPUNIT_ASSERT_EQUAL(status, B_OK);
+       CPPUNIT_ASSERT(strcmp(path.Path(), "/") != 0);
+}
+
+
+void
+VMGetMountPointTest::TestPartitionNameWithSlashesRemoved()
+{
+       KPartition partition("", "testing/slashes", true);
+       KPath path;
+
+       status_t status = get_mount_point(&partition, &path);
+
+       CPPUNIT_ASSERT_EQUAL(status, B_OK);
+       CPPUNIT_ASSERT(strcmp(path.Path(), "/testing/slashes") != 0);
+}
+
+
+void
+VMGetMountPointTest::TestPartitionMountPointExists()
+{
+       KPartition partition("", "testduplicate", true);
+       KPath path;
+
+       status_t status = get_mount_point(&partition, &path);
+
+       CPPUNIT_ASSERT_EQUAL(status, B_OK);
+       CPPUNIT_ASSERT(strcmp(path.Path(), "/testduplicate") != 0);
+}
+
+
diff --git a/src/tests/system/kernel/vm/VMGetMountPointTest.h 
b/src/tests/system/kernel/vm/VMGetMountPointTest.h
new file mode 100644
index 0000000000..46c99bf948
--- /dev/null
+++ b/src/tests/system/kernel/vm/VMGetMountPointTest.h
@@ -0,0 +1,26 @@
+/*
+ * Copyright 2018 Kacper Kasper <kacperkasper@xxxxxxxxx>
+ * All rights reserved. Distributed under the terms of the MIT License.
+ */
+#ifndef _VM_GET_MOUNT_POINT_TEST_H_
+#define _VM_GET_MOUNT_POINT_TEST_H_
+
+#include <TestCase.h>
+
+
+class VMGetMountPointTest : public BTestCase {
+       public:
+               VMGetMountPointTest(std::string name = "");
+
+               static CppUnit::Test *Suite();
+
+               void TestNullMountPointReturnsBadValue();
+               void TestPartitionWithoutFilesystemReturnsBadValue();
+               void TestPartitionContentNameUsedFirst();
+               void TestPartitionNameUsedSecond();
+               void TestPartitionWithoutAnyNameIsNotRoot();
+               void TestPartitionNameWithSlashesRemoved();
+               void TestPartitionMountPointExists();
+};
+
+#endif /* _VM_GET_MOUNT_POINT_TEST_H_ */


Other related posts:

  • » [haiku-commits] haiku: hrev52234 - in src: tests/system/kernel/vm system/kernel/vm - waddlesplash