xref: /haiku/src/tests/kits/storage/BasicTest.cpp (revision 425ac1b60a56f4df7a0e88bd784545c0ec4fa01f)
1 // BasicTest.cpp
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 
7 #include <set>
8 using std::set;
9 
10 #include "BasicTest.h"
11 
12 // count_available_fds
13 static
14 int32
count_available_fds()15 count_available_fds()
16 {
17 	set<int> fds;
18 	int fd;
19 	while ((fd = dup(1)) != -1)
20 		fds.insert(fd);
21 	for (set<int>::iterator it = fds.begin(); it != fds.end(); it++)
22 		close(*it);
23 	return fds.size();
24 }
25 
26 // constructor
BasicTest()27 BasicTest::BasicTest()
28 		 : BTestCase(),
29 		   fSubTestNumber(0),
30 		   fAvailableFDs(0)
31 {
32 }
33 
34 // setUp
35 void
setUp()36 BasicTest::setUp()
37 {
38 	BTestCase::setUp();
39 	fAvailableFDs = count_available_fds();
40 	SaveCWD();
41 	fSubTestNumber = 0;
42 }
43 
44 // tearDown
45 void
tearDown()46 BasicTest::tearDown()
47 {
48 	RestoreCWD();
49 	int32 availableFDs = count_available_fds();
50 	if (availableFDs != fAvailableFDs) {
51 		printf("WARNING: Number of available file descriptors has changed "
52 			   "during test: %ld -> %ld\n", fAvailableFDs, availableFDs);
53 		fAvailableFDs = availableFDs;
54 	}
55 	BTestCase::tearDown();
56 }
57 
58 // execCommand
59 //
60 // Calls system() with the supplied string.
61 void
execCommand(const string & cmdLine)62 BasicTest::execCommand(const string &cmdLine)
63 {
64 	system(cmdLine.c_str());
65 }
66 
67 // dumpStat
68 void
dumpStat(struct stat & st)69 BasicTest::dumpStat(struct stat &st)
70 {
71 	printf("stat:\n");
72 	printf("  st_dev    : %lx\n", st.st_dev);
73 	printf("  st_ino    : %Lx\n", st.st_ino);
74 	printf("  st_mode   : %x\n", st.st_mode);
75 	printf("  st_nlink  : %x\n", st.st_nlink);
76 	printf("  st_uid    : %x\n", st.st_uid);
77 	printf("  st_gid    : %x\n", st.st_gid);
78 	printf("  st_size   : %lld\n", st.st_size);
79 	printf("  st_blksize: %ld\n", st.st_blksize);
80 	printf("  st_atime  : %lx\n", st.st_atime);
81 	printf("  st_mtime  : %lx\n", st.st_mtime);
82 	printf("  st_ctime  : %lx\n", st.st_ctime);
83 	printf("  st_crtime : %lx\n", st.st_crtime);
84 }
85 
86 // createVolume
87 void
createVolume(string imageFile,string mountPoint,int32 megs,bool makeMountPoint)88 BasicTest::createVolume(string imageFile, string mountPoint, int32 megs,
89 						bool makeMountPoint)
90 {
91 	char megsString[16];
92 	sprintf(megsString, "%ld", megs);
93 	execCommand(string("dd if=/dev/zero of=") + imageFile
94 					+ " bs=1M count=" + megsString
95 					+ " &> /dev/null"
96 				+ " ; mkbfs " + imageFile
97 					+ " > /dev/null"
98 				+ " ; sync"
99 				+ (makeMountPoint ? " ; mkdir " + mountPoint : "")
100 				+ " ; mount " + imageFile + " " + mountPoint);
101 }
102 
103 // deleteVolume
104 void
deleteVolume(string imageFile,string mountPoint,bool deleteMountPoint)105 BasicTest::deleteVolume(string imageFile, string mountPoint,
106 						bool deleteMountPoint)
107 {
108 	execCommand(string("sync")
109 				+ " ; unmount " + mountPoint
110 				+ (deleteMountPoint ? " ; rmdir " + mountPoint : "")
111 				+ " ; rm " + imageFile);
112 }
113 
114