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