blob: 7b7737d6ded0456e3f2d7f0ac39e744fd634e2c6 [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
Christopher Ferris13f26a72016-01-13 13:47:58 -0800305TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700306 char buf[BUFSIZ];
307 wchar_t* ws = NULL;
308 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
309 EXPECT_STREQ("<(null)>", buf);
310
311 wchar_t chars[] = { L'h', L'i', 0 };
312 ws = chars;
313 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
314 EXPECT_STREQ("<hi>", buf);
315}
316
Christopher Ferris13f26a72016-01-13 13:47:58 -0800317TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700318#if defined(__BIONIC__)
Elliott Hughese2341d02014-05-02 18:16:32 -0700319 // http://b/14492135
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700320 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700321 int i = 1234;
322 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
323 EXPECT_EQ(1234, i);
324 EXPECT_STREQ("a n b", buf);
325#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800326 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700327#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700328}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700329
Christopher Ferris13f26a72016-01-13 13:47:58 -0800330TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700331 char buf[BUFSIZ];
332
333 snprintf(buf, sizeof(buf), "a");
334 EXPECT_STREQ("a", buf);
335
336 snprintf(buf, sizeof(buf), "%%");
337 EXPECT_STREQ("%", buf);
338
339 snprintf(buf, sizeof(buf), "01234");
340 EXPECT_STREQ("01234", buf);
341
342 snprintf(buf, sizeof(buf), "a%sb", "01234");
343 EXPECT_STREQ("a01234b", buf);
344
345 char* s = NULL;
346 snprintf(buf, sizeof(buf), "a%sb", s);
347 EXPECT_STREQ("a(null)b", buf);
348
349 snprintf(buf, sizeof(buf), "aa%scc", "bb");
350 EXPECT_STREQ("aabbcc", buf);
351
352 snprintf(buf, sizeof(buf), "a%cc", 'b');
353 EXPECT_STREQ("abc", buf);
354
355 snprintf(buf, sizeof(buf), "a%db", 1234);
356 EXPECT_STREQ("a1234b", buf);
357
358 snprintf(buf, sizeof(buf), "a%db", -8123);
359 EXPECT_STREQ("a-8123b", buf);
360
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700361 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700362 EXPECT_STREQ("a16b", buf);
363
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700364 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700365 EXPECT_STREQ("a16b", buf);
366
367 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
368 EXPECT_STREQ("a68719476736b", buf);
369
370 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
371 EXPECT_STREQ("a70000b", buf);
372
373 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
374 EXPECT_STREQ("a0xb0001234b", buf);
375
376 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
377 EXPECT_STREQ("a12abz", buf);
378
379 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
380 EXPECT_STREQ("a12ABz", buf);
381
382 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
383 EXPECT_STREQ("a00123456z", buf);
384
385 snprintf(buf, sizeof(buf), "a%5dz", 1234);
386 EXPECT_STREQ("a 1234z", buf);
387
388 snprintf(buf, sizeof(buf), "a%05dz", 1234);
389 EXPECT_STREQ("a01234z", buf);
390
391 snprintf(buf, sizeof(buf), "a%8dz", 1234);
392 EXPECT_STREQ("a 1234z", buf);
393
394 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
395 EXPECT_STREQ("a1234 z", buf);
396
397 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
398 EXPECT_STREQ("Aabcdef Z", buf);
399
400 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
401 EXPECT_STREQ("Ahello:1234Z", buf);
402
403 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
404 EXPECT_STREQ("a005:5:05z", buf);
405
406 void* p = NULL;
407 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700408#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700409 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800410#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700411 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800412#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700413
414 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
415 EXPECT_STREQ("a68719476736,6,7,8z", buf);
416
417 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
418 EXPECT_STREQ("a_1.230000_b", buf);
419
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700420 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700421 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400422
423 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
424 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700425}
426
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800427template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700428static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
429 int sscanf_fn(const T*, const T*, ...),
430 const T* fmt_string, const T* fmt, const T* fmt_plus,
431 const T* minus_inf, const T* inf_, const T* plus_inf,
432 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800433 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700434 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700435
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700436 // NaN.
437
438 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800439 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700440 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
441 EXPECT_TRUE(isnan(f));
442
443 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800444 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700445 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
446 EXPECT_TRUE(isnan(f));
447
448 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800449 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700450 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
451 EXPECT_TRUE(isnan(f));
452
453 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800454 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700455 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
456 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800457
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700458 // Inf.
459
460 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800461 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700462 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
463 EXPECT_EQ(HUGE_VALF, f);
464
465 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800466 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700467 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
468 EXPECT_EQ(-HUGE_VALF, f);
469
470 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800471 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700472 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
473 EXPECT_EQ(HUGE_VALF, f);
474
475 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800476 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700477 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
478 EXPECT_EQ(-HUGE_VALF, f);
479
480 // Check case-insensitivity.
481 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
482 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
483 EXPECT_EQ(HUGE_VALF, f);
484 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
485 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
486 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700487}
488
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700489TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
490 CheckInfNan(snprintf, sscanf, "%s",
491 "[%a]", "[%+a]",
492 "[-inf]", "[inf]", "[+inf]",
493 "[-nan]", "[nan]", "[+nan]");
494 CheckInfNan(snprintf, sscanf, "%s",
495 "[%A]", "[%+A]",
496 "[-INF]", "[INF]", "[+INF]",
497 "[-NAN]", "[NAN]", "[+NAN]");
498 CheckInfNan(snprintf, sscanf, "%s",
499 "[%e]", "[%+e]",
500 "[-inf]", "[inf]", "[+inf]",
501 "[-nan]", "[nan]", "[+nan]");
502 CheckInfNan(snprintf, sscanf, "%s",
503 "[%E]", "[%+E]",
504 "[-INF]", "[INF]", "[+INF]",
505 "[-NAN]", "[NAN]", "[+NAN]");
506 CheckInfNan(snprintf, sscanf, "%s",
507 "[%f]", "[%+f]",
508 "[-inf]", "[inf]", "[+inf]",
509 "[-nan]", "[nan]", "[+nan]");
510 CheckInfNan(snprintf, sscanf, "%s",
511 "[%F]", "[%+F]",
512 "[-INF]", "[INF]", "[+INF]",
513 "[-NAN]", "[NAN]", "[+NAN]");
514 CheckInfNan(snprintf, sscanf, "%s",
515 "[%g]", "[%+g]",
516 "[-inf]", "[inf]", "[+inf]",
517 "[-nan]", "[nan]", "[+nan]");
518 CheckInfNan(snprintf, sscanf, "%s",
519 "[%G]", "[%+G]",
520 "[-INF]", "[INF]", "[+INF]",
521 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800522}
Elliott Hughes7823f322014-04-14 12:11:28 -0700523
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700524TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
525 CheckInfNan(swprintf, swscanf, L"%s",
526 L"[%a]", L"[%+a]",
527 L"[-inf]", L"[inf]", L"[+inf]",
528 L"[-nan]", L"[nan]", L"[+nan]");
529 CheckInfNan(swprintf, swscanf, L"%s",
530 L"[%A]", L"[%+A]",
531 L"[-INF]", L"[INF]", L"[+INF]",
532 L"[-NAN]", L"[NAN]", L"[+NAN]");
533 CheckInfNan(swprintf, swscanf, L"%s",
534 L"[%e]", L"[%+e]",
535 L"[-inf]", L"[inf]", L"[+inf]",
536 L"[-nan]", L"[nan]", L"[+nan]");
537 CheckInfNan(swprintf, swscanf, L"%s",
538 L"[%E]", L"[%+E]",
539 L"[-INF]", L"[INF]", L"[+INF]",
540 L"[-NAN]", L"[NAN]", L"[+NAN]");
541 CheckInfNan(swprintf, swscanf, L"%s",
542 L"[%f]", L"[%+f]",
543 L"[-inf]", L"[inf]", L"[+inf]",
544 L"[-nan]", L"[nan]", L"[+nan]");
545 CheckInfNan(swprintf, swscanf, L"%s",
546 L"[%F]", L"[%+F]",
547 L"[-INF]", L"[INF]", L"[+INF]",
548 L"[-NAN]", L"[NAN]", L"[+NAN]");
549 CheckInfNan(swprintf, swscanf, L"%s",
550 L"[%g]", L"[%+g]",
551 L"[-inf]", L"[inf]", L"[+inf]",
552 L"[-nan]", L"[nan]", L"[+nan]");
553 CheckInfNan(swprintf, swscanf, L"%s",
554 L"[%G]", L"[%+G]",
555 L"[-INF]", L"[INF]", L"[+INF]",
556 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700557}
558
Dan Albert9601f162017-08-09 14:59:06 -0700559TEST(STDIO_TEST, swprintf) {
560 constexpr size_t nchars = 32;
561 wchar_t buf[nchars];
562
563 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
564 ASSERT_EQ(std::wstring(L"ab"), buf);
565 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
566 ASSERT_EQ(std::wstring(L"abcde"), buf);
567
568 // Unlike swprintf(), swprintf() returns -1 in case of truncation
569 // and doesn't necessarily zero-terminate the output!
570 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
571
572 const char kString[] = "Hello, World";
573 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
574 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
575 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
576 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
577}
578
579TEST(STDIO_TEST, swprintf_a) {
580 constexpr size_t nchars = 32;
581 wchar_t buf[nchars];
582
583 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
584 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
585}
586
587TEST(STDIO_TEST, swprintf_ls) {
588 constexpr size_t nchars = 32;
589 wchar_t buf[nchars];
590
591 static const wchar_t kWideString[] = L"Hello\uff41 World";
592 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
593 ASSERT_EQ(std::wstring(kWideString), buf);
594 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
595 ASSERT_EQ(std::wstring(kWideString), buf);
596}
597
Christopher Ferris13f26a72016-01-13 13:47:58 -0800598TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700599 char buf[BUFSIZ];
600 snprintf(buf, sizeof(buf), "%d", INT_MAX);
601 EXPECT_STREQ("2147483647", buf);
602}
603
Christopher Ferris13f26a72016-01-13 13:47:58 -0800604TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700605 char buf[BUFSIZ];
606 snprintf(buf, sizeof(buf), "%d", INT_MIN);
607 EXPECT_STREQ("-2147483648", buf);
608}
609
Christopher Ferris13f26a72016-01-13 13:47:58 -0800610TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700611 char buf[BUFSIZ];
612 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700613#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700614 EXPECT_STREQ("9223372036854775807", buf);
615#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700616 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700617#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700618}
619
Christopher Ferris13f26a72016-01-13 13:47:58 -0800620TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700621 char buf[BUFSIZ];
622 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700623#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700624 EXPECT_STREQ("-9223372036854775808", buf);
625#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700626 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700627#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700628}
629
Christopher Ferris13f26a72016-01-13 13:47:58 -0800630TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700631 char buf[BUFSIZ];
632 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
633 EXPECT_STREQ("9223372036854775807", buf);
634}
635
Christopher Ferris13f26a72016-01-13 13:47:58 -0800636TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700637 char buf[BUFSIZ];
638 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
639 EXPECT_STREQ("-9223372036854775808", buf);
640}
641
Christopher Ferris13f26a72016-01-13 13:47:58 -0800642TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700643 char buf[BUFSIZ];
644
645 snprintf(buf, sizeof(buf), "%e", 1.5);
646 EXPECT_STREQ("1.500000e+00", buf);
647
648 snprintf(buf, sizeof(buf), "%Le", 1.5l);
649 EXPECT_STREQ("1.500000e+00", buf);
650}
651
Christopher Ferris13f26a72016-01-13 13:47:58 -0800652TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700653 char buf[BUFSIZ];
654
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800655 snprintf(buf, sizeof(buf), "%e", -0.0);
656 EXPECT_STREQ("-0.000000e+00", buf);
657 snprintf(buf, sizeof(buf), "%E", -0.0);
658 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700659 snprintf(buf, sizeof(buf), "%f", -0.0);
660 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800661 snprintf(buf, sizeof(buf), "%F", -0.0);
662 EXPECT_STREQ("-0.000000", buf);
663 snprintf(buf, sizeof(buf), "%g", -0.0);
664 EXPECT_STREQ("-0", buf);
665 snprintf(buf, sizeof(buf), "%G", -0.0);
666 EXPECT_STREQ("-0", buf);
667 snprintf(buf, sizeof(buf), "%a", -0.0);
668 EXPECT_STREQ("-0x0p+0", buf);
669 snprintf(buf, sizeof(buf), "%A", -0.0);
670 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700671}
672
Christopher Ferris13f26a72016-01-13 13:47:58 -0800673TEST(STDIO_TEST, snprintf_utf8_15439554) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700674 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700675 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700676
Elliott Hughes69f05d22014-06-05 20:10:09 -0700677 // http://b/15439554
678 char buf[BUFSIZ];
679
680 // 1-byte character.
681 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
682 EXPECT_STREQ("1x2", buf);
683 // 2-byte character.
684 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
685 EXPECT_STREQ("1¢2", buf);
686 // 3-byte character.
687 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
688 EXPECT_STREQ("1€2", buf);
689 // 4-byte character.
690 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
691 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700692
Wally Yaua40fdbd2014-08-26 09:47:23 -0700693 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700694 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700695}
696
Elliott Hughes43f7c872016-02-05 11:18:41 -0800697static void* snprintf_small_stack_fn(void*) {
698 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
699 char buf[PATH_MAX];
700 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
701 return nullptr;
702}
703
704TEST(STDIO_TEST, snprintf_small_stack) {
705 // Is it safe to call snprintf on a thread with a small stack?
706 // (The snprintf implementation puts some pretty large buffers on the stack.)
707 pthread_attr_t a;
708 ASSERT_EQ(0, pthread_attr_init(&a));
709 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
710
711 pthread_t t;
712 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
713 ASSERT_EQ(0, pthread_join(t, nullptr));
714}
715
Elliott Hughes8200e552016-02-05 21:57:37 -0800716TEST(STDIO_TEST, snprintf_asterisk_overflow) {
717 char buf[128];
718 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
719 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
720 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
721 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
722 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
723
724 // INT_MAX-1, INT_MAX, INT_MAX+1.
725 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
726 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
727 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
728 ASSERT_EQ(ENOMEM, errno);
729}
730
Elliott Hughes70715da2016-08-01 16:35:17 -0700731TEST(STDIO_TEST, fprintf) {
732 TemporaryFile tf;
733
734 FILE* tfile = fdopen(tf.fd, "r+");
735 ASSERT_TRUE(tfile != nullptr);
736
737 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
738 AssertFileIs(tfile, "123 abc");
739 fclose(tfile);
740}
741
Christopher Ferris13f26a72016-01-13 13:47:58 -0800742TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700743 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700744 FILE* fp;
745
746 // Unbuffered case where the fprintf(3) itself fails.
747 ASSERT_NE(nullptr, fp = tmpfile());
748 setbuf(fp, NULL);
749 ASSERT_EQ(4, fprintf(fp, "epic"));
750 ASSERT_EQ(0, close(fileno(fp)));
751 ASSERT_EQ(-1, fprintf(fp, "fail"));
752 ASSERT_EQ(-1, fclose(fp));
753
754 // Buffered case where we won't notice until the fclose(3).
755 // It's likely this is what was actually seen in http://b/7229520,
756 // and that expecting fprintf to fail is setting yourself up for
757 // disappointment. Remember to check fclose(3)'s return value, kids!
758 ASSERT_NE(nullptr, fp = tmpfile());
759 ASSERT_EQ(4, fprintf(fp, "epic"));
760 ASSERT_EQ(0, close(fileno(fp)));
761 ASSERT_EQ(4, fprintf(fp, "fail"));
762 ASSERT_EQ(-1, fclose(fp));
763}
764
Christopher Ferris13f26a72016-01-13 13:47:58 -0800765TEST(STDIO_TEST, popen) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800766 FILE* fp = popen("cat /proc/version", "r");
767 ASSERT_TRUE(fp != NULL);
768
769 char buf[16];
770 char* s = fgets(buf, sizeof(buf), fp);
771 buf[13] = '\0';
772 ASSERT_STREQ("Linux version", s);
773
774 ASSERT_EQ(0, pclose(fp));
775}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700776
Christopher Ferris13f26a72016-01-13 13:47:58 -0800777TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700778 FILE* fp = fopen("/proc/version", "r");
779 ASSERT_TRUE(fp != NULL);
780 ASSERT_EQ('L', getc(fp));
781 ASSERT_EQ('i', getc(fp));
782 ASSERT_EQ('n', getc(fp));
783 ASSERT_EQ('u', getc(fp));
784 ASSERT_EQ('x', getc(fp));
785 fclose(fp);
786}
787
Christopher Ferris13f26a72016-01-13 13:47:58 -0800788TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700789 FILE* fp = fopen("/proc/version", "r");
790 ASSERT_TRUE(fp != NULL);
791 ASSERT_EQ(EOF, putc('x', fp));
792 fclose(fp);
793}
Elliott Hughes603332f2014-03-12 17:10:41 -0700794
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700795TEST(STDIO_TEST, sscanf_swscanf) {
796 struct stuff {
797 char s1[123];
798 int i1;
799 double d1;
800 float f1;
801 char s2[123];
802
803 void Check() {
804 ASSERT_STREQ("hello", s1);
805 ASSERT_EQ(123, i1);
806 ASSERT_DOUBLE_EQ(1.23, d1);
807 ASSERT_FLOAT_EQ(9.0f, f1);
808 ASSERT_STREQ("world", s2);
809 }
810 } s;
811
812 memset(&s, 0, sizeof(s));
813 ASSERT_EQ(5, sscanf(" hello 123 1.23 0x1.2p3 world",
814 "%s %i %lf %f %s",
815 s.s1, &s.i1, &s.d1, &s.f1, s.s2));
816 s.Check();
817
818 memset(&s, 0, sizeof(s));
819 ASSERT_EQ(5, swscanf(L" hello 123 1.23 0x1.2p3 world",
820 L"%s %i %lf %f %s",
821 s.s1, &s.i1, &s.d1, &s.f1, s.s2));
822 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -0700823}
Elliott Hughes53b24382014-05-02 18:29:25 -0700824
Christopher Ferris13f26a72016-01-13 13:47:58 -0800825TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -0700826 // If we open a file read-only...
827 FILE* fp = fopen("/proc/version", "r");
828
829 // ...all attempts to write to that file should return failure.
830
831 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
832 // glibc gets the wide-character functions wrong.
833
834 errno = 0;
835 EXPECT_EQ(EOF, putc('x', fp));
836 EXPECT_EQ(EBADF, errno);
837
838 errno = 0;
839 EXPECT_EQ(EOF, fprintf(fp, "hello"));
840 EXPECT_EQ(EBADF, errno);
841
842 errno = 0;
843 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -0700844#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -0700845 EXPECT_EQ(EBADF, errno);
846#endif
847
848 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -0700849 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
850 EXPECT_EQ(EBADF, errno);
851
852 errno = 0;
853 EXPECT_EQ(EOF, fputs("hello", fp));
854 EXPECT_EQ(EBADF, errno);
855
856 errno = 0;
857 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -0700858#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -0700859 EXPECT_EQ(EBADF, errno);
860#endif
861}
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100862
863// Tests that we can only have a consistent and correct fpos_t when using
864// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -0800865TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100866 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
867 uselocale(LC_GLOBAL_LOCALE);
868
869 FILE* fp = tmpfile();
870 ASSERT_TRUE(fp != NULL);
871
872 wchar_t mb_one_bytes = L'h';
873 wchar_t mb_two_bytes = 0x00a2;
874 wchar_t mb_three_bytes = 0x20ac;
875 wchar_t mb_four_bytes = 0x24b62;
876
877 // Write to file.
878 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
879 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
880 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
881 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
882
883 rewind(fp);
884
885 // Record each character position.
886 fpos_t pos1;
887 fpos_t pos2;
888 fpos_t pos3;
889 fpos_t pos4;
890 fpos_t pos5;
891 EXPECT_EQ(0, fgetpos(fp, &pos1));
892 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
893 EXPECT_EQ(0, fgetpos(fp, &pos2));
894 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
895 EXPECT_EQ(0, fgetpos(fp, &pos3));
896 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
897 EXPECT_EQ(0, fgetpos(fp, &pos4));
898 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
899 EXPECT_EQ(0, fgetpos(fp, &pos5));
900
Elliott Hughes063525c2014-05-13 11:19:57 -0700901#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100902 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
903 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
904 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
905 // structure.
906 ASSERT_EQ(0, static_cast<off_t>(pos1));
907 ASSERT_EQ(1, static_cast<off_t>(pos2));
908 ASSERT_EQ(3, static_cast<off_t>(pos3));
909 ASSERT_EQ(6, static_cast<off_t>(pos4));
910 ASSERT_EQ(10, static_cast<off_t>(pos5));
911#endif
912
913 // Exercise back and forth movements of the position.
914 ASSERT_EQ(0, fsetpos(fp, &pos2));
915 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
916 ASSERT_EQ(0, fsetpos(fp, &pos1));
917 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
918 ASSERT_EQ(0, fsetpos(fp, &pos4));
919 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
920 ASSERT_EQ(0, fsetpos(fp, &pos3));
921 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
922 ASSERT_EQ(0, fsetpos(fp, &pos5));
923 ASSERT_EQ(WEOF, fgetwc(fp));
924
925 fclose(fp);
926}
927
928// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -0800929TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100930 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
931 uselocale(LC_GLOBAL_LOCALE);
932
Calin Juravle9b95ea92014-05-14 17:07:10 +0100933 // In glibc-2.16 fseek doesn't work properly in wide mode
934 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
935 // to close and re-open the file. We do it in order to make the test pass
936 // with all glibcs.
937
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100938 TemporaryFile tf;
939 FILE* fp = fdopen(tf.fd, "w+");
940 ASSERT_TRUE(fp != NULL);
941
942 wchar_t mb_two_bytes = 0x00a2;
943 wchar_t mb_three_bytes = 0x20ac;
944 wchar_t mb_four_bytes = 0x24b62;
945
946 // Write to file.
947 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
948 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
949 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
950
951 fflush(fp);
952 fclose(fp);
953
954 fp = fopen(tf.filename, "r");
955 ASSERT_TRUE(fp != NULL);
956
957 // Store a valid position.
958 fpos_t mb_two_bytes_pos;
959 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
960
961 // Move inside mb_four_bytes with fseek.
962 long offset_inside_mb = 6;
963 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
964
965 // Store the "inside multi byte" position.
966 fpos_t pos_inside_mb;
967 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -0700968#if defined(__BIONIC__)
969 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
970#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100971
972 // Reading from within a byte should produce an error.
973 ASSERT_EQ(WEOF, fgetwc(fp));
974 ASSERT_EQ(EILSEQ, errno);
975
976 // Reverting to a valid position should work.
977 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
978 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
979
980 // Moving withing a multi byte with fsetpos should work but reading should
981 // produce an error.
982 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
983 ASSERT_EQ(WEOF, fgetwc(fp));
984 ASSERT_EQ(EILSEQ, errno);
985
Elliott Hughes3a4c4542017-07-19 17:20:24 -0700986 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100987}
Elliott Hughes6b841db2014-08-20 16:10:49 -0700988
Christopher Ferris13f26a72016-01-13 13:47:58 -0800989TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -0700990 char buf[16];
991 memset(buf, 0, sizeof(buf));
992 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
993 ASSERT_EQ('<', fputc('<', fp));
994 ASSERT_NE(EOF, fputs("abc>\n", fp));
995 fflush(fp);
996
Elliott Hughes3a4c4542017-07-19 17:20:24 -0700997 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -0700998 ASSERT_STREQ("<abc>\n", buf);
999
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001000 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001001 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001002 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001003}
1004
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001005TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001006 FILE* fp = fmemopen(nullptr, 128, "r+");
1007 ASSERT_NE(EOF, fputs("xyz\n", fp));
1008
Elliott Hughes70715da2016-08-01 16:35:17 -07001009 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001010 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001011}
1012
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001013TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1014 FILE* fp;
1015 char buf[8];
1016
1017 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1018 // shall be written at the current position or at the end of the buffer,
1019 // depending on the size of the contents."
1020 memset(buf, 'x', sizeof(buf));
1021 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1022 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1023 ASSERT_EQ(0, fflush(fp));
1024 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1025 // Now write and check that the NUL moves along with our writes...
1026 ASSERT_NE(EOF, fputs("hello", fp));
1027 ASSERT_EQ(0, fflush(fp));
1028 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1029 ASSERT_NE(EOF, fputs("wo", fp));
1030 ASSERT_EQ(0, fflush(fp));
1031 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1032 ASSERT_EQ(0, fclose(fp));
1033
1034 // "If a stream open for update is flushed or closed and the last write has
1035 // advanced the current buffer size, a null byte shall be written at the end
1036 // of the buffer if it fits."
1037 memset(buf, 'x', sizeof(buf));
1038 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1039 // Nothing written yet, so no advance...
1040 ASSERT_EQ(0, fflush(fp));
1041 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1042 ASSERT_NE(EOF, fputs("hello", fp));
1043 ASSERT_EQ(0, fclose(fp));
1044}
1045
1046TEST(STDIO_TEST, fmemopen_size) {
1047 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001048 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001049 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001050
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001051 // POSIX: "The stream shall also maintain the size of the current buffer
1052 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1053 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001054
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001055 // "For modes r and r+ the size shall be set to the value given by the size
1056 // argument."
1057 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1058 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1059 EXPECT_EQ(16, ftell(fp));
1060 EXPECT_EQ(16, ftello(fp));
1061 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1062 EXPECT_EQ(16, ftell(fp));
1063 EXPECT_EQ(16, ftello(fp));
1064 ASSERT_EQ(0, fclose(fp));
1065 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1066 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1067 EXPECT_EQ(16, ftell(fp));
1068 EXPECT_EQ(16, ftello(fp));
1069 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1070 EXPECT_EQ(16, ftell(fp));
1071 EXPECT_EQ(16, ftello(fp));
1072 ASSERT_EQ(0, fclose(fp));
1073
1074 // "For modes w and w+ the initial size shall be zero..."
1075 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1076 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1077 EXPECT_EQ(0, ftell(fp));
1078 EXPECT_EQ(0, ftello(fp));
1079 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1080 EXPECT_EQ(0, ftell(fp));
1081 EXPECT_EQ(0, ftello(fp));
1082 ASSERT_EQ(0, fclose(fp));
1083 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1084 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1085 EXPECT_EQ(0, ftell(fp));
1086 EXPECT_EQ(0, ftello(fp));
1087 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1088 EXPECT_EQ(0, ftell(fp));
1089 EXPECT_EQ(0, ftello(fp));
1090 ASSERT_EQ(0, fclose(fp));
1091
1092 // "...and for modes a and a+ the initial size shall be:
1093 // 1. Zero, if buf is a null pointer
1094 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1095 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1096 EXPECT_EQ(0, ftell(fp));
1097 EXPECT_EQ(0, ftello(fp));
1098 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1099 EXPECT_EQ(0, ftell(fp));
1100 EXPECT_EQ(0, ftello(fp));
1101 ASSERT_EQ(0, fclose(fp));
1102 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1103 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1104 EXPECT_EQ(0, ftell(fp));
1105 EXPECT_EQ(0, ftello(fp));
1106 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1107 EXPECT_EQ(0, ftell(fp));
1108 EXPECT_EQ(0, ftello(fp));
1109 ASSERT_EQ(0, fclose(fp));
1110
1111 // 2. The position of the first null byte in the buffer, if one is found
1112 memset(buf, 'x', sizeof(buf));
1113 buf[3] = '\0';
1114 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1115 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1116 EXPECT_EQ(3, ftell(fp));
1117 EXPECT_EQ(3, ftello(fp));
1118 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1119 EXPECT_EQ(3, ftell(fp));
1120 EXPECT_EQ(3, ftello(fp));
1121 ASSERT_EQ(0, fclose(fp));
1122 memset(buf, 'x', sizeof(buf));
1123 buf[3] = '\0';
1124 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1125 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1126 EXPECT_EQ(3, ftell(fp));
1127 EXPECT_EQ(3, ftello(fp));
1128 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1129 EXPECT_EQ(3, ftell(fp));
1130 EXPECT_EQ(3, ftello(fp));
1131 ASSERT_EQ(0, fclose(fp));
1132
1133 // 3. The value of the size argument, if buf is not a null pointer and no
1134 // null byte is found.
1135 memset(buf, 'x', sizeof(buf));
1136 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1137 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1138 EXPECT_EQ(16, ftell(fp));
1139 EXPECT_EQ(16, ftello(fp));
1140 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1141 EXPECT_EQ(16, ftell(fp));
1142 EXPECT_EQ(16, ftello(fp));
1143 ASSERT_EQ(0, fclose(fp));
1144 memset(buf, 'x', sizeof(buf));
1145 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1146 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1147 EXPECT_EQ(16, ftell(fp));
1148 EXPECT_EQ(16, ftello(fp));
1149 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1150 EXPECT_EQ(16, ftell(fp));
1151 EXPECT_EQ(16, ftello(fp));
1152 ASSERT_EQ(0, fclose(fp));
1153}
1154
1155TEST(STDIO_TEST, fmemopen_SEEK_END) {
1156 // fseek SEEK_END is relative to the current string length, not the buffer size.
1157 FILE* fp;
1158 char buf[8];
1159 memset(buf, 'x', sizeof(buf));
1160 strcpy(buf, "str");
1161 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1162 ASSERT_NE(EOF, fputs("string", fp));
1163 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1164 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1165 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1166 EXPECT_EQ(0, fclose(fp));
1167
1168 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1169 // than adding).
1170 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1171 ASSERT_NE(EOF, fputs("54321", fp));
1172 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1173 EXPECT_EQ('2', fgetc(fp));
1174 EXPECT_EQ(0, fclose(fp));
1175}
1176
1177TEST(STDIO_TEST, fmemopen_seek_invalid) {
1178 char buf[8];
1179 memset(buf, 'x', sizeof(buf));
1180 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1181 ASSERT_TRUE(fp != nullptr);
1182
1183 // POSIX: "An attempt to seek ... to a negative position or to a position
1184 // larger than the buffer size given in the size argument shall fail."
1185 // (There's no mention of what errno should be set to, and glibc doesn't
1186 // set errno in any of these cases.)
1187 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1188 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1189 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1190 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1191}
1192
1193TEST(STDIO_TEST, fmemopen_read_EOF) {
1194 // POSIX: "A read operation on the stream shall not advance the current
1195 // buffer position beyond the current buffer size."
1196 char buf[8];
1197 memset(buf, 'x', sizeof(buf));
1198 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1199 ASSERT_TRUE(fp != nullptr);
1200 char buf2[BUFSIZ];
1201 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1202 // POSIX: "Reaching the buffer size in a read operation shall count as
1203 // end-of-file.
1204 ASSERT_TRUE(feof(fp));
1205 ASSERT_EQ(EOF, fgetc(fp));
1206 ASSERT_EQ(0, fclose(fp));
1207}
1208
1209TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1210 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1211 char buf[] = "h\0e\0l\0l\0o";
1212 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1213 ASSERT_TRUE(fp != nullptr);
1214 ASSERT_EQ('h', fgetc(fp));
1215 ASSERT_EQ(0, fgetc(fp));
1216 ASSERT_EQ('e', fgetc(fp));
1217 ASSERT_EQ(0, fgetc(fp));
1218 ASSERT_EQ('l', fgetc(fp));
1219 ASSERT_EQ(0, fgetc(fp));
1220 // POSIX: "The read operation shall start at the current buffer position of
1221 // the stream."
1222 char buf2[8];
1223 memset(buf2, 'x', sizeof(buf2));
1224 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1225 ASSERT_EQ('l', buf2[0]);
1226 ASSERT_EQ(0, buf2[1]);
1227 ASSERT_EQ('o', buf2[2]);
1228 ASSERT_EQ(0, buf2[3]);
1229 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1230 ASSERT_TRUE(feof(fp));
1231 ASSERT_EQ(0, fclose(fp));
1232}
1233
1234TEST(STDIO_TEST, fmemopen_write) {
1235 FILE* fp;
1236 char buf[8];
1237
1238 // POSIX: "A write operation shall start either at the current position of
1239 // the stream (if mode has not specified 'a' as the first character)..."
1240 memset(buf, 'x', sizeof(buf));
1241 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1242 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1243 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1244 ASSERT_EQ(' ', fputc(' ', fp));
1245 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1246 ASSERT_EQ(0, fclose(fp));
1247
1248 // "...or at the current size of the stream (if mode had 'a' as the first
1249 // character)." (See the fmemopen_size test for what "size" means, but for
1250 // mode "a", it's the first NUL byte.)
1251 memset(buf, 'x', sizeof(buf));
1252 buf[3] = '\0';
1253 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1254 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1255 ASSERT_EQ(' ', fputc(' ', fp));
1256 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1257 ASSERT_EQ(0, fclose(fp));
1258
1259 // "If the current position at the end of the write is larger than the
1260 // current buffer size, the current buffer size shall be set to the current
1261 // position." (See the fmemopen_size test for what "size" means, but to
1262 // query it we SEEK_END with offset 0, and then ftell.)
1263 memset(buf, 'x', sizeof(buf));
1264 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1265 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1266 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1267 EXPECT_EQ(0, ftell(fp));
1268 ASSERT_EQ(' ', fputc(' ', fp));
1269 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1270 EXPECT_EQ(1, ftell(fp));
1271 ASSERT_NE(EOF, fputs("123", fp));
1272 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1273 EXPECT_EQ(4, ftell(fp));
1274 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1275 ASSERT_EQ(0, fclose(fp));
1276}
1277
1278TEST(STDIO_TEST, fmemopen_write_EOF) {
1279 // POSIX: "A write operation on the stream shall not advance the current
1280 // buffer size beyond the size given in the size argument."
1281 FILE* fp;
1282
1283 // Scalar writes...
1284 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1285 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1286 ASSERT_EQ('x', fputc('x', fp));
1287 ASSERT_EQ('x', fputc('x', fp));
1288 ASSERT_EQ('x', fputc('x', fp));
1289 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1290 ASSERT_EQ(0, fclose(fp));
1291
1292 // Vector writes...
1293 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1294 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1295 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1296 ASSERT_EQ(0, fclose(fp));
1297}
1298
1299TEST(STDIO_TEST, fmemopen_initial_position) {
1300 // POSIX: "The ... current position in the buffer ... shall be initially
1301 // set to either the beginning of the buffer (for r and w modes) ..."
1302 char buf[] = "hello\0world";
1303 FILE* fp;
1304 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1305 EXPECT_EQ(0L, ftell(fp));
1306 EXPECT_EQ(0, fclose(fp));
1307 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1308 EXPECT_EQ(0L, ftell(fp));
1309 EXPECT_EQ(0, fclose(fp));
1310 buf[0] = 'h'; // (Undo the effects of the above.)
1311
1312 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1313 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1314 EXPECT_EQ(5L, ftell(fp));
1315 EXPECT_EQ(0, fclose(fp));
1316
1317 // POSIX: "If no null byte is found in append mode, the initial position
1318 // shall be set to one byte after the end of the buffer."
1319 memset(buf, 'x', sizeof(buf));
1320 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1321 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1322 EXPECT_EQ(0, fclose(fp));
1323}
1324
1325TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1326 // POSIX: "If buf is a null pointer, the initial position shall always be
1327 // set to the beginning of the buffer."
1328 FILE* fp = fmemopen(nullptr, 128, "a+");
1329 ASSERT_TRUE(fp != nullptr);
1330 EXPECT_EQ(0L, ftell(fp));
1331 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1332 EXPECT_EQ(0, fclose(fp));
1333}
1334
1335TEST(STDIO_TEST, fmemopen_zero_length) {
1336 // POSIX says it's up to the implementation whether or not you can have a
1337 // zero-length buffer (but "A future version of this standard may require
1338 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1339 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1340 FILE* fp;
1341 char buf[16];
1342 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1343 ASSERT_EQ(EOF, fgetc(fp));
1344 ASSERT_TRUE(feof(fp));
1345 ASSERT_EQ(0, fclose(fp));
1346 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1347 ASSERT_EQ(EOF, fgetc(fp));
1348 ASSERT_TRUE(feof(fp));
1349 ASSERT_EQ(0, fclose(fp));
1350
1351 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1352 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1353 ASSERT_EQ(EOF, fputc('x', fp));
1354 ASSERT_EQ(0, fclose(fp));
1355 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1356 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1357 ASSERT_EQ(EOF, fputc('x', fp));
1358 ASSERT_EQ(0, fclose(fp));
1359}
1360
1361TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1362 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1363 // BSD fails, glibc doesn't. We side with the more lenient.
1364 FILE* fp;
1365 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1366 ASSERT_EQ(0, fclose(fp));
1367 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1368 ASSERT_EQ(0, fclose(fp));
1369}
1370
1371TEST(STDIO_TEST, fmemopen_fileno) {
1372 // There's no fd backing an fmemopen FILE*.
1373 FILE* fp = fmemopen(nullptr, 16, "r");
1374 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001375 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001376 ASSERT_EQ(-1, fileno(fp));
1377 ASSERT_EQ(EBADF, errno);
1378 ASSERT_EQ(0, fclose(fp));
1379}
1380
1381TEST(STDIO_TEST, fmemopen_append_after_seek) {
1382 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1383 // there had been an intervening seek.
1384
1385 FILE* fp;
1386 char buf[] = "hello\0world";
1387 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1388 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1389 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1390 ASSERT_NE(EOF, fputc('!', fp));
1391 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1392 ASSERT_EQ(0, fclose(fp));
1393
1394 memcpy(buf, "hello\0world", sizeof(buf));
1395 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1396 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1397 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1398 ASSERT_NE(EOF, fputc('!', fp));
1399 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1400 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001401}
1402
Christopher Ferris13f26a72016-01-13 13:47:58 -08001403TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001404 char* p = nullptr;
1405 size_t size = 0;
1406 FILE* fp = open_memstream(&p, &size);
1407 ASSERT_NE(EOF, fputs("hello, world!", fp));
1408 fclose(fp);
1409
1410 ASSERT_STREQ("hello, world!", p);
1411 ASSERT_EQ(strlen("hello, world!"), size);
1412 free(p);
1413}
1414
Christopher Ferris13f26a72016-01-13 13:47:58 -08001415TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001416#if defined(__BIONIC__)
1417 char* p;
1418 size_t size;
1419
1420 // Invalid buffer.
1421 errno = 0;
1422 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1423 ASSERT_EQ(EINVAL, errno);
1424
1425 // Invalid size.
1426 errno = 0;
1427 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1428 ASSERT_EQ(EINVAL, errno);
1429#else
Elliott Hughes9677fab2016-01-25 15:50:59 -08001430 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001431#endif
1432}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001433
Christopher Ferris13f26a72016-01-13 13:47:58 -08001434TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001435 int fd = open("/proc/version", O_RDONLY);
1436 ASSERT_TRUE(fd != -1);
1437
1438 // This fd doesn't have O_CLOEXEC...
1439 int flags = fcntl(fd, F_GETFD);
1440 ASSERT_TRUE(flags != -1);
1441 ASSERT_EQ(0, flags & FD_CLOEXEC);
1442
1443 FILE* fp = fdopen(fd, "re");
1444 ASSERT_TRUE(fp != NULL);
1445
1446 // ...but the new one does.
1447 flags = fcntl(fileno(fp), F_GETFD);
1448 ASSERT_TRUE(flags != -1);
1449 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
1450
1451 fclose(fp);
1452 close(fd);
1453}
1454
Christopher Ferris13f26a72016-01-13 13:47:58 -08001455TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001456 FILE* fp = fopen("/proc/version", "r");
1457 ASSERT_TRUE(fp != NULL);
1458
1459 // This FILE* doesn't have O_CLOEXEC...
1460 int flags = fcntl(fileno(fp), F_GETFD);
1461 ASSERT_TRUE(flags != -1);
1462 ASSERT_EQ(0, flags & FD_CLOEXEC);
1463
1464 fp = freopen("/proc/version", "re", fp);
1465
1466 // ...but the new one does.
1467 flags = fcntl(fileno(fp), F_GETFD);
1468 ASSERT_TRUE(flags != -1);
1469 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
1470
1471 fclose(fp);
1472}
Elliott Hughes20841a12014-12-01 16:13:30 -08001473
Elliott Hughesf226ee52016-02-03 11:24:28 -08001474TEST(STDIO_TEST, fopen64_freopen64) {
1475 FILE* fp = fopen64("/proc/version", "r");
1476 ASSERT_TRUE(fp != nullptr);
1477 fp = freopen64("/proc/version", "re", fp);
1478 ASSERT_TRUE(fp != nullptr);
1479 fclose(fp);
1480}
1481
Elliott Hughes20841a12014-12-01 16:13:30 -08001482// https://code.google.com/p/android/issues/detail?id=81155
1483// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001484TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08001485 FILE* fp = fopen("/dev/zero", "r");
1486 ASSERT_TRUE(fp != NULL);
1487
1488 // Make this stream unbuffered.
1489 setvbuf(fp, 0, _IONBF, 0);
1490
1491 char buf[65*1024];
1492 memset(buf, 0xff, sizeof(buf));
1493
1494 time_t t0 = time(NULL);
1495 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001496 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08001497 }
1498 time_t t1 = time(NULL);
1499
1500 fclose(fp);
1501
1502 // 1024 64KiB reads should have been very quick.
1503 ASSERT_LE(t1 - t0, 1);
1504
1505 for (size_t i = 0; i < 64*1024; ++i) {
1506 ASSERT_EQ('\0', buf[i]);
1507 }
1508 for (size_t i = 64*1024; i < 65*1024; ++i) {
1509 ASSERT_EQ('\xff', buf[i]);
1510 }
1511}
Elliott Hughes75b99382015-01-20 11:23:50 -08001512
Christopher Ferris13f26a72016-01-13 13:47:58 -08001513TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001514 std::string digits("0123456789");
1515 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08001516
1517 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
1518 char buf1[4 * 4];
1519 memset(buf1, 0, sizeof(buf1));
1520 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001521 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08001522 ASSERT_TRUE(feof(fp));
1523
1524 rewind(fp);
1525
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001526 // Try to read way too much so stdio tries to read more direct from the stream.
1527 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08001528 memset(buf2, 0, sizeof(buf2));
1529 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001530 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08001531 ASSERT_TRUE(feof(fp));
1532
1533 fclose(fp);
1534}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001535
1536static void test_fread_from_write_only_stream(size_t n) {
1537 FILE* fp = fopen("/dev/null", "w");
1538 std::vector<char> buf(n, 0);
1539 errno = 0;
1540 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
1541 ASSERT_EQ(EBADF, errno);
1542 ASSERT_TRUE(ferror(fp));
1543 ASSERT_FALSE(feof(fp));
1544 fclose(fp);
1545}
1546
Christopher Ferris13f26a72016-01-13 13:47:58 -08001547TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001548 test_fread_from_write_only_stream(1);
1549}
1550
Christopher Ferris13f26a72016-01-13 13:47:58 -08001551TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001552 test_fread_from_write_only_stream(64*1024);
1553}
1554
1555static void test_fwrite_after_fread(size_t n) {
1556 TemporaryFile tf;
1557
1558 FILE* fp = fdopen(tf.fd, "w+");
1559 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
1560 fflush(fp);
1561
1562 // We've flushed but not rewound, so there's nothing to read.
1563 std::vector<char> buf(n, 0);
1564 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
1565 ASSERT_TRUE(feof(fp));
1566
1567 // But hitting EOF doesn't prevent us from writing...
1568 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08001569 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001570
1571 // And if we rewind, everything's there.
1572 rewind(fp);
1573 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
1574 ASSERT_EQ('1', buf[0]);
1575 ASSERT_EQ('2', buf[1]);
1576
1577 fclose(fp);
1578}
1579
Christopher Ferris13f26a72016-01-13 13:47:58 -08001580TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001581 test_fwrite_after_fread(16);
1582}
1583
Christopher Ferris13f26a72016-01-13 13:47:58 -08001584TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001585 test_fwrite_after_fread(64*1024);
1586}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001587
1588// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08001589TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001590 TemporaryFile tf;
1591
1592 FILE* fp = fopen(tf.filename, "w+");
1593 ASSERT_TRUE(fp != nullptr);
1594
1595 char file_data[12288];
1596 for (size_t i = 0; i < 12288; i++) {
1597 file_data[i] = i;
1598 }
1599 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
1600 fclose(fp);
1601
1602 fp = fopen(tf.filename, "r");
1603 ASSERT_TRUE(fp != nullptr);
1604
1605 char buffer[8192];
1606 size_t cur_location = 0;
1607 // Small read to populate internal buffer.
1608 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
1609 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
1610
1611 cur_location = static_cast<size_t>(ftell(fp));
1612 // Large read to force reading into the user supplied buffer and bypassing
1613 // the internal buffer.
1614 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
1615 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
1616
1617 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08001618 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001619 cur_location = static_cast<size_t>(ftell(fp));
1620 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
1621 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
1622
1623 fclose(fp);
1624}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07001625
1626// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08001627TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07001628 TemporaryFile tf;
1629 char buf[6] = {0};
1630
1631 FILE* fw = fopen(tf.filename, "w");
1632 ASSERT_TRUE(fw != nullptr);
1633
1634 FILE* fr = fopen(tf.filename, "r");
1635 ASSERT_TRUE(fr != nullptr);
1636
1637 fwrite("a", 1, 1, fw);
1638 fflush(fw);
1639 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1640 ASSERT_STREQ("a", buf);
1641
1642 // 'fr' is now at EOF.
1643 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1644 ASSERT_TRUE(feof(fr));
1645
1646 // Write some more...
1647 fwrite("z", 1, 1, fw);
1648 fflush(fw);
1649
1650 // ...and check that we can read it back.
1651 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
1652 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1653 ASSERT_STREQ("z", buf);
1654
1655 // But now we're done.
1656 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1657
1658 fclose(fr);
1659 fclose(fw);
1660}
Elliott Hughes923f1652016-01-19 15:46:05 -08001661
1662TEST(STDIO_TEST, fclose_invalidates_fd) {
1663 // The typical error we're trying to help people catch involves accessing
1664 // memory after it's been freed. But we know that stdin/stdout/stderr are
1665 // special and don't get deallocated, so this test uses stdin.
1666 ASSERT_EQ(0, fclose(stdin));
1667
1668 // Even though using a FILE* after close is undefined behavior, I've closed
1669 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
1670 // especially because they might actually correspond to a real stream.
1671 errno = 0;
1672 ASSERT_EQ(-1, fileno(stdin));
1673 ASSERT_EQ(EBADF, errno);
1674}
Elliott Hughes2704bd12016-01-20 17:14:53 -08001675
1676TEST(STDIO_TEST, fseek_ftell_unseekable) {
1677#if defined(__BIONIC__) // glibc has fopencookie instead.
1678 auto read_fn = [](void*, char*, int) { return -1; };
1679 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
1680 ASSERT_TRUE(fp != nullptr);
1681
1682 // Check that ftell balks on an unseekable FILE*.
1683 errno = 0;
1684 ASSERT_EQ(-1, ftell(fp));
1685 ASSERT_EQ(ESPIPE, errno);
1686
1687 // SEEK_CUR is rewritten as SEEK_SET internally...
1688 errno = 0;
1689 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
1690 ASSERT_EQ(ESPIPE, errno);
1691
1692 // ...so it's worth testing the direct seek path too.
1693 errno = 0;
1694 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
1695 ASSERT_EQ(ESPIPE, errno);
1696
1697 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001698#else
1699 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1700#endif
1701}
1702
1703TEST(STDIO_TEST, funopen_EINVAL) {
1704#if defined(__BIONIC__)
1705 errno = 0;
1706 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
1707 ASSERT_EQ(EINVAL, errno);
1708#else
1709 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1710#endif
1711}
1712
1713TEST(STDIO_TEST, funopen_seek) {
1714#if defined(__BIONIC__)
1715 auto read_fn = [](void*, char*, int) { return -1; };
1716
1717 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
1718 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
1719
1720 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
1721 ASSERT_TRUE(fp != nullptr);
1722 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08001723#if defined(__LP64__)
1724 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
1725 EXPECT_EQ(0xfedcba12LL, pos);
1726#else
1727 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
1728 EXPECT_EQ(EOVERFLOW, errno);
1729#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001730
1731 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
1732 ASSERT_TRUE(fp64 != nullptr);
1733 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08001734 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
1735 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001736#else
1737 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
Elliott Hughes2704bd12016-01-20 17:14:53 -08001738#endif
1739}
Elliott Hughes71288cb2016-01-22 19:22:44 -08001740
1741TEST(STDIO_TEST, lots_of_concurrent_files) {
1742 std::vector<TemporaryFile*> tfs;
1743 std::vector<FILE*> fps;
1744
1745 for (size_t i = 0; i < 256; ++i) {
1746 TemporaryFile* tf = new TemporaryFile;
1747 tfs.push_back(tf);
1748 FILE* fp = fopen(tf->filename, "w+");
1749 fps.push_back(fp);
1750 fprintf(fp, "hello %zu!\n", i);
1751 fflush(fp);
1752 }
1753
1754 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08001755 char expected[BUFSIZ];
1756 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08001757
Elliott Hughes70715da2016-08-01 16:35:17 -07001758 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08001759 fclose(fps[i]);
1760 delete tfs[i];
1761 }
1762}
Elliott Hughes9677fab2016-01-25 15:50:59 -08001763
1764static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
1765 EXPECT_EQ(offset, ftell(fp));
1766 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08001767 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08001768 fpos_t pos;
1769 fpos64_t pos64;
1770 EXPECT_EQ(0, fgetpos(fp, &pos));
1771 EXPECT_EQ(0, fgetpos64(fp, &pos64));
1772#if defined(__BIONIC__)
1773 EXPECT_EQ(offset, static_cast<off64_t>(pos));
1774 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
1775#else
1776 GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n";
1777#endif
1778}
1779
1780TEST(STDIO_TEST, seek_tell_family_smoke) {
1781 TemporaryFile tf;
1782 FILE* fp = fdopen(tf.fd, "w+");
1783
1784 // Initially we should be at 0.
1785 AssertFileOffsetAt(fp, 0);
1786
1787 // Seek to offset 8192.
1788 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
1789 AssertFileOffsetAt(fp, 8192);
1790 fpos_t eight_k_pos;
1791 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
1792
1793 // Seek forward another 8192...
1794 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
1795 AssertFileOffsetAt(fp, 8192 + 8192);
1796 fpos64_t sixteen_k_pos64;
1797 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
1798
1799 // Seek back 8192...
1800 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
1801 AssertFileOffsetAt(fp, 8192);
1802
1803 // Since we haven't written anything, the end is also at 0.
1804 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1805 AssertFileOffsetAt(fp, 0);
1806
1807 // Check that our fpos64_t from 16KiB works...
1808 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
1809 AssertFileOffsetAt(fp, 8192 + 8192);
1810 // ...as does our fpos_t from 8192.
1811 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
1812 AssertFileOffsetAt(fp, 8192);
1813
1814 // Do fseeko and fseeko64 work too?
1815 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
1816 AssertFileOffsetAt(fp, 1234);
1817 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
1818 AssertFileOffsetAt(fp, 5678);
1819
1820 fclose(fp);
1821}
1822
1823TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
1824 TemporaryFile tf;
1825 FILE* fp = fdopen(tf.fd, "w+");
1826
1827 // Bad whence.
1828 errno = 0;
1829 ASSERT_EQ(-1, fseek(fp, 0, 123));
1830 ASSERT_EQ(EINVAL, errno);
1831 errno = 0;
1832 ASSERT_EQ(-1, fseeko(fp, 0, 123));
1833 ASSERT_EQ(EINVAL, errno);
1834 errno = 0;
1835 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
1836 ASSERT_EQ(EINVAL, errno);
1837
1838 // Bad offset.
1839 errno = 0;
1840 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
1841 ASSERT_EQ(EINVAL, errno);
1842 errno = 0;
1843 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
1844 ASSERT_EQ(EINVAL, errno);
1845 errno = 0;
1846 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
1847 ASSERT_EQ(EINVAL, errno);
1848
1849 fclose(fp);
1850}
Elliott Hughes20788ae2016-06-09 15:16:32 -07001851
1852TEST(STDIO_TEST, ctermid) {
1853 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
1854
1855 char buf[L_ctermid] = {};
1856 ASSERT_EQ(buf, ctermid(buf));
1857 ASSERT_STREQ("/dev/tty", buf);
1858}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07001859
1860TEST(STDIO_TEST, remove) {
1861 struct stat sb;
1862
1863 TemporaryFile tf;
1864 ASSERT_EQ(0, remove(tf.filename));
1865 ASSERT_EQ(-1, lstat(tf.filename, &sb));
1866 ASSERT_EQ(ENOENT, errno);
1867
1868 TemporaryDir td;
1869 ASSERT_EQ(0, remove(td.dirname));
1870 ASSERT_EQ(-1, lstat(td.dirname, &sb));
1871 ASSERT_EQ(ENOENT, errno);
1872
1873 errno = 0;
1874 ASSERT_EQ(-1, remove(tf.filename));
1875 ASSERT_EQ(ENOENT, errno);
1876
1877 errno = 0;
1878 ASSERT_EQ(-1, remove(td.dirname));
1879 ASSERT_EQ(ENOENT, errno);
1880}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07001881
1882TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
1883 char buf[16];
1884 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
1885 testing::KilledBySignal(SIGABRT),
1886#if defined(NOFORTIFY)
1887 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
1888#else
1889 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
1890#endif
1891 );
1892}
1893
1894TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
1895 std::string buf = "world";
1896 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
1897 testing::KilledBySignal(SIGABRT),
1898 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
1899}
1900
1901TEST(STDIO_TEST, sprintf_30445072) {
1902 std::string buf = "world";
1903 sprintf(&buf[0], "hello");
1904 ASSERT_EQ(buf, "hello");
1905}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07001906
1907TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
1908 TemporaryFile tf;
1909 SetFileTo(tf.filename, "0123456789");
1910 FILE* fp = fopen(tf.filename, "a");
1911 EXPECT_EQ(10, ftell(fp));
1912 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1913 EXPECT_EQ(2, ftell(fp));
1914 ASSERT_NE(EOF, fputs("xxx", fp));
1915 ASSERT_EQ(0, fflush(fp));
1916 EXPECT_EQ(13, ftell(fp));
1917 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1918 EXPECT_EQ(13, ftell(fp));
1919 ASSERT_EQ(0, fclose(fp));
1920 AssertFileIs(tf.filename, "0123456789xxx");
1921}
1922
1923TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
1924 TemporaryFile tf;
1925 SetFileTo(tf.filename, "0123456789");
1926 int fd = open(tf.filename, O_RDWR);
1927 ASSERT_NE(-1, fd);
1928 // POSIX: "The file position indicator associated with the new stream is set to the position
1929 // indicated by the file offset associated with the file descriptor."
1930 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
1931 FILE* fp = fdopen(fd, "a");
1932 EXPECT_EQ(4, ftell(fp));
1933 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1934 EXPECT_EQ(2, ftell(fp));
1935 ASSERT_NE(EOF, fputs("xxx", fp));
1936 ASSERT_EQ(0, fflush(fp));
1937 EXPECT_EQ(13, ftell(fp));
1938 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1939 EXPECT_EQ(13, ftell(fp));
1940 ASSERT_EQ(0, fclose(fp));
1941 AssertFileIs(tf.filename, "0123456789xxx");
1942}
1943
1944TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
1945 TemporaryFile tf;
1946 SetFileTo(tf.filename, "0123456789");
1947 FILE* other_fp = fopen("/proc/version", "r");
1948 FILE* fp = freopen(tf.filename, "a", other_fp);
1949 EXPECT_EQ(10, ftell(fp));
1950 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1951 EXPECT_EQ(2, ftell(fp));
1952 ASSERT_NE(EOF, fputs("xxx", fp));
1953 ASSERT_EQ(0, fflush(fp));
1954 EXPECT_EQ(13, ftell(fp));
1955 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1956 EXPECT_EQ(13, ftell(fp));
1957 ASSERT_EQ(0, fclose(fp));
1958 AssertFileIs(tf.filename, "0123456789xxx");
1959}
Elliott Hughesb15feb72017-07-31 17:20:18 -07001960
1961TEST(STDIO_TEST, constants) {
1962 ASSERT_LE(FILENAME_MAX, PATH_MAX);
1963 ASSERT_EQ(L_tmpnam, PATH_MAX);
1964}