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