00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
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 
00022 
00023 
00024 
00025 #define USING_ERR_CODES
00026 
00027 
00028 #define BOGUS_NAME "ZZ12ZT"
00029 
00030 
00031 
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   
00048   
00049 
00050   
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   
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   
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   
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   
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 
00086 
00087   
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 
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 
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 
00108 
00109   
00110   f1 = open("FILE1", O_RDONLY);
00111   TEST_POSITIVE(f1, "Unable to open FILE1");
00112 
00113   
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 
00120   
00121   
00122   f2 = open("FILE2", O_WRONLY);
00123   TEST_POSITIVE(f1, "Unable to open FILE2");
00124 
00125   
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 
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 
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   
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   
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 
00173 
00174   TEST_STATS();
00175 
00176   exit(0);
00177 }