Updates bootable USB tools
[akaros.git] / tests / file_test.c
index d05494a..f9ab9a4 100644 (file)
@@ -1,4 +1,6 @@
-#include <rstdio.h> 
+#define _LARGEFILE64_SOURCE /* needed to use lseek64 */
+
+#include <stdio.h> 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
@@ -6,7 +8,10 @@
 #include <unistd.h>
 #include <errno.h>
 #include <dirent.h>
+#include <stdlib.h>
+#include <string.h>
 
+#define DUMMY_STR "AAAaaaBBBbbb"
 int main() 
 { 
        FILE *file; 
@@ -21,13 +26,26 @@ int main()
        int retval;
        retval = read(fd, rbuf, 16);
        printf("Tried to read, got %d bytes of buf: %s\n", retval, rbuf);
-       strcpy(wbuf, "paul <3's the new 61c");
-       retval = write(fd, wbuf, 22);
+       strcpy(wbuf, DUMMY_STR);
+       retval = write(fd, wbuf, strlen(DUMMY_STR));
        printf("Tried to write, wrote %d bytes\n", retval);
+
        printf("Trying to seek to 0\n");
        lseek(fd, 0, SEEK_SET);
        retval = read(fd, rbuf, 64);
        printf("Tried to read again, got %d bytes of buf: %s\n", retval, rbuf);
+       if (strcmp(DUMMY_STR, rbuf)) {
+               printf("Failed to read back our dummy string!\n");
+               return -1;
+       }
+       printf("Trying to lseek64 to 0\n");
+       lseek64(fd, 0, SEEK_SET);
+       retval = read(fd, rbuf, 64);
+       printf("Tried to read again, got %d bytes of buf: %s\n", retval, rbuf);
+       if (strcmp(DUMMY_STR, rbuf)) {
+               printf("Failed to read back our dummy string!\n");
+               return -1;
+       }
 
        retval = access("/bin/laden", X_OK);
        if (errno != ENOENT)
@@ -40,6 +58,7 @@ int main()
        //retval = stat("/bin/mhello", &st);
        retval = fstat(fd, &st);
        printf("Tried to stat, was told %d\n", retval);
+       #if 0
        printf("STAT RESULTS\n---------------------\n");
        printf("dev       : %d\n", st.st_dev);
        printf("ino       : %d\n", st.st_ino);
@@ -54,6 +73,7 @@ int main()
        printf("atime     : %d\n", st.st_atime);
        printf("mtime     : %d\n", st.st_mtime);
        printf("ctime     : %d\n", st.st_ctime);
+       #endif
 
        retval = symlink("/dir1/random.txt", "/dir2/sym-test");
        if (retval < 0)
@@ -93,12 +113,92 @@ int main()
                printf("End of the directory\n");
        else
                printf("Dirent name: %s\n", result->d_name);
+       closedir(dir);
        
        /* Hardlink tests */
        printf("Linking to /bin/hello at /dir1/hardhello\n");
        retval = link("/bin/hello", "/dir1/hardhello");
        if (retval < 0)
                printf("WARNING! Link failed!\n");
+       //breakpoint();
+       printf("Now unlinking /dir1/hardhello\n");
+       retval = unlink("/dir1/hardhello");
+       if (retval < 0)
+               printf("WARNING! Unlink failed!\n");
+       printf("Linking to /bin/hello at /bin/hardhello2\n");
+       retval = link("/bin/hello", "/bin/hardhello2");
+       if (retval < 0)
+               printf("WARNING! Link failed!\n");
+       printf("Now unlinking symlink /dir2/sym-test\n");
+       retval = unlink("/dir2/sym-test");
+       if (retval < 0)
+               printf("WARNING! Unlink failed!\n");
 
+       /* getcwd, on the root dir */
+       char *cwd = getcwd(0, 0);
+       if (!cwd)
+               printf("WARNING! Couldn't get a CWD!\n");
+       else
+               printf("Got CWD (/): %s\n", cwd);
+       free(cwd);
+       /* chdir() tests */
+       printf("Testing basic chdir\n");
+       retval = access("dir1/f1.txt", R_OK);
+       if (retval < 0)
+               printf("WARNING! Access error for dir1/f1.txt!\n");
+       fd = open("/dir1", O_RDONLY);
+       printf("OPENED DIR1, got fd %d\n", fd);
+       if (fd < 1)
+               printf("WARNING!, failed to open /dir1!\n");
+       retval = fchdir(fd);
+       if (retval < 0)
+               printf("WARNING! fchdir failed for /dir1!\n");
+       close(fd);
+       retval = access("f1.txt", R_OK);
+       if (retval < 0)
+               printf("WARNING! Access error for f1.txt!\n");
+       cwd = getcwd(0, 0);
+       if (!cwd)
+               printf("WARNING! Couldn't get a CWD!\n");
+       else
+               printf("Got CWD (/dir1/): %s\n", cwd);
+       free(cwd);
+       /* change to a weird directory, see if we can still getcwd() */
+       retval = chdir("../dir2/../dir1/dir1-1");
+       if (retval < 0)
+               printf("WARNING! Chdir failed for dir1-1!\n");
+       cwd = getcwd(0, 0);
+       if (!cwd)
+               printf("WARNING! Couldn't get a CWD!\n");
+       else
+               printf("Got CWD (/dir1/dir1-1/): %s\n", cwd);
+       free(cwd);
+       fd = open("/hello.txt", O_RDONLY);
+       if (fd < 1)
+               printf("WARNING! failed to open hello.txt!\n");
+       retval = fchdir(fd);
+       if (!retval || errno != ENOTDIR)
+               printf("WARNING! didn't fail to fchdir to hello.txt %d %d\n", retval,
+                      errno);
+       close(fd);
+
+       /* Try a chmod() */
+       printf("Trying a chmod\n");
+       retval = chmod("/dir1/dir1-1/f1-1.txt", S_IRWXO);
+       if (retval < 0)
+               printf("WARNING! chmod failed with %d!\n", errno);
+
+       /* Try adding a directory or two! */
+       printf("Add dir3 and dir4, then remove dir4\n");
+       retval = mkdir("/dir3", S_IRWXU | S_IRWXG | S_IRWXO);
+       if (retval < 0)
+               printf("WARNING! mkdir failed with %d!\n", errno);
+       retval = mkdir("/dir4", S_IRWXU | S_IRWXG | S_IRWXO);
+       if (retval < 0)
+               printf("WARNING! mkdir failed with %d!\n", errno);
+       retval = rmdir("/dir4");
+       if (retval < 0)
+               printf("WARNING! rmdir failed with %d!\n", errno);
        breakpoint();
+
 }