blob: f0e0ab6d41e776a28a9497339218d09ae8fb0ad4 [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 Hughes3a4c4542017-07-19 17:20:24 -070030#include <string>
Elliott Hughese6bb5a22015-01-23 17:48:15 -080031#include <vector>
32
Elliott Hughesfb3873d2016-08-10 11:07:54 -070033#include "BionicDeathTest.h"
Calin Juravle03e4ebe2014-05-08 14:42:06 +010034#include "TemporaryFile.h"
Josh Gao2f06e102017-01-10 13:00:37 -080035#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070036
Christopher Ferris13f26a72016-01-13 13:47:58 -080037#if defined(NOFORTIFY)
38#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070039#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080040#else
41#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070042#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080043#endif
44
Elliott Hughes3a4c4542017-07-19 17:20:24 -070045using namespace std::string_literals;
46
Elliott Hughesfb3873d2016-08-10 11:07:54 -070047class stdio_DeathTest : public BionicDeathTest {};
48class stdio_nofortify_DeathTest : public BionicDeathTest {};
49
Elliott Hughes33a8cb12017-07-25 18:06:46 -070050static void SetFileTo(const char* path, const char* content) {
51 FILE* fp;
52 ASSERT_NE(nullptr, fp = fopen(path, "w"));
53 ASSERT_NE(EOF, fputs(content, fp));
54 ASSERT_EQ(0, fclose(fp));
55}
56
57static void AssertFileIs(const char* path, const char* expected) {
58 FILE* fp;
59 ASSERT_NE(nullptr, fp = fopen(path, "r"));
60 char* line = nullptr;
61 size_t length;
62 ASSERT_NE(EOF, getline(&line, &length, fp));
63 ASSERT_EQ(0, fclose(fp));
64 ASSERT_STREQ(expected, line);
65 free(line);
66}
67
Elliott Hughes70715da2016-08-01 16:35:17 -070068static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
69 rewind(fp);
70
71 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080072 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070073 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
74 ASSERT_STREQ(expected, line);
75
76 if (is_fmemopen) {
77 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
78 // extra empty line, but does on every C library I tested...
79 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
80 ASSERT_STREQ("", line);
81 }
82
83 // Make sure there isn't anything else in the file.
84 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
85}
86
Christopher Ferris13f26a72016-01-13 13:47:58 -080087TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080088 // Check that we have a _recursive_ mutex for flockfile.
89 flockfile(stderr);
90 feof(stderr); // We don't care about the result, but this needs to take the lock.
91 funlockfile(stderr);
92}
93
Christopher Ferris13f26a72016-01-13 13:47:58 -080094TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080095 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
96 FILE* fp = fopen("/dev/null", "w");
97 ASSERT_TRUE(fp != NULL);
98 flockfile(fp);
99 feof(fp);
100 funlockfile(fp);
101 fclose(fp);
102}
103
Christopher Ferris13f26a72016-01-13 13:47:58 -0800104TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700105 FILE* fp = tmpfile();
106 ASSERT_TRUE(fp != NULL);
107
108 int fd = fileno(fp);
109 ASSERT_NE(fd, -1);
110
111 struct stat sb;
112 int rc = fstat(fd, &sb);
113 ASSERT_NE(rc, -1);
114 ASSERT_EQ(sb.st_mode & 0777, 0600U);
115
116 rc = fprintf(fp, "hello\n");
117 ASSERT_EQ(rc, 6);
118
Elliott Hughes70715da2016-08-01 16:35:17 -0700119 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700120 fclose(fp);
121}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300122
Elliott Hughesf226ee52016-02-03 11:24:28 -0800123TEST(STDIO_TEST, tmpfile64) {
124 FILE* fp = tmpfile64();
125 ASSERT_TRUE(fp != nullptr);
126 fclose(fp);
127}
128
Christopher Ferris13f26a72016-01-13 13:47:58 -0800129TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100130 TemporaryFile tf;
131
132 int rc = dprintf(tf.fd, "hello\n");
133 ASSERT_EQ(rc, 6);
134
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800135 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700136 FILE* tfile = fdopen(tf.fd, "r");
137 ASSERT_TRUE(tfile != NULL);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100138
Elliott Hughes70715da2016-08-01 16:35:17 -0700139 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700140 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100141}
142
Christopher Ferris13f26a72016-01-13 13:47:58 -0800143TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300144 FILE* fp = tmpfile();
145 ASSERT_TRUE(fp != NULL);
146
147 const char* line_written = "This is a test";
148 int rc = fprintf(fp, "%s", line_written);
149 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
150
151 rewind(fp);
152
153 char* word_read = NULL;
154 size_t allocated_length = 0;
155
156 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
157 for (size_t i = 0; i < 5; ++i) {
158 ASSERT_FALSE(feof(fp));
159 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
160 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800161 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300162 }
163 // The last read should have set the end-of-file indicator for the stream.
164 ASSERT_TRUE(feof(fp));
165 clearerr(fp);
166
167 // getdelim returns -1 but doesn't set errno if we're already at EOF.
168 // It should set the end-of-file indicator for the stream, though.
169 errno = 0;
170 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800171 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300172 ASSERT_TRUE(feof(fp));
173
174 free(word_read);
175 fclose(fp);
176}
177
Christopher Ferris13f26a72016-01-13 13:47:58 -0800178TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300179 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800180 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300181
182 char* buffer = NULL;
183 size_t buffer_length = 0;
184
185 // The first argument can't be NULL.
186 errno = 0;
187 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800188 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300189
190 // The second argument can't be NULL.
191 errno = 0;
192 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800193 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300194
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700195 // The underlying fd can't be closed.
196 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300197 errno = 0;
198 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800199 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700200 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300201}
202
Christopher Ferris13f26a72016-01-13 13:47:58 -0800203TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700204 FILE* fp = fopen("/proc", "r");
205 ASSERT_TRUE(fp != NULL);
206 char* word_read;
207 size_t allocated_length;
208 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
209 fclose(fp);
210}
211
Christopher Ferris13f26a72016-01-13 13:47:58 -0800212TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300213 FILE* fp = tmpfile();
214 ASSERT_TRUE(fp != NULL);
215
216 const char* line_written = "This is a test for getline\n";
217 const size_t line_count = 5;
218
219 for (size_t i = 0; i < line_count; ++i) {
220 int rc = fprintf(fp, "%s", line_written);
221 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
222 }
223
224 rewind(fp);
225
226 char* line_read = NULL;
227 size_t allocated_length = 0;
228
229 size_t read_line_count = 0;
230 ssize_t read_char_count;
231 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
232 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
233 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800234 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300235 ++read_line_count;
236 }
237 ASSERT_EQ(read_line_count, line_count);
238
239 // The last read should have set the end-of-file indicator for the stream.
240 ASSERT_TRUE(feof(fp));
241 clearerr(fp);
242
243 // getline returns -1 but doesn't set errno if we're already at EOF.
244 // It should set the end-of-file indicator for the stream, though.
245 errno = 0;
246 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800247 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300248 ASSERT_TRUE(feof(fp));
249
250 free(line_read);
251 fclose(fp);
252}
253
Christopher Ferris13f26a72016-01-13 13:47:58 -0800254TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300255 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800256 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300257
258 char* buffer = NULL;
259 size_t buffer_length = 0;
260
261 // The first argument can't be NULL.
262 errno = 0;
263 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800264 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300265
266 // The second argument can't be NULL.
267 errno = 0;
268 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800269 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300270
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700271 // The underlying fd can't be closed.
272 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300273 errno = 0;
274 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800275 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700276 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300277}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000278
Christopher Ferris13f26a72016-01-13 13:47:58 -0800279TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800280 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800281 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800282 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
283 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000284 // error: format '%zd' expects argument of type 'signed size_t',
285 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
286 ssize_t v = 1;
287 char buf[32];
288 snprintf(buf, sizeof(buf), "%zd", v);
289}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800290
Elliott Hughes05493712014-04-17 17:30:03 -0700291// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800292TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700293 char buf[BUFSIZ];
294 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
295 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
296}
297
Christopher Ferris13f26a72016-01-13 13:47:58 -0800298TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700299 char buf[BUFSIZ];
300 wint_t wc = L'a';
301 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
302 EXPECT_STREQ("<a>", buf);
303}
304
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700305TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
306 char buf[BUFSIZ];
307 wchar_t wc = L'a';
308 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
309 EXPECT_STREQ("<a>", buf);
310}
311
Christopher Ferris13f26a72016-01-13 13:47:58 -0800312TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700313 char buf[BUFSIZ];
314 wchar_t* ws = NULL;
315 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
316 EXPECT_STREQ("<(null)>", buf);
317
318 wchar_t chars[] = { L'h', L'i', 0 };
319 ws = chars;
320 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
321 EXPECT_STREQ("<hi>", buf);
322}
323
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700324TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
325 char buf[BUFSIZ];
326 wchar_t* ws = NULL;
327 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
328 EXPECT_STREQ("<(null)>", buf);
329
330 wchar_t chars[] = { L'h', L'i', 0 };
331 ws = chars;
332 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
333 EXPECT_STREQ("<hi>", buf);
334}
335
Christopher Ferris13f26a72016-01-13 13:47:58 -0800336TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700337#if defined(__BIONIC__)
Elliott Hughese2341d02014-05-02 18:16:32 -0700338 // http://b/14492135
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700339 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700340 int i = 1234;
341 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
342 EXPECT_EQ(1234, i);
343 EXPECT_STREQ("a n b", buf);
344#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800345 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700346#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700347}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700348
Christopher Ferris13f26a72016-01-13 13:47:58 -0800349TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700350 char buf[BUFSIZ];
351
352 snprintf(buf, sizeof(buf), "a");
353 EXPECT_STREQ("a", buf);
354
355 snprintf(buf, sizeof(buf), "%%");
356 EXPECT_STREQ("%", buf);
357
358 snprintf(buf, sizeof(buf), "01234");
359 EXPECT_STREQ("01234", buf);
360
361 snprintf(buf, sizeof(buf), "a%sb", "01234");
362 EXPECT_STREQ("a01234b", buf);
363
364 char* s = NULL;
365 snprintf(buf, sizeof(buf), "a%sb", s);
366 EXPECT_STREQ("a(null)b", buf);
367
368 snprintf(buf, sizeof(buf), "aa%scc", "bb");
369 EXPECT_STREQ("aabbcc", buf);
370
371 snprintf(buf, sizeof(buf), "a%cc", 'b');
372 EXPECT_STREQ("abc", buf);
373
374 snprintf(buf, sizeof(buf), "a%db", 1234);
375 EXPECT_STREQ("a1234b", buf);
376
377 snprintf(buf, sizeof(buf), "a%db", -8123);
378 EXPECT_STREQ("a-8123b", buf);
379
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700380 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700381 EXPECT_STREQ("a16b", buf);
382
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700383 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700384 EXPECT_STREQ("a16b", buf);
385
386 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
387 EXPECT_STREQ("a68719476736b", buf);
388
389 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
390 EXPECT_STREQ("a70000b", buf);
391
392 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
393 EXPECT_STREQ("a0xb0001234b", buf);
394
395 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
396 EXPECT_STREQ("a12abz", buf);
397
398 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
399 EXPECT_STREQ("a12ABz", buf);
400
401 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
402 EXPECT_STREQ("a00123456z", buf);
403
404 snprintf(buf, sizeof(buf), "a%5dz", 1234);
405 EXPECT_STREQ("a 1234z", buf);
406
407 snprintf(buf, sizeof(buf), "a%05dz", 1234);
408 EXPECT_STREQ("a01234z", buf);
409
410 snprintf(buf, sizeof(buf), "a%8dz", 1234);
411 EXPECT_STREQ("a 1234z", buf);
412
413 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
414 EXPECT_STREQ("a1234 z", buf);
415
416 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
417 EXPECT_STREQ("Aabcdef Z", buf);
418
419 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
420 EXPECT_STREQ("Ahello:1234Z", buf);
421
422 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
423 EXPECT_STREQ("a005:5:05z", buf);
424
425 void* p = NULL;
426 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700427#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700428 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800429#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700430 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800431#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700432
433 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
434 EXPECT_STREQ("a68719476736,6,7,8z", buf);
435
436 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
437 EXPECT_STREQ("a_1.230000_b", buf);
438
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700439 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700440 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400441
442 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
443 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700444}
445
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800446template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700447static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
448 int sscanf_fn(const T*, const T*, ...),
449 const T* fmt_string, const T* fmt, const T* fmt_plus,
450 const T* minus_inf, const T* inf_, const T* plus_inf,
451 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800452 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700453 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700454
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700455 // NaN.
456
457 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800458 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700459 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
460 EXPECT_TRUE(isnan(f));
461
462 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800463 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700464 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
465 EXPECT_TRUE(isnan(f));
466
467 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800468 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700469 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
470 EXPECT_TRUE(isnan(f));
471
472 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800473 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700474 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
475 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800476
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700477 // Inf.
478
479 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800480 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700481 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
482 EXPECT_EQ(HUGE_VALF, f);
483
484 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800485 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700486 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
487 EXPECT_EQ(-HUGE_VALF, f);
488
489 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800490 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700491 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
492 EXPECT_EQ(HUGE_VALF, f);
493
494 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800495 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700496 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
497 EXPECT_EQ(-HUGE_VALF, f);
498
499 // Check case-insensitivity.
500 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
501 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
502 EXPECT_EQ(HUGE_VALF, f);
503 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
504 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
505 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700506}
507
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700508TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
509 CheckInfNan(snprintf, sscanf, "%s",
510 "[%a]", "[%+a]",
511 "[-inf]", "[inf]", "[+inf]",
512 "[-nan]", "[nan]", "[+nan]");
513 CheckInfNan(snprintf, sscanf, "%s",
514 "[%A]", "[%+A]",
515 "[-INF]", "[INF]", "[+INF]",
516 "[-NAN]", "[NAN]", "[+NAN]");
517 CheckInfNan(snprintf, sscanf, "%s",
518 "[%e]", "[%+e]",
519 "[-inf]", "[inf]", "[+inf]",
520 "[-nan]", "[nan]", "[+nan]");
521 CheckInfNan(snprintf, sscanf, "%s",
522 "[%E]", "[%+E]",
523 "[-INF]", "[INF]", "[+INF]",
524 "[-NAN]", "[NAN]", "[+NAN]");
525 CheckInfNan(snprintf, sscanf, "%s",
526 "[%f]", "[%+f]",
527 "[-inf]", "[inf]", "[+inf]",
528 "[-nan]", "[nan]", "[+nan]");
529 CheckInfNan(snprintf, sscanf, "%s",
530 "[%F]", "[%+F]",
531 "[-INF]", "[INF]", "[+INF]",
532 "[-NAN]", "[NAN]", "[+NAN]");
533 CheckInfNan(snprintf, sscanf, "%s",
534 "[%g]", "[%+g]",
535 "[-inf]", "[inf]", "[+inf]",
536 "[-nan]", "[nan]", "[+nan]");
537 CheckInfNan(snprintf, sscanf, "%s",
538 "[%G]", "[%+G]",
539 "[-INF]", "[INF]", "[+INF]",
540 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800541}
Elliott Hughes7823f322014-04-14 12:11:28 -0700542
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700543TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
544 CheckInfNan(swprintf, swscanf, L"%s",
545 L"[%a]", L"[%+a]",
546 L"[-inf]", L"[inf]", L"[+inf]",
547 L"[-nan]", L"[nan]", L"[+nan]");
548 CheckInfNan(swprintf, swscanf, L"%s",
549 L"[%A]", L"[%+A]",
550 L"[-INF]", L"[INF]", L"[+INF]",
551 L"[-NAN]", L"[NAN]", L"[+NAN]");
552 CheckInfNan(swprintf, swscanf, L"%s",
553 L"[%e]", L"[%+e]",
554 L"[-inf]", L"[inf]", L"[+inf]",
555 L"[-nan]", L"[nan]", L"[+nan]");
556 CheckInfNan(swprintf, swscanf, L"%s",
557 L"[%E]", L"[%+E]",
558 L"[-INF]", L"[INF]", L"[+INF]",
559 L"[-NAN]", L"[NAN]", L"[+NAN]");
560 CheckInfNan(swprintf, swscanf, L"%s",
561 L"[%f]", L"[%+f]",
562 L"[-inf]", L"[inf]", L"[+inf]",
563 L"[-nan]", L"[nan]", L"[+nan]");
564 CheckInfNan(swprintf, swscanf, L"%s",
565 L"[%F]", L"[%+F]",
566 L"[-INF]", L"[INF]", L"[+INF]",
567 L"[-NAN]", L"[NAN]", L"[+NAN]");
568 CheckInfNan(swprintf, swscanf, L"%s",
569 L"[%g]", L"[%+g]",
570 L"[-inf]", L"[inf]", L"[+inf]",
571 L"[-nan]", L"[nan]", L"[+nan]");
572 CheckInfNan(swprintf, swscanf, L"%s",
573 L"[%G]", L"[%+G]",
574 L"[-INF]", L"[INF]", L"[+INF]",
575 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700576}
577
Dan Albert9601f162017-08-09 14:59:06 -0700578TEST(STDIO_TEST, swprintf) {
579 constexpr size_t nchars = 32;
580 wchar_t buf[nchars];
581
582 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
583 ASSERT_EQ(std::wstring(L"ab"), buf);
584 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
585 ASSERT_EQ(std::wstring(L"abcde"), buf);
586
587 // Unlike swprintf(), swprintf() returns -1 in case of truncation
588 // and doesn't necessarily zero-terminate the output!
589 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
590
591 const char kString[] = "Hello, World";
592 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
593 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
594 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
595 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
596}
597
598TEST(STDIO_TEST, swprintf_a) {
599 constexpr size_t nchars = 32;
600 wchar_t buf[nchars];
601
602 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
603 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
604}
605
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700606TEST(STDIO_TEST, swprintf_lc) {
607 constexpr size_t nchars = 32;
608 wchar_t buf[nchars];
609
610 wint_t wc = L'a';
611 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
612 EXPECT_EQ(std::wstring(L"<a>"), buf);
613}
614
615TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
616 constexpr size_t nchars = 32;
617 wchar_t buf[nchars];
618
619 wint_t wc = L'a';
620 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
621 EXPECT_EQ(std::wstring(L"<a>"), buf);
622}
623
Elliott Hughes618303c2017-11-02 16:58:44 -0700624TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
625 constexpr size_t nchars = 32;
626 wchar_t buf[nchars];
627
628 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
629 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
630}
631
632TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
633 constexpr size_t nchars = 32;
634 wchar_t buf[nchars];
635
636 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
637 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
638}
639
640TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
641 constexpr size_t nchars = 32;
642 wchar_t buf[nchars];
643
644 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
645 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
646}
647
648TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
649 constexpr size_t nchars = 32;
650 wchar_t buf[nchars];
651
652 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
653 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
654}
655
Dan Albert9601f162017-08-09 14:59:06 -0700656TEST(STDIO_TEST, swprintf_ls) {
657 constexpr size_t nchars = 32;
658 wchar_t buf[nchars];
659
660 static const wchar_t kWideString[] = L"Hello\uff41 World";
661 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
662 ASSERT_EQ(std::wstring(kWideString), buf);
663 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
664 ASSERT_EQ(std::wstring(kWideString), buf);
665}
666
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700667TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
668 constexpr size_t nchars = 32;
669 wchar_t buf[nchars];
670
671 static const wchar_t kWideString[] = L"Hello\uff41 World";
672 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
673 ASSERT_EQ(std::wstring(kWideString), buf);
674 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
675 ASSERT_EQ(std::wstring(kWideString), buf);
676}
677
Christopher Ferris13f26a72016-01-13 13:47:58 -0800678TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700679 char buf[BUFSIZ];
680 snprintf(buf, sizeof(buf), "%d", INT_MAX);
681 EXPECT_STREQ("2147483647", buf);
682}
683
Christopher Ferris13f26a72016-01-13 13:47:58 -0800684TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700685 char buf[BUFSIZ];
686 snprintf(buf, sizeof(buf), "%d", INT_MIN);
687 EXPECT_STREQ("-2147483648", buf);
688}
689
Elliott Hughes618303c2017-11-02 16:58:44 -0700690TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
691 char buf[BUFSIZ];
692 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
693 EXPECT_STREQ("9223372036854775807", buf);
694}
695
696TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
697 char buf[BUFSIZ];
698 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
699 EXPECT_STREQ("-9223372036854775808", buf);
700}
701
702TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
703 char buf[BUFSIZ];
704 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
705 EXPECT_STREQ("18446744073709551615", buf);
706}
707
708TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
709 char buf[BUFSIZ];
710 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
711 EXPECT_STREQ("18446744073709551615", buf);
712}
713
Christopher Ferris13f26a72016-01-13 13:47:58 -0800714TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700715 char buf[BUFSIZ];
716 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700717#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700718 EXPECT_STREQ("9223372036854775807", buf);
719#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700720 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700721#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700722}
723
Christopher Ferris13f26a72016-01-13 13:47:58 -0800724TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700725 char buf[BUFSIZ];
726 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700727#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700728 EXPECT_STREQ("-9223372036854775808", buf);
729#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700730 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700731#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700732}
733
Christopher Ferris13f26a72016-01-13 13:47:58 -0800734TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700735 char buf[BUFSIZ];
736 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
737 EXPECT_STREQ("9223372036854775807", buf);
738}
739
Christopher Ferris13f26a72016-01-13 13:47:58 -0800740TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700741 char buf[BUFSIZ];
742 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
743 EXPECT_STREQ("-9223372036854775808", buf);
744}
745
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700746TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
747 char buf[BUFSIZ];
748 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
749 EXPECT_STREQ("37777777777", buf);
750}
751
752TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
753 char buf[BUFSIZ];
754 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
755 EXPECT_STREQ("4294967295", buf);
756}
757
758TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
759 char buf[BUFSIZ];
760 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
761 EXPECT_STREQ("ffffffff", buf);
762}
763
764TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
765 char buf[BUFSIZ];
766 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
767 EXPECT_STREQ("FFFFFFFF", buf);
768}
769
Christopher Ferris13f26a72016-01-13 13:47:58 -0800770TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700771 char buf[BUFSIZ];
772
773 snprintf(buf, sizeof(buf), "%e", 1.5);
774 EXPECT_STREQ("1.500000e+00", buf);
775
776 snprintf(buf, sizeof(buf), "%Le", 1.5l);
777 EXPECT_STREQ("1.500000e+00", buf);
778}
779
Christopher Ferris13f26a72016-01-13 13:47:58 -0800780TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700781 char buf[BUFSIZ];
782
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800783 snprintf(buf, sizeof(buf), "%e", -0.0);
784 EXPECT_STREQ("-0.000000e+00", buf);
785 snprintf(buf, sizeof(buf), "%E", -0.0);
786 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700787 snprintf(buf, sizeof(buf), "%f", -0.0);
788 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800789 snprintf(buf, sizeof(buf), "%F", -0.0);
790 EXPECT_STREQ("-0.000000", buf);
791 snprintf(buf, sizeof(buf), "%g", -0.0);
792 EXPECT_STREQ("-0", buf);
793 snprintf(buf, sizeof(buf), "%G", -0.0);
794 EXPECT_STREQ("-0", buf);
795 snprintf(buf, sizeof(buf), "%a", -0.0);
796 EXPECT_STREQ("-0x0p+0", buf);
797 snprintf(buf, sizeof(buf), "%A", -0.0);
798 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700799}
800
Christopher Ferris13f26a72016-01-13 13:47:58 -0800801TEST(STDIO_TEST, snprintf_utf8_15439554) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700802 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700803 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700804
Elliott Hughes69f05d22014-06-05 20:10:09 -0700805 // http://b/15439554
806 char buf[BUFSIZ];
807
808 // 1-byte character.
809 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
810 EXPECT_STREQ("1x2", buf);
811 // 2-byte character.
812 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
813 EXPECT_STREQ("1¢2", buf);
814 // 3-byte character.
815 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
816 EXPECT_STREQ("1€2", buf);
817 // 4-byte character.
818 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
819 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700820
Wally Yaua40fdbd2014-08-26 09:47:23 -0700821 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700822 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700823}
824
Elliott Hughes43f7c872016-02-05 11:18:41 -0800825static void* snprintf_small_stack_fn(void*) {
826 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
827 char buf[PATH_MAX];
828 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
829 return nullptr;
830}
831
832TEST(STDIO_TEST, snprintf_small_stack) {
833 // Is it safe to call snprintf on a thread with a small stack?
834 // (The snprintf implementation puts some pretty large buffers on the stack.)
835 pthread_attr_t a;
836 ASSERT_EQ(0, pthread_attr_init(&a));
837 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
838
839 pthread_t t;
840 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
841 ASSERT_EQ(0, pthread_join(t, nullptr));
842}
843
Elliott Hughes8200e552016-02-05 21:57:37 -0800844TEST(STDIO_TEST, snprintf_asterisk_overflow) {
845 char buf[128];
846 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
847 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
848 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
849 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
850 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
851
852 // INT_MAX-1, INT_MAX, INT_MAX+1.
853 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
854 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
855 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
856 ASSERT_EQ(ENOMEM, errno);
857}
858
Elliott Hughes70715da2016-08-01 16:35:17 -0700859TEST(STDIO_TEST, fprintf) {
860 TemporaryFile tf;
861
862 FILE* tfile = fdopen(tf.fd, "r+");
863 ASSERT_TRUE(tfile != nullptr);
864
865 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
866 AssertFileIs(tfile, "123 abc");
867 fclose(tfile);
868}
869
Christopher Ferris13f26a72016-01-13 13:47:58 -0800870TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700871 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700872 FILE* fp;
873
874 // Unbuffered case where the fprintf(3) itself fails.
875 ASSERT_NE(nullptr, fp = tmpfile());
876 setbuf(fp, NULL);
877 ASSERT_EQ(4, fprintf(fp, "epic"));
878 ASSERT_EQ(0, close(fileno(fp)));
879 ASSERT_EQ(-1, fprintf(fp, "fail"));
880 ASSERT_EQ(-1, fclose(fp));
881
882 // Buffered case where we won't notice until the fclose(3).
883 // It's likely this is what was actually seen in http://b/7229520,
884 // and that expecting fprintf to fail is setting yourself up for
885 // disappointment. Remember to check fclose(3)'s return value, kids!
886 ASSERT_NE(nullptr, fp = tmpfile());
887 ASSERT_EQ(4, fprintf(fp, "epic"));
888 ASSERT_EQ(0, close(fileno(fp)));
889 ASSERT_EQ(4, fprintf(fp, "fail"));
890 ASSERT_EQ(-1, fclose(fp));
891}
892
Christopher Ferris13f26a72016-01-13 13:47:58 -0800893TEST(STDIO_TEST, popen) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800894 FILE* fp = popen("cat /proc/version", "r");
895 ASSERT_TRUE(fp != NULL);
896
897 char buf[16];
898 char* s = fgets(buf, sizeof(buf), fp);
899 buf[13] = '\0';
900 ASSERT_STREQ("Linux version", s);
901
902 ASSERT_EQ(0, pclose(fp));
903}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700904
Christopher Ferris13f26a72016-01-13 13:47:58 -0800905TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700906 FILE* fp = fopen("/proc/version", "r");
907 ASSERT_TRUE(fp != NULL);
908 ASSERT_EQ('L', getc(fp));
909 ASSERT_EQ('i', getc(fp));
910 ASSERT_EQ('n', getc(fp));
911 ASSERT_EQ('u', getc(fp));
912 ASSERT_EQ('x', getc(fp));
913 fclose(fp);
914}
915
Christopher Ferris13f26a72016-01-13 13:47:58 -0800916TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700917 FILE* fp = fopen("/proc/version", "r");
918 ASSERT_TRUE(fp != NULL);
919 ASSERT_EQ(EOF, putc('x', fp));
920 fclose(fp);
921}
Elliott Hughes603332f2014-03-12 17:10:41 -0700922
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700923TEST(STDIO_TEST, sscanf_swscanf) {
924 struct stuff {
925 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800926 int i1, i2;
927 char cs1[3];
928 char s2[3];
929 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700930 double d1;
931 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800932 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700933
934 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800935 EXPECT_STREQ("hello", s1);
936 EXPECT_EQ(123, i1);
937 EXPECT_EQ(456, i2);
938 EXPECT_EQ('a', cs1[0]);
939 EXPECT_EQ('b', cs1[1]);
940 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
941 EXPECT_STREQ("AB", s2); // Terminating NUL.
942 EXPECT_EQ('!', c1);
943 EXPECT_DOUBLE_EQ(1.23, d1);
944 EXPECT_FLOAT_EQ(9.0f, f1);
945 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700946 }
947 } s;
948
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800949 memset(&s, 'x', sizeof(s));
950 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
951 "%s %i%i%2c%[A-Z]%c %lf %f %s",
952 s.s1, &s.i1, &s.i2, s.cs1, s.s2, &s.c1, &s.d1, &s.f1, s.s3));
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700953 s.Check();
954
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800955 memset(&s, 'x', sizeof(s));
956 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
957 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
958 s.s1, &s.i1, &s.i2, s.cs1, s.s2, &s.c1, &s.d1, &s.f1, s.s3));
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700959 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -0700960}
Elliott Hughes53b24382014-05-02 18:29:25 -0700961
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800962template <typename T>
963static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
964 const T* input, const T* fmt,
965 int expected_count, const char* expected_string) {
966 char buf[256] = {};
967 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
968 ASSERT_STREQ(expected_string, buf) << fmt;
969}
970
971TEST(STDIO_TEST, sscanf_ccl) {
972 // `abc` is just those characters.
973 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
974 // `a-c` is the range 'a' .. 'c'.
975 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
976 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
977 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
978 // `a-c-e` is equivalent to `a-e`.
979 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
980 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
981 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
982 // An initial '^' negates the set.
983 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
984 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
985 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
986 // The first character may be ']' or '-' without being special.
987 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
988 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
989 // The last character may be '-' without being special.
990 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
991 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
992 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
993}
994
995TEST(STDIO_TEST, swscanf_ccl) {
996 // `abc` is just those characters.
997 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
998 // `a-c` is the range 'a' .. 'c'.
999 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1000 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1001 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1002 // `a-c-e` is equivalent to `a-e`.
1003 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1004 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1005 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1006 // An initial '^' negates the set.
1007 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1008 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1009 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1010 // The first character may be ']' or '-' without being special.
1011 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1012 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1013 // The last character may be '-' without being special.
1014 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1015 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1016 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1017}
1018
Christopher Ferris13f26a72016-01-13 13:47:58 -08001019TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001020 // If we open a file read-only...
1021 FILE* fp = fopen("/proc/version", "r");
1022
1023 // ...all attempts to write to that file should return failure.
1024
1025 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1026 // glibc gets the wide-character functions wrong.
1027
1028 errno = 0;
1029 EXPECT_EQ(EOF, putc('x', fp));
1030 EXPECT_EQ(EBADF, errno);
1031
1032 errno = 0;
1033 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1034 EXPECT_EQ(EBADF, errno);
1035
1036 errno = 0;
1037 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001038#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001039 EXPECT_EQ(EBADF, errno);
1040#endif
1041
1042 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001043 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1044 EXPECT_EQ(EBADF, errno);
1045
1046 errno = 0;
1047 EXPECT_EQ(EOF, fputs("hello", fp));
1048 EXPECT_EQ(EBADF, errno);
1049
1050 errno = 0;
1051 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001052#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001053 EXPECT_EQ(EBADF, errno);
1054#endif
1055}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001056
1057// Tests that we can only have a consistent and correct fpos_t when using
1058// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001059TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001060 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1061 uselocale(LC_GLOBAL_LOCALE);
1062
1063 FILE* fp = tmpfile();
1064 ASSERT_TRUE(fp != NULL);
1065
1066 wchar_t mb_one_bytes = L'h';
1067 wchar_t mb_two_bytes = 0x00a2;
1068 wchar_t mb_three_bytes = 0x20ac;
1069 wchar_t mb_four_bytes = 0x24b62;
1070
1071 // Write to file.
1072 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1073 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1074 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1075 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1076
1077 rewind(fp);
1078
1079 // Record each character position.
1080 fpos_t pos1;
1081 fpos_t pos2;
1082 fpos_t pos3;
1083 fpos_t pos4;
1084 fpos_t pos5;
1085 EXPECT_EQ(0, fgetpos(fp, &pos1));
1086 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1087 EXPECT_EQ(0, fgetpos(fp, &pos2));
1088 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1089 EXPECT_EQ(0, fgetpos(fp, &pos3));
1090 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1091 EXPECT_EQ(0, fgetpos(fp, &pos4));
1092 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1093 EXPECT_EQ(0, fgetpos(fp, &pos5));
1094
Elliott Hughes063525c2014-05-13 11:19:57 -07001095#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001096 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1097 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1098 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1099 // structure.
1100 ASSERT_EQ(0, static_cast<off_t>(pos1));
1101 ASSERT_EQ(1, static_cast<off_t>(pos2));
1102 ASSERT_EQ(3, static_cast<off_t>(pos3));
1103 ASSERT_EQ(6, static_cast<off_t>(pos4));
1104 ASSERT_EQ(10, static_cast<off_t>(pos5));
1105#endif
1106
1107 // Exercise back and forth movements of the position.
1108 ASSERT_EQ(0, fsetpos(fp, &pos2));
1109 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1110 ASSERT_EQ(0, fsetpos(fp, &pos1));
1111 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1112 ASSERT_EQ(0, fsetpos(fp, &pos4));
1113 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1114 ASSERT_EQ(0, fsetpos(fp, &pos3));
1115 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1116 ASSERT_EQ(0, fsetpos(fp, &pos5));
1117 ASSERT_EQ(WEOF, fgetwc(fp));
1118
1119 fclose(fp);
1120}
1121
1122// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001123TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001124 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1125 uselocale(LC_GLOBAL_LOCALE);
1126
Calin Juravle9b95ea92014-05-14 17:07:10 +01001127 // In glibc-2.16 fseek doesn't work properly in wide mode
1128 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1129 // to close and re-open the file. We do it in order to make the test pass
1130 // with all glibcs.
1131
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001132 TemporaryFile tf;
1133 FILE* fp = fdopen(tf.fd, "w+");
1134 ASSERT_TRUE(fp != NULL);
1135
1136 wchar_t mb_two_bytes = 0x00a2;
1137 wchar_t mb_three_bytes = 0x20ac;
1138 wchar_t mb_four_bytes = 0x24b62;
1139
1140 // Write to file.
1141 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1142 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1143 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1144
1145 fflush(fp);
1146 fclose(fp);
1147
1148 fp = fopen(tf.filename, "r");
1149 ASSERT_TRUE(fp != NULL);
1150
1151 // Store a valid position.
1152 fpos_t mb_two_bytes_pos;
1153 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1154
1155 // Move inside mb_four_bytes with fseek.
1156 long offset_inside_mb = 6;
1157 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1158
1159 // Store the "inside multi byte" position.
1160 fpos_t pos_inside_mb;
1161 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001162#if defined(__BIONIC__)
1163 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1164#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001165
1166 // Reading from within a byte should produce an error.
1167 ASSERT_EQ(WEOF, fgetwc(fp));
1168 ASSERT_EQ(EILSEQ, errno);
1169
1170 // Reverting to a valid position should work.
1171 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1172 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1173
1174 // Moving withing a multi byte with fsetpos should work but reading should
1175 // produce an error.
1176 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1177 ASSERT_EQ(WEOF, fgetwc(fp));
1178 ASSERT_EQ(EILSEQ, errno);
1179
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001180 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001181}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001182
Christopher Ferris13f26a72016-01-13 13:47:58 -08001183TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001184 char buf[16];
1185 memset(buf, 0, sizeof(buf));
1186 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1187 ASSERT_EQ('<', fputc('<', fp));
1188 ASSERT_NE(EOF, fputs("abc>\n", fp));
1189 fflush(fp);
1190
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001191 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001192 ASSERT_STREQ("<abc>\n", buf);
1193
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001194 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001195 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001196 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001197}
1198
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001199TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001200 FILE* fp = fmemopen(nullptr, 128, "r+");
1201 ASSERT_NE(EOF, fputs("xyz\n", fp));
1202
Elliott Hughes70715da2016-08-01 16:35:17 -07001203 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001204 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001205}
1206
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001207TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1208 FILE* fp;
1209 char buf[8];
1210
1211 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1212 // shall be written at the current position or at the end of the buffer,
1213 // depending on the size of the contents."
1214 memset(buf, 'x', sizeof(buf));
1215 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1216 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1217 ASSERT_EQ(0, fflush(fp));
1218 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1219 // Now write and check that the NUL moves along with our writes...
1220 ASSERT_NE(EOF, fputs("hello", fp));
1221 ASSERT_EQ(0, fflush(fp));
1222 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1223 ASSERT_NE(EOF, fputs("wo", fp));
1224 ASSERT_EQ(0, fflush(fp));
1225 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1226 ASSERT_EQ(0, fclose(fp));
1227
1228 // "If a stream open for update is flushed or closed and the last write has
1229 // advanced the current buffer size, a null byte shall be written at the end
1230 // of the buffer if it fits."
1231 memset(buf, 'x', sizeof(buf));
1232 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1233 // Nothing written yet, so no advance...
1234 ASSERT_EQ(0, fflush(fp));
1235 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1236 ASSERT_NE(EOF, fputs("hello", fp));
1237 ASSERT_EQ(0, fclose(fp));
1238}
1239
1240TEST(STDIO_TEST, fmemopen_size) {
1241 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001242 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001243 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001244
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001245 // POSIX: "The stream shall also maintain the size of the current buffer
1246 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1247 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001248
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001249 // "For modes r and r+ the size shall be set to the value given by the size
1250 // argument."
1251 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1252 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1253 EXPECT_EQ(16, ftell(fp));
1254 EXPECT_EQ(16, ftello(fp));
1255 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1256 EXPECT_EQ(16, ftell(fp));
1257 EXPECT_EQ(16, ftello(fp));
1258 ASSERT_EQ(0, fclose(fp));
1259 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1260 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1261 EXPECT_EQ(16, ftell(fp));
1262 EXPECT_EQ(16, ftello(fp));
1263 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1264 EXPECT_EQ(16, ftell(fp));
1265 EXPECT_EQ(16, ftello(fp));
1266 ASSERT_EQ(0, fclose(fp));
1267
1268 // "For modes w and w+ the initial size shall be zero..."
1269 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1270 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1271 EXPECT_EQ(0, ftell(fp));
1272 EXPECT_EQ(0, ftello(fp));
1273 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1274 EXPECT_EQ(0, ftell(fp));
1275 EXPECT_EQ(0, ftello(fp));
1276 ASSERT_EQ(0, fclose(fp));
1277 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1278 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1279 EXPECT_EQ(0, ftell(fp));
1280 EXPECT_EQ(0, ftello(fp));
1281 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1282 EXPECT_EQ(0, ftell(fp));
1283 EXPECT_EQ(0, ftello(fp));
1284 ASSERT_EQ(0, fclose(fp));
1285
1286 // "...and for modes a and a+ the initial size shall be:
1287 // 1. Zero, if buf is a null pointer
1288 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1289 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1290 EXPECT_EQ(0, ftell(fp));
1291 EXPECT_EQ(0, ftello(fp));
1292 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1293 EXPECT_EQ(0, ftell(fp));
1294 EXPECT_EQ(0, ftello(fp));
1295 ASSERT_EQ(0, fclose(fp));
1296 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1297 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1298 EXPECT_EQ(0, ftell(fp));
1299 EXPECT_EQ(0, ftello(fp));
1300 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1301 EXPECT_EQ(0, ftell(fp));
1302 EXPECT_EQ(0, ftello(fp));
1303 ASSERT_EQ(0, fclose(fp));
1304
1305 // 2. The position of the first null byte in the buffer, if one is found
1306 memset(buf, 'x', sizeof(buf));
1307 buf[3] = '\0';
1308 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1309 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1310 EXPECT_EQ(3, ftell(fp));
1311 EXPECT_EQ(3, ftello(fp));
1312 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1313 EXPECT_EQ(3, ftell(fp));
1314 EXPECT_EQ(3, ftello(fp));
1315 ASSERT_EQ(0, fclose(fp));
1316 memset(buf, 'x', sizeof(buf));
1317 buf[3] = '\0';
1318 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1319 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1320 EXPECT_EQ(3, ftell(fp));
1321 EXPECT_EQ(3, ftello(fp));
1322 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1323 EXPECT_EQ(3, ftell(fp));
1324 EXPECT_EQ(3, ftello(fp));
1325 ASSERT_EQ(0, fclose(fp));
1326
1327 // 3. The value of the size argument, if buf is not a null pointer and no
1328 // null byte is found.
1329 memset(buf, 'x', sizeof(buf));
1330 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1331 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1332 EXPECT_EQ(16, ftell(fp));
1333 EXPECT_EQ(16, ftello(fp));
1334 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1335 EXPECT_EQ(16, ftell(fp));
1336 EXPECT_EQ(16, ftello(fp));
1337 ASSERT_EQ(0, fclose(fp));
1338 memset(buf, 'x', sizeof(buf));
1339 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1340 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1341 EXPECT_EQ(16, ftell(fp));
1342 EXPECT_EQ(16, ftello(fp));
1343 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1344 EXPECT_EQ(16, ftell(fp));
1345 EXPECT_EQ(16, ftello(fp));
1346 ASSERT_EQ(0, fclose(fp));
1347}
1348
1349TEST(STDIO_TEST, fmemopen_SEEK_END) {
1350 // fseek SEEK_END is relative to the current string length, not the buffer size.
1351 FILE* fp;
1352 char buf[8];
1353 memset(buf, 'x', sizeof(buf));
1354 strcpy(buf, "str");
1355 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1356 ASSERT_NE(EOF, fputs("string", fp));
1357 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1358 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1359 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1360 EXPECT_EQ(0, fclose(fp));
1361
1362 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1363 // than adding).
1364 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1365 ASSERT_NE(EOF, fputs("54321", fp));
1366 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1367 EXPECT_EQ('2', fgetc(fp));
1368 EXPECT_EQ(0, fclose(fp));
1369}
1370
1371TEST(STDIO_TEST, fmemopen_seek_invalid) {
1372 char buf[8];
1373 memset(buf, 'x', sizeof(buf));
1374 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1375 ASSERT_TRUE(fp != nullptr);
1376
1377 // POSIX: "An attempt to seek ... to a negative position or to a position
1378 // larger than the buffer size given in the size argument shall fail."
1379 // (There's no mention of what errno should be set to, and glibc doesn't
1380 // set errno in any of these cases.)
1381 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1382 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1383 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1384 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1385}
1386
1387TEST(STDIO_TEST, fmemopen_read_EOF) {
1388 // POSIX: "A read operation on the stream shall not advance the current
1389 // buffer position beyond the current buffer size."
1390 char buf[8];
1391 memset(buf, 'x', sizeof(buf));
1392 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1393 ASSERT_TRUE(fp != nullptr);
1394 char buf2[BUFSIZ];
1395 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1396 // POSIX: "Reaching the buffer size in a read operation shall count as
1397 // end-of-file.
1398 ASSERT_TRUE(feof(fp));
1399 ASSERT_EQ(EOF, fgetc(fp));
1400 ASSERT_EQ(0, fclose(fp));
1401}
1402
1403TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1404 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1405 char buf[] = "h\0e\0l\0l\0o";
1406 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1407 ASSERT_TRUE(fp != nullptr);
1408 ASSERT_EQ('h', fgetc(fp));
1409 ASSERT_EQ(0, fgetc(fp));
1410 ASSERT_EQ('e', fgetc(fp));
1411 ASSERT_EQ(0, fgetc(fp));
1412 ASSERT_EQ('l', fgetc(fp));
1413 ASSERT_EQ(0, fgetc(fp));
1414 // POSIX: "The read operation shall start at the current buffer position of
1415 // the stream."
1416 char buf2[8];
1417 memset(buf2, 'x', sizeof(buf2));
1418 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1419 ASSERT_EQ('l', buf2[0]);
1420 ASSERT_EQ(0, buf2[1]);
1421 ASSERT_EQ('o', buf2[2]);
1422 ASSERT_EQ(0, buf2[3]);
1423 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1424 ASSERT_TRUE(feof(fp));
1425 ASSERT_EQ(0, fclose(fp));
1426}
1427
1428TEST(STDIO_TEST, fmemopen_write) {
1429 FILE* fp;
1430 char buf[8];
1431
1432 // POSIX: "A write operation shall start either at the current position of
1433 // the stream (if mode has not specified 'a' as the first character)..."
1434 memset(buf, 'x', sizeof(buf));
1435 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1436 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1437 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1438 ASSERT_EQ(' ', fputc(' ', fp));
1439 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1440 ASSERT_EQ(0, fclose(fp));
1441
1442 // "...or at the current size of the stream (if mode had 'a' as the first
1443 // character)." (See the fmemopen_size test for what "size" means, but for
1444 // mode "a", it's the first NUL byte.)
1445 memset(buf, 'x', sizeof(buf));
1446 buf[3] = '\0';
1447 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1448 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1449 ASSERT_EQ(' ', fputc(' ', fp));
1450 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1451 ASSERT_EQ(0, fclose(fp));
1452
1453 // "If the current position at the end of the write is larger than the
1454 // current buffer size, the current buffer size shall be set to the current
1455 // position." (See the fmemopen_size test for what "size" means, but to
1456 // query it we SEEK_END with offset 0, and then ftell.)
1457 memset(buf, 'x', sizeof(buf));
1458 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1459 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1460 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1461 EXPECT_EQ(0, ftell(fp));
1462 ASSERT_EQ(' ', fputc(' ', fp));
1463 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1464 EXPECT_EQ(1, ftell(fp));
1465 ASSERT_NE(EOF, fputs("123", fp));
1466 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1467 EXPECT_EQ(4, ftell(fp));
1468 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1469 ASSERT_EQ(0, fclose(fp));
1470}
1471
1472TEST(STDIO_TEST, fmemopen_write_EOF) {
1473 // POSIX: "A write operation on the stream shall not advance the current
1474 // buffer size beyond the size given in the size argument."
1475 FILE* fp;
1476
1477 // Scalar writes...
1478 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1479 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1480 ASSERT_EQ('x', fputc('x', fp));
1481 ASSERT_EQ('x', fputc('x', fp));
1482 ASSERT_EQ('x', fputc('x', fp));
1483 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1484 ASSERT_EQ(0, fclose(fp));
1485
1486 // Vector writes...
1487 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1488 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1489 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1490 ASSERT_EQ(0, fclose(fp));
1491}
1492
1493TEST(STDIO_TEST, fmemopen_initial_position) {
1494 // POSIX: "The ... current position in the buffer ... shall be initially
1495 // set to either the beginning of the buffer (for r and w modes) ..."
1496 char buf[] = "hello\0world";
1497 FILE* fp;
1498 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1499 EXPECT_EQ(0L, ftell(fp));
1500 EXPECT_EQ(0, fclose(fp));
1501 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1502 EXPECT_EQ(0L, ftell(fp));
1503 EXPECT_EQ(0, fclose(fp));
1504 buf[0] = 'h'; // (Undo the effects of the above.)
1505
1506 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1507 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1508 EXPECT_EQ(5L, ftell(fp));
1509 EXPECT_EQ(0, fclose(fp));
1510
1511 // POSIX: "If no null byte is found in append mode, the initial position
1512 // shall be set to one byte after the end of the buffer."
1513 memset(buf, 'x', sizeof(buf));
1514 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1515 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1516 EXPECT_EQ(0, fclose(fp));
1517}
1518
1519TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1520 // POSIX: "If buf is a null pointer, the initial position shall always be
1521 // set to the beginning of the buffer."
1522 FILE* fp = fmemopen(nullptr, 128, "a+");
1523 ASSERT_TRUE(fp != nullptr);
1524 EXPECT_EQ(0L, ftell(fp));
1525 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1526 EXPECT_EQ(0, fclose(fp));
1527}
1528
1529TEST(STDIO_TEST, fmemopen_zero_length) {
1530 // POSIX says it's up to the implementation whether or not you can have a
1531 // zero-length buffer (but "A future version of this standard may require
1532 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1533 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1534 FILE* fp;
1535 char buf[16];
1536 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1537 ASSERT_EQ(EOF, fgetc(fp));
1538 ASSERT_TRUE(feof(fp));
1539 ASSERT_EQ(0, fclose(fp));
1540 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1541 ASSERT_EQ(EOF, fgetc(fp));
1542 ASSERT_TRUE(feof(fp));
1543 ASSERT_EQ(0, fclose(fp));
1544
1545 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1546 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1547 ASSERT_EQ(EOF, fputc('x', fp));
1548 ASSERT_EQ(0, fclose(fp));
1549 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1550 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1551 ASSERT_EQ(EOF, fputc('x', fp));
1552 ASSERT_EQ(0, fclose(fp));
1553}
1554
1555TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1556 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1557 // BSD fails, glibc doesn't. We side with the more lenient.
1558 FILE* fp;
1559 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1560 ASSERT_EQ(0, fclose(fp));
1561 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1562 ASSERT_EQ(0, fclose(fp));
1563}
1564
1565TEST(STDIO_TEST, fmemopen_fileno) {
1566 // There's no fd backing an fmemopen FILE*.
1567 FILE* fp = fmemopen(nullptr, 16, "r");
1568 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001569 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001570 ASSERT_EQ(-1, fileno(fp));
1571 ASSERT_EQ(EBADF, errno);
1572 ASSERT_EQ(0, fclose(fp));
1573}
1574
1575TEST(STDIO_TEST, fmemopen_append_after_seek) {
1576 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1577 // there had been an intervening seek.
1578
1579 FILE* fp;
1580 char buf[] = "hello\0world";
1581 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1582 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1583 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1584 ASSERT_NE(EOF, fputc('!', fp));
1585 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1586 ASSERT_EQ(0, fclose(fp));
1587
1588 memcpy(buf, "hello\0world", sizeof(buf));
1589 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1590 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1591 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1592 ASSERT_NE(EOF, fputc('!', fp));
1593 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1594 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001595}
1596
Christopher Ferris13f26a72016-01-13 13:47:58 -08001597TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001598 char* p = nullptr;
1599 size_t size = 0;
1600 FILE* fp = open_memstream(&p, &size);
1601 ASSERT_NE(EOF, fputs("hello, world!", fp));
1602 fclose(fp);
1603
1604 ASSERT_STREQ("hello, world!", p);
1605 ASSERT_EQ(strlen("hello, world!"), size);
1606 free(p);
1607}
1608
Christopher Ferris13f26a72016-01-13 13:47:58 -08001609TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001610#if defined(__BIONIC__)
1611 char* p;
1612 size_t size;
1613
1614 // Invalid buffer.
1615 errno = 0;
1616 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1617 ASSERT_EQ(EINVAL, errno);
1618
1619 // Invalid size.
1620 errno = 0;
1621 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1622 ASSERT_EQ(EINVAL, errno);
1623#else
Elliott Hughes9677fab2016-01-25 15:50:59 -08001624 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001625#endif
1626}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001627
Christopher Ferris13f26a72016-01-13 13:47:58 -08001628TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001629 int fd = open("/proc/version", O_RDONLY);
1630 ASSERT_TRUE(fd != -1);
1631
1632 // This fd doesn't have O_CLOEXEC...
1633 int flags = fcntl(fd, F_GETFD);
1634 ASSERT_TRUE(flags != -1);
1635 ASSERT_EQ(0, flags & FD_CLOEXEC);
1636
1637 FILE* fp = fdopen(fd, "re");
1638 ASSERT_TRUE(fp != NULL);
1639
1640 // ...but the new one does.
1641 flags = fcntl(fileno(fp), F_GETFD);
1642 ASSERT_TRUE(flags != -1);
1643 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
1644
1645 fclose(fp);
1646 close(fd);
1647}
1648
Christopher Ferris13f26a72016-01-13 13:47:58 -08001649TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001650 FILE* fp = fopen("/proc/version", "r");
1651 ASSERT_TRUE(fp != NULL);
1652
1653 // This FILE* doesn't have O_CLOEXEC...
1654 int flags = fcntl(fileno(fp), F_GETFD);
1655 ASSERT_TRUE(flags != -1);
1656 ASSERT_EQ(0, flags & FD_CLOEXEC);
1657
1658 fp = freopen("/proc/version", "re", fp);
1659
1660 // ...but the new one does.
1661 flags = fcntl(fileno(fp), F_GETFD);
1662 ASSERT_TRUE(flags != -1);
1663 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
1664
1665 fclose(fp);
1666}
Elliott Hughes20841a12014-12-01 16:13:30 -08001667
Elliott Hughesf226ee52016-02-03 11:24:28 -08001668TEST(STDIO_TEST, fopen64_freopen64) {
1669 FILE* fp = fopen64("/proc/version", "r");
1670 ASSERT_TRUE(fp != nullptr);
1671 fp = freopen64("/proc/version", "re", fp);
1672 ASSERT_TRUE(fp != nullptr);
1673 fclose(fp);
1674}
1675
Elliott Hughes20841a12014-12-01 16:13:30 -08001676// https://code.google.com/p/android/issues/detail?id=81155
1677// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001678TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08001679 FILE* fp = fopen("/dev/zero", "r");
1680 ASSERT_TRUE(fp != NULL);
1681
1682 // Make this stream unbuffered.
1683 setvbuf(fp, 0, _IONBF, 0);
1684
1685 char buf[65*1024];
1686 memset(buf, 0xff, sizeof(buf));
1687
1688 time_t t0 = time(NULL);
1689 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001690 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08001691 }
1692 time_t t1 = time(NULL);
1693
1694 fclose(fp);
1695
1696 // 1024 64KiB reads should have been very quick.
1697 ASSERT_LE(t1 - t0, 1);
1698
1699 for (size_t i = 0; i < 64*1024; ++i) {
1700 ASSERT_EQ('\0', buf[i]);
1701 }
1702 for (size_t i = 64*1024; i < 65*1024; ++i) {
1703 ASSERT_EQ('\xff', buf[i]);
1704 }
1705}
Elliott Hughes75b99382015-01-20 11:23:50 -08001706
Christopher Ferris13f26a72016-01-13 13:47:58 -08001707TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001708 std::string digits("0123456789");
1709 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08001710
1711 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
1712 char buf1[4 * 4];
1713 memset(buf1, 0, sizeof(buf1));
1714 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001715 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08001716 ASSERT_TRUE(feof(fp));
1717
1718 rewind(fp);
1719
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001720 // Try to read way too much so stdio tries to read more direct from the stream.
1721 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08001722 memset(buf2, 0, sizeof(buf2));
1723 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001724 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08001725 ASSERT_TRUE(feof(fp));
1726
1727 fclose(fp);
1728}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001729
1730static void test_fread_from_write_only_stream(size_t n) {
1731 FILE* fp = fopen("/dev/null", "w");
1732 std::vector<char> buf(n, 0);
1733 errno = 0;
1734 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
1735 ASSERT_EQ(EBADF, errno);
1736 ASSERT_TRUE(ferror(fp));
1737 ASSERT_FALSE(feof(fp));
1738 fclose(fp);
1739}
1740
Christopher Ferris13f26a72016-01-13 13:47:58 -08001741TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001742 test_fread_from_write_only_stream(1);
1743}
1744
Christopher Ferris13f26a72016-01-13 13:47:58 -08001745TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001746 test_fread_from_write_only_stream(64*1024);
1747}
1748
1749static void test_fwrite_after_fread(size_t n) {
1750 TemporaryFile tf;
1751
1752 FILE* fp = fdopen(tf.fd, "w+");
1753 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
1754 fflush(fp);
1755
1756 // We've flushed but not rewound, so there's nothing to read.
1757 std::vector<char> buf(n, 0);
1758 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
1759 ASSERT_TRUE(feof(fp));
1760
1761 // But hitting EOF doesn't prevent us from writing...
1762 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08001763 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001764
1765 // And if we rewind, everything's there.
1766 rewind(fp);
1767 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
1768 ASSERT_EQ('1', buf[0]);
1769 ASSERT_EQ('2', buf[1]);
1770
1771 fclose(fp);
1772}
1773
Christopher Ferris13f26a72016-01-13 13:47:58 -08001774TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001775 test_fwrite_after_fread(16);
1776}
1777
Christopher Ferris13f26a72016-01-13 13:47:58 -08001778TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001779 test_fwrite_after_fread(64*1024);
1780}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001781
1782// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08001783TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001784 TemporaryFile tf;
1785
1786 FILE* fp = fopen(tf.filename, "w+");
1787 ASSERT_TRUE(fp != nullptr);
1788
1789 char file_data[12288];
1790 for (size_t i = 0; i < 12288; i++) {
1791 file_data[i] = i;
1792 }
1793 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
1794 fclose(fp);
1795
1796 fp = fopen(tf.filename, "r");
1797 ASSERT_TRUE(fp != nullptr);
1798
1799 char buffer[8192];
1800 size_t cur_location = 0;
1801 // Small read to populate internal buffer.
1802 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
1803 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
1804
1805 cur_location = static_cast<size_t>(ftell(fp));
1806 // Large read to force reading into the user supplied buffer and bypassing
1807 // the internal buffer.
1808 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
1809 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
1810
1811 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08001812 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001813 cur_location = static_cast<size_t>(ftell(fp));
1814 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
1815 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
1816
1817 fclose(fp);
1818}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07001819
1820// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08001821TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07001822 TemporaryFile tf;
1823 char buf[6] = {0};
1824
1825 FILE* fw = fopen(tf.filename, "w");
1826 ASSERT_TRUE(fw != nullptr);
1827
1828 FILE* fr = fopen(tf.filename, "r");
1829 ASSERT_TRUE(fr != nullptr);
1830
1831 fwrite("a", 1, 1, fw);
1832 fflush(fw);
1833 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1834 ASSERT_STREQ("a", buf);
1835
1836 // 'fr' is now at EOF.
1837 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1838 ASSERT_TRUE(feof(fr));
1839
1840 // Write some more...
1841 fwrite("z", 1, 1, fw);
1842 fflush(fw);
1843
1844 // ...and check that we can read it back.
1845 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
1846 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1847 ASSERT_STREQ("z", buf);
1848
1849 // But now we're done.
1850 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1851
1852 fclose(fr);
1853 fclose(fw);
1854}
Elliott Hughes923f1652016-01-19 15:46:05 -08001855
1856TEST(STDIO_TEST, fclose_invalidates_fd) {
1857 // The typical error we're trying to help people catch involves accessing
1858 // memory after it's been freed. But we know that stdin/stdout/stderr are
1859 // special and don't get deallocated, so this test uses stdin.
1860 ASSERT_EQ(0, fclose(stdin));
1861
1862 // Even though using a FILE* after close is undefined behavior, I've closed
1863 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
1864 // especially because they might actually correspond to a real stream.
1865 errno = 0;
1866 ASSERT_EQ(-1, fileno(stdin));
1867 ASSERT_EQ(EBADF, errno);
1868}
Elliott Hughes2704bd12016-01-20 17:14:53 -08001869
1870TEST(STDIO_TEST, fseek_ftell_unseekable) {
1871#if defined(__BIONIC__) // glibc has fopencookie instead.
1872 auto read_fn = [](void*, char*, int) { return -1; };
1873 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
1874 ASSERT_TRUE(fp != nullptr);
1875
1876 // Check that ftell balks on an unseekable FILE*.
1877 errno = 0;
1878 ASSERT_EQ(-1, ftell(fp));
1879 ASSERT_EQ(ESPIPE, errno);
1880
1881 // SEEK_CUR is rewritten as SEEK_SET internally...
1882 errno = 0;
1883 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
1884 ASSERT_EQ(ESPIPE, errno);
1885
1886 // ...so it's worth testing the direct seek path too.
1887 errno = 0;
1888 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
1889 ASSERT_EQ(ESPIPE, errno);
1890
1891 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001892#else
1893 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1894#endif
1895}
1896
1897TEST(STDIO_TEST, funopen_EINVAL) {
1898#if defined(__BIONIC__)
1899 errno = 0;
1900 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
1901 ASSERT_EQ(EINVAL, errno);
1902#else
1903 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1904#endif
1905}
1906
1907TEST(STDIO_TEST, funopen_seek) {
1908#if defined(__BIONIC__)
1909 auto read_fn = [](void*, char*, int) { return -1; };
1910
1911 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
1912 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
1913
1914 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
1915 ASSERT_TRUE(fp != nullptr);
1916 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08001917#if defined(__LP64__)
1918 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
1919 EXPECT_EQ(0xfedcba12LL, pos);
1920#else
1921 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
1922 EXPECT_EQ(EOVERFLOW, errno);
1923#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001924
1925 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
1926 ASSERT_TRUE(fp64 != nullptr);
1927 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08001928 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
1929 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001930#else
1931 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
Elliott Hughes2704bd12016-01-20 17:14:53 -08001932#endif
1933}
Elliott Hughes71288cb2016-01-22 19:22:44 -08001934
1935TEST(STDIO_TEST, lots_of_concurrent_files) {
1936 std::vector<TemporaryFile*> tfs;
1937 std::vector<FILE*> fps;
1938
1939 for (size_t i = 0; i < 256; ++i) {
1940 TemporaryFile* tf = new TemporaryFile;
1941 tfs.push_back(tf);
1942 FILE* fp = fopen(tf->filename, "w+");
1943 fps.push_back(fp);
1944 fprintf(fp, "hello %zu!\n", i);
1945 fflush(fp);
1946 }
1947
1948 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08001949 char expected[BUFSIZ];
1950 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08001951
Elliott Hughes70715da2016-08-01 16:35:17 -07001952 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08001953 fclose(fps[i]);
1954 delete tfs[i];
1955 }
1956}
Elliott Hughes9677fab2016-01-25 15:50:59 -08001957
1958static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
1959 EXPECT_EQ(offset, ftell(fp));
1960 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08001961 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08001962 fpos_t pos;
1963 fpos64_t pos64;
1964 EXPECT_EQ(0, fgetpos(fp, &pos));
1965 EXPECT_EQ(0, fgetpos64(fp, &pos64));
1966#if defined(__BIONIC__)
1967 EXPECT_EQ(offset, static_cast<off64_t>(pos));
1968 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
1969#else
1970 GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n";
1971#endif
1972}
1973
1974TEST(STDIO_TEST, seek_tell_family_smoke) {
1975 TemporaryFile tf;
1976 FILE* fp = fdopen(tf.fd, "w+");
1977
1978 // Initially we should be at 0.
1979 AssertFileOffsetAt(fp, 0);
1980
1981 // Seek to offset 8192.
1982 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
1983 AssertFileOffsetAt(fp, 8192);
1984 fpos_t eight_k_pos;
1985 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
1986
1987 // Seek forward another 8192...
1988 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
1989 AssertFileOffsetAt(fp, 8192 + 8192);
1990 fpos64_t sixteen_k_pos64;
1991 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
1992
1993 // Seek back 8192...
1994 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
1995 AssertFileOffsetAt(fp, 8192);
1996
1997 // Since we haven't written anything, the end is also at 0.
1998 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1999 AssertFileOffsetAt(fp, 0);
2000
2001 // Check that our fpos64_t from 16KiB works...
2002 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2003 AssertFileOffsetAt(fp, 8192 + 8192);
2004 // ...as does our fpos_t from 8192.
2005 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2006 AssertFileOffsetAt(fp, 8192);
2007
2008 // Do fseeko and fseeko64 work too?
2009 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2010 AssertFileOffsetAt(fp, 1234);
2011 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2012 AssertFileOffsetAt(fp, 5678);
2013
2014 fclose(fp);
2015}
2016
2017TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2018 TemporaryFile tf;
2019 FILE* fp = fdopen(tf.fd, "w+");
2020
2021 // Bad whence.
2022 errno = 0;
2023 ASSERT_EQ(-1, fseek(fp, 0, 123));
2024 ASSERT_EQ(EINVAL, errno);
2025 errno = 0;
2026 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2027 ASSERT_EQ(EINVAL, errno);
2028 errno = 0;
2029 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2030 ASSERT_EQ(EINVAL, errno);
2031
2032 // Bad offset.
2033 errno = 0;
2034 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2035 ASSERT_EQ(EINVAL, errno);
2036 errno = 0;
2037 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2038 ASSERT_EQ(EINVAL, errno);
2039 errno = 0;
2040 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2041 ASSERT_EQ(EINVAL, errno);
2042
2043 fclose(fp);
2044}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002045
2046TEST(STDIO_TEST, ctermid) {
2047 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2048
2049 char buf[L_ctermid] = {};
2050 ASSERT_EQ(buf, ctermid(buf));
2051 ASSERT_STREQ("/dev/tty", buf);
2052}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002053
2054TEST(STDIO_TEST, remove) {
2055 struct stat sb;
2056
2057 TemporaryFile tf;
2058 ASSERT_EQ(0, remove(tf.filename));
2059 ASSERT_EQ(-1, lstat(tf.filename, &sb));
2060 ASSERT_EQ(ENOENT, errno);
2061
2062 TemporaryDir td;
2063 ASSERT_EQ(0, remove(td.dirname));
2064 ASSERT_EQ(-1, lstat(td.dirname, &sb));
2065 ASSERT_EQ(ENOENT, errno);
2066
2067 errno = 0;
2068 ASSERT_EQ(-1, remove(tf.filename));
2069 ASSERT_EQ(ENOENT, errno);
2070
2071 errno = 0;
2072 ASSERT_EQ(-1, remove(td.dirname));
2073 ASSERT_EQ(ENOENT, errno);
2074}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002075
2076TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
2077 char buf[16];
2078 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2079 testing::KilledBySignal(SIGABRT),
2080#if defined(NOFORTIFY)
2081 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2082#else
2083 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2084#endif
2085 );
2086}
2087
2088TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
2089 std::string buf = "world";
2090 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2091 testing::KilledBySignal(SIGABRT),
2092 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2093}
2094
2095TEST(STDIO_TEST, sprintf_30445072) {
2096 std::string buf = "world";
2097 sprintf(&buf[0], "hello");
2098 ASSERT_EQ(buf, "hello");
2099}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002100
2101TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2102 TemporaryFile tf;
2103 SetFileTo(tf.filename, "0123456789");
2104 FILE* fp = fopen(tf.filename, "a");
2105 EXPECT_EQ(10, ftell(fp));
2106 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2107 EXPECT_EQ(2, ftell(fp));
2108 ASSERT_NE(EOF, fputs("xxx", fp));
2109 ASSERT_EQ(0, fflush(fp));
2110 EXPECT_EQ(13, ftell(fp));
2111 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2112 EXPECT_EQ(13, ftell(fp));
2113 ASSERT_EQ(0, fclose(fp));
2114 AssertFileIs(tf.filename, "0123456789xxx");
2115}
2116
2117TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2118 TemporaryFile tf;
2119 SetFileTo(tf.filename, "0123456789");
2120 int fd = open(tf.filename, O_RDWR);
2121 ASSERT_NE(-1, fd);
2122 // POSIX: "The file position indicator associated with the new stream is set to the position
2123 // indicated by the file offset associated with the file descriptor."
2124 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2125 FILE* fp = fdopen(fd, "a");
2126 EXPECT_EQ(4, ftell(fp));
2127 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2128 EXPECT_EQ(2, ftell(fp));
2129 ASSERT_NE(EOF, fputs("xxx", fp));
2130 ASSERT_EQ(0, fflush(fp));
2131 EXPECT_EQ(13, ftell(fp));
2132 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2133 EXPECT_EQ(13, ftell(fp));
2134 ASSERT_EQ(0, fclose(fp));
2135 AssertFileIs(tf.filename, "0123456789xxx");
2136}
2137
2138TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2139 TemporaryFile tf;
2140 SetFileTo(tf.filename, "0123456789");
2141 FILE* other_fp = fopen("/proc/version", "r");
2142 FILE* fp = freopen(tf.filename, "a", other_fp);
2143 EXPECT_EQ(10, ftell(fp));
2144 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2145 EXPECT_EQ(2, ftell(fp));
2146 ASSERT_NE(EOF, fputs("xxx", fp));
2147 ASSERT_EQ(0, fflush(fp));
2148 EXPECT_EQ(13, ftell(fp));
2149 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2150 EXPECT_EQ(13, ftell(fp));
2151 ASSERT_EQ(0, fclose(fp));
2152 AssertFileIs(tf.filename, "0123456789xxx");
2153}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002154
2155TEST(STDIO_TEST, constants) {
2156 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2157 ASSERT_EQ(L_tmpnam, PATH_MAX);
2158}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002159
2160TEST(STDIO_TEST, perror) {
2161 ExecTestHelper eth;
2162 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2163 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2164 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2165}
2166
2167TEST(STDIO_TEST, puts) {
2168 ExecTestHelper eth;
2169 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2170}
2171
2172TEST(STDIO_TEST, unlocked) {
2173 TemporaryFile tf;
2174
2175 FILE* fp = fopen(tf.filename, "w+");
2176 ASSERT_TRUE(fp != nullptr);
2177
2178 clearerr_unlocked(fp);
2179 ASSERT_FALSE(feof_unlocked(fp));
2180 ASSERT_FALSE(ferror_unlocked(fp));
2181
2182 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2183
2184 ASSERT_NE(EOF, putc_unlocked('a', fp));
2185 ASSERT_NE(EOF, putc('b', fp));
2186 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2187 ASSERT_NE(EOF, fputc('d', fp));
2188
2189 rewind(fp);
2190 ASSERT_EQ('a', getc_unlocked(fp));
2191 ASSERT_EQ('b', getc(fp));
2192 ASSERT_EQ('c', fgetc_unlocked(fp));
2193 ASSERT_EQ('d', fgetc(fp));
2194
2195 rewind(fp);
2196 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2197 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2198 ASSERT_EQ(0, fflush_unlocked(fp));
2199
2200 rewind(fp);
2201 char buf[BUFSIZ] = {};
2202 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2203 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2204 ASSERT_STREQ("ABCD", buf);
2205
2206 rewind(fp);
2207 ASSERT_NE(EOF, fputs("hello ", fp));
2208 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2209 ASSERT_NE(EOF, fputc('\n', fp));
2210
2211 rewind(fp);
2212 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2213 ASSERT_STREQ("hello world\n", buf);
2214
2215 ASSERT_EQ(0, fclose(fp));
2216}
Ryan Prichardbf549862017-11-07 15:30:32 -08002217
2218TEST(STDIO_TEST, fseek_64bit) {
2219 TemporaryFile tf;
2220 FILE* fp = fopen64(tf.filename, "w+");
2221 ASSERT_TRUE(fp != nullptr);
2222 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2223 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2224 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2225 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2226 ASSERT_EQ(0, fclose(fp));
2227}
2228
2229// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2230// isn't representable in long/off_t.
2231TEST(STDIO_TEST, fseek_overflow_32bit) {
2232 TemporaryFile tf;
2233 FILE* fp = fopen64(tf.filename, "w+");
2234 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2235
2236 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2237#if defined(__BIONIC__) && !defined(__LP64__)
2238 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2239 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2240 ASSERT_EQ(EOVERFLOW, errno);
2241#endif
2242
2243 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2244 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2245 // and SEEK_END -- many C libraries check neither.)
2246 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2247 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2248
2249 fclose(fp);
2250}