1/*
2 * Copyright 2018 Kacper Kasper <kacperkasper@gmail.com>
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6
7#include "VMGetMountPointTest.h"
8
9#include <string.h>
10
11#include <fs/KPath.h>
12
13#include <cppunit/TestCaller.h>
14#include <cppunit/TestSuite.h>
15#include <TestUtils.h>
16
17
18// Kernel stubs
19
20
21extern "C" team_id
22team_get_kernel_team_id(void)
23{
24	return 0;
25}
26
27
28extern "C" team_id
29team_get_current_team_id(void)
30{
31	return 0;
32}
33
34
35extern "C" status_t
36vfs_normalize_path(const char* path, char* buffer, size_t bufferSize,
37	bool traverseLink, bool kernel)
38{
39	return B_NOT_SUPPORTED;
40}
41
42struct stat;
43
44extern "C" int
45stat(const char* path, struct stat* s)
46{
47	if(strcmp(path, "/testduplicate") == 0)
48		return 0;
49	else
50		return -1;
51}
52
53namespace BPrivate {
54namespace DiskDevice {
55
56class KPartition {
57public:
58	KPartition(std::string name, std::string contentName, bool containsFilesystem)
59		: fName(name), fContentName(contentName), fContainsFilesystem(containsFilesystem)
60		{}
61
62	const char *Name() const;
63	const char *ContentName() const;
64	bool ContainsFileSystem() const;
65
66private:
67	std::string fName;
68	std::string fContentName;
69	bool fContainsFilesystem;
70};
71
72
73const char *
74KPartition::Name() const
75{
76	return fName.c_str();
77}
78
79
80const char *
81KPartition::ContentName() const
82{
83	return fContentName.c_str();
84}
85
86
87bool
88KPartition::ContainsFileSystem() const
89{
90	return fContainsFilesystem;
91}
92
93}
94}
95
96
97using BPrivate::DiskDevice::KPartition;
98
99
100status_t
101get_mount_point(KPartition* partition, KPath* mountPoint);
102
103
104//	#pragma mark -
105
106
107VMGetMountPointTest::VMGetMountPointTest(std::string name)
108	: BTestCase(name)
109{
110}
111
112#define ADD_TEST(s, cls, m) \
113	s->addTest(new CppUnit::TestCaller<cls>(#cls "::" #m, &cls::m));
114
115
116CppUnit::Test*
117VMGetMountPointTest::Suite()
118{
119	CppUnit::TestSuite *suite = new CppUnit::TestSuite("VMGetMountPointTest");
120
121	ADD_TEST(suite, VMGetMountPointTest, TestNullMountPointReturnsBadValue);
122	ADD_TEST(suite, VMGetMountPointTest, TestPartitionWithoutFilesystemReturnsBadValue);
123	ADD_TEST(suite, VMGetMountPointTest, TestPartitionContentNameUsedFirst);
124	ADD_TEST(suite, VMGetMountPointTest, TestPartitionNameUsedSecond);
125	ADD_TEST(suite, VMGetMountPointTest, TestPartitionWithoutAnyNameIsNotRoot);
126	ADD_TEST(suite, VMGetMountPointTest, TestPartitionNameWithSlashesRemoved);
127	ADD_TEST(suite, VMGetMountPointTest, TestPartitionMountPointExists);
128
129	return suite;
130}
131
132
133void
134VMGetMountPointTest::TestNullMountPointReturnsBadValue()
135{
136	status_t status = get_mount_point(NULL, NULL);
137
138	CPPUNIT_ASSERT_EQUAL(status, B_BAD_VALUE);
139}
140
141
142void
143VMGetMountPointTest::TestPartitionWithoutFilesystemReturnsBadValue()
144{
145	KPartition partition("", "", false);
146	KPath path;
147
148	status_t status = get_mount_point(&partition, &path);
149
150	CPPUNIT_ASSERT_EQUAL(status, B_BAD_VALUE);
151}
152
153
154void
155VMGetMountPointTest::TestPartitionContentNameUsedFirst()
156{
157	KPartition partition("test1", "test2", true);
158	KPath path;
159
160	status_t status = get_mount_point(&partition, &path);
161
162	CPPUNIT_ASSERT_EQUAL(status, B_OK);
163	CPPUNIT_ASSERT(strcmp(path.Path(), "/test2") == 0);
164}
165
166
167void
168VMGetMountPointTest::TestPartitionNameUsedSecond()
169{
170	KPartition partition("test1", "", true);
171	KPath path;
172
173	status_t status = get_mount_point(&partition, &path);
174
175	CPPUNIT_ASSERT_EQUAL(status, B_OK);
176	CPPUNIT_ASSERT(strcmp(path.Path(), "/test1") == 0);
177}
178
179
180void
181VMGetMountPointTest::TestPartitionWithoutAnyNameIsNotRoot()
182{
183	KPartition partition("", "", true);
184	KPath path;
185
186	status_t status = get_mount_point(&partition, &path);
187
188	CPPUNIT_ASSERT_EQUAL(status, B_OK);
189	CPPUNIT_ASSERT(strcmp(path.Path(), "/") != 0);
190}
191
192
193void
194VMGetMountPointTest::TestPartitionNameWithSlashesRemoved()
195{
196	KPartition partition("", "testing/slashes", true);
197	KPath path;
198
199	status_t status = get_mount_point(&partition, &path);
200
201	CPPUNIT_ASSERT_EQUAL(status, B_OK);
202	CPPUNIT_ASSERT(strcmp(path.Path(), "/testing/slashes") != 0);
203}
204
205
206void
207VMGetMountPointTest::TestPartitionMountPointExists()
208{
209	KPartition partition("", "testduplicate", true);
210	KPath path;
211
212	status_t status = get_mount_point(&partition, &path);
213
214	CPPUNIT_ASSERT_EQUAL(status, B_OK);
215	CPPUNIT_ASSERT(strcmp(path.Path(), "/testduplicate") != 0);
216}
217
218
219