xref: /haiku/src/tests/kits/storage/DirectoryTest.cpp (revision 7120e97489acbf17d86d3f33e3b2e68974fd4b23)
1 // DirectoryTest.cpp
2 
3 #include <stdio.h>
4 #include <string>
5 #include <unistd.h>
6 #include <sys/stat.h>
7 
8 #include <Directory.h>
9 #include <Entry.h>
10 #include <File.h>
11 #include <Path.h>
12 #include <SymLink.h>
13 
14 #include "DirectoryTest.h"
15 
16 // Suite
17 DirectoryTest::Test*
18 DirectoryTest::Suite()
19 {
20 	CppUnit::TestSuite *suite = new CppUnit::TestSuite();
21 	typedef CppUnit::TestCaller<DirectoryTest> TC;
22 
23 	NodeTest::AddBaseClassTests<DirectoryTest>("BDirectory::", suite);
24 
25 	suite->addTest( new TC("BDirectory::Init Test 1",
26 						   &DirectoryTest::InitTest1) );
27 	suite->addTest( new TC("BDirectory::Init Test 2",
28 						   &DirectoryTest::InitTest2) );
29 	suite->addTest( new TC("BDirectory::GetEntry Test",
30 						   &DirectoryTest::GetEntryTest) );
31 	suite->addTest( new TC("BDirectory::IsRoot Test",
32 						   &DirectoryTest::IsRootTest) );
33 	suite->addTest( new TC("BDirectory::FindEntry Test",
34 						   &DirectoryTest::FindEntryTest) );
35 	suite->addTest( new TC("BDirectory::Contains Test",
36 						   &DirectoryTest::ContainsTest) );
37 	suite->addTest( new TC("BDirectory::GetStatFor Test",
38 						   &DirectoryTest::GetStatForTest) );
39 	suite->addTest( new TC("BDirectory::EntryIteration Test",
40 						   &DirectoryTest::EntryIterationTest) );
41 	suite->addTest( new TC("BDirectory::Creation Test",
42 						   &DirectoryTest::EntryCreationTest) );
43 	suite->addTest( new TC("BDirectory::Assignment Test",
44 						   &DirectoryTest::AssignmentTest) );
45 	suite->addTest( new TC("BDirectory::CreateDirectory Test",
46 						   &DirectoryTest::CreateDirectoryTest) );
47 
48 	return suite;
49 }
50 
51 // CreateRONodes
52 void
53 DirectoryTest::CreateRONodes(TestNodes& testEntries)
54 {
55 	testEntries.clear();
56 	const char *filename;
57 	filename = "/";
58 	testEntries.add(new BDirectory(filename), filename);
59 	filename = "/boot";
60 	testEntries.add(new BDirectory(filename), filename);
61 	filename = "/boot/home";
62 	testEntries.add(new BDirectory(filename), filename);
63 	filename = existingDirname;
64 	testEntries.add(new BDirectory(filename), filename);
65 }
66 
67 // CreateRWNodes
68 void
69 DirectoryTest::CreateRWNodes(TestNodes& testEntries)
70 {
71 	testEntries.clear();
72 	const char *filename;
73 	filename = existingDirname;
74 	testEntries.add(new BDirectory(filename), filename);
75 	filename = existingSubDirname;
76 	testEntries.add(new BDirectory(filename), filename);
77 }
78 
79 // CreateUninitializedNodes
80 void
81 DirectoryTest::CreateUninitializedNodes(TestNodes& testEntries)
82 {
83 	testEntries.clear();
84 	testEntries.add(new BDirectory, "");
85 }
86 
87 // setUp
88 void DirectoryTest::setUp()
89 {
90 	NodeTest::setUp();
91 }
92 
93 // tearDown
94 void DirectoryTest::tearDown()
95 {
96 	NodeTest::tearDown();
97 }
98 
99 // InitTest1
100 void
101 DirectoryTest::InitTest1()
102 {
103 	const char *existingFile = existingFilename;
104 	const char *existingSuperFile = existingSuperFilename;
105 	const char *existingRelFile = existingRelFilename;
106 	const char *existing = existingDirname;
107 	const char *existingSub = existingSubDirname;
108 	const char *existingRelSub = existingRelSubDirname;
109 	const char *nonExisting = nonExistingDirname;
110 	const char *nonExistingSuper = nonExistingSuperDirname;
111 	const char *nonExistingRel = nonExistingRelDirname;
112 	// 1. default constructor
113 	NextSubTest();
114 	{
115 		BDirectory dir;
116 		CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
117 	}
118 
119 	// 2. BDirectory(const char*)
120 	NextSubTest();
121 	{
122 		BDirectory dir(existing);
123 		CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
124 	}
125 	NextSubTest();
126 	{
127 		BDirectory dir(nonExisting);
128 		CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
129 	}
130 	NextSubTest();
131 	{
132 		BDirectory dir((const char *)NULL);
133 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
134 	}
135 	NextSubTest();
136 	{
137 		BDirectory dir("");
138 		// R5 returns B_ENTRY_NOT_FOUND instead of B_BAD_VALUE.
139 		CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
140 	}
141 	NextSubTest();
142 	{
143 		BDirectory dir(existingFile);
144 		// R5 returns B_BAD_VALUE instead of B_NOT_A_DIRECTORY.
145 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
146 	}
147 	NextSubTest();
148 	{
149 		BDirectory dir(tooLongEntryname);
150 		CPPUNIT_ASSERT( dir.InitCheck() == B_NAME_TOO_LONG );
151 	}
152 	NextSubTest();
153 	{
154 		BDirectory dir(fileDirname);
155 		// R5 returns B_ENTRY_NOT_FOUND instead of B_NOT_A_DIRECTORY.
156 		CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
157 	}
158 
159 	// 3. BDirectory(const BEntry*)
160 	NextSubTest();
161 	{
162 		BEntry entry(existing);
163 		CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
164 		BDirectory dir(&entry);
165 		CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
166 	}
167 	NextSubTest();
168 	{
169 		BEntry entry(nonExisting);
170 		CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
171 		BDirectory dir(&entry);
172 		CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
173 	}
174 	NextSubTest();
175 	{
176 		BDirectory dir((BEntry *)NULL);
177 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
178 	}
179 	NextSubTest();
180 	{
181 		BEntry entry;
182 		BDirectory dir(&entry);
183 		CPPUNIT_ASSERT( equals(dir.InitCheck(), B_BAD_ADDRESS, B_BAD_VALUE) );
184 	}
185 	NextSubTest();
186 	{
187 		BEntry entry(existingFile);
188 		CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
189 		BDirectory dir(&entry);
190 		// R5 returns B_BAD_VALUE instead of B_NOT_A_DIRECTORY.
191 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
192 	}
193 	NextSubTest();
194 	{
195 		BEntry entry(tooLongEntryname);
196 		// R5 returns E2BIG instead of B_NAME_TOO_LONG
197 		CPPUNIT_ASSERT( equals(entry.InitCheck(), E2BIG, B_NAME_TOO_LONG) );
198 		BDirectory dir(&entry);
199 		CPPUNIT_ASSERT( equals(dir.InitCheck(), B_BAD_ADDRESS, B_BAD_VALUE) );
200 	}
201 
202 	// 4. BDirectory(const entry_ref*)
203 	NextSubTest();
204 	{
205 		BEntry entry(existing);
206 		CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
207 		entry_ref ref;
208 		CPPUNIT_ASSERT( entry.GetRef(&ref) == B_OK );
209 		BDirectory dir(&ref);
210 		CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
211 	}
212 	NextSubTest();
213 	{
214 		BEntry entry(nonExisting);
215 		CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
216 		entry_ref ref;
217 		CPPUNIT_ASSERT( entry.GetRef(&ref) == B_OK );
218 		BDirectory dir(&ref);
219 		CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
220 	}
221 	NextSubTest();
222 	{
223 		BDirectory dir((entry_ref *)NULL);
224 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
225 	}
226 	NextSubTest();
227 	{
228 		BEntry entry(existingFile);
229 		CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
230 		entry_ref ref;
231 		CPPUNIT_ASSERT( entry.GetRef(&ref) == B_OK );
232 		BDirectory dir(&ref);
233 		// R5 returns B_BAD_VALUE instead of B_NOT_A_DIRECTORY.
234 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
235 	}
236 
237 	// 5. BDirectory(const node_ref*)
238 	NextSubTest();
239 	{
240 		BNode node(existing);
241 		CPPUNIT_ASSERT( node.InitCheck() == B_OK );
242 		node_ref nref;
243 		CPPUNIT_ASSERT( node.GetNodeRef(&nref) == B_OK );
244 		BDirectory dir(&nref);
245 		CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
246 	}
247 // R5: crashs, when passing a NULL node_ref.
248 #if !TEST_R5
249 	NextSubTest();
250 	{
251 		BDirectory dir((node_ref *)NULL);
252 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
253 	}
254 #endif
255 	NextSubTest();
256 	{
257 		BNode node(existingFile);
258 		CPPUNIT_ASSERT( node.InitCheck() == B_OK );
259 		node_ref nref;
260 		CPPUNIT_ASSERT( node.GetNodeRef(&nref) == B_OK );
261 		BDirectory dir(&nref);
262 		// R5: returns B_BAD_VALUE instead of B_NOT_A_DIRECTORY.
263 		// OBOS: returns B_ENTRY_NOT_FOUND instead of B_NOT_A_DIRECTORY.
264 		CPPUNIT_ASSERT( equals(dir.InitCheck(), B_BAD_VALUE, B_ENTRY_NOT_FOUND) );
265 	}
266 
267 	// 6. BDirectory(const BDirectory*, const char*)
268 	NextSubTest();
269 	{
270 		BDirectory pathDir(existing);
271 		CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
272 		BDirectory dir(&pathDir, existingRelSub);
273 		CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
274 	}
275 	NextSubTest();
276 	{
277 		BDirectory pathDir(existing);
278 		CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
279 		BDirectory dir(&pathDir, existingSub);
280 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
281 	}
282 	NextSubTest();
283 	{
284 		BDirectory pathDir(nonExistingSuper);
285 		CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
286 		BDirectory dir(&pathDir, nonExistingRel);
287 		CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
288 	}
289 	NextSubTest();
290 	{
291 		BDirectory dir((BDirectory *)NULL, (const char *)NULL);
292 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
293 	}
294 	NextSubTest();
295 	{
296 		BDirectory dir((BDirectory *)NULL, existingSub);
297 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
298 	}
299 	NextSubTest();
300 	{
301 		BDirectory pathDir(existing);
302 		CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
303 		BDirectory dir(&pathDir, (const char *)NULL);
304 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
305 	}
306 	NextSubTest();
307 	{
308 		BDirectory pathDir(existing);
309 		CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
310 		BDirectory dir(&pathDir, "");
311 		// This does not fail in R5, but inits the object to pathDir.
312 		CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
313 	}
314 	NextSubTest();
315 	{
316 		BDirectory pathDir(existingSuperFile);
317 		CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
318 		BDirectory dir(&pathDir, existingRelFile);
319 		// R5 returns B_BAD_VALUE instead of B_NOT_A_DIRECTORY.
320 		CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
321 	}
322 	NextSubTest();
323 	{
324 		BDirectory pathDir(tooLongSuperEntryname);
325 		CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
326 		BDirectory dir(&pathDir, tooLongRelEntryname);
327 		CPPUNIT_ASSERT( dir.InitCheck() == B_NAME_TOO_LONG );
328 	}
329 	NextSubTest();
330 	{
331 		BDirectory pathDir(fileSuperDirname);
332 		CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
333 		BDirectory dir(&pathDir, fileRelDirname);
334 		// R5 returns B_ENTRY_NOT_FOUND instead of B_NOT_A_DIRECTORY.
335 		CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
336 	}
337 }
338 
339 // InitTest2
340 void
341 DirectoryTest::InitTest2()
342 {
343 	const char *existingFile = existingFilename;
344 	const char *existingSuperFile = existingSuperFilename;
345 	const char *existingRelFile = existingRelFilename;
346 	const char *existing = existingDirname;
347 	const char *existingSub = existingSubDirname;
348 	const char *existingRelSub = existingRelSubDirname;
349 	const char *nonExisting = nonExistingDirname;
350 	const char *nonExistingSuper = nonExistingSuperDirname;
351 	const char *nonExistingRel = nonExistingRelDirname;
352 	BDirectory dir;
353 	// 2. SetTo(const char*)
354 	NextSubTest();
355 	CPPUNIT_ASSERT( dir.SetTo(existing) == B_OK );
356 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
357 	dir.Unset();
358 	//
359 	NextSubTest();
360 	CPPUNIT_ASSERT( dir.SetTo(nonExisting) == B_ENTRY_NOT_FOUND );
361 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
362 	dir.Unset();
363 	//
364 	NextSubTest();
365 	CPPUNIT_ASSERT( dir.SetTo((const char *)NULL) == B_BAD_VALUE );
366 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
367 	dir.Unset();
368 	//
369 	NextSubTest();
370 	// R5 returns B_ENTRY_NOT_FOUND instead of B_BAD_VALUE.
371 	CPPUNIT_ASSERT( dir.SetTo("") == B_ENTRY_NOT_FOUND );
372 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
373 	dir.Unset();
374 	//
375 	NextSubTest();
376 	// R5 returns B_BAD_VALUE instead of B_NOT_A_DIRECTORY.
377 	CPPUNIT_ASSERT( dir.SetTo(existingFile) == B_BAD_VALUE );
378 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
379 	dir.Unset();
380 	//
381 	NextSubTest();
382 	CPPUNIT_ASSERT( dir.SetTo(tooLongEntryname) == B_NAME_TOO_LONG );
383 	CPPUNIT_ASSERT( dir.InitCheck() == B_NAME_TOO_LONG );
384 	dir.Unset();
385 	//
386 	NextSubTest();
387 	// R5 returns B_ENTRY_NOT_FOUND instead of B_NOT_A_DIRECTORY.
388 	CPPUNIT_ASSERT( dir.SetTo(fileDirname) == B_ENTRY_NOT_FOUND );
389 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
390 	dir.Unset();
391 
392 	// 3. BDirectory(const BEntry*)
393 	NextSubTest();
394 	BEntry entry(existing);
395 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
396 	CPPUNIT_ASSERT( dir.SetTo(&entry) == B_OK );
397 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
398 	dir.Unset();
399 	//
400 	NextSubTest();
401 	entry.SetTo(nonExisting);
402 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
403 	CPPUNIT_ASSERT( dir.SetTo(&entry) == B_ENTRY_NOT_FOUND );
404 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
405 	dir.Unset();
406 	//
407 	NextSubTest();
408 	CPPUNIT_ASSERT( dir.SetTo((BEntry *)NULL) == B_BAD_VALUE );
409 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
410 	dir.Unset();
411 	//
412 	NextSubTest();
413 	entry.Unset();
414 	CPPUNIT_ASSERT( equals(dir.SetTo(&entry), B_BAD_ADDRESS, B_BAD_VALUE) );
415 	CPPUNIT_ASSERT( equals(dir.InitCheck(), B_BAD_ADDRESS, B_BAD_VALUE) );
416 	dir.Unset();
417 	//
418 	NextSubTest();
419 	entry.SetTo(existingFile);
420 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
421 	// R5 returns B_BAD_VALUE instead of B_NOT_A_DIRECTORY.
422 	CPPUNIT_ASSERT( dir.SetTo(&entry) == B_BAD_VALUE );
423 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
424 	dir.Unset();
425 	//
426 	NextSubTest();
427 	entry.SetTo(tooLongEntryname);
428 	// R5 returns E2BIG instead of B_NAME_TOO_LONG
429 	CPPUNIT_ASSERT( equals(entry.InitCheck(), E2BIG, B_NAME_TOO_LONG) );
430 	CPPUNIT_ASSERT( equals(dir.SetTo(&entry), B_BAD_ADDRESS, B_BAD_VALUE) );
431 	CPPUNIT_ASSERT( equals(dir.InitCheck(), B_BAD_ADDRESS, B_BAD_VALUE) );
432 	dir.Unset();
433 
434 	// 4. BDirectory(const entry_ref*)
435 	NextSubTest();
436 	entry.SetTo(existing);
437 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
438 	entry_ref ref;
439 	CPPUNIT_ASSERT( entry.GetRef(&ref) == B_OK );
440 	CPPUNIT_ASSERT( dir.SetTo(&ref) == B_OK );
441 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
442 	dir.Unset();
443 	//
444 	NextSubTest();
445 	entry.SetTo(nonExisting);
446 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
447 	CPPUNIT_ASSERT( entry.GetRef(&ref) == B_OK );
448 	CPPUNIT_ASSERT( dir.SetTo(&ref) == B_ENTRY_NOT_FOUND );
449 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
450 	dir.Unset();
451 	//
452 	NextSubTest();
453 	CPPUNIT_ASSERT( dir.SetTo((entry_ref *)NULL) == B_BAD_VALUE );
454 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
455 	dir.Unset();
456 	//
457 	NextSubTest();
458 	entry.SetTo(existingFile);
459 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
460 	CPPUNIT_ASSERT( entry.GetRef(&ref) == B_OK );
461 	// R5 returns B_BAD_VALUE instead of B_NOT_A_DIRECTORY.
462 	CPPUNIT_ASSERT( dir.SetTo(&ref) == B_BAD_VALUE );
463 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
464 	dir.Unset();
465 
466 	// 5. BDirectory(const node_ref*)
467 	NextSubTest();
468 	BNode node(existing);
469 	CPPUNIT_ASSERT( node.InitCheck() == B_OK );
470 	node_ref nref;
471 	CPPUNIT_ASSERT( node.GetNodeRef(&nref) == B_OK );
472 	CPPUNIT_ASSERT( dir.SetTo(&nref) == B_OK );
473 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
474 	dir.Unset();
475 	//
476 // R5: crashs, when passing a NULL node_ref.
477 #if !TEST_R5
478 	NextSubTest();
479 	CPPUNIT_ASSERT( dir.SetTo((node_ref *)NULL) == B_BAD_VALUE );
480 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
481 	dir.Unset();
482 #endif
483 	//
484 	NextSubTest();
485 	CPPUNIT_ASSERT( node.SetTo(existingFile) == B_OK );
486 	CPPUNIT_ASSERT( node.GetNodeRef(&nref) == B_OK );
487 	// R5 returns B_BAD_VALUE instead of B_NOT_A_DIRECTORY.
488 	// OBOS: returns B_ENTRY_NOT_FOUND instead of B_NOT_A_DIRECTORY.
489 	CPPUNIT_ASSERT( equals(dir.SetTo(&nref), B_BAD_VALUE, B_ENTRY_NOT_FOUND) );
490 	CPPUNIT_ASSERT( equals(dir.InitCheck(), B_BAD_VALUE, B_ENTRY_NOT_FOUND) );
491 	dir.Unset();
492 
493 	// 6. BDirectory(const BDirectory*, const char*)
494 	NextSubTest();
495 	BDirectory pathDir(existing);
496 	CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
497 	CPPUNIT_ASSERT( dir.SetTo(&pathDir, existingRelSub) == B_OK );
498 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
499 	dir.Unset();
500 	//
501 	NextSubTest();
502 	pathDir.SetTo(existing);
503 	CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
504 	CPPUNIT_ASSERT( dir.SetTo(&pathDir, existingSub) == B_BAD_VALUE );
505 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
506 	dir.Unset();
507 	//
508 	NextSubTest();
509 	pathDir.SetTo(nonExistingSuper);
510 	CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
511 	CPPUNIT_ASSERT( dir.SetTo(&pathDir, nonExistingRel) == B_ENTRY_NOT_FOUND );
512 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
513 	dir.Unset();
514 	//
515 	NextSubTest();
516 	CPPUNIT_ASSERT( dir.SetTo((BDirectory *)NULL, (const char *)NULL) == B_BAD_VALUE );
517 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
518 	dir.Unset();
519 	//
520 	NextSubTest();
521 	CPPUNIT_ASSERT( dir.SetTo((BDirectory *)NULL, existingSub) == B_BAD_VALUE );
522 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
523 	dir.Unset();
524 	//
525 	NextSubTest();
526 	pathDir.SetTo(existing);
527 	CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
528 	CPPUNIT_ASSERT( dir.SetTo(&pathDir, (const char *)NULL) == B_BAD_VALUE );
529 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
530 	dir.Unset();
531 	//
532 	NextSubTest();
533 	pathDir.SetTo(existing);
534 	CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
535 	// This does not fail in R5, but inits the object to pathDir.
536 	CPPUNIT_ASSERT( dir.SetTo(&pathDir, "") == B_OK );
537 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
538 	dir.Unset();
539 	//
540 	NextSubTest();
541 	pathDir.SetTo(existingSuperFile);
542 	CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
543 	// R5 returns B_BAD_VALUE instead of B_NOT_A_DIRECTORY.
544 	CPPUNIT_ASSERT( dir.SetTo(&pathDir, existingRelFile) == B_BAD_VALUE );
545 	CPPUNIT_ASSERT( dir.InitCheck() == B_BAD_VALUE );
546 	dir.Unset();
547 	//
548 	NextSubTest();
549 	pathDir.SetTo(tooLongSuperEntryname);
550 	CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
551 	CPPUNIT_ASSERT( dir.SetTo(&pathDir, tooLongRelEntryname)
552 					== B_NAME_TOO_LONG );
553 	CPPUNIT_ASSERT( dir.InitCheck() == B_NAME_TOO_LONG );
554 	dir.Unset();
555 	//
556 	NextSubTest();
557 	pathDir.SetTo(fileSuperDirname);
558 	CPPUNIT_ASSERT( pathDir.InitCheck() == B_OK );
559 	// R5 returns B_ENTRY_NOT_FOUND instead of B_NOT_A_DIRECTORY.
560 	CPPUNIT_ASSERT( dir.SetTo(&pathDir, fileRelDirname) == B_ENTRY_NOT_FOUND );
561 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
562 	dir.Unset();
563 }
564 
565 // GetEntryTest
566 void
567 DirectoryTest::GetEntryTest()
568 {
569 	const char *existing = existingDirname;
570 	const char *nonExisting = nonExistingDirname;
571 	//
572 	NextSubTest();
573 	BDirectory dir;
574 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
575 	BEntry entry;
576 	CPPUNIT_ASSERT( dir.GetEntry(&entry) == B_NO_INIT );
577 	dir.Unset();
578 	entry.Unset();
579 	//
580 	NextSubTest();
581 	CPPUNIT_ASSERT( dir.SetTo(existing) == B_OK );
582 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
583 	CPPUNIT_ASSERT( dir.GetEntry(&entry) == B_OK );
584 	CPPUNIT_ASSERT( entry == BEntry(existing) );
585 	dir.Unset();
586 	entry.Unset();
587 	//
588 #if !TEST_R5
589 // R5: crashs, when passing a NULL BEntry.
590 	NextSubTest();
591 	CPPUNIT_ASSERT( dir.SetTo(existing) == B_OK );
592 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
593 	CPPUNIT_ASSERT( dir.GetEntry((BEntry *)NULL) == B_BAD_VALUE );
594 	dir.Unset();
595 	entry.Unset();
596 #endif
597 	//
598 	NextSubTest();
599 	CPPUNIT_ASSERT( dir.SetTo(nonExisting) == B_ENTRY_NOT_FOUND );
600 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
601 	CPPUNIT_ASSERT( dir.GetEntry(&entry) == B_NO_INIT );
602 	dir.Unset();
603 	entry.Unset();
604 }
605 
606 // IsRootTest
607 void
608 DirectoryTest::IsRootTest()
609 {
610 	//
611 	NextSubTest();
612 	BDirectory dir;
613 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
614 	CPPUNIT_ASSERT( dir.IsRootDirectory() == false );
615 	//
616 	NextSubTest();
617 	CPPUNIT_ASSERT( dir.SetTo("/boot") == B_OK );
618 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
619 	CPPUNIT_ASSERT( dir.IsRootDirectory() == true );
620 	//
621 	NextSubTest();
622 	CPPUNIT_ASSERT( dir.SetTo("/boot/beos") == B_OK );
623 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
624 	CPPUNIT_ASSERT( dir.IsRootDirectory() == false );
625 	//
626 	NextSubTest();
627 	CPPUNIT_ASSERT( dir.SetTo("/tmp") == B_OK );
628 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
629 	CPPUNIT_ASSERT( dir.IsRootDirectory() == false );
630 	//
631 	NextSubTest();
632 	CPPUNIT_ASSERT( dir.SetTo("/") == B_OK );
633 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
634 	CPPUNIT_ASSERT( dir.IsRootDirectory() == true );
635 }
636 
637 // FindEntryTest
638 void
639 DirectoryTest::FindEntryTest()
640 {
641 	const char *existingFile = existingFilename;
642 	const char *existing = existingDirname;
643 	const char *existingSub = existingSubDirname;
644 	const char *existingRelSub = existingRelSubDirname;
645 	const char *nonExisting = nonExistingDirname;
646 	const char *nonExistingSuper = nonExistingSuperDirname;
647 	const char *nonExistingRel = nonExistingRelDirname;
648 	const char *dirLink = dirLinkname;
649 	const char *badLink = badLinkname;
650 	const char *cyclicLink1 = cyclicLinkname1;
651 	// existing absolute path, uninitialized BDirectory
652 	NextSubTest();
653 	BDirectory dir;
654 	BEntry entry;
655 	BPath path;
656 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
657 	CPPUNIT_ASSERT( dir.FindEntry(existing, &entry) == B_OK );
658 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
659 	CPPUNIT_ASSERT( entry.GetPath(&path) == B_OK );
660 	CPPUNIT_ASSERT( path == existing == B_OK );
661 	dir.Unset();
662 	entry.Unset();
663 	path.Unset();
664 	// existing absolute path, badly initialized BDirectory
665 	NextSubTest();
666 	CPPUNIT_ASSERT( dir.SetTo(nonExisting) == B_ENTRY_NOT_FOUND );
667 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
668 	CPPUNIT_ASSERT( dir.FindEntry(existing, &entry) == B_OK );
669 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
670 	CPPUNIT_ASSERT( entry.GetPath(&path) == B_OK );
671 	CPPUNIT_ASSERT( path == existing == B_OK );
672 	dir.Unset();
673 	entry.Unset();
674 	path.Unset();
675 	// existing path relative to current dir, uninitialized BDirectory
676 	NextSubTest();
677 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
678 	chdir(existing);
679 	CPPUNIT_ASSERT( dir.FindEntry(existingRelSub, &entry) == B_OK );
680 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
681 	CPPUNIT_ASSERT( entry.GetPath(&path) == B_OK );
682 	CPPUNIT_ASSERT( path == existingSub == B_OK );
683 	dir.Unset();
684 	entry.Unset();
685 	path.Unset();
686 	chdir("/");
687 	// existing path relative to current dir,
688 	// initialized BDirectory != current dir
689 	NextSubTest();
690 	CPPUNIT_ASSERT( dir.SetTo(existingSub) == B_OK );
691 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
692 	chdir(existing);
693 	CPPUNIT_ASSERT( entry.SetTo(existingFile) == B_OK );
694 	CPPUNIT_ASSERT( dir.FindEntry(existingRelSub, &entry) == B_ENTRY_NOT_FOUND );
695 	CPPUNIT_ASSERT( entry.InitCheck() == B_NO_INIT );
696 	dir.Unset();
697 	entry.Unset();
698 	path.Unset();
699 	chdir("/");
700 	// abstract entry
701 	NextSubTest();
702 	CPPUNIT_ASSERT( dir.SetTo(nonExistingSuper) == B_OK );
703 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
704 	chdir(existing);
705 	CPPUNIT_ASSERT( entry.SetTo(existingFile) == B_OK );
706 	CPPUNIT_ASSERT( dir.FindEntry(nonExistingRel, &entry) == B_ENTRY_NOT_FOUND );
707 	CPPUNIT_ASSERT( entry.InitCheck() == B_NO_INIT );
708 	dir.Unset();
709 	entry.Unset();
710 	path.Unset();
711 	chdir("/");
712 	// bad args
713 	NextSubTest();
714 	CPPUNIT_ASSERT( dir.SetTo(existing) == B_OK );
715 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
716 // R5: crashs, when passing a NULL BEntry.
717 #if !TEST_R5
718 	CPPUNIT_ASSERT( dir.FindEntry(existingRelSub, NULL) == B_BAD_VALUE );
719 #endif
720 	CPPUNIT_ASSERT( entry.SetTo(existingFile) == B_OK );
721 	CPPUNIT_ASSERT( dir.FindEntry(NULL, &entry) == B_BAD_VALUE );
722 	CPPUNIT_ASSERT( equals(entry.InitCheck(), B_BAD_VALUE, B_NO_INIT) );
723 // R5: crashs, when passing a NULL BEntry.
724 #if !TEST_R5
725 	CPPUNIT_ASSERT( dir.FindEntry(NULL, NULL) == B_BAD_VALUE );
726 #endif
727 	dir.Unset();
728 	entry.Unset();
729 	path.Unset();
730 	// don't traverse a valid link
731 	NextSubTest();
732 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
733 	CPPUNIT_ASSERT( dir.FindEntry(dirLink, &entry) == B_OK );
734 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
735 	CPPUNIT_ASSERT( entry.GetPath(&path) == B_OK );
736 	CPPUNIT_ASSERT( path == dirLink == B_OK );
737 	dir.Unset();
738 	entry.Unset();
739 	path.Unset();
740 	// traverse a valid link
741 	NextSubTest();
742 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
743 	CPPUNIT_ASSERT( dir.FindEntry(dirLink, &entry, true) == B_OK );
744 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
745 	CPPUNIT_ASSERT( entry.GetPath(&path) == B_OK );
746 	CPPUNIT_ASSERT( path == existing == B_OK );
747 	dir.Unset();
748 	entry.Unset();
749 	path.Unset();
750 	// don't traverse an invalid link
751 	NextSubTest();
752 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
753 	CPPUNIT_ASSERT( dir.FindEntry(badLink, &entry) == B_OK );
754 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
755 	CPPUNIT_ASSERT( entry.GetPath(&path) == B_OK );
756 	CPPUNIT_ASSERT( path == badLink == B_OK );
757 	dir.Unset();
758 	entry.Unset();
759 	path.Unset();
760 	// traverse an invalid link
761 	NextSubTest();
762 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
763 	CPPUNIT_ASSERT( dir.FindEntry(badLink, &entry, true) == B_ENTRY_NOT_FOUND );
764 	CPPUNIT_ASSERT( equals(entry.InitCheck(), B_ENTRY_NOT_FOUND, B_NO_INIT) );
765 	dir.Unset();
766 	entry.Unset();
767 	path.Unset();
768 	// don't traverse a cyclic link
769 	NextSubTest();
770 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
771 	CPPUNIT_ASSERT( dir.FindEntry(cyclicLink1, &entry) == B_OK );
772 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
773 	CPPUNIT_ASSERT( entry.GetPath(&path) == B_OK );
774 	CPPUNIT_ASSERT( path == cyclicLink1 == B_OK );
775 	dir.Unset();
776 	entry.Unset();
777 	path.Unset();
778 	// traverse a cyclic link
779 	NextSubTest();
780 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
781 	CPPUNIT_ASSERT( dir.FindEntry(cyclicLink1, &entry, true) == B_LINK_LIMIT );
782 	CPPUNIT_ASSERT( entry.InitCheck() == B_LINK_LIMIT );
783 	dir.Unset();
784 	entry.Unset();
785 	path.Unset();
786 }
787 
788 // ContainsTest
789 void
790 DirectoryTest::ContainsTest()
791 {
792 	const char *existingFile = existingFilename;
793 	const char *existingSuperFile = existingSuperFilename;
794 	const char *existingRelFile = existingRelFilename;
795 	const char *existing = existingDirname;
796 	const char *existingSuper = existingSuperDirname;
797 	const char *existingSub = existingSubDirname;
798 	const char *existingRelSub = existingRelSubDirname;
799 	const char *nonExisting = nonExistingDirname;
800 	const char *dirLink = dirLinkname;
801 	const char *dirSuperLink = dirSuperLinkname;
802 	// 1. Contains(const char *, int32)
803 	// existing entry, initialized BDirectory
804 	NextSubTest();
805 	BDirectory dir(existing);
806 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
807 	CPPUNIT_ASSERT( dir.Contains(existingSub) == true );
808 	dir.Unset();
809 	// existing entry, uninitialized BDirectory
810 	// R5 returns true!
811 	NextSubTest();
812 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
813 	CPPUNIT_ASSERT( dir.Contains(existing) == true );
814 	dir.Unset();
815 	// non-existing entry, uninitialized BDirectory
816 	NextSubTest();
817 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
818 	CPPUNIT_ASSERT( dir.Contains(nonExisting) == false );
819 	dir.Unset();
820 	// existing entry, badly initialized BDirectory
821 	NextSubTest();
822 	CPPUNIT_ASSERT( dir.SetTo(nonExisting) == B_ENTRY_NOT_FOUND );
823 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
824 	CPPUNIT_ASSERT( dir.Contains(existing) == true );
825 	dir.Unset();
826 	// non-existing entry, badly initialized BDirectory
827 	NextSubTest();
828 	CPPUNIT_ASSERT( dir.SetTo(nonExisting) == B_ENTRY_NOT_FOUND );
829 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
830 	CPPUNIT_ASSERT( dir.Contains(nonExisting) == false );
831 	dir.Unset();
832 	// initialized BDirectory, bad args
833 	NextSubTest();
834 	CPPUNIT_ASSERT( dir.SetTo(existing) == B_OK );
835 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
836 	CPPUNIT_ASSERT( dir.Contains((const char*)NULL) == true );
837 	dir.Unset();
838 	// uninitialized BDirectory, bad args
839 	NextSubTest();
840 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
841 	CPPUNIT_ASSERT( dir.Contains((const char*)NULL) == false );
842 	dir.Unset();
843 	// existing entry (second level, absolute path), initialized BDirectory
844 	NextSubTest();
845 	CPPUNIT_ASSERT( dir.SetTo(existingSuper) == B_OK );
846 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
847 	CPPUNIT_ASSERT( dir.Contains(existingSub) == true );
848 	dir.Unset();
849 	// existing entry (second level, name only), initialized BDirectory
850 	NextSubTest();
851 	CPPUNIT_ASSERT( dir.SetTo(existingSuper) == B_OK );
852 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
853 	CPPUNIT_ASSERT( dir.Contains(existingRelSub) == false );
854 	dir.Unset();
855 	// initialized BDirectory, self containing
856 	NextSubTest();
857 	CPPUNIT_ASSERT( dir.SetTo(existing) == B_OK );
858 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
859 	CPPUNIT_ASSERT( dir.Contains(existing) == true );
860 	dir.Unset();
861 	// existing entry (dir), initialized BDirectory, matching node kind
862 	NextSubTest();
863 	CPPUNIT_ASSERT( dir.SetTo(existingSuper) == B_OK );
864 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
865 	CPPUNIT_ASSERT( dir.Contains(existing, B_DIRECTORY_NODE) == true );
866 	dir.Unset();
867 	// existing entry (dir), initialized BDirectory, mismatching node kind
868 	NextSubTest();
869 	CPPUNIT_ASSERT( dir.SetTo(existingSuper) == B_OK );
870 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
871 	CPPUNIT_ASSERT( dir.Contains(existing, B_FILE_NODE) == false );
872 	dir.Unset();
873 	// existing entry (file), initialized BDirectory, matching node kind
874 	NextSubTest();
875 	CPPUNIT_ASSERT( dir.SetTo(existingSuperFile) == B_OK );
876 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
877 	CPPUNIT_ASSERT( dir.Contains(existingFile, B_FILE_NODE) == true );
878 	dir.Unset();
879 	// existing entry (file), initialized BDirectory, mismatching node kind
880 	NextSubTest();
881 	CPPUNIT_ASSERT( dir.SetTo(existingSuperFile) == B_OK );
882 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
883 	CPPUNIT_ASSERT( dir.Contains(existingFile, B_SYMLINK_NODE) == false );
884 	dir.Unset();
885 	// existing entry (link), initialized BDirectory, matching node kind
886 	NextSubTest();
887 	CPPUNIT_ASSERT( dir.SetTo(dirSuperLink) == B_OK );
888 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
889 	CPPUNIT_ASSERT( dir.Contains(dirLink, B_SYMLINK_NODE) == true );
890 	dir.Unset();
891 	// existing entry (link), initialized BDirectory, mismatching node kind
892 	NextSubTest();
893 	CPPUNIT_ASSERT( dir.SetTo(dirSuperLink) == B_OK );
894 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
895 	CPPUNIT_ASSERT( dir.Contains(dirLink, B_DIRECTORY_NODE) == false );
896 	dir.Unset();
897 	// existing entry (relative path), initialized BDirectory,
898 	// matching node kind
899 	NextSubTest();
900 	CPPUNIT_ASSERT( dir.SetTo(existingSuperFile) == B_OK );
901 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
902 	CPPUNIT_ASSERT( dir.Contains(existingRelFile, B_FILE_NODE) == true );
903 	dir.Unset();
904 	// existing entry (relative path), initialized BDirectory,
905 	// mismatching node kind
906 	NextSubTest();
907 	CPPUNIT_ASSERT( dir.SetTo(existingSuperFile) == B_OK );
908 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
909 	CPPUNIT_ASSERT( dir.Contains(existingRelFile, B_SYMLINK_NODE) == false );
910 	dir.Unset();
911 
912 	// 2. Contains(const BEntry *, int32)
913 	// existing entry, initialized BDirectory
914 	NextSubTest();
915 	CPPUNIT_ASSERT( dir.SetTo(existing) == B_OK );
916 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
917 	BEntry entry(existingSub);
918 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
919 	CPPUNIT_ASSERT( dir.Contains(&entry) == true );
920 	dir.Unset();
921 	// existing entry, uninitialized BDirectory
922 	// R5: unlike the other version, this one returns false
923 	// OBOS: both versions return true
924 	NextSubTest();
925 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
926 	CPPUNIT_ASSERT( entry.SetTo(existing) == B_OK );
927 	CPPUNIT_ASSERT( equals(dir.Contains(&entry), false, true) );
928 	dir.Unset();
929 	entry.Unset();
930 	// non-existing entry, uninitialized BDirectory
931 	NextSubTest();
932 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
933 	CPPUNIT_ASSERT( entry.SetTo(nonExisting) == B_OK);
934 	CPPUNIT_ASSERT( dir.Contains(&entry) == false );
935 	dir.Unset();
936 	entry.Unset();
937 	// existing entry, badly initialized BDirectory
938 	// R5: unlike the other version, this one returns false
939 	// OBOS: both versions return true
940 	NextSubTest();
941 	CPPUNIT_ASSERT( dir.SetTo(nonExisting) == B_ENTRY_NOT_FOUND );
942 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
943 	CPPUNIT_ASSERT( entry.SetTo(existing) == B_OK);
944 	CPPUNIT_ASSERT( equals(dir.Contains(&entry), false, true) );
945 	dir.Unset();
946 	entry.Unset();
947 	// non-existing entry, badly initialized BDirectory
948 	NextSubTest();
949 	CPPUNIT_ASSERT( dir.SetTo(nonExisting) == B_ENTRY_NOT_FOUND );
950 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
951 	CPPUNIT_ASSERT( entry.SetTo(nonExisting) == B_OK);
952 	CPPUNIT_ASSERT( dir.Contains(&entry) == false );
953 	dir.Unset();
954 	entry.Unset();
955 	// initialized BDirectory, bad args
956 // R5 crashs, when passing a NULL BEntry
957 #if !TEST_R5
958 	NextSubTest();
959 	CPPUNIT_ASSERT( dir.SetTo(existing) == B_OK );
960 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
961 	CPPUNIT_ASSERT( dir.Contains((const BEntry*)NULL) == false );
962 	dir.Unset();
963 #endif
964 	// uninitialized BDirectory, bad args
965 	NextSubTest();
966 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
967 	CPPUNIT_ASSERT( dir.Contains((const BEntry*)NULL) == false );
968 	dir.Unset();
969 	// existing entry (second level, absolute path), initialized BDirectory
970 	NextSubTest();
971 	CPPUNIT_ASSERT( dir.SetTo(existingSuper) == B_OK );
972 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
973 	CPPUNIT_ASSERT( entry.SetTo(existingSub) == B_OK);
974 	CPPUNIT_ASSERT( dir.Contains(&entry) == true );
975 	dir.Unset();
976 	entry.Unset();
977 	// initialized BDirectory, self containing
978 	// R5: behavior is different from Contains(const char*)
979 	// OBOS: both versions return true
980 	NextSubTest();
981 	CPPUNIT_ASSERT( dir.SetTo(existing) == B_OK );
982 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
983 	CPPUNIT_ASSERT( entry.SetTo(existing) == B_OK);
984 	CPPUNIT_ASSERT( equals(dir.Contains(&entry), false, true) );
985 	dir.Unset();
986 	entry.Unset();
987 	// existing entry (dir), initialized BDirectory, matching node kind
988 	NextSubTest();
989 	CPPUNIT_ASSERT( dir.SetTo(existingSuper) == B_OK );
990 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
991 	CPPUNIT_ASSERT( entry.SetTo(existing) == B_OK);
992 	CPPUNIT_ASSERT( dir.Contains(&entry, B_DIRECTORY_NODE) == true );
993 	dir.Unset();
994 	entry.Unset();
995 	// existing entry (dir), initialized BDirectory, mismatching node kind
996 	NextSubTest();
997 	CPPUNIT_ASSERT( dir.SetTo(existingSuper) == B_OK );
998 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
999 	CPPUNIT_ASSERT( entry.SetTo(existing) == B_OK);
1000 	CPPUNIT_ASSERT( dir.Contains(&entry, B_FILE_NODE) == false );
1001 	dir.Unset();
1002 	entry.Unset();
1003 	// existing entry (file), initialized BDirectory, matching node kind
1004 // R5 bug: returns false
1005 #if !TEST_R5
1006 	NextSubTest();
1007 	CPPUNIT_ASSERT( dir.SetTo(existingSuperFile) == B_OK );
1008 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1009 	CPPUNIT_ASSERT( entry.SetTo(existingFile) == B_OK);
1010 	CPPUNIT_ASSERT( dir.Contains(&entry, B_FILE_NODE) == true );
1011 	dir.Unset();
1012 	entry.Unset();
1013 #endif
1014 	// existing entry (file), initialized BDirectory, mismatching node kind
1015 	NextSubTest();
1016 	CPPUNIT_ASSERT( dir.SetTo(existingSuperFile) == B_OK );
1017 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1018 	CPPUNIT_ASSERT( entry.SetTo(existingFile) == B_OK);
1019 	CPPUNIT_ASSERT( dir.Contains(&entry, B_SYMLINK_NODE) == false );
1020 	dir.Unset();
1021 	entry.Unset();
1022 	// existing entry (link), initialized BDirectory, matching node kind
1023 // R5 bug: returns false
1024 #if !TEST_R5
1025 	NextSubTest();
1026 	CPPUNIT_ASSERT( dir.SetTo(dirSuperLink) == B_OK );
1027 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1028 	CPPUNIT_ASSERT( entry.SetTo(dirLink) == B_OK);
1029 	CPPUNIT_ASSERT( dir.Contains(&entry, B_SYMLINK_NODE) == true );
1030 	dir.Unset();
1031 	entry.Unset();
1032 #endif
1033 	// existing entry (link), initialized BDirectory, mismatching node kind
1034 // R5 bug: returns true
1035 #if !TEST_R5
1036 	NextSubTest();
1037 	CPPUNIT_ASSERT( dir.SetTo(dirSuperLink) == B_OK );
1038 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1039 	CPPUNIT_ASSERT( entry.SetTo(dirLink) == B_OK);
1040 	CPPUNIT_ASSERT( dir.Contains(&entry, B_DIRECTORY_NODE) == false );
1041 	dir.Unset();
1042 	entry.Unset();
1043 #endif
1044 }
1045 
1046 // GetStatForTest
1047 void
1048 DirectoryTest::GetStatForTest()
1049 {
1050 	const char *existing = existingDirname;
1051 	const char *existingSuper = existingSuperDirname;
1052 	const char *existingRel = existingRelDirname;
1053 	const char *nonExisting = nonExistingDirname;
1054 	// uninitialized dir, existing entry, absolute path
1055 	NextSubTest();
1056 	BDirectory dir;
1057 	BEntry entry;
1058 	struct stat stat1, stat2;
1059 	memset(&stat1, 0, sizeof(struct stat));
1060 	memset(&stat2, 0, sizeof(struct stat));
1061 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
1062 	CPPUNIT_ASSERT( dir.GetStatFor(existing, &stat1) == B_NO_INIT );
1063 	dir.Unset();
1064 	entry.Unset();
1065 	// badly initialized dir, existing entry, absolute path
1066 	NextSubTest();
1067 	CPPUNIT_ASSERT( dir.SetTo(nonExisting) == B_ENTRY_NOT_FOUND );
1068 	CPPUNIT_ASSERT( dir.InitCheck() == B_ENTRY_NOT_FOUND );
1069 	memset(&stat1, 0, sizeof(struct stat));
1070 	memset(&stat2, 0, sizeof(struct stat));
1071 	CPPUNIT_ASSERT( dir.GetStatFor(existing, &stat1) == B_NO_INIT );
1072 	dir.Unset();
1073 	entry.Unset();
1074 	// initialized dir, existing entry, absolute path
1075 	NextSubTest();
1076 	CPPUNIT_ASSERT( dir.SetTo("/") == B_OK );
1077 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1078 	memset(&stat1, 0, sizeof(struct stat));
1079 	memset(&stat2, 0, sizeof(struct stat));
1080 	CPPUNIT_ASSERT( dir.GetStatFor(existing, &stat1) == B_OK );
1081 	CPPUNIT_ASSERT( entry.SetTo(existing) == B_OK );
1082 	CPPUNIT_ASSERT( entry.GetStat(&stat2) == B_OK );
1083 	CPPUNIT_ASSERT( stat1 == stat2 );
1084 	dir.Unset();
1085 	entry.Unset();
1086 	// initialized dir, existing entry, relative path
1087 	NextSubTest();
1088 	CPPUNIT_ASSERT( dir.SetTo(existingSuper) == B_OK );
1089 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1090 	memset(&stat1, 0, sizeof(struct stat));
1091 	memset(&stat2, 0, sizeof(struct stat));
1092 	CPPUNIT_ASSERT( dir.GetStatFor(existingRel, &stat1) == B_OK );
1093 	CPPUNIT_ASSERT( entry.SetTo(existing) == B_OK );
1094 	CPPUNIT_ASSERT( entry.GetStat(&stat2) == B_OK );
1095 	CPPUNIT_ASSERT( stat1 == stat2 );
1096 	dir.Unset();
1097 	entry.Unset();
1098 	// initialized dir, non-existing entry, absolute path
1099 	NextSubTest();
1100 	CPPUNIT_ASSERT( dir.SetTo("/") == B_OK );
1101 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1102 	memset(&stat1, 0, sizeof(struct stat));
1103 	memset(&stat2, 0, sizeof(struct stat));
1104 	CPPUNIT_ASSERT( dir.GetStatFor(nonExisting, &stat1) == B_ENTRY_NOT_FOUND );
1105 	dir.Unset();
1106 	entry.Unset();
1107 	// initialized dir, bad args (NULL path)
1108 	// R5 returns B_OK and the stat structure for the directory
1109 	NextSubTest();
1110 	CPPUNIT_ASSERT( dir.SetTo("/") == B_OK );
1111 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1112 	memset(&stat1, 0, sizeof(struct stat));
1113 	memset(&stat2, 0, sizeof(struct stat));
1114 	CPPUNIT_ASSERT( dir.GetStatFor(NULL, &stat1) == B_OK );
1115 	CPPUNIT_ASSERT( entry.SetTo("/") == B_OK );
1116 	CPPUNIT_ASSERT( entry.GetStat(&stat2) == B_OK );
1117 	CPPUNIT_ASSERT( stat1 == stat2 );
1118 	dir.Unset();
1119 	entry.Unset();
1120 	// initialized dir, bad args (empty path)
1121 	// R5 returns B_ENTRY_NOT_FOUND
1122 	NextSubTest();
1123 	CPPUNIT_ASSERT( dir.SetTo("/") == B_OK );
1124 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1125 	CPPUNIT_ASSERT( dir.GetStatFor("", &stat1) == B_ENTRY_NOT_FOUND );
1126 	dir.Unset();
1127 	entry.Unset();
1128 	// initialized dir, bad args
1129 	// R5 returns B_BAD_ADDRESS instead of B_BAD_VALUE
1130 	NextSubTest();
1131 	CPPUNIT_ASSERT( dir.SetTo("/") == B_OK );
1132 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1133 	CPPUNIT_ASSERT( equals(dir.GetStatFor(existing, NULL), B_BAD_ADDRESS,
1134 						   B_BAD_VALUE) );
1135 	CPPUNIT_ASSERT( equals(dir.GetStatFor(NULL, NULL), B_BAD_ADDRESS,
1136 						   B_BAD_VALUE) );
1137 	dir.Unset();
1138 	entry.Unset();
1139 }
1140 
1141 // EntryIterationTest
1142 void
1143 DirectoryTest::EntryIterationTest()
1144 {
1145 	const char *existingFile = existingFilename;
1146 	const char *nonExisting = nonExistingDirname;
1147 	const char *testDir1 = testDirname1;
1148 	// create a test directory
1149 	execCommand(string("mkdir ") + testDir1);
1150 	// 1. empty directory
1151 	TestSet testSet;
1152 	testSet.add(".");
1153 	testSet.add("..");
1154 	// GetNextEntry
1155 	NextSubTest();
1156 	BDirectory dir(testDir1);
1157 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1158 	BEntry entry;
1159 	CPPUNIT_ASSERT( dir.GetNextEntry(&entry) == B_ENTRY_NOT_FOUND );
1160 	CPPUNIT_ASSERT( dir.Rewind() == B_OK );
1161 	dir.Unset();
1162 	entry.Unset();
1163 	// GetNextRef
1164 	NextSubTest();
1165 	entry_ref ref;
1166 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1167 	CPPUNIT_ASSERT( dir.GetNextRef(&ref) == B_ENTRY_NOT_FOUND );
1168 	CPPUNIT_ASSERT( dir.Rewind() == B_OK );
1169 	dir.Unset();
1170 	entry.Unset();
1171 	// GetNextDirents
1172 	NextSubTest();
1173 	size_t bufSize = (sizeof(dirent) + B_FILE_NAME_LENGTH) * 10;
1174 	char buffer[bufSize];
1175 	dirent *ents = (dirent *)buffer;
1176 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1177 	while (dir.GetNextDirents(ents, bufSize, 1) == 1)
1178 		CPPUNIT_ASSERT( testSet.test(ents->d_name) == true );
1179 	CPPUNIT_ASSERT( testSet.testDone() == true );
1180 	CPPUNIT_ASSERT( dir.Rewind() == B_OK );
1181 	dir.Unset();
1182 	entry.Unset();
1183 	testSet.rewind();
1184 	// CountEntries
1185 	NextSubTest();
1186 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1187 	CPPUNIT_ASSERT( dir.CountEntries() == 0 );
1188 	dir.Unset();
1189 
1190 	// 2. non-empty directory
1191 	string dirPathName(string(testDir1) + "/");
1192 	string entryName("file1");
1193 	execCommand(string("touch ") + dirPathName + entryName);
1194 	testSet.add(entryName);
1195 	entryName = ("file2");
1196 	execCommand(string("touch ") + dirPathName + entryName);
1197 	testSet.add(entryName);
1198 	entryName = ("file3");
1199 	execCommand(string("touch ") + dirPathName + entryName);
1200 	testSet.add(entryName);
1201 	entryName = ("dir1");
1202 	execCommand(string("mkdir ") + dirPathName + entryName);
1203 	testSet.add(entryName);
1204 	entryName = ("dir2");
1205 	execCommand(string("mkdir ") + dirPathName + entryName);
1206 	testSet.add(entryName);
1207 	entryName = ("dir3");
1208 	execCommand(string("mkdir ") + dirPathName + entryName);
1209 	testSet.add(entryName);
1210 	entryName = ("link1");
1211 	execCommand(string("ln -s ") + existingFile + " "
1212 				+ dirPathName + entryName);
1213 	testSet.add(entryName);
1214 	entryName = ("link2");
1215 	execCommand(string("ln -s ") + existingFile + " "
1216 				+ dirPathName + entryName);
1217 	testSet.add(entryName);
1218 	entryName = ("link3");
1219 	execCommand(string("ln -s ") + existingFile + " "
1220 				+ dirPathName + entryName);
1221 	testSet.add(entryName);
1222 	// GetNextEntry
1223 	NextSubTest();
1224 	testSet.test(".");
1225 	testSet.test("..");
1226 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1227 	while (dir.GetNextEntry(&entry) == B_OK) {
1228 		BPath path;
1229 		CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
1230 		CPPUNIT_ASSERT( entry.GetPath(&path) == B_OK );
1231 		CPPUNIT_ASSERT( testSet.test(path.Leaf()) == true );
1232 	}
1233 	CPPUNIT_ASSERT( testSet.testDone() == true );
1234 	CPPUNIT_ASSERT( dir.Rewind() == B_OK );
1235 	dir.Unset();
1236 	entry.Unset();
1237 	testSet.rewind();
1238 	// GetNextRef
1239 	NextSubTest();
1240 	testSet.test(".");
1241 	testSet.test("..");
1242 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1243 	while (dir.GetNextRef(&ref) == B_OK)
1244 		CPPUNIT_ASSERT( testSet.test(ref.name) == true );
1245 	CPPUNIT_ASSERT( testSet.testDone() == true );
1246 	CPPUNIT_ASSERT( dir.Rewind() == B_OK );
1247 	dir.Unset();
1248 	entry.Unset();
1249 	testSet.rewind();
1250 	// GetNextDirents
1251 	NextSubTest();
1252 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1253 	while (dir.GetNextDirents(ents, bufSize, 1) == 1)
1254 		CPPUNIT_ASSERT( testSet.test(ents->d_name) == true );
1255 	CPPUNIT_ASSERT( testSet.testDone() == true );
1256 	CPPUNIT_ASSERT( dir.Rewind() == B_OK );
1257 	dir.Unset();
1258 	entry.Unset();
1259 	testSet.rewind();
1260 	// CountEntries
1261 	NextSubTest();
1262 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1263 	CPPUNIT_ASSERT( dir.CountEntries() == 9 );
1264 	CPPUNIT_ASSERT( dir.GetNextRef(&ref) == B_OK );
1265 	CPPUNIT_ASSERT( dir.CountEntries() == 9 );
1266 	dir.Unset();
1267 
1268 	// 3. interleaving use of the different methods
1269 	NextSubTest();
1270 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1271 	while (dir.GetNextDirents(ents, bufSize, 1) == 1) {
1272 		CPPUNIT_ASSERT( testSet.test(ents->d_name) == true );
1273 		if (dir.GetNextRef(&ref) == B_OK)
1274 			CPPUNIT_ASSERT( testSet.test(ref.name) == true );
1275 		if (dir.GetNextEntry(&entry) == B_OK) {
1276 			BPath path;
1277 			CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
1278 			CPPUNIT_ASSERT( entry.GetPath(&path) == B_OK );
1279 			CPPUNIT_ASSERT( testSet.test(path.Leaf()) == true );
1280 		}
1281 	}
1282 	testSet.test(".", false);	// in case they have been skipped
1283 	testSet.test("..", false);	//
1284 	CPPUNIT_ASSERT( testSet.testDone() == true );
1285 	CPPUNIT_ASSERT( dir.Rewind() == B_OK );
1286 	dir.Unset();
1287 	entry.Unset();
1288 	testSet.rewind();
1289 
1290 	// 4. uninitialized BDirectory
1291 	NextSubTest();
1292 	dir.Unset();
1293 	// R5: unlike the others GetNextRef() returns B_NO_INIT
1294 	CPPUNIT_ASSERT( dir.GetNextEntry(&entry) == B_FILE_ERROR );
1295 	CPPUNIT_ASSERT( equals(dir.GetNextRef(&ref), B_NO_INIT, B_FILE_ERROR) );
1296 	CPPUNIT_ASSERT( dir.Rewind() == B_FILE_ERROR );
1297 	CPPUNIT_ASSERT( dir.GetNextDirents(ents, bufSize, 1) == B_FILE_ERROR );
1298 	CPPUNIT_ASSERT( dir.CountEntries() == B_FILE_ERROR );
1299 	dir.Unset();
1300 
1301 	// 5. badly initialized BDirectory
1302 	NextSubTest();
1303 	CPPUNIT_ASSERT( dir.SetTo(nonExisting) == B_ENTRY_NOT_FOUND );
1304 	// R5: unlike the others GetNextRef() returns B_NO_INIT
1305 	CPPUNIT_ASSERT( dir.GetNextEntry(&entry) == B_FILE_ERROR );
1306 	CPPUNIT_ASSERT( equals(dir.GetNextRef(&ref), B_NO_INIT, B_FILE_ERROR) );
1307 	CPPUNIT_ASSERT( dir.Rewind() == B_FILE_ERROR );
1308 	CPPUNIT_ASSERT( dir.GetNextDirents(ents, bufSize, 1) == B_FILE_ERROR );
1309 	CPPUNIT_ASSERT( dir.CountEntries() == B_FILE_ERROR );
1310 	dir.Unset();
1311 
1312 	// 6. bad args
1313 // R5 crashs, when passing a NULL BEntry or entry_ref
1314 #if !TEST_R5
1315 	NextSubTest();
1316 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1317 	CPPUNIT_ASSERT( dir.GetNextEntry(NULL) == B_BAD_VALUE );
1318 	CPPUNIT_ASSERT( dir.GetNextRef(NULL) == B_BAD_VALUE );
1319 	CPPUNIT_ASSERT( equals(dir.GetNextDirents(NULL, bufSize, 1),
1320 						   B_BAD_ADDRESS, B_BAD_VALUE) );
1321 	dir.Unset();
1322 #endif
1323 
1324 	// 7. link traversation
1325 	NextSubTest();
1326 	execCommand(string("rm -rf ") + testDir1);
1327 	execCommand(string("mkdir ") + testDir1);
1328 	entryName = ("link1");
1329 	execCommand(string("ln -s ") + existingFile + " "
1330 				+ dirPathName + entryName);
1331 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1332 	CPPUNIT_ASSERT( dir.GetNextEntry(&entry, true) == B_OK );
1333 	BPath path;
1334 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
1335 	BEntry entry2(existingFile);
1336 	CPPUNIT_ASSERT( entry2.InitCheck() == B_OK );
1337 	CPPUNIT_ASSERT( entry == entry2 );
1338 	dir.Unset();
1339 	entry.Unset();
1340 }
1341 
1342 // EntryCreationTest
1343 void
1344 DirectoryTest::EntryCreationTest()
1345 {
1346 #ifdef TEST_R5
1347 	Outputf("(test currently omitted due to build errors related to BSymLink::SetTo())\n");
1348 #else
1349 	const char *existingFile = existingFilename;
1350 	const char *existing = existingDirname;
1351 	const char *testDir1 = testDirname1;
1352 	// create a test directory
1353 	execCommand(string("mkdir ") + testDir1);
1354 	// 1. relative path
1355 	BDirectory dir(testDir1);
1356 	CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1357 	// CreateDirectory
1358 	// dir doesn't already exist
1359 	NextSubTest();
1360 	BDirectory subdir;
1361 	string dirPathName(string(testDir1) + "/");
1362 	string entryName("subdir1");
1363 	CPPUNIT_ASSERT( dir.CreateDirectory(entryName.c_str(), &subdir) == B_OK );
1364 	CPPUNIT_ASSERT( subdir.InitCheck() == B_OK );
1365 	BEntry entry;
1366 	CPPUNIT_ASSERT( subdir.GetEntry(&entry) == B_OK );
1367 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
1368 	CPPUNIT_ASSERT( entry == BEntry((dirPathName + entryName).c_str()) );
1369 	subdir.Unset();
1370 	CPPUNIT_ASSERT( subdir.SetTo((dirPathName + entryName).c_str()) == B_OK );
1371 	subdir.Unset();
1372 	// dir does already exist
1373 	NextSubTest();
1374 	CPPUNIT_ASSERT( dir.CreateDirectory(entryName.c_str(), &subdir)
1375 					== B_FILE_EXISTS );
1376 	CPPUNIT_ASSERT( subdir.InitCheck() == B_NO_INIT );
1377 	subdir.Unset();
1378 	// CreateFile
1379 	// file doesn't already exist
1380 	NextSubTest();
1381 	BFile file;
1382 	entryName = "file1";
1383 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, true) == B_OK );
1384 	CPPUNIT_ASSERT( file.InitCheck() == B_OK );
1385 	file.Unset();
1386 	CPPUNIT_ASSERT( file.SetTo((dirPathName + entryName).c_str(),
1387 							   B_READ_ONLY) == B_OK );
1388 	file.Unset();
1389 	// file does already exist, don't fail
1390 	NextSubTest();
1391 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, false) == B_OK );
1392 	CPPUNIT_ASSERT( file.InitCheck() == B_OK );
1393 	file.Unset();
1394 	CPPUNIT_ASSERT( file.SetTo((dirPathName + entryName).c_str(),
1395 							   B_READ_ONLY) == B_OK );
1396 	file.Unset();
1397 	// file does already exist, fail
1398 	NextSubTest();
1399 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, true)
1400 					== B_FILE_EXISTS );
1401 	CPPUNIT_ASSERT( file.InitCheck() == B_NO_INIT );
1402 	file.Unset();
1403 	// CreateSymLink
1404 	// link doesn't already exist
1405 	NextSubTest();
1406 	BSymLink link;
1407 	entryName = "link1";
1408 	CPPUNIT_ASSERT( dir.CreateSymLink(entryName.c_str(), existingFile, &link)
1409 					== B_OK );
1410 	CPPUNIT_ASSERT( link.InitCheck() == B_OK );
1411 	link.Unset();
1412 	CPPUNIT_ASSERT( link.SetTo((dirPathName + entryName).c_str()) == B_OK );
1413 	link.Unset();
1414 	// link does already exist
1415 	NextSubTest();
1416 	CPPUNIT_ASSERT( dir.CreateSymLink(entryName.c_str(), existingFile, &link)
1417 					== B_FILE_EXISTS );
1418 	CPPUNIT_ASSERT( link.InitCheck() == B_NO_INIT );
1419 	link.Unset();
1420 
1421 	// 2. absolute path
1422 	dir.Unset();
1423 	execCommand(string("rm -rf ") + testDir1);
1424 	execCommand(string("mkdir ") + testDir1);
1425 	CPPUNIT_ASSERT( dir.SetTo(existing) == B_OK );
1426 	// CreateDirectory
1427 	// dir doesn't already exist
1428 	NextSubTest();
1429 	entryName = dirPathName + "subdir1";
1430 	CPPUNIT_ASSERT( dir.CreateDirectory(entryName.c_str(), &subdir) == B_OK );
1431 	CPPUNIT_ASSERT( subdir.InitCheck() == B_OK );
1432 	CPPUNIT_ASSERT( subdir.GetEntry(&entry) == B_OK );
1433 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
1434 	CPPUNIT_ASSERT( entry == BEntry(entryName.c_str()) );
1435 	subdir.Unset();
1436 	CPPUNIT_ASSERT( subdir.SetTo(entryName.c_str()) == B_OK );
1437 	subdir.Unset();
1438 	// dir does already exist
1439 	NextSubTest();
1440 	CPPUNIT_ASSERT( dir.CreateDirectory(entryName.c_str(), &subdir)
1441 					== B_FILE_EXISTS );
1442 	CPPUNIT_ASSERT( subdir.InitCheck() == B_NO_INIT );
1443 	subdir.Unset();
1444 	// CreateFile
1445 	// file doesn't already exist
1446 	NextSubTest();
1447 	entryName = dirPathName + "file1";
1448 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, true) == B_OK );
1449 	CPPUNIT_ASSERT( file.InitCheck() == B_OK );
1450 	file.Unset();
1451 	CPPUNIT_ASSERT( file.SetTo(entryName.c_str(), B_READ_ONLY) == B_OK );
1452 	file.Unset();
1453 	// file does already exist, don't fail
1454 	NextSubTest();
1455 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, false) == B_OK );
1456 	CPPUNIT_ASSERT( file.InitCheck() == B_OK );
1457 	file.Unset();
1458 	CPPUNIT_ASSERT( file.SetTo(entryName.c_str(), B_READ_ONLY) == B_OK );
1459 	file.Unset();
1460 	// file does already exist, fail
1461 	NextSubTest();
1462 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, true)
1463 					== B_FILE_EXISTS );
1464 	CPPUNIT_ASSERT( file.InitCheck() == B_NO_INIT );
1465 	file.Unset();
1466 	// CreateSymLink
1467 	// link doesn't already exist
1468 	NextSubTest();
1469 	entryName = dirPathName + "link1";
1470 	CPPUNIT_ASSERT( dir.CreateSymLink(entryName.c_str(), existingFile, &link)
1471 					== B_OK );
1472 	CPPUNIT_ASSERT( link.InitCheck() == B_OK );
1473 	link.Unset();
1474 	CPPUNIT_ASSERT( link.SetTo(entryName.c_str()) == B_OK );
1475 	link.Unset();
1476 	// link does already exist
1477 	NextSubTest();
1478 	CPPUNIT_ASSERT( dir.CreateSymLink(entryName.c_str(), existingFile, &link)
1479 					== B_FILE_EXISTS );
1480 	CPPUNIT_ASSERT( link.InitCheck() == B_NO_INIT );
1481 	link.Unset();
1482 
1483 	// 3. uninitialized BDirectory, absolute path
1484 	dir.Unset();
1485 	execCommand(string("rm -rf ") + testDir1);
1486 	execCommand(string("mkdir ") + testDir1);
1487 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
1488 	// CreateDirectory
1489 	// dir doesn't already exist
1490 	NextSubTest();
1491 	entryName = dirPathName + "subdir1";
1492 	CPPUNIT_ASSERT( dir.CreateDirectory(entryName.c_str(), &subdir) == B_OK );
1493 	CPPUNIT_ASSERT( subdir.InitCheck() == B_OK );
1494 	CPPUNIT_ASSERT( subdir.GetEntry(&entry) == B_OK );
1495 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
1496 	CPPUNIT_ASSERT( entry == BEntry(entryName.c_str()) );
1497 	subdir.Unset();
1498 	CPPUNIT_ASSERT( subdir.SetTo(entryName.c_str()) == B_OK );
1499 	subdir.Unset();
1500 	// dir does already exist
1501 	NextSubTest();
1502 	CPPUNIT_ASSERT( dir.CreateDirectory(entryName.c_str(), &subdir)
1503 					== B_FILE_EXISTS );
1504 	CPPUNIT_ASSERT( subdir.InitCheck() == B_NO_INIT );
1505 	subdir.Unset();
1506 	// CreateFile
1507 	// file doesn't already exist
1508 	NextSubTest();
1509 	entryName = dirPathName + "file1";
1510 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, true) == B_OK );
1511 	CPPUNIT_ASSERT( file.InitCheck() == B_OK );
1512 	file.Unset();
1513 	CPPUNIT_ASSERT( file.SetTo(entryName.c_str(), B_READ_ONLY) == B_OK );
1514 	file.Unset();
1515 	// file does already exist, don't fail
1516 	NextSubTest();
1517 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, false) == B_OK );
1518 	CPPUNIT_ASSERT( file.InitCheck() == B_OK );
1519 	file.Unset();
1520 	CPPUNIT_ASSERT( file.SetTo(entryName.c_str(), B_READ_ONLY) == B_OK );
1521 	file.Unset();
1522 	// file does already exist, fail
1523 	NextSubTest();
1524 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, true)
1525 					== B_FILE_EXISTS );
1526 	CPPUNIT_ASSERT( file.InitCheck() == B_NO_INIT );
1527 	file.Unset();
1528 	// CreateSymLink
1529 	// link doesn't already exist
1530 	NextSubTest();
1531 	entryName = dirPathName + "link1";
1532 	CPPUNIT_ASSERT( dir.CreateSymLink(entryName.c_str(), existingFile, &link)
1533 					== B_OK );
1534 	CPPUNIT_ASSERT( link.InitCheck() == B_OK );
1535 	link.Unset();
1536 	CPPUNIT_ASSERT( link.SetTo(entryName.c_str()) == B_OK );
1537 	link.Unset();
1538 	// link does already exist
1539 	NextSubTest();
1540 	CPPUNIT_ASSERT( dir.CreateSymLink(entryName.c_str(), existingFile, &link)
1541 					== B_FILE_EXISTS );
1542 	CPPUNIT_ASSERT( link.InitCheck() == B_NO_INIT );
1543 	link.Unset();
1544 
1545 	// 4. uninitialized BDirectory, relative path, current directory
1546 	dir.Unset();
1547 	execCommand(string("rm -rf ") + testDir1);
1548 	execCommand(string("mkdir ") + testDir1);
1549 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
1550 	chdir(testDir1);
1551 	// CreateDirectory
1552 	// dir doesn't already exist
1553 	NextSubTest();
1554 	entryName = "subdir1";
1555 	CPPUNIT_ASSERT( dir.CreateDirectory(entryName.c_str(), &subdir) == B_OK );
1556 	CPPUNIT_ASSERT( subdir.InitCheck() == B_OK );
1557 	CPPUNIT_ASSERT( subdir.GetEntry(&entry) == B_OK );
1558 	CPPUNIT_ASSERT( entry.InitCheck() == B_OK );
1559 	CPPUNIT_ASSERT( entry == BEntry((dirPathName + entryName).c_str()) );
1560 	subdir.Unset();
1561 	CPPUNIT_ASSERT( subdir.SetTo((dirPathName + entryName).c_str()) == B_OK );
1562 	subdir.Unset();
1563 	// dir does already exist
1564 	NextSubTest();
1565 	CPPUNIT_ASSERT( dir.CreateDirectory(entryName.c_str(), &subdir)
1566 					== B_FILE_EXISTS );
1567 	CPPUNIT_ASSERT( subdir.InitCheck() == B_NO_INIT );
1568 	subdir.Unset();
1569 	// CreateFile
1570 	// file doesn't already exist
1571 	NextSubTest();
1572 	entryName = "file1";
1573 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, true) == B_OK );
1574 	CPPUNIT_ASSERT( file.InitCheck() == B_OK );
1575 	file.Unset();
1576 	CPPUNIT_ASSERT( file.SetTo((dirPathName + entryName).c_str(),
1577 							   B_READ_ONLY) == B_OK );
1578 	file.Unset();
1579 	// file does already exist, don't fail
1580 	NextSubTest();
1581 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, false) == B_OK );
1582 	CPPUNIT_ASSERT( file.InitCheck() == B_OK );
1583 	file.Unset();
1584 	CPPUNIT_ASSERT( file.SetTo((dirPathName + entryName).c_str(),
1585 							   B_READ_ONLY) == B_OK );
1586 	file.Unset();
1587 	// file does already exist, fail
1588 	NextSubTest();
1589 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), &file, true)
1590 					== B_FILE_EXISTS );
1591 	CPPUNIT_ASSERT( file.InitCheck() == B_NO_INIT );
1592 	file.Unset();
1593 	// CreateSymLink
1594 	// link doesn't already exist
1595 	NextSubTest();
1596 	entryName = "link1";
1597 	CPPUNIT_ASSERT( dir.CreateSymLink(entryName.c_str(), existingFile, &link)
1598 					== B_OK );
1599 	CPPUNIT_ASSERT( link.InitCheck() == B_OK );
1600 	link.Unset();
1601 	CPPUNIT_ASSERT( link.SetTo((dirPathName + entryName).c_str()) == B_OK );
1602 	link.Unset();
1603 	// link does already exist
1604 	NextSubTest();
1605 	CPPUNIT_ASSERT( dir.CreateSymLink(entryName.c_str(), existingFile, &link)
1606 					== B_FILE_EXISTS );
1607 	CPPUNIT_ASSERT( link.InitCheck() == B_NO_INIT );
1608 	link.Unset();
1609 	chdir("/");
1610 
1611 	// 5. bad args
1612 	dir.Unset();
1613 	execCommand(string("rm -rf ") + testDir1);
1614 	execCommand(string("mkdir ") + testDir1);
1615 	CPPUNIT_ASSERT( dir.SetTo(testDir1) == B_OK );
1616 	// CreateDirectory
1617 	NextSubTest();
1618 	entryName = "subdir1";
1619 	CPPUNIT_ASSERT( equals(dir.CreateDirectory(NULL, &subdir),
1620 						   B_BAD_ADDRESS, B_BAD_VALUE) );
1621 	CPPUNIT_ASSERT( subdir.InitCheck() == B_NO_INIT );
1622 	subdir.Unset();
1623 	// CreateFile
1624 	// R5: unlike CreateDirectory/SymLink() CreateFile() returns
1625 	//	   B_ENTRY_NOT_FOUND
1626 	NextSubTest();
1627 	entryName = "file1";
1628 	CPPUNIT_ASSERT( equals(dir.CreateFile(NULL, &file), B_ENTRY_NOT_FOUND,
1629 						   B_BAD_VALUE) );
1630 	CPPUNIT_ASSERT( file.InitCheck() == B_NO_INIT );
1631 	file.Unset();
1632 	// CreateSymLink
1633 	NextSubTest();
1634 	entryName = "link1";
1635 	CPPUNIT_ASSERT( equals(dir.CreateSymLink(NULL, existingFile, &link),
1636 						   B_BAD_ADDRESS, B_BAD_VALUE) );
1637 	CPPUNIT_ASSERT( link.InitCheck() == B_NO_INIT );
1638 	CPPUNIT_ASSERT( equals(dir.CreateSymLink(entryName.c_str(), NULL, &link),
1639 						   B_BAD_ADDRESS, B_BAD_VALUE) );
1640 	CPPUNIT_ASSERT( link.InitCheck() == B_NO_INIT );
1641 	link.Unset();
1642 
1643 	// 6. uninitialized BDirectory, absolute path, no second param
1644 	dir.Unset();
1645 	execCommand(string("rm -rf ") + testDir1);
1646 	execCommand(string("mkdir ") + testDir1);
1647 	CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
1648 	// CreateDirectory
1649 	// dir doesn't already exist
1650 	NextSubTest();
1651 	entryName = dirPathName + "subdir1";
1652 	CPPUNIT_ASSERT( dir.CreateDirectory(entryName.c_str(), NULL) == B_OK );
1653 	CPPUNIT_ASSERT( subdir.SetTo(entryName.c_str()) == B_OK );
1654 	subdir.Unset();
1655 	// dir does already exist
1656 	NextSubTest();
1657 	CPPUNIT_ASSERT( dir.CreateDirectory(entryName.c_str(), NULL)
1658 					== B_FILE_EXISTS );
1659 	subdir.Unset();
1660 	// CreateFile
1661 	// file doesn't already exist
1662 	NextSubTest();
1663 	entryName = dirPathName + "file1";
1664 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), NULL, true) == B_OK );
1665 	CPPUNIT_ASSERT( file.SetTo(entryName.c_str(), B_READ_ONLY) == B_OK );
1666 	file.Unset();
1667 	// file does already exist, don't fail
1668 	NextSubTest();
1669 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), NULL, false) == B_OK );
1670 	CPPUNIT_ASSERT( file.SetTo(entryName.c_str(), B_READ_ONLY) == B_OK );
1671 	file.Unset();
1672 	// file does already exist, fail
1673 	NextSubTest();
1674 	CPPUNIT_ASSERT( dir.CreateFile(entryName.c_str(), NULL, true)
1675 					== B_FILE_EXISTS );
1676 	// CreateSymLink
1677 	// link doesn't already exist
1678 	NextSubTest();
1679 	entryName = dirPathName + "link1";
1680 	CPPUNIT_ASSERT( dir.CreateSymLink(entryName.c_str(), existingFile, NULL)
1681 					== B_OK );
1682 	CPPUNIT_ASSERT( link.SetTo(entryName.c_str()) == B_OK );
1683 	link.Unset();
1684 	// link does already exist
1685 	NextSubTest();
1686 	CPPUNIT_ASSERT( dir.CreateSymLink(entryName.c_str(), existingFile, NULL)
1687 					== B_FILE_EXISTS );
1688 #endif // ifndef TEST_R5
1689 }
1690 
1691 // AssignmentTest
1692 void
1693 DirectoryTest::AssignmentTest()
1694 {
1695 	const char *existing = existingDirname;
1696 	// 1. copy constructor
1697 	// uninitialized
1698 	NextSubTest();
1699 	{
1700 		BDirectory dir;
1701 		CPPUNIT_ASSERT( dir.InitCheck() == B_NO_INIT );
1702 		BDirectory dir2(dir);
1703 		// R5 returns B_BAD_VALUE instead of B_NO_INIT
1704 		CPPUNIT_ASSERT( equals(dir2.InitCheck(), B_BAD_VALUE, B_NO_INIT) );
1705 	}
1706 	// existing dir
1707 	NextSubTest();
1708 	{
1709 		BDirectory dir(existing);
1710 		CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1711 		BDirectory dir2(dir);
1712 		CPPUNIT_ASSERT( dir2.InitCheck() == B_OK );
1713 	}
1714 
1715 	// 2. assignment operator
1716 	// uninitialized
1717 	NextSubTest();
1718 	{
1719 		BDirectory dir;
1720 		BDirectory dir2;
1721 		dir2 = dir;
1722 		// R5 returns B_BAD_VALUE instead of B_NO_INIT
1723 		CPPUNIT_ASSERT( equals(dir2.InitCheck(), B_BAD_VALUE, B_NO_INIT) );
1724 	}
1725 	NextSubTest();
1726 	{
1727 		BDirectory dir;
1728 		BDirectory dir2(existing);
1729 		CPPUNIT_ASSERT( dir2.InitCheck() == B_OK );
1730 		dir2 = dir;
1731 		// R5 returns B_BAD_VALUE instead of B_NO_INIT
1732 		CPPUNIT_ASSERT( equals(dir2.InitCheck(), B_BAD_VALUE, B_NO_INIT) );
1733 	}
1734 	// existing dir
1735 	NextSubTest();
1736 	{
1737 		BDirectory dir(existing);
1738 		CPPUNIT_ASSERT( dir.InitCheck() == B_OK );
1739 		BDirectory dir2;
1740 		dir2 = dir;
1741 		CPPUNIT_ASSERT( dir2.InitCheck() == B_OK );
1742 	}
1743 }
1744 
1745 // CreateDirectoryTest
1746 void
1747 DirectoryTest::CreateDirectoryTest()
1748 {
1749 	const char *existingFile = existingFilename;
1750 	const char *testDir1 = testDirname1;
1751 	const char *dirLink = dirLinkname;
1752 	const char *fileLink = fileLinkname;
1753 	// 1. absolute path
1754 	execCommand(string("mkdir ") + testDir1);
1755 	// two levels
1756 	NextSubTest();
1757 	string dirPathName(string(testDir1) + "/");
1758 	string entryName(dirPathName + "subdir1/subdir1.1");
1759 	CPPUNIT_ASSERT( create_directory(entryName.c_str(), 0x1ff) == B_OK );
1760 	BDirectory subdir;
1761 	CPPUNIT_ASSERT( subdir.SetTo(entryName.c_str()) == B_OK );
1762 	subdir.Unset();
1763 	// one level
1764 	NextSubTest();
1765 	entryName = dirPathName + "subdir2";
1766 	CPPUNIT_ASSERT( create_directory(entryName.c_str(), 0x1ff) == B_OK );
1767 	CPPUNIT_ASSERT( subdir.SetTo(entryName.c_str()) == B_OK );
1768 	subdir.Unset();
1769 	// existing dir
1770 	NextSubTest();
1771 	entryName = dirPathName;
1772 	CPPUNIT_ASSERT( create_directory(entryName.c_str(), 0x1ff) == B_OK );
1773 	CPPUNIT_ASSERT( subdir.SetTo(entryName.c_str()) == B_OK );
1774 	subdir.Unset();
1775 
1776 	// 2. relative path
1777 	execCommand(string("rm -rf ") + testDir1);
1778 	execCommand(string("mkdir ") + testDir1);
1779 	chdir(testDir1);
1780 	// two levels
1781 	NextSubTest();
1782 	entryName = "subdir1/subdir1.1";
1783 	CPPUNIT_ASSERT( create_directory(entryName.c_str(), 0x1ff) == B_OK );
1784 	CPPUNIT_ASSERT( subdir.SetTo(entryName.c_str()) == B_OK );
1785 	subdir.Unset();
1786 	// one level
1787 	NextSubTest();
1788 	entryName = "subdir2";
1789 	CPPUNIT_ASSERT( create_directory(entryName.c_str(), 0x1ff) == B_OK );
1790 	CPPUNIT_ASSERT( subdir.SetTo(entryName.c_str()) == B_OK );
1791 	subdir.Unset();
1792 	// existing dir
1793 	NextSubTest();
1794 	entryName = ".";
1795 	CPPUNIT_ASSERT( create_directory(entryName.c_str(), 0x1ff) == B_OK );
1796 	CPPUNIT_ASSERT( subdir.SetTo(entryName.c_str()) == B_OK );
1797 	subdir.Unset();
1798 	chdir("/");
1799 
1800 	// 3. error cases
1801 	// existing file/link
1802 	NextSubTest();
1803 	CPPUNIT_ASSERT( equals(create_directory(existingFile, 0x1ff), B_BAD_VALUE,
1804 						   B_NOT_A_DIRECTORY) );
1805 	CPPUNIT_ASSERT( equals(create_directory(fileLink, 0x1ff), B_BAD_VALUE,
1806 						   B_NOT_A_DIRECTORY) );
1807 	CPPUNIT_ASSERT( create_directory(dirLink, 0x1ff) == B_OK );
1808 	// bad args
1809 	NextSubTest();
1810 	CPPUNIT_ASSERT( create_directory(NULL, 0x1ff) == B_BAD_VALUE );
1811 }
1812 
1813