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
16void
17StatableTest::setUp()
18{
19	BasicTest::setUp();
20}
21
22// tearDown
23void
24StatableTest::tearDown()
25{
26	BasicTest::tearDown();
27}
28
29// GetStatTest
30void
31StatableTest::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
63void
64StatableTest::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
92void
93StatableTest::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		CPPUNIT_ASSERT( volume == BVolume(st.st_dev) );
140	}
141	testEntries.delete_all();
142	// test with uninitialized objects
143	NextSubTest();
144	CreateUninitializedStatables(testEntries);
145	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
146		node_ref ref;
147		uid_t owner;
148		gid_t group;
149		mode_t perms;
150		off_t size;
151		time_t mtime;
152		time_t ctime;
153		time_t atime;
154		BVolume volume;
155		CPPUNIT_ASSERT( statable->GetNodeRef(&ref) == B_NO_INIT );
156		CPPUNIT_ASSERT( statable->GetOwner(&owner) == B_NO_INIT );
157		CPPUNIT_ASSERT( statable->GetGroup(&group) == B_NO_INIT );
158		CPPUNIT_ASSERT( statable->GetPermissions(&perms) == B_NO_INIT );
159		CPPUNIT_ASSERT( statable->GetSize(&size) == B_NO_INIT );
160		CPPUNIT_ASSERT( statable->GetModificationTime(&mtime) == B_NO_INIT );
161		CPPUNIT_ASSERT( statable->GetCreationTime(&ctime) == B_NO_INIT );
162		CPPUNIT_ASSERT( statable->GetAccessTime(&atime) == B_NO_INIT );
163		CPPUNIT_ASSERT( statable->GetVolume(&volume) == B_NO_INIT );
164	}
165	testEntries.delete_all();
166	// bad args
167	NextSubTest();
168	CreateROStatables(testEntries);
169	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
170// R5: crashs, if passing NULL to any of these methods
171#if !TEST_R5
172		CPPUNIT_ASSERT( statable->GetNodeRef(NULL) == B_BAD_VALUE );
173		CPPUNIT_ASSERT( statable->GetOwner(NULL)  == B_BAD_VALUE );
174		CPPUNIT_ASSERT( statable->GetGroup(NULL)  == B_BAD_VALUE );
175		CPPUNIT_ASSERT( statable->GetPermissions(NULL)  == B_BAD_VALUE );
176		CPPUNIT_ASSERT( statable->GetSize(NULL)  == B_BAD_VALUE );
177		CPPUNIT_ASSERT( statable->GetModificationTime(NULL)  == B_BAD_VALUE );
178		CPPUNIT_ASSERT( statable->GetCreationTime(NULL)  == B_BAD_VALUE );
179		CPPUNIT_ASSERT( statable->GetAccessTime(NULL)  == B_BAD_VALUE );
180		CPPUNIT_ASSERT( statable->GetVolume(NULL)  == B_BAD_VALUE );
181#endif
182	}
183	testEntries.delete_all();
184}
185
186// SetXYZTest
187void
188StatableTest::SetXYZTest()
189{
190	TestStatables testEntries;
191	BStatable *statable;
192	string entryName;
193	// test with existing entries
194	NextSubTest();
195	CreateRWStatables(testEntries);
196	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
197		struct stat st;
198		uid_t owner = 0xdad;
199		gid_t group = 0xdee;
200		mode_t perms = 0x0ab;	// -w- r-x -wx	-- unusual enough? ;-)
201		time_t mtime = 1234567;
202		time_t ctime = 654321;
203// R5: access time unused
204#if !TEST_R5 && !TEST_OBOS /* !!!POSIX ONLY!!! */
205		time_t atime = 2345678;
206#endif
207		CPPUNIT_ASSERT( statable->SetOwner(owner) == B_OK );
208		CPPUNIT_ASSERT( statable->SetGroup(group) == B_OK );
209		CPPUNIT_ASSERT( statable->SetPermissions(perms) == B_OK );
210		CPPUNIT_ASSERT( statable->SetModificationTime(mtime) == B_OK );
211		CPPUNIT_ASSERT( statable->SetCreationTime(ctime) == B_OK );
212#if !TEST_R5 && !TEST_OBOS /* !!!POSIX ONLY!!! */
213		CPPUNIT_ASSERT( statable->SetAccessTime(atime) == B_OK );
214#endif
215		CPPUNIT_ASSERT( lstat(entryName.c_str(), &st) == 0 );
216		CPPUNIT_ASSERT( owner == st.st_uid );
217		CPPUNIT_ASSERT( group == st.st_gid );
218		CPPUNIT_ASSERT( perms == (st.st_mode & S_IUMSK) );
219		CPPUNIT_ASSERT( mtime == st.st_mtime );
220		CPPUNIT_ASSERT( ctime == st.st_crtime );
221#if !TEST_R5 && !TEST_OBOS /* !!!POSIX ONLY!!! */
222		CPPUNIT_ASSERT( atime == st.st_atime );
223#endif
224	}
225	testEntries.delete_all();
226	// test with uninitialized objects
227	NextSubTest();
228	CreateUninitializedStatables(testEntries);
229	for (testEntries.rewind(); testEntries.getNext(statable, entryName); ) {
230		uid_t owner = 0xdad;
231		gid_t group = 0xdee;
232		mode_t perms = 0x0ab;	// -w- r-x -wx	-- unusual enough? ;-)
233		time_t mtime = 1234567;
234		time_t ctime = 654321;
235		time_t atime = 2345678;
236		CPPUNIT_ASSERT( statable->SetOwner(owner) != B_OK );
237		CPPUNIT_ASSERT( statable->SetGroup(group) != B_OK );
238		CPPUNIT_ASSERT( statable->SetPermissions(perms) != B_OK );
239		CPPUNIT_ASSERT( statable->SetModificationTime(mtime) != B_OK );
240		CPPUNIT_ASSERT( statable->SetCreationTime(ctime) != B_OK );
241		CPPUNIT_ASSERT( statable->SetAccessTime(atime) != B_OK );
242	}
243	testEntries.delete_all();
244}
245