aboutsummaryrefslogtreecommitdiff
path: root/stdio-common/tst-fopen.c
blob: 8c1fefd116f9f58198ca39aa971caf563e273cc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/* Basic test for fopen.
   Copyright (C) 2025 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <support/check.h>
#include <support/temp_file.h>
#include <support/xstdio.h>

#define APPENDED_TEXT "This is appended text. "
#define DEFAULT_TEXT "Lorem ipsum dolor sit amet, consectetur " \
  "adipiscing elit, sed do eiusmod tempor incididunt ut labore et " \
  "dolore magna aliqua."
#define MAX_BUFFER_SIZE 300


static int
do_test (void)
{
  char *temp_file;
  FILE *fd_file = NULL;
  char read_buffer[MAX_BUFFER_SIZE] = "";
  size_t ret;

  /* Prepare files. */
  int fd = create_temp_file ("tst-fopen.", &temp_file);
  TEST_VERIFY_EXIT (fd != -1);
  fd_file = fdopen (fd, "w");
  ret = fwrite (DEFAULT_TEXT, sizeof (char), strlen (DEFAULT_TEXT), fd_file);
  TEST_COMPARE (ret, strlen (DEFAULT_TEXT));
  xfclose (fd_file);

  /* Test 1: This checks for fopen with mode "r".  Open text file for
     reading.  The stream is positioned at the beginning of the file. */
  printf ("Test 1: This checks for fopen with mode \"r\".\n");
  fd_file = fopen (temp_file, "r");
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_VERIFY (fd_file != NULL);
  TEST_COMPARE (ftell (fd_file), 0);
  /* Read should succeed. */
  ret = fread (read_buffer, sizeof (char), MAX_BUFFER_SIZE, fd_file);
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_COMPARE (ret, strlen (DEFAULT_TEXT));
  TEST_VERIFY (strcmp (read_buffer, DEFAULT_TEXT) == 0);
  /* Write should fail. */
  errno = 0;
  ret = fwrite (DEFAULT_TEXT, sizeof (char), strlen (DEFAULT_TEXT), fd_file);
  TEST_VERIFY (ferror (fd_file) != 0);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (ret, 0);
  clearerr (fd_file);
  /* Opening non-existent file should fail. */
  xfclose (fd_file);
  errno = 0;
  fd_file = fopen ("file-that-does-not-exist", "r");
  TEST_VERIFY (fd_file == NULL);
  TEST_COMPARE (errno, ENOENT);
  TEST_VERIFY (fd_file == NULL);

  memset (read_buffer, 0, MAX_BUFFER_SIZE);

  /* Test 2: This checks for fopen with mode "r+".  Open for reading and
     writing.  The stream is positioned at the beginning of the file. */
  printf ("Test 2: This checks for fopen with mode \"r+\".\n");
  fd_file = fopen (temp_file, "r+");
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_VERIFY (fd_file != NULL);
  TEST_COMPARE (ftell (fd_file), 0);
  /* Read should succeed. */
  ret = fread (read_buffer, sizeof (char), MAX_BUFFER_SIZE, fd_file);
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_COMPARE (ret, strlen (DEFAULT_TEXT));
  TEST_VERIFY (strcmp (read_buffer, DEFAULT_TEXT) == 0);
  fflush (fd_file);
  /* File position indicator expected at 0 + read bytes. */
  TEST_COMPARE (ftell (fd_file), ret);
  /* Write should succeed. */
  ret = fwrite (DEFAULT_TEXT, sizeof (char), strlen (DEFAULT_TEXT), fd_file);
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_COMPARE (ret, strlen (DEFAULT_TEXT));
  /* Opening non-existent file should fail. */
  xfclose (fd_file);
  errno = 0;
  fd_file = fopen ("file-that-does-not-exist", "r+");
  TEST_VERIFY (fd_file == NULL);
  TEST_COMPARE (errno, ENOENT);
  TEST_VERIFY (fd_file == NULL);

  memset (read_buffer, 0, MAX_BUFFER_SIZE);

  /* Test 3: This checks for fopen with mode "w".  Truncate file to zero
     length or create text file for writing.  The stream is positioned
     at the beginning of the file. */
  printf ("Test 3: This checks for fopen with mode \"w\".\n");
  fd_file = fopen (temp_file, "w");
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_VERIFY (fd_file != NULL);
  TEST_COMPARE (ftell (fd_file), 0);
  /* Read should fail. */
  errno = 0;
  ret = fread (read_buffer, sizeof (char), MAX_BUFFER_SIZE, fd_file);
  TEST_VERIFY (ferror (fd_file) != 0);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (ret, 0);
  clearerr (fd_file);
  /* Write should succeed. */
  ret = fwrite (DEFAULT_TEXT, sizeof (char), strlen (DEFAULT_TEXT), fd_file);
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_COMPARE (ret, strlen (DEFAULT_TEXT));
  /* Opening non-existent file should succeed. */
  xfclose (fd_file);
  fd_file = fopen ("/tmp/file-that-does-not-exist", "w");
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_VERIFY (fd_file != NULL);
  TEST_COMPARE (ftell (fd_file), 0);

  xfclose (fd_file);
  remove ("/tmp/file-that-does-not-exist");
  memset (read_buffer, 0, MAX_BUFFER_SIZE);

  /* Test 4: This checks for fopen with mode "w+".  Open for reading and
     writing.  The file is created if it does not exist, otherwise it is
     truncated.  The stream is positioned at the beginning of the file.
   */
  printf ("Test 4: This checks for fopen with mode \"w+\".\n");
  fd_file = fopen (temp_file, "w+");
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_VERIFY (fd_file != NULL);
  TEST_COMPARE (ftell (fd_file), 0);
  /* Read should succeed. */
  ret = fread (read_buffer, sizeof (char), MAX_BUFFER_SIZE, fd_file);
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_COMPARE (ret, 0);
  TEST_VERIFY (read_buffer[0] == '\0');
  /* Write should succeed. */
  ret = fwrite (DEFAULT_TEXT, sizeof (char), strlen (DEFAULT_TEXT), fd_file);
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_COMPARE (ret, strlen (DEFAULT_TEXT));
  /* Opening non-existent file should succeed. */
  xfclose (fd_file);
  fd_file = fopen ("/tmp/file-that-does-not-exist", "w+");
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_VERIFY (fd_file != NULL);
  TEST_COMPARE (ftell (fd_file), 0);

  xfclose (fd_file);
  remove ("/tmp/file-that-does-not-exist");
  memset (read_buffer, 0, MAX_BUFFER_SIZE);

  /* Test 5: This checks for fopen with mode "a".  Open for appending
     (writing at end of file).  The file is created if it does not
     exist.  The stream is positioned at the end of the file. */
  printf ("Test 5: This checks for fopen with mode \"a\".\n");
  fd_file = fopen (temp_file, "a");
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_VERIFY (fd_file != NULL);
  TEST_COMPARE (ftell (fd_file), strlen (DEFAULT_TEXT));
  /* Read should fail. */
  errno = 0;
  ret = fread (read_buffer, sizeof (char), MAX_BUFFER_SIZE, fd_file);
  TEST_VERIFY (ferror (fd_file) != 0);
  TEST_COMPARE (errno, EBADF);
  TEST_COMPARE (ret, 0);
  clearerr (fd_file);
  /* Write should succeed. */
  ret = fwrite (APPENDED_TEXT, sizeof (char), strlen (APPENDED_TEXT), fd_file);
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_COMPARE (ret, strlen (APPENDED_TEXT));
  /* The file position indicator for the stream is advanced by the
   *  number of bytes successfully read or written. */
  TEST_COMPARE (ftell (fd_file), strlen (DEFAULT_TEXT) + ret);
  /* Opening non-existent file should succeed. */
  xfclose (fd_file);
  fd_file = fopen ("/tmp/file-that-does-not-exist", "a");
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_VERIFY (fd_file != NULL);
  TEST_COMPARE (ftell (fd_file), 0);

  xfclose (fd_file);
  remove ("/tmp/file-that-does-not-exist");
  memset (read_buffer, 0, MAX_BUFFER_SIZE);

  /* Test 6: This checks for fopen with mode "a+".  Open for reading and
     appending (writing at end of file).  The file is created if it does
     not exist.  Output is always appended to the end of the file.  The
     initial file position for reading is at the beginning of the file,
     but it is advanced to the end prior to each write. */
  printf ("Test 6: This checks for fopen with mode \"a+\".\n");
  errno = 0;
  fd_file = fopen (temp_file, "a+");
  TEST_COMPARE (errno, 0);
  TEST_VERIFY (fd_file != NULL);
  TEST_COMPARE (ftell (fd_file), 0);
  /* Read should succeed. */
  ret = fread (read_buffer, sizeof (char), MAX_BUFFER_SIZE, fd_file);
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_COMPARE (ret, strlen (DEFAULT_TEXT) + strlen (APPENDED_TEXT));
  TEST_VERIFY (strcmp (read_buffer, DEFAULT_TEXT APPENDED_TEXT) == 0);
  /* Write should succeed. */
  const char* SECOND_APPEND = "This is second append.";
  ret = fwrite (SECOND_APPEND, sizeof (char), strlen (SECOND_APPEND), fd_file);
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_COMPARE (ret, strlen (SECOND_APPEND));
  /* The file position indicator for the stream is advanced by the
     number of bytes successfully read or written. */
  TEST_COMPARE (ftell (fd_file),
                strlen (DEFAULT_TEXT) + strlen (APPENDED_TEXT) + ret);
  /* Opening non-existent file should succeed. */
  xfclose (fd_file);
  fd_file = fopen ("/tmp/file-that-does-not-exist", "a+");
  TEST_COMPARE (ferror (fd_file), 0);
  TEST_VERIFY (fd_file != NULL);
  TEST_COMPARE (ftell (fd_file), 0);

  xfclose (fd_file);
  remove ("/tmp/file-that-does-not-exist");
  memset (read_buffer, 0, MAX_BUFFER_SIZE);

  /* Test 7: This checks for fopen with other valid modes set, such as
     "rc", "we" or "am".  The test calls fopen with these modes and
     checks that no errors appear.  */
  printf ("Test 7: This checks for fopen with other valid modes set, "
          "such as \"rc\", \"we\" or \"am\".\n");
  /* These modes all operate correctly with the file already present. */
  static const char *valid_modes[] =
    { "rc", "we", "am", "r+x", "wb+", "ab", 0 };
  const char **p = valid_modes;
  while (*p != 0)
    {
      fd_file = fopen (temp_file, *p);
      TEST_COMPARE (ferror (fd_file), 0);
      TEST_VERIFY (fd_file != NULL);
      xfclose (fd_file);
      ++p;
    }

  /* Test 8: This checks for fopen with invalid modes.  The test calls
     fopen with these modes and checks that opening existing files with
     invalid mode fails and that opening non-existing files with invalid
     mode doesn't create a new file. */
  printf ("Test 8: This checks for fopen with invalid modes.\n");
  static const char *invalid_modes[] = { "0", "tr", "z", "x", " ", 0 };
  p = invalid_modes;
  while (*p != 0)
    {
      errno = 0;
      fd_file = fopen (temp_file, *p);
      TEST_VERIFY (fd_file == NULL);
      TEST_COMPARE (errno, EINVAL);
      errno = 0;
      fd_file = fopen ("/tmp/file-that-does-not-exist", *p);
      TEST_VERIFY (fd_file == NULL);
      TEST_COMPARE (errno, EINVAL);
      ++p;
      TEST_VERIFY (access ("/tmp/file-that-does-not-exist", F_OK) == -1);
    }

  return 0;
}

#include <support/test-driver.c>