xref: /minix/minix/tests/test65.c (revision 83133719)
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
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
62 skip(char const *msg)
63 {
64 	system("umount " RAMDISK SILENT);
65 	printf("%s\n", msg);
66 	quit();
67 }
68 
69 void
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
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
106 verify_tools(void)
107 {
108 	int status;
109 
110 	subtest = 1;
111 	status = system("which mkntfs > /dev/null 2>&1");
112 	if (WEXITSTATUS(status) != 0) {
113 		skip("mkntfs not found. Please install ntfsprogs (pkgin in "
114 			"ntfsprogs)");
115 	}
116 	status = system("which ntfs-3g > /dev/null 2>&1");
117 	if (WEXITSTATUS(status) != 0) {
118 		skip("ntfs-3g not found. Please install fuse-ntfs-3g-1.1120 "
119 			"(pkgin in fuse-ntfs-3g-1.1120)");
120 	}
121 }
122 
123 int
124 main(int argc, char *argv[])
125 {
126 	start(65);
127 	verify_tools();
128 	create_partition();
129 	basic_test();
130 	quit();
131 	return(-1);	/* Unreachable */
132 }
133 
134