xref: /haiku/src/tests/kits/storage/StatableTest.cpp (revision 51978af14a173e7fae0563b562be5603bc652aeb)
1 // StatableTest.cpp
2 
3 #include <sys/stat.h>
4 
5 #include <cppunit/TestCaller.h>
6 #include <cppunit/TestSuite.h>
7 
8 #include <Statable.h>
9 #include <Entry.h>
10 #include <Node.h>
11 #include <Volume.h>
12 
13 #include "StatableTest.h"
14 
15 // setUp
16 void
17 StatableTest::setUp()
18 {
19 	BasicTest::setUp();
20 }
21 
22 // tearDown
23 void
24 StatableTest::tearDown()
25 {
26 	BasicTest::tearDown();
27 }
28 
29 // GetStatTest
30 void
31 StatableTest::GetStatTest()
32 {
33 	TestStatables testEntries;
34 	BStatable *statable;
35 	string entryName;
36 	// existing entries
37 	NextSubTest();
38 	CreateROStatables(testEntries);
39 	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
40 		struct stat st1, st2;
41 		CPPUNIT_ASSERT( statable->GetStat(&st1) == B_OK );
42 		CPPUNIT_ASSERT( lstat(entryName.c_str(), &st2) == 0 );
43 		CPPUNIT_ASSERT( st1 == st2 );
44 	}
45 	testEntries.delete_all();
46 	// uninitialized objects
47 	NextSubTest();
48 	CreateUninitializedStatables(testEntries);
49 	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
50 		struct stat st1;
51 		CPPUNIT_ASSERT( statable->GetStat(&st1) == B_NO_INIT );
52 	}
53 	testEntries.delete_all();
54 	// bad args
55 	NextSubTest();
56 	CreateROStatables(testEntries);
57 	for (testEntries.rewind(); testEntries.getNext(statable, entryName); )
58 		CPPUNIT_ASSERT( statable->GetStat(NULL) != B_OK );
59 	testEntries.delete_all();
60 }
61 
62 // IsXYZTest
63 void
64 StatableTest::IsXYZTest()
65 {
66 	TestStatables testEntries;
67 	BStatable *statable;
68 	string entryName;
69 	// existing entries
70 	NextSubTest();
71 	CreateROStatables(testEntries);
72 	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
73 		struct stat st;
74 		CPPUNIT_ASSERT( lstat(entryName.c_str(), &st) == 0 );
75 		CPPUNIT_ASSERT( statable->IsDirectory() == S_ISDIR(st.st_mode) );
76 		CPPUNIT_ASSERT( statable->IsFile() == S_ISREG(st.st_mode) );
77 		CPPUNIT_ASSERT( statable->IsSymLink() == S_ISLNK(st.st_mode) );
78 	}
79 	testEntries.delete_all();
80 	// uninitialized objects
81 	NextSubTest();
82 	CreateUninitializedStatables(testEntries);
83 	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
84 		CPPUNIT_ASSERT( statable->IsDirectory() == false );
85 		CPPUNIT_ASSERT( statable->IsFile() == false );
86 		CPPUNIT_ASSERT( statable->IsSymLink() == false );
87 	}
88 	testEntries.delete_all();
89 }
90 
91 // GetXYZTest
92 void
93 StatableTest::GetXYZTest()
94 {
95 	TestStatables testEntries;
96 	BStatable *statable;
97 	string entryName;
98 	// test with existing entries
99 	NextSubTest();
100 	CreateROStatables(testEntries);
101 	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
102 		struct stat st;
103 		node_ref ref;
104 		uid_t owner;
105 		gid_t group;
106 		mode_t perms;
107 		off_t size;
108 		time_t mtime;
109 		time_t ctime;
110 // R5: access time unused
111 #if !TEST_R5 && !TEST_OBOS /* !!!POSIX ONLY!!! */
112 		time_t atime;
113 #endif
114 		BVolume volume;
115 		CPPUNIT_ASSERT( lstat(entryName.c_str(), &st) == 0 );
116 		CPPUNIT_ASSERT( statable->GetNodeRef(&ref) == B_OK );
117 		CPPUNIT_ASSERT( statable->GetOwner(&owner) == B_OK );
118 		CPPUNIT_ASSERT( statable->GetGroup(&group) == B_OK );
119 		CPPUNIT_ASSERT( statable->GetPermissions(&perms) == B_OK );
120 		CPPUNIT_ASSERT( statable->GetSize(&size) == B_OK );
121 		CPPUNIT_ASSERT( statable->GetModificationTime(&mtime) == B_OK );
122 		CPPUNIT_ASSERT( statable->GetCreationTime(&ctime) == B_OK );
123 #if !TEST_R5 && !TEST_OBOS /* !!!POSIX ONLY!!! */
124 		CPPUNIT_ASSERT( statable->GetAccessTime(&atime) == B_OK );
125 #endif
126 		CPPUNIT_ASSERT( statable->GetVolume(&volume) == B_OK );
127 		CPPUNIT_ASSERT( ref.device == st.st_dev && ref.node == st.st_ino );
128 		CPPUNIT_ASSERT( owner == st.st_uid );
129 		CPPUNIT_ASSERT( group == st.st_gid );
130 // R5: returns not only the permission bits, so we need to filter for the test
131 //		CPPUNIT_ASSERT( perms == (st.st_mode & S_IUMSK) );
132 		CPPUNIT_ASSERT( (perms & S_IUMSK) == (st.st_mode & S_IUMSK) );
133 		CPPUNIT_ASSERT( size == st.st_size );
134 		CPPUNIT_ASSERT( mtime == st.st_mtime );
135 		CPPUNIT_ASSERT( ctime == st.st_crtime );
136 #if !TEST_R5 && !TEST_OBOS /* !!!POSIX ONLY!!! */
137 		CPPUNIT_ASSERT( atime == st.st_atime );
138 #endif
139 // OBOS: BVolume::==() is not implemented yet
140 #if !TEST_OBOS /* !!!POSIX ONLY!!! */
141 		CPPUNIT_ASSERT( volume == BVolume(st.st_dev) );
142 #endif
143 	}
144 	testEntries.delete_all();
145 	// test with uninitialized objects
146 	NextSubTest();
147 	CreateUninitializedStatables(testEntries);
148 	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
149 		node_ref ref;
150 		uid_t owner;
151 		gid_t group;
152 		mode_t perms;
153 		off_t size;
154 		time_t mtime;
155 		time_t ctime;
156 		time_t atime;
157 		BVolume volume;
158 		CPPUNIT_ASSERT( statable->GetNodeRef(&ref) == B_NO_INIT );
159 		CPPUNIT_ASSERT( statable->GetOwner(&owner) == B_NO_INIT );
160 		CPPUNIT_ASSERT( statable->GetGroup(&group) == B_NO_INIT );
161 		CPPUNIT_ASSERT( statable->GetPermissions(&perms) == B_NO_INIT );
162 		CPPUNIT_ASSERT( statable->GetSize(&size) == B_NO_INIT );
163 		CPPUNIT_ASSERT( statable->GetModificationTime(&mtime) == B_NO_INIT );
164 		CPPUNIT_ASSERT( statable->GetCreationTime(&ctime) == B_NO_INIT );
165 		CPPUNIT_ASSERT( statable->GetAccessTime(&atime) == B_NO_INIT );
166 		CPPUNIT_ASSERT( statable->GetVolume(&volume) == B_NO_INIT );
167 	}
168 	testEntries.delete_all();
169 	// bad args
170 	NextSubTest();
171 	CreateROStatables(testEntries);
172 	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
173 // R5: crashs, if passing NULL to any of these methods
174 #if !TEST_R5
175 		CPPUNIT_ASSERT( statable->GetNodeRef(NULL) == B_BAD_VALUE );
176 		CPPUNIT_ASSERT( statable->GetOwner(NULL)  == B_BAD_VALUE );
177 		CPPUNIT_ASSERT( statable->GetGroup(NULL)  == B_BAD_VALUE );
178 		CPPUNIT_ASSERT( statable->GetPermissions(NULL)  == B_BAD_VALUE );
179 		CPPUNIT_ASSERT( statable->GetSize(NULL)  == B_BAD_VALUE );
180 		CPPUNIT_ASSERT( statable->GetModificationTime(NULL)  == B_BAD_VALUE );
181 		CPPUNIT_ASSERT( statable->GetCreationTime(NULL)  == B_BAD_VALUE );
182 		CPPUNIT_ASSERT( statable->GetAccessTime(NULL)  == B_BAD_VALUE );
183 		CPPUNIT_ASSERT( statable->GetVolume(NULL)  == B_BAD_VALUE );
184 #endif
185 	}
186 	testEntries.delete_all();
187 }
188 
189 // SetXYZTest
190 void
191 StatableTest::SetXYZTest()
192 {
193 	TestStatables testEntries;
194 	BStatable *statable;
195 	string entryName;
196 	// test with existing entries
197 	NextSubTest();
198 	CreateRWStatables(testEntries);
199 	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
200 		struct stat st;
201 		uid_t owner = 0xdad;
202 		gid_t group = 0xdee;
203 // OBOS: no fchmod(), no FD time setters
204 #if !TEST_OBOS /* !!!POSIX ONLY!!! */
205 		mode_t perms = 0x0ab;	// -w- r-x -wx	-- unusual enough? ;-)
206 		time_t mtime = 1234567;
207 		time_t ctime = 654321;
208 #endif
209 // R5: access time unused
210 #if !TEST_R5 && !TEST_OBOS /* !!!POSIX ONLY!!! */
211 		time_t atime = 2345678;
212 #endif
213 // OBOS: no fchmod(), no FD time setters
214 		CPPUNIT_ASSERT( statable->SetOwner(owner) == B_OK );
215 		CPPUNIT_ASSERT( statable->SetGroup(group) == B_OK );
216 #if !TEST_OBOS /* !!!POSIX ONLY!!! */
217 		CPPUNIT_ASSERT( statable->SetPermissions(perms) == B_OK );
218 		CPPUNIT_ASSERT( statable->SetModificationTime(mtime) == B_OK );
219 		CPPUNIT_ASSERT( statable->SetCreationTime(ctime) == B_OK );
220 #endif
221 #if !TEST_R5 && !TEST_OBOS /* !!!POSIX ONLY!!! */
222 		CPPUNIT_ASSERT( statable->SetAccessTime(atime) == B_OK );
223 #endif
224 		CPPUNIT_ASSERT( lstat(entryName.c_str(), &st) == 0 );
225 		CPPUNIT_ASSERT( owner == st.st_uid );
226 		CPPUNIT_ASSERT( group == st.st_gid );
227 #if !TEST_OBOS /* !!!POSIX ONLY!!! */
228 		CPPUNIT_ASSERT( perms == (st.st_mode & S_IUMSK) );
229 		CPPUNIT_ASSERT( mtime == st.st_mtime );
230 		CPPUNIT_ASSERT( ctime == st.st_crtime );
231 #endif
232 #if !TEST_R5 && !TEST_OBOS /* !!!POSIX ONLY!!! */
233 		CPPUNIT_ASSERT( atime == st.st_atime );
234 #endif
235 	}
236 	testEntries.delete_all();
237 	// test with uninitialized objects
238 	NextSubTest();
239 	CreateUninitializedStatables(testEntries);
240 	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
241 		uid_t owner = 0xdad;
242 		gid_t group = 0xdee;
243 		mode_t perms = 0x0ab;	// -w- r-x -wx	-- unusual enough? ;-)
244 		time_t mtime = 1234567;
245 		time_t ctime = 654321;
246 		time_t atime = 2345678;
247 		CPPUNIT_ASSERT( statable->SetOwner(owner) != B_OK );
248 		CPPUNIT_ASSERT( statable->SetGroup(group) != B_OK );
249 		CPPUNIT_ASSERT( statable->SetPermissions(perms) != B_OK );
250 		CPPUNIT_ASSERT( statable->SetModificationTime(mtime) != B_OK );
251 		CPPUNIT_ASSERT( statable->SetCreationTime(ctime) != B_OK );
252 		CPPUNIT_ASSERT( statable->SetAccessTime(atime) != B_OK );
253 	}
254 	testEntries.delete_all();
255 }
256