xref: /minix/minix/tests/test65.c (revision 99e8768d)
1 #include <sys/mount.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <sys/wait.h>
5 #include <fcntl.h>
6 #include <stdio.h>
7 #include <unistd.h>
8 
9 int max_error = 0;
10 #include "common.h"
11 
12 
13 #define TESTMNT		"testmnt"
14 #define TESTFILE	"test.txt"
15 #define TESTSTRING	"foobar"
16 #define RAMDISK		"/dev/ram5"
17 #define RAMDISK_SIZE	"2048"
18 #define SILENT		" > /dev/null 2>&1"
19 
20 void basic_test(void);
21 void bomb(char const *msg);
22 void skip(char const *msg);
23 void create_partition(void);
24 void verify_tools(void);
25 
26 void
basic_test(void)27 basic_test(void)
28 {
29 /* Write a string to a file, read it back, and confirm it's identical */
30 	int status;
31 	char cmd_buf[1024];
32 	char file_buf[sizeof(TESTSTRING)*10];
33 	int fd;
34 
35 	subtest = 3;
36 
37 	/* Write test string to test file */
38 	snprintf(cmd_buf, sizeof(cmd_buf), "echo -n %s > %s/%s\n",
39 		TESTSTRING, TESTMNT, TESTFILE);
40 	status = system(cmd_buf);
41 	if (WEXITSTATUS(status) != 0)
42 		bomb("Unable to echo string to file");
43 
44 	/* Flush to disk and unmount, remount */
45 	system("sync");
46 	system("umount " RAMDISK SILENT);
47 	snprintf(cmd_buf, sizeof(cmd_buf), "mount -t ntfs-3g %s %s %s",
48 		RAMDISK, TESTMNT, SILENT);
49 	status = system(cmd_buf);
50 	if (WEXITSTATUS(status) != 0)
51 		bomb("Unable to mount NTFS partition (1)");
52 
53 	/* Open file and verify contents */
54 	if ((fd = open(TESTMNT "/" TESTFILE, O_RDONLY)) < 0) e(1);
55 	if (read(fd, file_buf, sizeof(file_buf)) != strlen(TESTSTRING)) e(2);
56 	(void) close(fd);
57 	system("umount " RAMDISK SILENT);
58 	if (strncmp(file_buf, TESTSTRING, strlen(TESTSTRING))) e(3);
59 }
60 
61 void
skip(char const * msg)62 skip(char const *msg)
63 {
64 	system("umount " RAMDISK SILENT);
65 	printf("%s\n", msg);
66 	quit();
67 }
68 
69 void
bomb(char const * msg)70 bomb(char const *msg)
71 {
72 	system("umount " RAMDISK SILENT);
73 	printf("%s\n", msg);
74 	e(99);
75 	quit();
76 }
77 
78 void
create_partition(void)79 create_partition(void)
80 {
81 	int status;
82 	char mntcmd[1024];
83 
84 	subtest = 1;
85 
86 	if (getuid() != 0 && setuid(0) != 0) e(1);
87 	status = system("ramdisk " RAMDISK_SIZE " " RAMDISK SILENT);
88 	if (WEXITSTATUS(status) != 0)
89 		bomb("Unable to create ramdisk");
90 
91 	status = system("mkntfs " RAMDISK SILENT);
92 	if (WEXITSTATUS(status) != 0)
93 		bomb("Unable to create NTFS file system on " RAMDISK);
94 
95 	if (mkdir(TESTMNT, 0755) != 0)
96 		bomb("Unable to create directory for mounting");
97 
98 	snprintf(mntcmd, sizeof(mntcmd), "mount -t ntfs-3g %s %s %s",
99 		RAMDISK, TESTMNT, SILENT);
100 	status = system(mntcmd);
101 	if (WEXITSTATUS(status) != 0)
102 		bomb("Unable to mount NTFS partition (1)");
103 }
104 
105 void
verify_tools(void)106 verify_tools(void)
107 {
108 	int status1, status2;
109 
110 	subtest = 1;
111 
112 	status1 = system("which mkntfs > /dev/null 2>&1");
113 	status2 = system("which ntfs-3g > /dev/null 2>&1");
114 	if (WEXITSTATUS(status1) != 0 || WEXITSTATUS(status2) != 0)
115 		skip("mkntfs or ntfs-3g not found, skipping test");
116 }
117 
118 int
main(int argc,char * argv[])119 main(int argc, char *argv[])
120 {
121 	start(65);
122 	verify_tools();
123 	create_partition();
124 	basic_test();
125 	quit();
126 	return(-1);	/* Unreachable */
127 }
128 
129