vmm: refactor userspace's emsr_fakewrite()
[akaros.git] / tests / file_test.c
1 #define _LARGEFILE64_SOURCE /* needed to use lseek64 */
2
3 #include <stdio.h> 
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <parlib/arch/arch.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #include <dirent.h>
11 #include <stdlib.h>
12 #include <string.h>
13
14 #define DUMMY_STR "AAAaaaBBBbbb"
15 int main() 
16
17         FILE *file; 
18
19         file = fopen("/dir1/f1.txt","w+b");
20         if (file == NULL)
21                 printf ("Failed to open file \n");
22         fprintf(file,"%s","hello, world\n"); 
23         fclose(file); 
24
25         int fd = open("../../..//////dir1/test.txt", O_RDWR | O_CREAT );
26         char rbuf[256] = {0}, wbuf[256] = {0};
27         int retval;
28
29         retval = read(fd, rbuf, 16);
30         printf("Tried to read, got %d bytes of buf: %s\n", retval, rbuf);
31         strcpy(wbuf, DUMMY_STR);
32         retval = write(fd, wbuf, strlen(DUMMY_STR));
33         printf("Tried to write, wrote %d bytes\n", retval);
34
35         printf("Trying to seek to 0\n");
36         lseek(fd, 0, SEEK_SET);
37         retval = read(fd, rbuf, 64);
38         printf("Tried to read again, got %d bytes of buf: %s\n", retval, rbuf);
39         if (strcmp(DUMMY_STR, rbuf)) {
40                 printf("Failed to read back our dummy string!\n");
41                 return -1;
42         }
43         printf("Trying to lseek64 to 0\n");
44         lseek64(fd, 0, SEEK_SET);
45         retval = read(fd, rbuf, 64);
46         printf("Tried to read again, got %d bytes of buf: %s\n", retval, rbuf);
47         if (strcmp(DUMMY_STR, rbuf)) {
48                 printf("Failed to read back our dummy string!\n");
49                 return -1;
50         }
51
52         retval = access("/bin/laden", X_OK);
53         if (errno != ENOENT)
54                 printf("WARNING! Access error for Osama!\n");
55         retval = access("////../../////dir1/f1.txt", R_OK);
56         if (retval < 0)
57                 printf("WARNING! Access error for f1.txt!\n");
58
59         struct stat st = {0};
60         //
61         //retval = stat("/bin/mhello", &st);
62         retval = fstat(fd, &st);
63         printf("Tried to stat, was told %d\n", retval);
64         #if 0
65         printf("STAT RESULTS\n---------------------\n");
66         printf("dev       : %d\n", st.st_dev);
67         printf("ino       : %d\n", st.st_ino);
68         printf("mode      : %d\n", st.st_mode);
69         printf("nlink     : %d\n", st.st_nlink);
70         printf("uid       : %d\n", st.st_uid);
71         printf("gid       : %d\n", st.st_gid);
72         printf("rdev      : %d\n", st.st_rdev);
73         printf("size      : %d\n", st.st_size);
74         printf("blksize   : %d\n", st.st_blksize);
75         printf("blocks    : %d\n", st.st_blocks);
76         printf("atime     : %d\n", st.st_atime);
77         printf("mtime     : %d\n", st.st_mtime);
78         printf("ctime     : %d\n", st.st_ctime);
79         #endif
80
81         retval = symlink("/dir1/random.txt", "/dir2/sym-test");
82         if (retval < 0)
83                 printf("WARNING! Symlink creation failed!\n");
84         retval = readlink("/dir2/sym-test", rbuf, 256);
85         if (retval < 0)
86                 printf("WARNING! Readlink failed!\n");
87         else
88                 printf("Readlink read %d bytes\n", retval);
89         
90         /* Readdir tests: two ways to do it: */
91         DIR *dir = opendir("/dir1/");
92         struct dirent dirent_r, *dirent, *result = 0;
93         #if 0
94         dirent = readdir(dir);
95         printf("Readdir: d_ino %lld, d_off: %lld, d_reclen: %d, d_name: %s\n",
96                dirent->d_ino, dirent->d_off, dirent->d_reclen, dirent->d_name);
97         printf("TAKE TWO:\n-----------\n");
98         dirent = readdir(dir);
99         printf("Readdir: d_ino %lld, d_off: %lld, d_reclen: %d, d_name: %s\n",
100                dirent->d_ino, dirent->d_off, dirent->d_reclen, dirent->d_name);
101         #endif
102
103         retval = readdir_r(dir, &dirent_r, &result);
104         if (retval > 0)
105                 printf("WARNING! Readdir_r failed!, retval %d\n", retval);
106         if (!result)
107                 printf("End of the directory\n");
108         else
109                 printf("Dirent name: %s\n", result->d_name);
110         printf("TAKE TWO:\n-----------\n");
111         memset(&dirent_r, 0, sizeof(struct dirent));
112         retval = readdir_r(dir, &dirent_r, &result);
113         if (retval > 0)
114                 printf("WARNING! Readdir_r failed!, retval %d\n", retval);
115         if (!result)
116                 printf("End of the directory\n");
117         else
118                 printf("Dirent name: %s\n", result->d_name);
119         closedir(dir);
120         
121         /* Hardlink tests */
122         printf("Linking to /bin/hello at /dir1/hardhello\n");
123         retval = link("/bin/hello", "/dir1/hardhello");
124         if (retval < 0)
125                 printf("WARNING! Link failed!\n");
126         //breakpoint();
127         printf("Now unlinking /dir1/hardhello\n");
128         retval = unlink("/dir1/hardhello");
129         if (retval < 0)
130                 printf("WARNING! Unlink failed!\n");
131         printf("Linking to /bin/hello at /bin/hardhello2\n");
132         retval = link("/bin/hello", "/bin/hardhello2");
133         if (retval < 0)
134                 printf("WARNING! Link failed!\n");
135         printf("Now unlinking symlink /dir2/sym-test\n");
136         retval = unlink("/dir2/sym-test");
137         if (retval < 0)
138                 printf("WARNING! Unlink failed!\n");
139
140         /* getcwd, on the root dir */
141         char *cwd = getcwd(0, 0);
142         if (!cwd)
143                 printf("WARNING! Couldn't get a CWD!\n");
144         else
145                 printf("Got CWD (/): %s\n", cwd);
146         free(cwd);
147         /* chdir() tests */
148         printf("Testing basic chdir\n");
149         retval = access("dir1/f1.txt", R_OK);
150         if (retval < 0)
151                 printf("WARNING! Access error for dir1/f1.txt!\n");
152         fd = open("/dir1", O_RDONLY);
153         printf("OPENED DIR1, got fd %d\n", fd);
154         if (fd < 1)
155                 printf("WARNING!, failed to open /dir1!\n");
156         retval = fchdir(fd);
157         if (retval < 0)
158                 printf("WARNING! fchdir failed for /dir1!\n");
159         close(fd);
160         retval = access("f1.txt", R_OK);
161         if (retval < 0)
162                 printf("WARNING! Access error for f1.txt!\n");
163         cwd = getcwd(0, 0);
164         if (!cwd)
165                 printf("WARNING! Couldn't get a CWD!\n");
166         else
167                 printf("Got CWD (/dir1/): %s\n", cwd);
168         free(cwd);
169         /* change to a weird directory, see if we can still getcwd() */
170         retval = chdir("../dir2/../dir1/dir1-1");
171         if (retval < 0)
172                 printf("WARNING! Chdir failed for dir1-1!\n");
173         cwd = getcwd(0, 0);
174         if (!cwd)
175                 printf("WARNING! Couldn't get a CWD!\n");
176         else
177                 printf("Got CWD (/dir1/dir1-1/): %s\n", cwd);
178         free(cwd);
179         fd = open("/hello.txt", O_RDONLY);
180         if (fd < 1)
181                 printf("WARNING! failed to open hello.txt!\n");
182         retval = fchdir(fd);
183         if (!retval || errno != ENOTDIR)
184                 printf("WARNING! didn't fail to fchdir to hello.txt %d %d\n",
185                        retval, errno);
186         close(fd);
187
188         /* Try a chmod() */
189         printf("Trying a chmod\n");
190         retval = chmod("/dir1/dir1-1/f1-1.txt", S_IRWXO);
191         if (retval < 0)
192                 printf("WARNING! chmod failed with %d!\n", errno);
193
194         /* Try adding a directory or two! */
195         printf("Add dir3 and dir4, then remove dir4\n");
196         retval = mkdir("/dir3", S_IRWXU | S_IRWXG | S_IRWXO);
197         if (retval < 0)
198                 printf("WARNING! mkdir failed with %d!\n", errno);
199         retval = mkdir("/dir4", S_IRWXU | S_IRWXG | S_IRWXO);
200         if (retval < 0)
201                 printf("WARNING! mkdir failed with %d!\n", errno);
202         retval = rmdir("/dir4");
203         if (retval < 0)
204                 printf("WARNING! rmdir failed with %d!\n", errno);
205         breakpoint();
206         return 0;
207
208 }