files2.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Title : files2
  3. * Author : Tim Brecht
  4. * Date : Sat Oct 2 19:08:17 EST 1999
  5. *
  6. * Some more example tests using open, close, read and write.
  7. * Assumes that files name FILE1 and FILE2 do not exist in current directory
  8. *
  9. * Modified: Thu Dec 23 17:15:50 EST 2004
  10. * TBB - updated for Winter 2005 term.
  11. * TBB - cleaned up and added to uw-tests for Winter 2013 term.
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include "../lib/testutils.h"
  19. /* Define/Uncomment this if/when returning specific error codes using errno.h */
  20. /*
  21. #define USING_ERR_CODES
  22. */
  23. #define USING_ERR_CODES
  24. /* This is a name that doesn't exist so it opening it should fail */
  25. #define BOGUS_NAME "ZZ12ZT"
  26. /* Limt the number of times we try to open one file to avoid
  27. * potentally really long executions
  28. */
  29. #define COUNT_LIMIT (4*1024)
  30. static int fd_array[COUNT_LIMIT];
  31. int
  32. main()
  33. {
  34. int f1, f2, f3;
  35. int i = 42;
  36. int j = -999;
  37. int rc = 0;
  38. int count = 0;
  39. int saved_errno = 0;
  40. /* Uncomment this when you have failures and you are trying to debug */
  41. // TEST_VERBOSE_ON();
  42. /* Check that we can create the file */
  43. rc = open("FILE1", O_RDWR | O_CREAT | O_TRUNC);
  44. TEST_POSITIVE(rc, "Unable to create FILE1 (assumes that it doesn't exist)");
  45. /* Check that we can create the file */
  46. rc = open("FILE2", O_RDWR | O_CREAT | O_TRUNC);
  47. TEST_POSITIVE(rc, "Unable to create FILE2 (assumes that it doesn't exist)");
  48. /* check that we can open the same file many times */
  49. f1 = open("FILE1", O_RDWR);
  50. TEST_POSITIVE(f1, "Unable to open FILE1 first time");
  51. f2 = open("FILE1", O_RDWR);
  52. TEST_POSITIVE(f2, "Unable to open FILE1 second time");
  53. f3 = open("FILE1", O_RDWR);
  54. TEST_POSITIVE(f3, "Unable to open FILE1 third time");
  55. /* Check that f1 != f2 != f3 */
  56. TEST_NOT_EQUAL(f1, f2, "Using same fd for multiple opens f1 = f2");
  57. TEST_NOT_EQUAL(f2, f3, "Using same fd for multiple opens f2 = f3");
  58. rc = close(f1);
  59. TEST_EQUAL(rc, SUCCESS, "Unable to close f1");
  60. rc = close(f2);
  61. TEST_EQUAL(rc, SUCCESS, "Unable to close f2");
  62. rc = close(f3);
  63. TEST_EQUAL(rc, SUCCESS, "Unable to close f3");
  64. /* Try writing to a closed file should fail */
  65. rc = write(f1, (char *) &i, sizeof(i));
  66. saved_errno = errno;
  67. TEST_NEGATIVE(rc, "write to closed file f1 should fail");
  68. #ifdef USING_ERR_CODES
  69. TEST_EQUAL(saved_errno, EBADF, "Expected EBADF when writing to closed file f1");
  70. #endif /* USING_ERR_CODES */
  71. /* Try reading from a closed file should fail */
  72. rc = read(f2, (char *) &j, sizeof(j));
  73. saved_errno = errno;
  74. TEST_NEGATIVE(rc, "read from closed file f2 should fail");
  75. #ifdef USING_ERR_CODES
  76. TEST_EQUAL(saved_errno, EBADF, "Expected EBADF when reading from closed file f2");
  77. #endif /* USING_ERR_CODES */
  78. rc = close(0xdeadbeef);
  79. saved_errno = errno;
  80. TEST_NEGATIVE(rc, "close on invalid file id didn't return error code");
  81. #ifdef USING_ERR_CODES
  82. TEST_EQUAL(saved_errno, EBADF, "Expected EBADF when closing invalid file fd");
  83. #endif /* USING_ERR_CODES */
  84. rc = open(BOGUS_NAME, O_RDWR);
  85. saved_errno = errno;
  86. TEST_NEGATIVE(rc, "open non-existant file returns incorrect value");
  87. #ifdef USING_ERR_CODES
  88. TEST_EQUAL(saved_errno, ENOENT, "Expected ENOENT when opening non existant file");
  89. #endif /* USING_ERR_CODES */
  90. /* Open read only */
  91. f1 = open("FILE1", O_RDONLY);
  92. TEST_POSITIVE(f1, "Unable to open FILE1");
  93. /* Try writing to read only file */
  94. rc = write(f1, "hello", 5);
  95. saved_errno = errno;
  96. TEST_NEGATIVE(rc, "Trying to write to read only file does not fail");
  97. #ifdef USING_ERR_CODES
  98. TEST_EQUAL(saved_errno, EBADF, "Expected EBAD when trying to write to read only file");
  99. #endif /* USING_ERR_CODES */
  100. /* Open write only */
  101. f2 = open("FILE2", O_WRONLY);
  102. TEST_POSITIVE(f1, "Unable to open FILE2");
  103. /* Try reading from write only file */
  104. rc = read(f2, &i, 1);
  105. saved_errno = errno;
  106. TEST_NEGATIVE(rc, "Trying to read from write only file does not fail");
  107. #ifdef USING_ERR_CODES
  108. TEST_EQUAL(saved_errno, EBADF, "Expected EBAD when trying to read from write only file");
  109. #endif /* USING_ERR_CODES */
  110. rc = close(f1);
  111. TEST_EQUAL(rc, SUCCESS, "Unable to close f1");
  112. rc = close(f2);
  113. TEST_EQUAL(rc, SUCCESS, "Unable to close f2");
  114. do {
  115. f1 = open("FILE1", O_RDWR);
  116. saved_errno = errno;
  117. if (f1 >= 0) {
  118. fd_array[count] = f1;
  119. count++;
  120. }
  121. } while (f1 >= 0 && count < COUNT_LIMIT);
  122. if (count == COUNT_LIMIT) {
  123. printf("WARNING: THERE MAY NOT BE A LIMIT ON THE NUMBER OF OPEN FILES\n");
  124. } else {
  125. TEST_NEGATIVE(f1, "Opening too many files doesn't return error code");
  126. #ifdef USING_ERR_CODES
  127. TEST_EQUAL_ONE_OF(saved_errno, EMFILE, ENFILE, "Expected one of EMFILE or ENFILE when opening too many files");
  128. #endif /* USING_ERR_CODES */
  129. }
  130. TEST_POSITIVE(count, "Count of open files expected to be > 0");
  131. printf("Number of files opened = %d\n", count);
  132. /* Close them all so we have more fds available to do other things with */
  133. for (i=0; i<count; i++) {
  134. rc = close(fd_array[i]);
  135. TEST_EQUAL(rc, 0, "Expected close to return 0 for success");
  136. }
  137. /* Try passing a bogus address, should return failure code, should not crash kernel */
  138. rc = open((char *) 0xffffffff, O_RDWR);
  139. saved_errno = errno;
  140. TEST_NEGATIVE(rc, "open file using bad address doesn't return error code");
  141. #ifdef USING_ERR_CODES
  142. TEST_EQUAL(saved_errno, EFAULT, "Expected EFAULT for invalid address for filename");
  143. #endif /* USING_ERR_CODES */
  144. TEST_STATS();
  145. exit(0);
  146. }