blob: ebb27d4a58ce7130f373f5bdd14dc600e1c7c242 [file] [log] [blame]
Elliott Hughes91875dc2012-09-24 17:55:15 -07001/*
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>
Elliott Hughes1d13c642013-09-23 16:02:39 -070020#include <limits.h>
Elliott Hughes7823f322014-04-14 12:11:28 -070021#include <math.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070022#include <stdio.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <unistd.h>
Elliott Hughes05493712014-04-17 17:30:03 -070026#include <wchar.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070027
28TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) {
29 FILE* fp = tmpfile();
30 ASSERT_TRUE(fp != NULL);
31
32 int fd = fileno(fp);
33 ASSERT_NE(fd, -1);
34
35 struct stat sb;
36 int rc = fstat(fd, &sb);
37 ASSERT_NE(rc, -1);
38 ASSERT_EQ(sb.st_mode & 0777, 0600U);
39
40 rc = fprintf(fp, "hello\n");
41 ASSERT_EQ(rc, 6);
42
43 rewind(fp);
44
45 char buf[16];
46 char* s = fgets(buf, sizeof(buf), fp);
47 ASSERT_TRUE(s != NULL);
48 ASSERT_STREQ("hello\n", s);
49
50 fclose(fp);
51}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030052
53TEST(stdio, getdelim) {
54 FILE* fp = tmpfile();
55 ASSERT_TRUE(fp != NULL);
56
57 const char* line_written = "This is a test";
58 int rc = fprintf(fp, "%s", line_written);
59 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
60
61 rewind(fp);
62
63 char* word_read = NULL;
64 size_t allocated_length = 0;
65
66 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
67 for (size_t i = 0; i < 5; ++i) {
68 ASSERT_FALSE(feof(fp));
69 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
70 ASSERT_GE(allocated_length, strlen(expected[i]));
71 ASSERT_STREQ(word_read, expected[i]);
72 }
73 // The last read should have set the end-of-file indicator for the stream.
74 ASSERT_TRUE(feof(fp));
75 clearerr(fp);
76
77 // getdelim returns -1 but doesn't set errno if we're already at EOF.
78 // It should set the end-of-file indicator for the stream, though.
79 errno = 0;
80 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080081 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030082 ASSERT_TRUE(feof(fp));
83
84 free(word_read);
85 fclose(fp);
86}
87
88TEST(stdio, getdelim_invalid) {
89 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -080090 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030091
92 char* buffer = NULL;
93 size_t buffer_length = 0;
94
95 // The first argument can't be NULL.
96 errno = 0;
97 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080098 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030099
100 // The second argument can't be NULL.
101 errno = 0;
102 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800103 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300104
105 // The stream can't be closed.
106 fclose(fp);
107 errno = 0;
108 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
Christopher Ferriscbd85b92013-11-15 15:16:01 -0800109 // glibc sometimes doesn't set errno in this particular case.
110#if defined(__BIONIC__)
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800111 ASSERT_EQ(EBADF, errno);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800112#endif // __BIONIC__
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300113}
114
115TEST(stdio, getline) {
116 FILE* fp = tmpfile();
117 ASSERT_TRUE(fp != NULL);
118
119 const char* line_written = "This is a test for getline\n";
120 const size_t line_count = 5;
121
122 for (size_t i = 0; i < line_count; ++i) {
123 int rc = fprintf(fp, "%s", line_written);
124 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
125 }
126
127 rewind(fp);
128
129 char* line_read = NULL;
130 size_t allocated_length = 0;
131
132 size_t read_line_count = 0;
133 ssize_t read_char_count;
134 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
135 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
136 ASSERT_GE(allocated_length, strlen(line_written));
137 ASSERT_STREQ(line_read, line_written);
138 ++read_line_count;
139 }
140 ASSERT_EQ(read_line_count, line_count);
141
142 // The last read should have set the end-of-file indicator for the stream.
143 ASSERT_TRUE(feof(fp));
144 clearerr(fp);
145
146 // getline returns -1 but doesn't set errno if we're already at EOF.
147 // It should set the end-of-file indicator for the stream, though.
148 errno = 0;
149 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800150 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300151 ASSERT_TRUE(feof(fp));
152
153 free(line_read);
154 fclose(fp);
155}
156
157TEST(stdio, getline_invalid) {
158 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800159 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300160
161 char* buffer = NULL;
162 size_t buffer_length = 0;
163
164 // The first argument can't be NULL.
165 errno = 0;
166 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800167 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300168
169 // The second argument can't be NULL.
170 errno = 0;
171 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800172 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300173
174 // The stream can't be closed.
175 fclose(fp);
176 errno = 0;
177 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
Christopher Ferriscbd85b92013-11-15 15:16:01 -0800178 // glibc sometimes doesn't set errno in this particular case.
179#if defined(__BIONIC__)
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800180 ASSERT_EQ(EBADF, errno);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800181#endif // __BIONIC__
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300182}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000183
184TEST(stdio, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800185 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800186 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800187 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
188 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000189 // error: format '%zd' expects argument of type 'signed size_t',
190 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
191 ssize_t v = 1;
192 char buf[32];
193 snprintf(buf, sizeof(buf), "%zd", v);
194}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800195
Elliott Hughes05493712014-04-17 17:30:03 -0700196// https://code.google.com/p/android/issues/detail?id=64886
197TEST(stdio, snprintf_a) {
198 char buf[BUFSIZ];
199 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
200 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
201}
202
203TEST(stdio, snprintf_lc) {
204 char buf[BUFSIZ];
205 wint_t wc = L'a';
206 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
207 EXPECT_STREQ("<a>", buf);
208}
209
210TEST(stdio, snprintf_ls) {
211 char buf[BUFSIZ];
212 wchar_t* ws = NULL;
213 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
214 EXPECT_STREQ("<(null)>", buf);
215
216 wchar_t chars[] = { L'h', L'i', 0 };
217 ws = chars;
218 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
219 EXPECT_STREQ("<hi>", buf);
220}
221
222TEST(stdio, snprintf_n) {
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700223 char buf[32];
224 int i = 0;
Elliott Hughes05493712014-04-17 17:30:03 -0700225 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "a %n b", &i));
226 EXPECT_EQ(2, i);
227 EXPECT_STREQ("a b", buf);
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700228}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700229
Elliott Hughes1d13c642013-09-23 16:02:39 -0700230TEST(stdio, snprintf_smoke) {
231 char buf[BUFSIZ];
232
233 snprintf(buf, sizeof(buf), "a");
234 EXPECT_STREQ("a", buf);
235
236 snprintf(buf, sizeof(buf), "%%");
237 EXPECT_STREQ("%", buf);
238
239 snprintf(buf, sizeof(buf), "01234");
240 EXPECT_STREQ("01234", buf);
241
242 snprintf(buf, sizeof(buf), "a%sb", "01234");
243 EXPECT_STREQ("a01234b", buf);
244
245 char* s = NULL;
246 snprintf(buf, sizeof(buf), "a%sb", s);
247 EXPECT_STREQ("a(null)b", buf);
248
249 snprintf(buf, sizeof(buf), "aa%scc", "bb");
250 EXPECT_STREQ("aabbcc", buf);
251
252 snprintf(buf, sizeof(buf), "a%cc", 'b');
253 EXPECT_STREQ("abc", buf);
254
255 snprintf(buf, sizeof(buf), "a%db", 1234);
256 EXPECT_STREQ("a1234b", buf);
257
258 snprintf(buf, sizeof(buf), "a%db", -8123);
259 EXPECT_STREQ("a-8123b", buf);
260
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700261 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700262 EXPECT_STREQ("a16b", buf);
263
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700264 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700265 EXPECT_STREQ("a16b", buf);
266
267 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
268 EXPECT_STREQ("a68719476736b", buf);
269
270 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
271 EXPECT_STREQ("a70000b", buf);
272
273 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
274 EXPECT_STREQ("a0xb0001234b", buf);
275
276 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
277 EXPECT_STREQ("a12abz", buf);
278
279 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
280 EXPECT_STREQ("a12ABz", buf);
281
282 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
283 EXPECT_STREQ("a00123456z", buf);
284
285 snprintf(buf, sizeof(buf), "a%5dz", 1234);
286 EXPECT_STREQ("a 1234z", buf);
287
288 snprintf(buf, sizeof(buf), "a%05dz", 1234);
289 EXPECT_STREQ("a01234z", buf);
290
291 snprintf(buf, sizeof(buf), "a%8dz", 1234);
292 EXPECT_STREQ("a 1234z", buf);
293
294 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
295 EXPECT_STREQ("a1234 z", buf);
296
297 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
298 EXPECT_STREQ("Aabcdef Z", buf);
299
300 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
301 EXPECT_STREQ("Ahello:1234Z", buf);
302
303 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
304 EXPECT_STREQ("a005:5:05z", buf);
305
306 void* p = NULL;
307 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700308#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700309 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800310#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700311 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800312#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700313
314 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
315 EXPECT_STREQ("a68719476736,6,7,8z", buf);
316
317 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
318 EXPECT_STREQ("a_1.230000_b", buf);
319
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700320 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700321 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400322
323 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
324 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700325}
326
Elliott Hughes7823f322014-04-14 12:11:28 -0700327TEST(stdio, snprintf_f_special) {
328 char buf[BUFSIZ];
329 snprintf(buf, sizeof(buf), "%f", nanf(""));
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700330 EXPECT_STRCASEEQ("NaN", buf);
Elliott Hughes7823f322014-04-14 12:11:28 -0700331
332 snprintf(buf, sizeof(buf), "%f", HUGE_VALF);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700333 EXPECT_STRCASEEQ("Inf", buf);
Elliott Hughes7823f322014-04-14 12:11:28 -0700334}
335
336TEST(stdio, snprintf_g_special) {
337 char buf[BUFSIZ];
338 snprintf(buf, sizeof(buf), "%g", nan(""));
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700339 EXPECT_STRCASEEQ("NaN", buf);
Elliott Hughes7823f322014-04-14 12:11:28 -0700340
341 snprintf(buf, sizeof(buf), "%g", HUGE_VAL);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700342 EXPECT_STRCASEEQ("Inf", buf);
Elliott Hughes7823f322014-04-14 12:11:28 -0700343}
344
Elliott Hughes1d13c642013-09-23 16:02:39 -0700345TEST(stdio, snprintf_d_INT_MAX) {
346 char buf[BUFSIZ];
347 snprintf(buf, sizeof(buf), "%d", INT_MAX);
348 EXPECT_STREQ("2147483647", buf);
349}
350
351TEST(stdio, snprintf_d_INT_MIN) {
352 char buf[BUFSIZ];
353 snprintf(buf, sizeof(buf), "%d", INT_MIN);
354 EXPECT_STREQ("-2147483648", buf);
355}
356
357TEST(stdio, snprintf_ld_LONG_MAX) {
358 char buf[BUFSIZ];
359 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Elliott Hughes925753a2013-10-18 13:17:18 -0700360#if __LP64__
361 EXPECT_STREQ("9223372036854775807", buf);
362#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700363 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700364#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700365}
366
367TEST(stdio, snprintf_ld_LONG_MIN) {
368 char buf[BUFSIZ];
369 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Elliott Hughes925753a2013-10-18 13:17:18 -0700370#if __LP64__
371 EXPECT_STREQ("-9223372036854775808", buf);
372#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700373 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700374#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700375}
376
377TEST(stdio, snprintf_lld_LLONG_MAX) {
378 char buf[BUFSIZ];
379 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
380 EXPECT_STREQ("9223372036854775807", buf);
381}
382
383TEST(stdio, snprintf_lld_LLONG_MIN) {
384 char buf[BUFSIZ];
385 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
386 EXPECT_STREQ("-9223372036854775808", buf);
387}
388
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700389TEST(stdio, snprintf_e) {
390 char buf[BUFSIZ];
391
392 snprintf(buf, sizeof(buf), "%e", 1.5);
393 EXPECT_STREQ("1.500000e+00", buf);
394
395 snprintf(buf, sizeof(buf), "%Le", 1.5l);
396 EXPECT_STREQ("1.500000e+00", buf);
397}
398
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800399TEST(stdio, popen) {
400 FILE* fp = popen("cat /proc/version", "r");
401 ASSERT_TRUE(fp != NULL);
402
403 char buf[16];
404 char* s = fgets(buf, sizeof(buf), fp);
405 buf[13] = '\0';
406 ASSERT_STREQ("Linux version", s);
407
408 ASSERT_EQ(0, pclose(fp));
409}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700410
411TEST(stdio, getc) {
412 FILE* fp = fopen("/proc/version", "r");
413 ASSERT_TRUE(fp != NULL);
414 ASSERT_EQ('L', getc(fp));
415 ASSERT_EQ('i', getc(fp));
416 ASSERT_EQ('n', getc(fp));
417 ASSERT_EQ('u', getc(fp));
418 ASSERT_EQ('x', getc(fp));
419 fclose(fp);
420}
421
422TEST(stdio, putc) {
423 FILE* fp = fopen("/proc/version", "r");
424 ASSERT_TRUE(fp != NULL);
425 ASSERT_EQ(EOF, putc('x', fp));
426 fclose(fp);
427}
Elliott Hughes603332f2014-03-12 17:10:41 -0700428
429TEST(stdio, sscanf) {
430 char s1[123];
431 int i1;
432 double d1;
433 char s2[123];
434 ASSERT_EQ(3, sscanf(" hello 123 1.23 ", "%s %i %lf %s", s1, &i1, &d1, s2));
435 ASSERT_STREQ("hello", s1);
436 ASSERT_EQ(123, i1);
Christopher Ferrisf171b342014-03-17 16:40:26 -0700437 ASSERT_DOUBLE_EQ(1.23, d1);
Elliott Hughes603332f2014-03-12 17:10:41 -0700438}
Elliott Hughes53b24382014-05-02 18:29:25 -0700439
440TEST(stdio, cantwrite_EBADF) {
441 // If we open a file read-only...
442 FILE* fp = fopen("/proc/version", "r");
443
444 // ...all attempts to write to that file should return failure.
445
446 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
447 // glibc gets the wide-character functions wrong.
448
449 errno = 0;
450 EXPECT_EQ(EOF, putc('x', fp));
451 EXPECT_EQ(EBADF, errno);
452
453 errno = 0;
454 EXPECT_EQ(EOF, fprintf(fp, "hello"));
455 EXPECT_EQ(EBADF, errno);
456
457 errno = 0;
458 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
459#if !defined(__GLIBC__)
460 EXPECT_EQ(EBADF, errno);
461#endif
462
463 errno = 0;
464 EXPECT_EQ(EOF, putw(1234, fp));
465 EXPECT_EQ(EBADF, errno);
466
467 errno = 0;
468 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
469 EXPECT_EQ(EBADF, errno);
470
471 errno = 0;
472 EXPECT_EQ(EOF, fputs("hello", fp));
473 EXPECT_EQ(EBADF, errno);
474
475 errno = 0;
476 EXPECT_EQ(WEOF, fputwc(L'x', fp));
477#if !defined(__GLIBC__)
478 EXPECT_EQ(EBADF, errno);
479#endif
480}