xref: /haiku/src/tests/kits/storage/BasicTest.cpp (revision 52a380120846174213ccce9c4aab0dda17c72083)
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 		 : StorageKit::TestCase(),
27 		   fSubTestNumber(0),
28 		   fAvailableFDs(0)
29 {
30 }
31 
32 // setUp
33 void
34 BasicTest::setUp()
35 {
36 	fAvailableFDs = count_available_fds();
37 	SaveCWD();
38 	fSubTestNumber = 0;
39 }
40 
41 // tearDown
42 void
43 BasicTest::tearDown()
44 {
45 	RestoreCWD();
46 	nextSubTestBlock();
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 }
54 
55 // nextSubTest
56 void
57 BasicTest::nextSubTest()
58 {
59 	if (shell.BeVerbose()) {
60 		printf("[%ld]", fSubTestNumber++);
61 		fflush(stdout);
62 	}
63 }
64 
65 // nextSubTestBlock
66 void
67 BasicTest::nextSubTestBlock()
68 {
69 	if (shell.BeVerbose())
70 		printf("\n");
71 	fSubTestNumber = 0;
72 }
73 
74 // execCommand
75 //
76 // Calls system() with the supplied string.
77 void
78 BasicTest::execCommand(const string &cmdLine)
79 {
80 	system(cmdLine.c_str());
81 }
82 
83 // dumpStat
84 void
85 BasicTest::dumpStat(struct stat &st)
86 {
87 	printf("stat:\n");
88 	printf("  st_dev    : %lx\n", st.st_dev);
89 	printf("  st_ino    : %Lx\n", st.st_ino);
90 	printf("  st_mode   : %x\n", st.st_mode);
91 	printf("  st_nlink  : %x\n", st.st_nlink);
92 	printf("  st_uid    : %x\n", st.st_uid);
93 	printf("  st_gid    : %x\n", st.st_gid);
94 	printf("  st_size   : %Ld\n", st.st_size);
95 	printf("  st_blksize: %ld\n", st.st_blksize);
96 	printf("  st_atime  : %lx\n", st.st_atime);
97 	printf("  st_mtime  : %lx\n", st.st_mtime);
98 	printf("  st_ctime  : %lx\n", st.st_ctime);
99 	printf("  st_crtime : %lx\n", st.st_crtime);
100 }
101 
102 // createVolume
103 void
104 BasicTest::createVolume(string imageFile, string mountPoint, int32 megs)
105 {
106 	char megsString[16];
107 	sprintf(megsString, "%ld", megs);
108 	execCommand(string("dd if=/dev/zero of=") + imageFile
109 					+ " bs=1M count=" + megsString
110 					+ " &> /dev/null"
111 				+ " ; mkbfs " + imageFile
112 					+ " > /dev/null"
113 				+ " ; sync"
114 				+ " ; mkdir " + mountPoint
115 				+ " ; mount " + imageFile + " " + mountPoint);
116 }
117 
118 // deleteVolume
119 void
120 BasicTest::deleteVolume(string imageFile, string mountPoint)
121 {
122 	execCommand(string("sync")
123 				+ " ; unmount " + mountPoint
124 				+ " ; rmdir " + mountPoint
125 				+ " ; rm " + imageFile);
126 }
127 
128