os161-1.99
 All Data Structures
files2.c
00001 /*
00002  * Title   : files2
00003  * Author  : Tim Brecht
00004  * Date    : Sat Oct  2 19:08:17 EST 1999
00005  *
00006  * Some more example tests using open, close, read and write.
00007  * Assumes that files name FILE1 and FILE2 do not exist in current directory
00008  *
00009  * Modified: Thu Dec 23 17:15:50 EST 2004
00010  *   TBB - updated for Winter 2005 term.
00011  *   TBB - cleaned up and added to uw-tests for Winter 2013 term.
00012  */
00013 
00014 #include <stdio.h>
00015 #include <stdlib.h>
00016 #include <unistd.h>
00017 #include <errno.h>
00018 #include <fcntl.h>
00019 #include "../lib/testutils.h"
00020 
00021 /* Define/Uncomment this if/when returning specific error codes using errno.h */
00022 /*
00023 #define USING_ERR_CODES
00024 */
00025 #define USING_ERR_CODES
00026 
00027 /* This is a name that doesn't exist so it opening it should fail */
00028 #define BOGUS_NAME "ZZ12ZT"
00029 
00030 /* Limt the number of times we try to open one file to avoid
00031  * potentally really long executions
00032  */
00033 #define COUNT_LIMIT  (4*1024)
00034 
00035 static int fd_array[COUNT_LIMIT];
00036 
00037 int
00038 main()
00039 {
00040   int f1, f2, f3;
00041   int i = 42;
00042   int j = -999;
00043   int rc = 0;
00044   int count = 0;
00045         int saved_errno = 0;
00046 
00047   /* Uncomment this when you have failures and you are trying to debug */
00048   // TEST_VERBOSE_ON();
00049 
00050   /* Check that we can create the file */
00051   rc = open("FILE1", O_RDWR | O_CREAT | O_TRUNC);
00052   TEST_POSITIVE(rc, "Unable to create FILE1 (assumes that it doesn't exist)");
00053 
00054   /* Check that we can create the file */
00055   rc = open("FILE2", O_RDWR | O_CREAT | O_TRUNC);
00056   TEST_POSITIVE(rc, "Unable to create FILE2 (assumes that it doesn't exist)");
00057 
00058   /* check that we can open the same file many times */
00059   f1 = open("FILE1", O_RDWR);
00060   TEST_POSITIVE(f1, "Unable to open FILE1 first time");
00061 
00062   f2 = open("FILE1", O_RDWR);
00063   TEST_POSITIVE(f2, "Unable to open FILE1 second time");
00064 
00065   f3 = open("FILE1", O_RDWR);
00066   TEST_POSITIVE(f3, "Unable to open FILE1 third time");
00067 
00068   /* Check that f1 != f2 != f3 */
00069   TEST_NOT_EQUAL(f1, f2, "Using same fd for multiple opens f1 = f2");
00070   TEST_NOT_EQUAL(f2, f3, "Using same fd for multiple opens f2 = f3");
00071 
00072   rc = close(f1);
00073   TEST_EQUAL(rc, SUCCESS, "Unable to close f1");
00074   rc = close(f2);
00075   TEST_EQUAL(rc, SUCCESS, "Unable to close f2");
00076   rc = close(f3);
00077   TEST_EQUAL(rc, SUCCESS, "Unable to close f3");
00078 
00079   /* Try writing to a closed file should fail */
00080   rc = write(f1, (char *) &i, sizeof(i));
00081         saved_errno = errno;
00082   TEST_NEGATIVE(rc, "write to closed file f1 should fail");
00083 #ifdef USING_ERR_CODES
00084   TEST_EQUAL(saved_errno, EBADF, "Expected EBADF when writing to closed file f1");
00085 #endif /* USING_ERR_CODES */
00086 
00087   /* Try reading from a closed file should fail */
00088   rc = read(f2, (char *) &j, sizeof(j));
00089         saved_errno = errno;
00090   TEST_NEGATIVE(rc, "read from closed file f2 should fail");
00091 #ifdef USING_ERR_CODES
00092   TEST_EQUAL(saved_errno, EBADF, "Expected EBADF when reading from closed file f2");
00093 #endif /* USING_ERR_CODES */
00094 
00095   rc = close(0xdeadbeef);
00096         saved_errno = errno;
00097   TEST_NEGATIVE(rc, "close on invalid file id didn't return error code");
00098 #ifdef USING_ERR_CODES
00099   TEST_EQUAL(saved_errno, EBADF, "Expected EBADF when closing invalid file fd");
00100 #endif /* USING_ERR_CODES */
00101 
00102   rc = open(BOGUS_NAME, O_RDWR);
00103         saved_errno = errno;
00104   TEST_NEGATIVE(rc, "open non-existant file returns incorrect value");
00105 #ifdef USING_ERR_CODES
00106   TEST_EQUAL(saved_errno, ENOENT, "Expected ENOENT when opening non existant file");
00107 #endif /* USING_ERR_CODES */
00108 
00109   /* Open read only */
00110   f1 = open("FILE1", O_RDONLY);
00111   TEST_POSITIVE(f1, "Unable to open FILE1");
00112 
00113   /* Try writing to read only file */
00114   rc = write(f1, "hello", 5);
00115         saved_errno = errno;
00116   TEST_NEGATIVE(rc, "Trying to write to read only file does not fail");
00117 #ifdef USING_ERR_CODES
00118   TEST_EQUAL(saved_errno, EBADF, "Expected EBAD when trying to write to read only file");
00119 #endif /* USING_ERR_CODES */
00120   
00121   /* Open write only */
00122   f2 = open("FILE2", O_WRONLY);
00123   TEST_POSITIVE(f1, "Unable to open FILE2");
00124 
00125   /* Try reading from write only file */
00126   rc = read(f2, &i, 1);
00127         saved_errno = errno;
00128   TEST_NEGATIVE(rc, "Trying to read from write only file does not fail");
00129 #ifdef USING_ERR_CODES
00130   TEST_EQUAL(saved_errno, EBADF, "Expected EBAD when trying to read from write only file");
00131 #endif /* USING_ERR_CODES */
00132 
00133   rc = close(f1);
00134   TEST_EQUAL(rc, SUCCESS, "Unable to close f1");
00135 
00136   rc = close(f2);
00137   TEST_EQUAL(rc, SUCCESS, "Unable to close f2");
00138   
00139   do {
00140     f1 = open("FILE1", O_RDWR);
00141                 saved_errno = errno;
00142     if (f1 >= 0) {
00143       fd_array[count] = f1;
00144                         count++;
00145                 }
00146   } while (f1 >= 0 && count < COUNT_LIMIT);
00147 
00148   if (count == COUNT_LIMIT) {
00149     printf("WARNING: THERE MAY NOT BE A LIMIT ON THE NUMBER OF OPEN FILES\n");
00150   } else {
00151     TEST_NEGATIVE(f1, "Opening too many files doesn't return error code");
00152 #ifdef USING_ERR_CODES
00153     TEST_EQUAL_ONE_OF(saved_errno, EMFILE, ENFILE, "Expected one of EMFILE or ENFILE when opening too many files");
00154 #endif /* USING_ERR_CODES */
00155   }
00156 
00157   TEST_POSITIVE(count, "Count of open files expected to be > 0");
00158   printf("Number of files opened = %d\n", count);
00159 
00160   /* Close them all so we have more fds available to do other things with */
00161   for (i=0; i<count; i++) {
00162     rc = close(fd_array[i]);
00163     TEST_EQUAL(rc, 0, "Expected close to return 0 for success");
00164   }
00165 
00166   /* Try passing a bogus address, should return failure code, should not crash kernel */
00167   rc = open((char *) 0xffffffff, O_RDWR);
00168         saved_errno = errno;
00169   TEST_NEGATIVE(rc, "open file using bad address doesn't return error code");
00170 #ifdef USING_ERR_CODES
00171   TEST_EQUAL(saved_errno, EFAULT, "Expected EFAULT for invalid address for filename");
00172 #endif /* USING_ERR_CODES */
00173 
00174   TEST_STATS();
00175 
00176   exit(0);
00177 }
 All Data Structures