blob: ecba4ad3b290c20c602a2c6e57627dc747fc2dae [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 Hughesc9244bd2014-05-14 13:31:35 -070020#include <fcntl.h>
Elliott Hughes1d13c642013-09-23 16:02:39 -070021#include <limits.h>
Elliott Hughes7823f322014-04-14 12:11:28 -070022#include <math.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070023#include <stdio.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <unistd.h>
Elliott Hughes05493712014-04-17 17:30:03 -070027#include <wchar.h>
Calin Juravle03e4ebe2014-05-08 14:42:06 +010028#include <locale.h>
29
Elliott Hughese6bb5a22015-01-23 17:48:15 -080030#include <vector>
31
Elliott Hughesfb3873d2016-08-10 11:07:54 -070032#include "BionicDeathTest.h"
Calin Juravle03e4ebe2014-05-08 14:42:06 +010033#include "TemporaryFile.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070034
Christopher Ferris13f26a72016-01-13 13:47:58 -080035#if defined(NOFORTIFY)
36#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070037#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080038#else
39#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070040#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080041#endif
42
Elliott Hughesfb3873d2016-08-10 11:07:54 -070043class stdio_DeathTest : public BionicDeathTest {};
44class stdio_nofortify_DeathTest : public BionicDeathTest {};
45
Elliott Hughes70715da2016-08-01 16:35:17 -070046static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
47 rewind(fp);
48
49 char line[1024];
50 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
51 ASSERT_STREQ(expected, line);
52
53 if (is_fmemopen) {
54 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
55 // extra empty line, but does on every C library I tested...
56 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
57 ASSERT_STREQ("", line);
58 }
59
60 // Make sure there isn't anything else in the file.
61 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
62}
63
Christopher Ferris13f26a72016-01-13 13:47:58 -080064TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080065 // Check that we have a _recursive_ mutex for flockfile.
66 flockfile(stderr);
67 feof(stderr); // We don't care about the result, but this needs to take the lock.
68 funlockfile(stderr);
69}
70
Christopher Ferris13f26a72016-01-13 13:47:58 -080071TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080072 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
73 FILE* fp = fopen("/dev/null", "w");
74 ASSERT_TRUE(fp != NULL);
75 flockfile(fp);
76 feof(fp);
77 funlockfile(fp);
78 fclose(fp);
79}
80
Christopher Ferris13f26a72016-01-13 13:47:58 -080081TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -070082 FILE* fp = tmpfile();
83 ASSERT_TRUE(fp != NULL);
84
85 int fd = fileno(fp);
86 ASSERT_NE(fd, -1);
87
88 struct stat sb;
89 int rc = fstat(fd, &sb);
90 ASSERT_NE(rc, -1);
91 ASSERT_EQ(sb.st_mode & 0777, 0600U);
92
93 rc = fprintf(fp, "hello\n");
94 ASSERT_EQ(rc, 6);
95
Elliott Hughes70715da2016-08-01 16:35:17 -070096 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -070097 fclose(fp);
98}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030099
Elliott Hughesf226ee52016-02-03 11:24:28 -0800100TEST(STDIO_TEST, tmpfile64) {
101 FILE* fp = tmpfile64();
102 ASSERT_TRUE(fp != nullptr);
103 fclose(fp);
104}
105
Christopher Ferris13f26a72016-01-13 13:47:58 -0800106TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100107 TemporaryFile tf;
108
109 int rc = dprintf(tf.fd, "hello\n");
110 ASSERT_EQ(rc, 6);
111
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800112 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700113 FILE* tfile = fdopen(tf.fd, "r");
114 ASSERT_TRUE(tfile != NULL);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100115
Elliott Hughes70715da2016-08-01 16:35:17 -0700116 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700117 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100118}
119
Christopher Ferris13f26a72016-01-13 13:47:58 -0800120TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300121 FILE* fp = tmpfile();
122 ASSERT_TRUE(fp != NULL);
123
124 const char* line_written = "This is a test";
125 int rc = fprintf(fp, "%s", line_written);
126 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
127
128 rewind(fp);
129
130 char* word_read = NULL;
131 size_t allocated_length = 0;
132
133 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
134 for (size_t i = 0; i < 5; ++i) {
135 ASSERT_FALSE(feof(fp));
136 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
137 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800138 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300139 }
140 // The last read should have set the end-of-file indicator for the stream.
141 ASSERT_TRUE(feof(fp));
142 clearerr(fp);
143
144 // getdelim returns -1 but doesn't set errno if we're already at EOF.
145 // It should set the end-of-file indicator for the stream, though.
146 errno = 0;
147 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800148 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300149 ASSERT_TRUE(feof(fp));
150
151 free(word_read);
152 fclose(fp);
153}
154
Christopher Ferris13f26a72016-01-13 13:47:58 -0800155TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300156 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800157 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300158
159 char* buffer = NULL;
160 size_t buffer_length = 0;
161
162 // The first argument can't be NULL.
163 errno = 0;
164 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800165 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300166
167 // The second argument can't be NULL.
168 errno = 0;
169 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800170 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300171
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700172 // The underlying fd can't be closed.
173 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300174 errno = 0;
175 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800176 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700177 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300178}
179
Christopher Ferris13f26a72016-01-13 13:47:58 -0800180TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700181 FILE* fp = fopen("/proc", "r");
182 ASSERT_TRUE(fp != NULL);
183 char* word_read;
184 size_t allocated_length;
185 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
186 fclose(fp);
187}
188
Christopher Ferris13f26a72016-01-13 13:47:58 -0800189TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300190 FILE* fp = tmpfile();
191 ASSERT_TRUE(fp != NULL);
192
193 const char* line_written = "This is a test for getline\n";
194 const size_t line_count = 5;
195
196 for (size_t i = 0; i < line_count; ++i) {
197 int rc = fprintf(fp, "%s", line_written);
198 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
199 }
200
201 rewind(fp);
202
203 char* line_read = NULL;
204 size_t allocated_length = 0;
205
206 size_t read_line_count = 0;
207 ssize_t read_char_count;
208 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
209 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
210 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800211 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300212 ++read_line_count;
213 }
214 ASSERT_EQ(read_line_count, line_count);
215
216 // The last read should have set the end-of-file indicator for the stream.
217 ASSERT_TRUE(feof(fp));
218 clearerr(fp);
219
220 // getline returns -1 but doesn't set errno if we're already at EOF.
221 // It should set the end-of-file indicator for the stream, though.
222 errno = 0;
223 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800224 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300225 ASSERT_TRUE(feof(fp));
226
227 free(line_read);
228 fclose(fp);
229}
230
Christopher Ferris13f26a72016-01-13 13:47:58 -0800231TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300232 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800233 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300234
235 char* buffer = NULL;
236 size_t buffer_length = 0;
237
238 // The first argument can't be NULL.
239 errno = 0;
240 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800241 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300242
243 // The second argument can't be NULL.
244 errno = 0;
245 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800246 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300247
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700248 // The underlying fd can't be closed.
249 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300250 errno = 0;
251 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800252 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700253 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300254}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000255
Christopher Ferris13f26a72016-01-13 13:47:58 -0800256TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800257 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800258 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800259 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
260 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000261 // error: format '%zd' expects argument of type 'signed size_t',
262 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
263 ssize_t v = 1;
264 char buf[32];
265 snprintf(buf, sizeof(buf), "%zd", v);
266}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800267
Elliott Hughes05493712014-04-17 17:30:03 -0700268// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800269TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700270 char buf[BUFSIZ];
271 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
272 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
273}
274
Christopher Ferris13f26a72016-01-13 13:47:58 -0800275TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700276 char buf[BUFSIZ];
277 wint_t wc = L'a';
278 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
279 EXPECT_STREQ("<a>", buf);
280}
281
Christopher Ferris13f26a72016-01-13 13:47:58 -0800282TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700283 char buf[BUFSIZ];
284 wchar_t* ws = NULL;
285 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
286 EXPECT_STREQ("<(null)>", buf);
287
288 wchar_t chars[] = { L'h', L'i', 0 };
289 ws = chars;
290 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
291 EXPECT_STREQ("<hi>", buf);
292}
293
Christopher Ferris13f26a72016-01-13 13:47:58 -0800294TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700295#if defined(__BIONIC__)
Elliott Hughese2341d02014-05-02 18:16:32 -0700296 // http://b/14492135
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700297 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700298 int i = 1234;
299 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
300 EXPECT_EQ(1234, i);
301 EXPECT_STREQ("a n b", buf);
302#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800303 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700304#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700305}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700306
Christopher Ferris13f26a72016-01-13 13:47:58 -0800307TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700308 char buf[BUFSIZ];
309
310 snprintf(buf, sizeof(buf), "a");
311 EXPECT_STREQ("a", buf);
312
313 snprintf(buf, sizeof(buf), "%%");
314 EXPECT_STREQ("%", buf);
315
316 snprintf(buf, sizeof(buf), "01234");
317 EXPECT_STREQ("01234", buf);
318
319 snprintf(buf, sizeof(buf), "a%sb", "01234");
320 EXPECT_STREQ("a01234b", buf);
321
322 char* s = NULL;
323 snprintf(buf, sizeof(buf), "a%sb", s);
324 EXPECT_STREQ("a(null)b", buf);
325
326 snprintf(buf, sizeof(buf), "aa%scc", "bb");
327 EXPECT_STREQ("aabbcc", buf);
328
329 snprintf(buf, sizeof(buf), "a%cc", 'b');
330 EXPECT_STREQ("abc", buf);
331
332 snprintf(buf, sizeof(buf), "a%db", 1234);
333 EXPECT_STREQ("a1234b", buf);
334
335 snprintf(buf, sizeof(buf), "a%db", -8123);
336 EXPECT_STREQ("a-8123b", buf);
337
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700338 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700339 EXPECT_STREQ("a16b", buf);
340
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700341 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700342 EXPECT_STREQ("a16b", buf);
343
344 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
345 EXPECT_STREQ("a68719476736b", buf);
346
347 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
348 EXPECT_STREQ("a70000b", buf);
349
350 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
351 EXPECT_STREQ("a0xb0001234b", buf);
352
353 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
354 EXPECT_STREQ("a12abz", buf);
355
356 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
357 EXPECT_STREQ("a12ABz", buf);
358
359 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
360 EXPECT_STREQ("a00123456z", buf);
361
362 snprintf(buf, sizeof(buf), "a%5dz", 1234);
363 EXPECT_STREQ("a 1234z", buf);
364
365 snprintf(buf, sizeof(buf), "a%05dz", 1234);
366 EXPECT_STREQ("a01234z", buf);
367
368 snprintf(buf, sizeof(buf), "a%8dz", 1234);
369 EXPECT_STREQ("a 1234z", buf);
370
371 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
372 EXPECT_STREQ("a1234 z", buf);
373
374 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
375 EXPECT_STREQ("Aabcdef Z", buf);
376
377 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
378 EXPECT_STREQ("Ahello:1234Z", buf);
379
380 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
381 EXPECT_STREQ("a005:5:05z", buf);
382
383 void* p = NULL;
384 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700385#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700386 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800387#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700388 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800389#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700390
391 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
392 EXPECT_STREQ("a68719476736,6,7,8z", buf);
393
394 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
395 EXPECT_STREQ("a_1.230000_b", buf);
396
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700397 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700398 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400399
400 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
401 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700402}
403
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800404template <typename T>
405void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
406 const T* fmt, const T* fmt_plus,
407 const T* minus_inf, const T* inf_, const T* plus_inf,
408 const T* minus_nan, const T* nan_, const T* plus_nan) {
409 T buf[BUFSIZ];
Elliott Hughes7823f322014-04-14 12:11:28 -0700410
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800411 snprintf_fn(buf, sizeof(buf), fmt, nan(""));
412 EXPECT_STREQ(nan_, buf) << fmt;
413 snprintf_fn(buf, sizeof(buf), fmt, -nan(""));
414 EXPECT_STREQ(minus_nan, buf) << fmt;
415 snprintf_fn(buf, sizeof(buf), fmt_plus, nan(""));
416 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
417 snprintf_fn(buf, sizeof(buf), fmt_plus, -nan(""));
418 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
419
420 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VAL);
421 EXPECT_STREQ(inf_, buf) << fmt;
422 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VAL);
423 EXPECT_STREQ(minus_inf, buf) << fmt;
424 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VAL);
425 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
426 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VAL);
427 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7823f322014-04-14 12:11:28 -0700428}
429
Christopher Ferris13f26a72016-01-13 13:47:58 -0800430TEST(STDIO_TEST, snprintf_inf_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800431 CheckInfNan(snprintf, "%a", "%+a", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
432 CheckInfNan(snprintf, "%A", "%+A", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
433 CheckInfNan(snprintf, "%e", "%+e", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
434 CheckInfNan(snprintf, "%E", "%+E", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
435 CheckInfNan(snprintf, "%f", "%+f", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
436 CheckInfNan(snprintf, "%F", "%+F", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
437 CheckInfNan(snprintf, "%g", "%+g", "-inf", "inf", "+inf", "-nan", "nan", "+nan");
438 CheckInfNan(snprintf, "%G", "%+G", "-INF", "INF", "+INF", "-NAN", "NAN", "+NAN");
439}
Elliott Hughes7823f322014-04-14 12:11:28 -0700440
Christopher Ferris13f26a72016-01-13 13:47:58 -0800441TEST(STDIO_TEST, wsprintf_inf_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800442 CheckInfNan(swprintf, L"%a", L"%+a", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
443 CheckInfNan(swprintf, L"%A", L"%+A", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
444 CheckInfNan(swprintf, L"%e", L"%+e", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
445 CheckInfNan(swprintf, L"%E", L"%+E", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
446 CheckInfNan(swprintf, L"%f", L"%+f", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
447 CheckInfNan(swprintf, L"%F", L"%+F", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
448 CheckInfNan(swprintf, L"%g", L"%+g", L"-inf", L"inf", L"+inf", L"-nan", L"nan", L"+nan");
449 CheckInfNan(swprintf, L"%G", L"%+G", L"-INF", L"INF", L"+INF", L"-NAN", L"NAN", L"+NAN");
Elliott Hughes7823f322014-04-14 12:11:28 -0700450}
451
Christopher Ferris13f26a72016-01-13 13:47:58 -0800452TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700453 char buf[BUFSIZ];
454 snprintf(buf, sizeof(buf), "%d", INT_MAX);
455 EXPECT_STREQ("2147483647", buf);
456}
457
Christopher Ferris13f26a72016-01-13 13:47:58 -0800458TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700459 char buf[BUFSIZ];
460 snprintf(buf, sizeof(buf), "%d", INT_MIN);
461 EXPECT_STREQ("-2147483648", buf);
462}
463
Christopher Ferris13f26a72016-01-13 13:47:58 -0800464TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700465 char buf[BUFSIZ];
466 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Elliott Hughes925753a2013-10-18 13:17:18 -0700467#if __LP64__
468 EXPECT_STREQ("9223372036854775807", buf);
469#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700470 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700471#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700472}
473
Christopher Ferris13f26a72016-01-13 13:47:58 -0800474TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700475 char buf[BUFSIZ];
476 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Elliott Hughes925753a2013-10-18 13:17:18 -0700477#if __LP64__
478 EXPECT_STREQ("-9223372036854775808", buf);
479#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700480 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700481#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700482}
483
Christopher Ferris13f26a72016-01-13 13:47:58 -0800484TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700485 char buf[BUFSIZ];
486 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
487 EXPECT_STREQ("9223372036854775807", buf);
488}
489
Christopher Ferris13f26a72016-01-13 13:47:58 -0800490TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700491 char buf[BUFSIZ];
492 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
493 EXPECT_STREQ("-9223372036854775808", buf);
494}
495
Christopher Ferris13f26a72016-01-13 13:47:58 -0800496TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700497 char buf[BUFSIZ];
498
499 snprintf(buf, sizeof(buf), "%e", 1.5);
500 EXPECT_STREQ("1.500000e+00", buf);
501
502 snprintf(buf, sizeof(buf), "%Le", 1.5l);
503 EXPECT_STREQ("1.500000e+00", buf);
504}
505
Christopher Ferris13f26a72016-01-13 13:47:58 -0800506TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700507 char buf[BUFSIZ];
508
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800509 snprintf(buf, sizeof(buf), "%e", -0.0);
510 EXPECT_STREQ("-0.000000e+00", buf);
511 snprintf(buf, sizeof(buf), "%E", -0.0);
512 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700513 snprintf(buf, sizeof(buf), "%f", -0.0);
514 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800515 snprintf(buf, sizeof(buf), "%F", -0.0);
516 EXPECT_STREQ("-0.000000", buf);
517 snprintf(buf, sizeof(buf), "%g", -0.0);
518 EXPECT_STREQ("-0", buf);
519 snprintf(buf, sizeof(buf), "%G", -0.0);
520 EXPECT_STREQ("-0", buf);
521 snprintf(buf, sizeof(buf), "%a", -0.0);
522 EXPECT_STREQ("-0x0p+0", buf);
523 snprintf(buf, sizeof(buf), "%A", -0.0);
524 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700525}
526
Christopher Ferris13f26a72016-01-13 13:47:58 -0800527TEST(STDIO_TEST, snprintf_utf8_15439554) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700528 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700529 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700530
Elliott Hughes69f05d22014-06-05 20:10:09 -0700531 // http://b/15439554
532 char buf[BUFSIZ];
533
534 // 1-byte character.
535 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
536 EXPECT_STREQ("1x2", buf);
537 // 2-byte character.
538 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
539 EXPECT_STREQ("1¢2", buf);
540 // 3-byte character.
541 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
542 EXPECT_STREQ("1€2", buf);
543 // 4-byte character.
544 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
545 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700546
Wally Yaua40fdbd2014-08-26 09:47:23 -0700547 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700548 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700549}
550
Elliott Hughes43f7c872016-02-05 11:18:41 -0800551static void* snprintf_small_stack_fn(void*) {
552 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
553 char buf[PATH_MAX];
554 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
555 return nullptr;
556}
557
558TEST(STDIO_TEST, snprintf_small_stack) {
559 // Is it safe to call snprintf on a thread with a small stack?
560 // (The snprintf implementation puts some pretty large buffers on the stack.)
561 pthread_attr_t a;
562 ASSERT_EQ(0, pthread_attr_init(&a));
563 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
564
565 pthread_t t;
566 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
567 ASSERT_EQ(0, pthread_join(t, nullptr));
568}
569
Elliott Hughes8200e552016-02-05 21:57:37 -0800570TEST(STDIO_TEST, snprintf_asterisk_overflow) {
571 char buf[128];
572 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
573 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
574 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
575 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
576 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
577
578 // INT_MAX-1, INT_MAX, INT_MAX+1.
579 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
580 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
581 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
582 ASSERT_EQ(ENOMEM, errno);
583}
584
Elliott Hughes70715da2016-08-01 16:35:17 -0700585TEST(STDIO_TEST, fprintf) {
586 TemporaryFile tf;
587
588 FILE* tfile = fdopen(tf.fd, "r+");
589 ASSERT_TRUE(tfile != nullptr);
590
591 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
592 AssertFileIs(tfile, "123 abc");
593 fclose(tfile);
594}
595
Christopher Ferris13f26a72016-01-13 13:47:58 -0800596TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700597 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700598 FILE* fp;
599
600 // Unbuffered case where the fprintf(3) itself fails.
601 ASSERT_NE(nullptr, fp = tmpfile());
602 setbuf(fp, NULL);
603 ASSERT_EQ(4, fprintf(fp, "epic"));
604 ASSERT_EQ(0, close(fileno(fp)));
605 ASSERT_EQ(-1, fprintf(fp, "fail"));
606 ASSERT_EQ(-1, fclose(fp));
607
608 // Buffered case where we won't notice until the fclose(3).
609 // It's likely this is what was actually seen in http://b/7229520,
610 // and that expecting fprintf to fail is setting yourself up for
611 // disappointment. Remember to check fclose(3)'s return value, kids!
612 ASSERT_NE(nullptr, fp = tmpfile());
613 ASSERT_EQ(4, fprintf(fp, "epic"));
614 ASSERT_EQ(0, close(fileno(fp)));
615 ASSERT_EQ(4, fprintf(fp, "fail"));
616 ASSERT_EQ(-1, fclose(fp));
617}
618
Christopher Ferris13f26a72016-01-13 13:47:58 -0800619TEST(STDIO_TEST, popen) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800620 FILE* fp = popen("cat /proc/version", "r");
621 ASSERT_TRUE(fp != NULL);
622
623 char buf[16];
624 char* s = fgets(buf, sizeof(buf), fp);
625 buf[13] = '\0';
626 ASSERT_STREQ("Linux version", s);
627
628 ASSERT_EQ(0, pclose(fp));
629}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700630
Christopher Ferris13f26a72016-01-13 13:47:58 -0800631TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700632 FILE* fp = fopen("/proc/version", "r");
633 ASSERT_TRUE(fp != NULL);
634 ASSERT_EQ('L', getc(fp));
635 ASSERT_EQ('i', getc(fp));
636 ASSERT_EQ('n', getc(fp));
637 ASSERT_EQ('u', getc(fp));
638 ASSERT_EQ('x', getc(fp));
639 fclose(fp);
640}
641
Christopher Ferris13f26a72016-01-13 13:47:58 -0800642TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700643 FILE* fp = fopen("/proc/version", "r");
644 ASSERT_TRUE(fp != NULL);
645 ASSERT_EQ(EOF, putc('x', fp));
646 fclose(fp);
647}
Elliott Hughes603332f2014-03-12 17:10:41 -0700648
Christopher Ferris13f26a72016-01-13 13:47:58 -0800649TEST(STDIO_TEST, sscanf) {
Elliott Hughes603332f2014-03-12 17:10:41 -0700650 char s1[123];
651 int i1;
652 double d1;
653 char s2[123];
654 ASSERT_EQ(3, sscanf(" hello 123 1.23 ", "%s %i %lf %s", s1, &i1, &d1, s2));
655 ASSERT_STREQ("hello", s1);
656 ASSERT_EQ(123, i1);
Christopher Ferrisf171b342014-03-17 16:40:26 -0700657 ASSERT_DOUBLE_EQ(1.23, d1);
Elliott Hughes603332f2014-03-12 17:10:41 -0700658}
Elliott Hughes53b24382014-05-02 18:29:25 -0700659
Christopher Ferris13f26a72016-01-13 13:47:58 -0800660TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -0700661 // If we open a file read-only...
662 FILE* fp = fopen("/proc/version", "r");
663
664 // ...all attempts to write to that file should return failure.
665
666 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
667 // glibc gets the wide-character functions wrong.
668
669 errno = 0;
670 EXPECT_EQ(EOF, putc('x', fp));
671 EXPECT_EQ(EBADF, errno);
672
673 errno = 0;
674 EXPECT_EQ(EOF, fprintf(fp, "hello"));
675 EXPECT_EQ(EBADF, errno);
676
677 errno = 0;
678 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -0700679#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -0700680 EXPECT_EQ(EBADF, errno);
681#endif
682
683 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -0700684 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
685 EXPECT_EQ(EBADF, errno);
686
687 errno = 0;
688 EXPECT_EQ(EOF, fputs("hello", fp));
689 EXPECT_EQ(EBADF, errno);
690
691 errno = 0;
692 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -0700693#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -0700694 EXPECT_EQ(EBADF, errno);
695#endif
696}
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100697
698// Tests that we can only have a consistent and correct fpos_t when using
699// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -0800700TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100701 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
702 uselocale(LC_GLOBAL_LOCALE);
703
704 FILE* fp = tmpfile();
705 ASSERT_TRUE(fp != NULL);
706
707 wchar_t mb_one_bytes = L'h';
708 wchar_t mb_two_bytes = 0x00a2;
709 wchar_t mb_three_bytes = 0x20ac;
710 wchar_t mb_four_bytes = 0x24b62;
711
712 // Write to file.
713 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
714 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
715 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
716 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
717
718 rewind(fp);
719
720 // Record each character position.
721 fpos_t pos1;
722 fpos_t pos2;
723 fpos_t pos3;
724 fpos_t pos4;
725 fpos_t pos5;
726 EXPECT_EQ(0, fgetpos(fp, &pos1));
727 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
728 EXPECT_EQ(0, fgetpos(fp, &pos2));
729 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
730 EXPECT_EQ(0, fgetpos(fp, &pos3));
731 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
732 EXPECT_EQ(0, fgetpos(fp, &pos4));
733 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
734 EXPECT_EQ(0, fgetpos(fp, &pos5));
735
Elliott Hughes063525c2014-05-13 11:19:57 -0700736#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100737 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
738 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
739 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
740 // structure.
741 ASSERT_EQ(0, static_cast<off_t>(pos1));
742 ASSERT_EQ(1, static_cast<off_t>(pos2));
743 ASSERT_EQ(3, static_cast<off_t>(pos3));
744 ASSERT_EQ(6, static_cast<off_t>(pos4));
745 ASSERT_EQ(10, static_cast<off_t>(pos5));
746#endif
747
748 // Exercise back and forth movements of the position.
749 ASSERT_EQ(0, fsetpos(fp, &pos2));
750 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
751 ASSERT_EQ(0, fsetpos(fp, &pos1));
752 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
753 ASSERT_EQ(0, fsetpos(fp, &pos4));
754 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
755 ASSERT_EQ(0, fsetpos(fp, &pos3));
756 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
757 ASSERT_EQ(0, fsetpos(fp, &pos5));
758 ASSERT_EQ(WEOF, fgetwc(fp));
759
760 fclose(fp);
761}
762
763// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -0800764TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100765 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
766 uselocale(LC_GLOBAL_LOCALE);
767
Calin Juravle9b95ea92014-05-14 17:07:10 +0100768 // In glibc-2.16 fseek doesn't work properly in wide mode
769 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
770 // to close and re-open the file. We do it in order to make the test pass
771 // with all glibcs.
772
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100773 TemporaryFile tf;
774 FILE* fp = fdopen(tf.fd, "w+");
775 ASSERT_TRUE(fp != NULL);
776
777 wchar_t mb_two_bytes = 0x00a2;
778 wchar_t mb_three_bytes = 0x20ac;
779 wchar_t mb_four_bytes = 0x24b62;
780
781 // Write to file.
782 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
783 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
784 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
785
786 fflush(fp);
787 fclose(fp);
788
789 fp = fopen(tf.filename, "r");
790 ASSERT_TRUE(fp != NULL);
791
792 // Store a valid position.
793 fpos_t mb_two_bytes_pos;
794 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
795
796 // Move inside mb_four_bytes with fseek.
797 long offset_inside_mb = 6;
798 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
799
800 // Store the "inside multi byte" position.
801 fpos_t pos_inside_mb;
802 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -0700803#if defined(__BIONIC__)
804 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
805#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100806
807 // Reading from within a byte should produce an error.
808 ASSERT_EQ(WEOF, fgetwc(fp));
809 ASSERT_EQ(EILSEQ, errno);
810
811 // Reverting to a valid position should work.
812 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
813 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
814
815 // Moving withing a multi byte with fsetpos should work but reading should
816 // produce an error.
817 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
818 ASSERT_EQ(WEOF, fgetwc(fp));
819 ASSERT_EQ(EILSEQ, errno);
820
821 fclose(fp);
822}
Elliott Hughes6b841db2014-08-20 16:10:49 -0700823
Christopher Ferris13f26a72016-01-13 13:47:58 -0800824TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -0700825 char buf[16];
826 memset(buf, 0, sizeof(buf));
827 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
828 ASSERT_EQ('<', fputc('<', fp));
829 ASSERT_NE(EOF, fputs("abc>\n", fp));
830 fflush(fp);
831
832 ASSERT_STREQ("<abc>\n", buf);
833
Elliott Hughes70715da2016-08-01 16:35:17 -0700834 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes6b841db2014-08-20 16:10:49 -0700835 fclose(fp);
836}
837
Christopher Ferris13f26a72016-01-13 13:47:58 -0800838TEST(STDIO_TEST, fmemopen_NULL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -0700839 FILE* fp = fmemopen(nullptr, 128, "r+");
840 ASSERT_NE(EOF, fputs("xyz\n", fp));
841
Elliott Hughes70715da2016-08-01 16:35:17 -0700842 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes6b841db2014-08-20 16:10:49 -0700843 fclose(fp);
844}
845
Christopher Ferris13f26a72016-01-13 13:47:58 -0800846TEST(STDIO_TEST, fmemopen_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -0700847 char buf[16];
848
849 // Invalid size.
850 errno = 0;
851 ASSERT_EQ(nullptr, fmemopen(buf, 0, "r+"));
852 ASSERT_EQ(EINVAL, errno);
853
854 // No '+' with NULL buffer.
855 errno = 0;
856 ASSERT_EQ(nullptr, fmemopen(nullptr, 0, "r"));
857 ASSERT_EQ(EINVAL, errno);
858}
859
Christopher Ferris13f26a72016-01-13 13:47:58 -0800860TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -0700861 char* p = nullptr;
862 size_t size = 0;
863 FILE* fp = open_memstream(&p, &size);
864 ASSERT_NE(EOF, fputs("hello, world!", fp));
865 fclose(fp);
866
867 ASSERT_STREQ("hello, world!", p);
868 ASSERT_EQ(strlen("hello, world!"), size);
869 free(p);
870}
871
Christopher Ferris13f26a72016-01-13 13:47:58 -0800872TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -0700873#if defined(__BIONIC__)
874 char* p;
875 size_t size;
876
877 // Invalid buffer.
878 errno = 0;
879 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
880 ASSERT_EQ(EINVAL, errno);
881
882 // Invalid size.
883 errno = 0;
884 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
885 ASSERT_EQ(EINVAL, errno);
886#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800887 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughes6b841db2014-08-20 16:10:49 -0700888#endif
889}
Elliott Hughes31165ed2014-09-23 17:34:29 -0700890
Christopher Ferris13f26a72016-01-13 13:47:58 -0800891TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -0700892 int fd = open("/proc/version", O_RDONLY);
893 ASSERT_TRUE(fd != -1);
894
895 // This fd doesn't have O_CLOEXEC...
896 int flags = fcntl(fd, F_GETFD);
897 ASSERT_TRUE(flags != -1);
898 ASSERT_EQ(0, flags & FD_CLOEXEC);
899
900 FILE* fp = fdopen(fd, "re");
901 ASSERT_TRUE(fp != NULL);
902
903 // ...but the new one does.
904 flags = fcntl(fileno(fp), F_GETFD);
905 ASSERT_TRUE(flags != -1);
906 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
907
908 fclose(fp);
909 close(fd);
910}
911
Christopher Ferris13f26a72016-01-13 13:47:58 -0800912TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -0700913 FILE* fp = fopen("/proc/version", "r");
914 ASSERT_TRUE(fp != NULL);
915
916 // This FILE* doesn't have O_CLOEXEC...
917 int flags = fcntl(fileno(fp), F_GETFD);
918 ASSERT_TRUE(flags != -1);
919 ASSERT_EQ(0, flags & FD_CLOEXEC);
920
921 fp = freopen("/proc/version", "re", fp);
922
923 // ...but the new one does.
924 flags = fcntl(fileno(fp), F_GETFD);
925 ASSERT_TRUE(flags != -1);
926 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
927
928 fclose(fp);
929}
Elliott Hughes20841a12014-12-01 16:13:30 -0800930
Elliott Hughesf226ee52016-02-03 11:24:28 -0800931TEST(STDIO_TEST, fopen64_freopen64) {
932 FILE* fp = fopen64("/proc/version", "r");
933 ASSERT_TRUE(fp != nullptr);
934 fp = freopen64("/proc/version", "re", fp);
935 ASSERT_TRUE(fp != nullptr);
936 fclose(fp);
937}
938
Elliott Hughes20841a12014-12-01 16:13:30 -0800939// https://code.google.com/p/android/issues/detail?id=81155
940// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -0800941TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -0800942 FILE* fp = fopen("/dev/zero", "r");
943 ASSERT_TRUE(fp != NULL);
944
945 // Make this stream unbuffered.
946 setvbuf(fp, 0, _IONBF, 0);
947
948 char buf[65*1024];
949 memset(buf, 0xff, sizeof(buf));
950
951 time_t t0 = time(NULL);
952 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -0800953 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -0800954 }
955 time_t t1 = time(NULL);
956
957 fclose(fp);
958
959 // 1024 64KiB reads should have been very quick.
960 ASSERT_LE(t1 - t0, 1);
961
962 for (size_t i = 0; i < 64*1024; ++i) {
963 ASSERT_EQ('\0', buf[i]);
964 }
965 for (size_t i = 64*1024; i < 65*1024; ++i) {
966 ASSERT_EQ('\xff', buf[i]);
967 }
968}
Elliott Hughes75b99382015-01-20 11:23:50 -0800969
Christopher Ferris13f26a72016-01-13 13:47:58 -0800970TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800971 std::string digits("0123456789");
972 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -0800973
974 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
975 char buf1[4 * 4];
976 memset(buf1, 0, sizeof(buf1));
977 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800978 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -0800979 ASSERT_TRUE(feof(fp));
980
981 rewind(fp);
982
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800983 // Try to read way too much so stdio tries to read more direct from the stream.
984 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -0800985 memset(buf2, 0, sizeof(buf2));
986 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800987 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -0800988 ASSERT_TRUE(feof(fp));
989
990 fclose(fp);
991}
Elliott Hughese6bb5a22015-01-23 17:48:15 -0800992
993static void test_fread_from_write_only_stream(size_t n) {
994 FILE* fp = fopen("/dev/null", "w");
995 std::vector<char> buf(n, 0);
996 errno = 0;
997 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
998 ASSERT_EQ(EBADF, errno);
999 ASSERT_TRUE(ferror(fp));
1000 ASSERT_FALSE(feof(fp));
1001 fclose(fp);
1002}
1003
Christopher Ferris13f26a72016-01-13 13:47:58 -08001004TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001005 test_fread_from_write_only_stream(1);
1006}
1007
Christopher Ferris13f26a72016-01-13 13:47:58 -08001008TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001009 test_fread_from_write_only_stream(64*1024);
1010}
1011
1012static void test_fwrite_after_fread(size_t n) {
1013 TemporaryFile tf;
1014
1015 FILE* fp = fdopen(tf.fd, "w+");
1016 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
1017 fflush(fp);
1018
1019 // We've flushed but not rewound, so there's nothing to read.
1020 std::vector<char> buf(n, 0);
1021 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
1022 ASSERT_TRUE(feof(fp));
1023
1024 // But hitting EOF doesn't prevent us from writing...
1025 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08001026 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001027
1028 // And if we rewind, everything's there.
1029 rewind(fp);
1030 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
1031 ASSERT_EQ('1', buf[0]);
1032 ASSERT_EQ('2', buf[1]);
1033
1034 fclose(fp);
1035}
1036
Christopher Ferris13f26a72016-01-13 13:47:58 -08001037TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001038 test_fwrite_after_fread(16);
1039}
1040
Christopher Ferris13f26a72016-01-13 13:47:58 -08001041TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001042 test_fwrite_after_fread(64*1024);
1043}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001044
1045// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08001046TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001047 TemporaryFile tf;
1048
1049 FILE* fp = fopen(tf.filename, "w+");
1050 ASSERT_TRUE(fp != nullptr);
1051
1052 char file_data[12288];
1053 for (size_t i = 0; i < 12288; i++) {
1054 file_data[i] = i;
1055 }
1056 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
1057 fclose(fp);
1058
1059 fp = fopen(tf.filename, "r");
1060 ASSERT_TRUE(fp != nullptr);
1061
1062 char buffer[8192];
1063 size_t cur_location = 0;
1064 // Small read to populate internal buffer.
1065 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
1066 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
1067
1068 cur_location = static_cast<size_t>(ftell(fp));
1069 // Large read to force reading into the user supplied buffer and bypassing
1070 // the internal buffer.
1071 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
1072 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
1073
1074 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08001075 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001076 cur_location = static_cast<size_t>(ftell(fp));
1077 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
1078 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
1079
1080 fclose(fp);
1081}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07001082
1083// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08001084TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07001085 TemporaryFile tf;
1086 char buf[6] = {0};
1087
1088 FILE* fw = fopen(tf.filename, "w");
1089 ASSERT_TRUE(fw != nullptr);
1090
1091 FILE* fr = fopen(tf.filename, "r");
1092 ASSERT_TRUE(fr != nullptr);
1093
1094 fwrite("a", 1, 1, fw);
1095 fflush(fw);
1096 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1097 ASSERT_STREQ("a", buf);
1098
1099 // 'fr' is now at EOF.
1100 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1101 ASSERT_TRUE(feof(fr));
1102
1103 // Write some more...
1104 fwrite("z", 1, 1, fw);
1105 fflush(fw);
1106
1107 // ...and check that we can read it back.
1108 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
1109 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1110 ASSERT_STREQ("z", buf);
1111
1112 // But now we're done.
1113 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1114
1115 fclose(fr);
1116 fclose(fw);
1117}
Elliott Hughes923f1652016-01-19 15:46:05 -08001118
1119TEST(STDIO_TEST, fclose_invalidates_fd) {
1120 // The typical error we're trying to help people catch involves accessing
1121 // memory after it's been freed. But we know that stdin/stdout/stderr are
1122 // special and don't get deallocated, so this test uses stdin.
1123 ASSERT_EQ(0, fclose(stdin));
1124
1125 // Even though using a FILE* after close is undefined behavior, I've closed
1126 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
1127 // especially because they might actually correspond to a real stream.
1128 errno = 0;
1129 ASSERT_EQ(-1, fileno(stdin));
1130 ASSERT_EQ(EBADF, errno);
1131}
Elliott Hughes2704bd12016-01-20 17:14:53 -08001132
1133TEST(STDIO_TEST, fseek_ftell_unseekable) {
1134#if defined(__BIONIC__) // glibc has fopencookie instead.
1135 auto read_fn = [](void*, char*, int) { return -1; };
1136 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
1137 ASSERT_TRUE(fp != nullptr);
1138
1139 // Check that ftell balks on an unseekable FILE*.
1140 errno = 0;
1141 ASSERT_EQ(-1, ftell(fp));
1142 ASSERT_EQ(ESPIPE, errno);
1143
1144 // SEEK_CUR is rewritten as SEEK_SET internally...
1145 errno = 0;
1146 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
1147 ASSERT_EQ(ESPIPE, errno);
1148
1149 // ...so it's worth testing the direct seek path too.
1150 errno = 0;
1151 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
1152 ASSERT_EQ(ESPIPE, errno);
1153
1154 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001155#else
1156 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1157#endif
1158}
1159
1160TEST(STDIO_TEST, funopen_EINVAL) {
1161#if defined(__BIONIC__)
1162 errno = 0;
1163 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
1164 ASSERT_EQ(EINVAL, errno);
1165#else
1166 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1167#endif
1168}
1169
1170TEST(STDIO_TEST, funopen_seek) {
1171#if defined(__BIONIC__)
1172 auto read_fn = [](void*, char*, int) { return -1; };
1173
1174 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
1175 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
1176
1177 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
1178 ASSERT_TRUE(fp != nullptr);
1179 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08001180#if defined(__LP64__)
1181 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
1182 EXPECT_EQ(0xfedcba12LL, pos);
1183#else
1184 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
1185 EXPECT_EQ(EOVERFLOW, errno);
1186#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001187
1188 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
1189 ASSERT_TRUE(fp64 != nullptr);
1190 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08001191 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
1192 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001193#else
1194 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
Elliott Hughes2704bd12016-01-20 17:14:53 -08001195#endif
1196}
Elliott Hughes71288cb2016-01-22 19:22:44 -08001197
1198TEST(STDIO_TEST, lots_of_concurrent_files) {
1199 std::vector<TemporaryFile*> tfs;
1200 std::vector<FILE*> fps;
1201
1202 for (size_t i = 0; i < 256; ++i) {
1203 TemporaryFile* tf = new TemporaryFile;
1204 tfs.push_back(tf);
1205 FILE* fp = fopen(tf->filename, "w+");
1206 fps.push_back(fp);
1207 fprintf(fp, "hello %zu!\n", i);
1208 fflush(fp);
1209 }
1210
1211 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08001212 char expected[BUFSIZ];
1213 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08001214
Elliott Hughes70715da2016-08-01 16:35:17 -07001215 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08001216 fclose(fps[i]);
1217 delete tfs[i];
1218 }
1219}
Elliott Hughes9677fab2016-01-25 15:50:59 -08001220
1221static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
1222 EXPECT_EQ(offset, ftell(fp));
1223 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08001224 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08001225 fpos_t pos;
1226 fpos64_t pos64;
1227 EXPECT_EQ(0, fgetpos(fp, &pos));
1228 EXPECT_EQ(0, fgetpos64(fp, &pos64));
1229#if defined(__BIONIC__)
1230 EXPECT_EQ(offset, static_cast<off64_t>(pos));
1231 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
1232#else
1233 GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n";
1234#endif
1235}
1236
1237TEST(STDIO_TEST, seek_tell_family_smoke) {
1238 TemporaryFile tf;
1239 FILE* fp = fdopen(tf.fd, "w+");
1240
1241 // Initially we should be at 0.
1242 AssertFileOffsetAt(fp, 0);
1243
1244 // Seek to offset 8192.
1245 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
1246 AssertFileOffsetAt(fp, 8192);
1247 fpos_t eight_k_pos;
1248 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
1249
1250 // Seek forward another 8192...
1251 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
1252 AssertFileOffsetAt(fp, 8192 + 8192);
1253 fpos64_t sixteen_k_pos64;
1254 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
1255
1256 // Seek back 8192...
1257 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
1258 AssertFileOffsetAt(fp, 8192);
1259
1260 // Since we haven't written anything, the end is also at 0.
1261 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1262 AssertFileOffsetAt(fp, 0);
1263
1264 // Check that our fpos64_t from 16KiB works...
1265 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
1266 AssertFileOffsetAt(fp, 8192 + 8192);
1267 // ...as does our fpos_t from 8192.
1268 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
1269 AssertFileOffsetAt(fp, 8192);
1270
1271 // Do fseeko and fseeko64 work too?
1272 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
1273 AssertFileOffsetAt(fp, 1234);
1274 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
1275 AssertFileOffsetAt(fp, 5678);
1276
1277 fclose(fp);
1278}
1279
1280TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
1281 TemporaryFile tf;
1282 FILE* fp = fdopen(tf.fd, "w+");
1283
1284 // Bad whence.
1285 errno = 0;
1286 ASSERT_EQ(-1, fseek(fp, 0, 123));
1287 ASSERT_EQ(EINVAL, errno);
1288 errno = 0;
1289 ASSERT_EQ(-1, fseeko(fp, 0, 123));
1290 ASSERT_EQ(EINVAL, errno);
1291 errno = 0;
1292 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
1293 ASSERT_EQ(EINVAL, errno);
1294
1295 // Bad offset.
1296 errno = 0;
1297 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
1298 ASSERT_EQ(EINVAL, errno);
1299 errno = 0;
1300 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
1301 ASSERT_EQ(EINVAL, errno);
1302 errno = 0;
1303 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
1304 ASSERT_EQ(EINVAL, errno);
1305
1306 fclose(fp);
1307}
Elliott Hughes20788ae2016-06-09 15:16:32 -07001308
1309TEST(STDIO_TEST, ctermid) {
1310 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
1311
1312 char buf[L_ctermid] = {};
1313 ASSERT_EQ(buf, ctermid(buf));
1314 ASSERT_STREQ("/dev/tty", buf);
1315}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07001316
1317TEST(STDIO_TEST, remove) {
1318 struct stat sb;
1319
1320 TemporaryFile tf;
1321 ASSERT_EQ(0, remove(tf.filename));
1322 ASSERT_EQ(-1, lstat(tf.filename, &sb));
1323 ASSERT_EQ(ENOENT, errno);
1324
1325 TemporaryDir td;
1326 ASSERT_EQ(0, remove(td.dirname));
1327 ASSERT_EQ(-1, lstat(td.dirname, &sb));
1328 ASSERT_EQ(ENOENT, errno);
1329
1330 errno = 0;
1331 ASSERT_EQ(-1, remove(tf.filename));
1332 ASSERT_EQ(ENOENT, errno);
1333
1334 errno = 0;
1335 ASSERT_EQ(-1, remove(td.dirname));
1336 ASSERT_EQ(ENOENT, errno);
1337}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07001338
1339TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
1340 char buf[16];
1341 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
1342 testing::KilledBySignal(SIGABRT),
1343#if defined(NOFORTIFY)
1344 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
1345#else
1346 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
1347#endif
1348 );
1349}
1350
1351TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
1352 std::string buf = "world";
1353 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
1354 testing::KilledBySignal(SIGABRT),
1355 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
1356}
1357
1358TEST(STDIO_TEST, sprintf_30445072) {
1359 std::string buf = "world";
1360 sprintf(&buf[0], "hello");
1361 ASSERT_EQ(buf, "hello");
1362}