stdio_test.cpp revision cbd85b9cc1c5dd0bb1a5691143cd5adcbecdba77
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
20#include <limits.h>
21#include <stdio.h>
22#include <sys/types.h>
23#include <sys/stat.h>
24#include <unistd.h>
25
26TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) {
27  FILE* fp = tmpfile();
28  ASSERT_TRUE(fp != NULL);
29
30  int fd = fileno(fp);
31  ASSERT_NE(fd, -1);
32
33  struct stat sb;
34  int rc = fstat(fd, &sb);
35  ASSERT_NE(rc, -1);
36  ASSERT_EQ(sb.st_mode & 0777, 0600U);
37
38  rc = fprintf(fp, "hello\n");
39  ASSERT_EQ(rc, 6);
40
41  rewind(fp);
42
43  char buf[16];
44  char* s = fgets(buf, sizeof(buf), fp);
45  ASSERT_TRUE(s != NULL);
46  ASSERT_STREQ("hello\n", s);
47
48  fclose(fp);
49}
50
51TEST(stdio, getdelim) {
52  FILE* fp = tmpfile();
53  ASSERT_TRUE(fp != NULL);
54
55  const char* line_written = "This  is a test";
56  int rc = fprintf(fp, "%s", line_written);
57  ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
58
59  rewind(fp);
60
61  char* word_read = NULL;
62  size_t allocated_length = 0;
63
64  const char* expected[] = { "This ", " ", "is ", "a ", "test" };
65  for (size_t i = 0; i < 5; ++i) {
66    ASSERT_FALSE(feof(fp));
67    ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
68    ASSERT_GE(allocated_length, strlen(expected[i]));
69    ASSERT_STREQ(word_read, expected[i]);
70  }
71  // The last read should have set the end-of-file indicator for the stream.
72  ASSERT_TRUE(feof(fp));
73  clearerr(fp);
74
75  // getdelim returns -1 but doesn't set errno if we're already at EOF.
76  // It should set the end-of-file indicator for the stream, though.
77  errno = 0;
78  ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
79  ASSERT_EQ(0, errno);
80  ASSERT_TRUE(feof(fp));
81
82  free(word_read);
83  fclose(fp);
84}
85
86TEST(stdio, getdelim_invalid) {
87  FILE* fp = tmpfile();
88
89  char* buffer = NULL;
90  size_t buffer_length = 0;
91
92  // The first argument can't be NULL.
93  errno = 0;
94  ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
95  ASSERT_EQ(EINVAL, errno);
96
97  // The second argument can't be NULL.
98  errno = 0;
99  ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
100  ASSERT_EQ(EINVAL, errno);
101
102  // The stream can't be closed.
103  fclose(fp);
104  errno = 0;
105  ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
106  // glibc sometimes doesn't set errno in this particular case.
107#if defined(__BIONIC__)
108  ASSERT_EQ(EBADF, errno);
109#endif
110}
111
112TEST(stdio, getline) {
113  FILE* fp = tmpfile();
114  ASSERT_TRUE(fp != NULL);
115
116  const char* line_written = "This is a test for getline\n";
117  const size_t line_count = 5;
118
119  for (size_t i = 0; i < line_count; ++i) {
120    int rc = fprintf(fp, "%s", line_written);
121    ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
122  }
123
124  rewind(fp);
125
126  char* line_read = NULL;
127  size_t allocated_length = 0;
128
129  size_t read_line_count = 0;
130  ssize_t read_char_count;
131  while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
132    ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
133    ASSERT_GE(allocated_length, strlen(line_written));
134    ASSERT_STREQ(line_read, line_written);
135    ++read_line_count;
136  }
137  ASSERT_EQ(read_line_count, line_count);
138
139  // The last read should have set the end-of-file indicator for the stream.
140  ASSERT_TRUE(feof(fp));
141  clearerr(fp);
142
143  // getline returns -1 but doesn't set errno if we're already at EOF.
144  // It should set the end-of-file indicator for the stream, though.
145  errno = 0;
146  ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
147  ASSERT_EQ(0, errno);
148  ASSERT_TRUE(feof(fp));
149
150  free(line_read);
151  fclose(fp);
152}
153
154TEST(stdio, getline_invalid) {
155  FILE* fp = tmpfile();
156
157  char* buffer = NULL;
158  size_t buffer_length = 0;
159
160  // The first argument can't be NULL.
161  errno = 0;
162  ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
163  ASSERT_EQ(EINVAL, errno);
164
165  // The second argument can't be NULL.
166  errno = 0;
167  ASSERT_EQ(getline(&buffer, NULL, fp), -1);
168  ASSERT_EQ(EINVAL, errno);
169
170  // The stream can't be closed.
171  fclose(fp);
172  errno = 0;
173  ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
174  // glibc sometimes doesn't set errno in this particular case.
175#if defined(__BIONIC__)
176  ASSERT_EQ(EBADF, errno);
177#endif
178}
179
180TEST(stdio, printf_ssize_t) {
181  // http://b/8253769
182  ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
183  ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
184  // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
185  // error: format '%zd' expects argument of type 'signed size_t',
186  //     but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
187  ssize_t v = 1;
188  char buf[32];
189  snprintf(buf, sizeof(buf), "%zd", v);
190}
191
192#if !defined(__GLIBC__)
193TEST(stdio, snprintf_n_format_specifier_not_implemented) {
194  char buf[32];
195  int i = 0;
196  // We deliberately don't implement %n, so it's treated like
197  // any other unrecognized format specifier.
198  EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
199  EXPECT_EQ(0, i);
200  EXPECT_STREQ("a n b", buf);
201}
202#endif
203
204TEST(stdio, snprintf_smoke) {
205  char buf[BUFSIZ];
206
207  snprintf(buf, sizeof(buf), "a");
208  EXPECT_STREQ("a", buf);
209
210  snprintf(buf, sizeof(buf), "%%");
211  EXPECT_STREQ("%", buf);
212
213  snprintf(buf, sizeof(buf), "01234");
214  EXPECT_STREQ("01234", buf);
215
216  snprintf(buf, sizeof(buf), "a%sb", "01234");
217  EXPECT_STREQ("a01234b", buf);
218
219  char* s = NULL;
220  snprintf(buf, sizeof(buf), "a%sb", s);
221  EXPECT_STREQ("a(null)b", buf);
222
223  snprintf(buf, sizeof(buf), "aa%scc", "bb");
224  EXPECT_STREQ("aabbcc", buf);
225
226  snprintf(buf, sizeof(buf), "a%cc", 'b');
227  EXPECT_STREQ("abc", buf);
228
229  snprintf(buf, sizeof(buf), "a%db", 1234);
230  EXPECT_STREQ("a1234b", buf);
231
232  snprintf(buf, sizeof(buf), "a%db", -8123);
233  EXPECT_STREQ("a-8123b", buf);
234
235  snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
236  EXPECT_STREQ("a16b", buf);
237
238  snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
239  EXPECT_STREQ("a16b", buf);
240
241  snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
242  EXPECT_STREQ("a68719476736b", buf);
243
244  snprintf(buf, sizeof(buf), "a%ldb", 70000L);
245  EXPECT_STREQ("a70000b", buf);
246
247  snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
248  EXPECT_STREQ("a0xb0001234b", buf);
249
250  snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
251  EXPECT_STREQ("a12abz", buf);
252
253  snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
254  EXPECT_STREQ("a12ABz", buf);
255
256  snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
257  EXPECT_STREQ("a00123456z", buf);
258
259  snprintf(buf, sizeof(buf), "a%5dz", 1234);
260  EXPECT_STREQ("a 1234z", buf);
261
262  snprintf(buf, sizeof(buf), "a%05dz", 1234);
263  EXPECT_STREQ("a01234z", buf);
264
265  snprintf(buf, sizeof(buf), "a%8dz", 1234);
266  EXPECT_STREQ("a    1234z", buf);
267
268  snprintf(buf, sizeof(buf), "a%-8dz", 1234);
269  EXPECT_STREQ("a1234    z", buf);
270
271  snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
272  EXPECT_STREQ("Aabcdef     Z", buf);
273
274  snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
275  EXPECT_STREQ("Ahello:1234Z", buf);
276
277  snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
278  EXPECT_STREQ("a005:5:05z", buf);
279
280  void* p = NULL;
281  snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
282#if defined(__BIONIC__)
283  EXPECT_STREQ("a5,0x0z", buf);
284#else
285  EXPECT_STREQ("a5,(nil)z", buf);
286#endif
287
288  snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
289  EXPECT_STREQ("a68719476736,6,7,8z", buf);
290
291  snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
292  EXPECT_STREQ("a_1.230000_b", buf);
293
294  snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
295  EXPECT_STREQ("a_3.14_b", buf);
296}
297
298TEST(stdio, snprintf_d_INT_MAX) {
299  char buf[BUFSIZ];
300  snprintf(buf, sizeof(buf), "%d", INT_MAX);
301  EXPECT_STREQ("2147483647", buf);
302}
303
304TEST(stdio, snprintf_d_INT_MIN) {
305  char buf[BUFSIZ];
306  snprintf(buf, sizeof(buf), "%d", INT_MIN);
307  EXPECT_STREQ("-2147483648", buf);
308}
309
310TEST(stdio, snprintf_ld_LONG_MAX) {
311  char buf[BUFSIZ];
312  snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
313#if __LP64__
314  EXPECT_STREQ("9223372036854775807", buf);
315#else
316  EXPECT_STREQ("2147483647", buf);
317#endif
318}
319
320TEST(stdio, snprintf_ld_LONG_MIN) {
321  char buf[BUFSIZ];
322  snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
323#if __LP64__
324  EXPECT_STREQ("-9223372036854775808", buf);
325#else
326  EXPECT_STREQ("-2147483648", buf);
327#endif
328}
329
330TEST(stdio, snprintf_lld_LLONG_MAX) {
331  char buf[BUFSIZ];
332  snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
333  EXPECT_STREQ("9223372036854775807", buf);
334}
335
336TEST(stdio, snprintf_lld_LLONG_MIN) {
337  char buf[BUFSIZ];
338  snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
339  EXPECT_STREQ("-9223372036854775808", buf);
340}
341
342TEST(stdio, popen) {
343  FILE* fp = popen("cat /proc/version", "r");
344  ASSERT_TRUE(fp != NULL);
345
346  char buf[16];
347  char* s = fgets(buf, sizeof(buf), fp);
348  buf[13] = '\0';
349  ASSERT_STREQ("Linux version", s);
350
351  ASSERT_EQ(0, pclose(fp));
352}
353
354TEST(stdio, getc) {
355  FILE* fp = fopen("/proc/version", "r");
356  ASSERT_TRUE(fp != NULL);
357  ASSERT_EQ('L', getc(fp));
358  ASSERT_EQ('i', getc(fp));
359  ASSERT_EQ('n', getc(fp));
360  ASSERT_EQ('u', getc(fp));
361  ASSERT_EQ('x', getc(fp));
362  fclose(fp);
363}
364
365TEST(stdio, putc) {
366  FILE* fp = fopen("/proc/version", "r");
367  ASSERT_TRUE(fp != NULL);
368  ASSERT_EQ(EOF, putc('x', fp));
369  fclose(fp);
370}
371