blob: 655ad3f87fda3b931ad36f5f524d7c5f50b2cccd [file] [log] [blame]
Elliott Hughes91875dc2012-09-24 17:55:15 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
Elliott Hughes1d13c642013-09-23 16:02:39 -070020#include <limits.h>
Elliott Hughes7823f322014-04-14 12:11:28 -070021#include <math.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070022#include <stdio.h>
23#include <sys/types.h>
24#include <sys/stat.h>
25#include <unistd.h>
Elliott Hughes05493712014-04-17 17:30:03 -070026#include <wchar.h>
Calin Juravle03e4ebe2014-05-08 14:42:06 +010027#include <locale.h>
28
29#include "TemporaryFile.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070030
31TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) {
32 FILE* fp = tmpfile();
33 ASSERT_TRUE(fp != NULL);
34
35 int fd = fileno(fp);
36 ASSERT_NE(fd, -1);
37
38 struct stat sb;
39 int rc = fstat(fd, &sb);
40 ASSERT_NE(rc, -1);
41 ASSERT_EQ(sb.st_mode & 0777, 0600U);
42
43 rc = fprintf(fp, "hello\n");
44 ASSERT_EQ(rc, 6);
45
46 rewind(fp);
47
48 char buf[16];
49 char* s = fgets(buf, sizeof(buf), fp);
50 ASSERT_TRUE(s != NULL);
51 ASSERT_STREQ("hello\n", s);
52
53 fclose(fp);
54}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030055
56TEST(stdio, getdelim) {
57 FILE* fp = tmpfile();
58 ASSERT_TRUE(fp != NULL);
59
60 const char* line_written = "This is a test";
61 int rc = fprintf(fp, "%s", line_written);
62 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
63
64 rewind(fp);
65
66 char* word_read = NULL;
67 size_t allocated_length = 0;
68
69 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
70 for (size_t i = 0; i < 5; ++i) {
71 ASSERT_FALSE(feof(fp));
72 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
73 ASSERT_GE(allocated_length, strlen(expected[i]));
74 ASSERT_STREQ(word_read, expected[i]);
75 }
76 // The last read should have set the end-of-file indicator for the stream.
77 ASSERT_TRUE(feof(fp));
78 clearerr(fp);
79
80 // getdelim returns -1 but doesn't set errno if we're already at EOF.
81 // It should set the end-of-file indicator for the stream, though.
82 errno = 0;
83 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -080084 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030085 ASSERT_TRUE(feof(fp));
86
87 free(word_read);
88 fclose(fp);
89}
90
91TEST(stdio, getdelim_invalid) {
92 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -080093 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +030094
95 char* buffer = NULL;
96 size_t buffer_length = 0;
97
98 // The first argument can't be NULL.
99 errno = 0;
100 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800101 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300102
103 // The second argument can't be NULL.
104 errno = 0;
105 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800106 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300107
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700108 // The underlying fd can't be closed.
109 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300110 errno = 0;
111 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800112 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700113 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300114}
115
116TEST(stdio, getline) {
117 FILE* fp = tmpfile();
118 ASSERT_TRUE(fp != NULL);
119
120 const char* line_written = "This is a test for getline\n";
121 const size_t line_count = 5;
122
123 for (size_t i = 0; i < line_count; ++i) {
124 int rc = fprintf(fp, "%s", line_written);
125 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
126 }
127
128 rewind(fp);
129
130 char* line_read = NULL;
131 size_t allocated_length = 0;
132
133 size_t read_line_count = 0;
134 ssize_t read_char_count;
135 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
136 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
137 ASSERT_GE(allocated_length, strlen(line_written));
138 ASSERT_STREQ(line_read, line_written);
139 ++read_line_count;
140 }
141 ASSERT_EQ(read_line_count, line_count);
142
143 // The last read should have set the end-of-file indicator for the stream.
144 ASSERT_TRUE(feof(fp));
145 clearerr(fp);
146
147 // getline returns -1 but doesn't set errno if we're already at EOF.
148 // It should set the end-of-file indicator for the stream, though.
149 errno = 0;
150 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800151 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300152 ASSERT_TRUE(feof(fp));
153
154 free(line_read);
155 fclose(fp);
156}
157
158TEST(stdio, getline_invalid) {
159 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800160 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300161
162 char* buffer = NULL;
163 size_t buffer_length = 0;
164
165 // The first argument can't be NULL.
166 errno = 0;
167 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800168 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300169
170 // The second argument can't be NULL.
171 errno = 0;
172 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800173 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300174
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700175 // The underlying fd can't be closed.
176 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300177 errno = 0;
178 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800179 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700180 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300181}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000182
183TEST(stdio, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800184 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800185 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800186 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
187 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000188 // error: format '%zd' expects argument of type 'signed size_t',
189 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
190 ssize_t v = 1;
191 char buf[32];
192 snprintf(buf, sizeof(buf), "%zd", v);
193}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800194
Elliott Hughes05493712014-04-17 17:30:03 -0700195// https://code.google.com/p/android/issues/detail?id=64886
196TEST(stdio, snprintf_a) {
197 char buf[BUFSIZ];
198 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
199 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
200}
201
202TEST(stdio, snprintf_lc) {
203 char buf[BUFSIZ];
204 wint_t wc = L'a';
205 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
206 EXPECT_STREQ("<a>", buf);
207}
208
209TEST(stdio, snprintf_ls) {
210 char buf[BUFSIZ];
211 wchar_t* ws = NULL;
212 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
213 EXPECT_STREQ("<(null)>", buf);
214
215 wchar_t chars[] = { L'h', L'i', 0 };
216 ws = chars;
217 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
218 EXPECT_STREQ("<hi>", buf);
219}
220
221TEST(stdio, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700222#if defined(__BIONIC__)
Elliott Hughese2341d02014-05-02 18:16:32 -0700223 // http://b/14492135
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700224 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700225 int i = 1234;
226 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
227 EXPECT_EQ(1234, i);
228 EXPECT_STREQ("a n b", buf);
229#else
230 GTEST_LOG_(INFO) << "This test does nothing.\n";
231#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700232}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700233
Elliott Hughes1d13c642013-09-23 16:02:39 -0700234TEST(stdio, snprintf_smoke) {
235 char buf[BUFSIZ];
236
237 snprintf(buf, sizeof(buf), "a");
238 EXPECT_STREQ("a", buf);
239
240 snprintf(buf, sizeof(buf), "%%");
241 EXPECT_STREQ("%", buf);
242
243 snprintf(buf, sizeof(buf), "01234");
244 EXPECT_STREQ("01234", buf);
245
246 snprintf(buf, sizeof(buf), "a%sb", "01234");
247 EXPECT_STREQ("a01234b", buf);
248
249 char* s = NULL;
250 snprintf(buf, sizeof(buf), "a%sb", s);
251 EXPECT_STREQ("a(null)b", buf);
252
253 snprintf(buf, sizeof(buf), "aa%scc", "bb");
254 EXPECT_STREQ("aabbcc", buf);
255
256 snprintf(buf, sizeof(buf), "a%cc", 'b');
257 EXPECT_STREQ("abc", buf);
258
259 snprintf(buf, sizeof(buf), "a%db", 1234);
260 EXPECT_STREQ("a1234b", buf);
261
262 snprintf(buf, sizeof(buf), "a%db", -8123);
263 EXPECT_STREQ("a-8123b", buf);
264
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700265 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700266 EXPECT_STREQ("a16b", buf);
267
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700268 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700269 EXPECT_STREQ("a16b", buf);
270
271 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
272 EXPECT_STREQ("a68719476736b", buf);
273
274 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
275 EXPECT_STREQ("a70000b", buf);
276
277 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
278 EXPECT_STREQ("a0xb0001234b", buf);
279
280 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
281 EXPECT_STREQ("a12abz", buf);
282
283 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
284 EXPECT_STREQ("a12ABz", buf);
285
286 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
287 EXPECT_STREQ("a00123456z", buf);
288
289 snprintf(buf, sizeof(buf), "a%5dz", 1234);
290 EXPECT_STREQ("a 1234z", buf);
291
292 snprintf(buf, sizeof(buf), "a%05dz", 1234);
293 EXPECT_STREQ("a01234z", buf);
294
295 snprintf(buf, sizeof(buf), "a%8dz", 1234);
296 EXPECT_STREQ("a 1234z", buf);
297
298 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
299 EXPECT_STREQ("a1234 z", buf);
300
301 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
302 EXPECT_STREQ("Aabcdef Z", buf);
303
304 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
305 EXPECT_STREQ("Ahello:1234Z", buf);
306
307 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
308 EXPECT_STREQ("a005:5:05z", buf);
309
310 void* p = NULL;
311 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700312#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700313 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800314#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700315 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800316#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700317
318 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
319 EXPECT_STREQ("a68719476736,6,7,8z", buf);
320
321 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
322 EXPECT_STREQ("a_1.230000_b", buf);
323
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700324 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700325 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400326
327 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
328 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700329}
330
Elliott Hughes7823f322014-04-14 12:11:28 -0700331TEST(stdio, snprintf_f_special) {
332 char buf[BUFSIZ];
333 snprintf(buf, sizeof(buf), "%f", nanf(""));
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700334 EXPECT_STRCASEEQ("NaN", buf);
Elliott Hughes7823f322014-04-14 12:11:28 -0700335
336 snprintf(buf, sizeof(buf), "%f", HUGE_VALF);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700337 EXPECT_STRCASEEQ("Inf", buf);
Elliott Hughes7823f322014-04-14 12:11:28 -0700338}
339
340TEST(stdio, snprintf_g_special) {
341 char buf[BUFSIZ];
342 snprintf(buf, sizeof(buf), "%g", nan(""));
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700343 EXPECT_STRCASEEQ("NaN", buf);
Elliott Hughes7823f322014-04-14 12:11:28 -0700344
345 snprintf(buf, sizeof(buf), "%g", HUGE_VAL);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700346 EXPECT_STRCASEEQ("Inf", buf);
Elliott Hughes7823f322014-04-14 12:11:28 -0700347}
348
Elliott Hughes1d13c642013-09-23 16:02:39 -0700349TEST(stdio, snprintf_d_INT_MAX) {
350 char buf[BUFSIZ];
351 snprintf(buf, sizeof(buf), "%d", INT_MAX);
352 EXPECT_STREQ("2147483647", buf);
353}
354
355TEST(stdio, snprintf_d_INT_MIN) {
356 char buf[BUFSIZ];
357 snprintf(buf, sizeof(buf), "%d", INT_MIN);
358 EXPECT_STREQ("-2147483648", buf);
359}
360
361TEST(stdio, snprintf_ld_LONG_MAX) {
362 char buf[BUFSIZ];
363 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Elliott Hughes925753a2013-10-18 13:17:18 -0700364#if __LP64__
365 EXPECT_STREQ("9223372036854775807", buf);
366#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700367 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700368#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700369}
370
371TEST(stdio, snprintf_ld_LONG_MIN) {
372 char buf[BUFSIZ];
373 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Elliott Hughes925753a2013-10-18 13:17:18 -0700374#if __LP64__
375 EXPECT_STREQ("-9223372036854775808", buf);
376#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700377 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700378#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700379}
380
381TEST(stdio, snprintf_lld_LLONG_MAX) {
382 char buf[BUFSIZ];
383 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
384 EXPECT_STREQ("9223372036854775807", buf);
385}
386
387TEST(stdio, snprintf_lld_LLONG_MIN) {
388 char buf[BUFSIZ];
389 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
390 EXPECT_STREQ("-9223372036854775808", buf);
391}
392
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700393TEST(stdio, snprintf_e) {
394 char buf[BUFSIZ];
395
396 snprintf(buf, sizeof(buf), "%e", 1.5);
397 EXPECT_STREQ("1.500000e+00", buf);
398
399 snprintf(buf, sizeof(buf), "%Le", 1.5l);
400 EXPECT_STREQ("1.500000e+00", buf);
401}
402
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800403TEST(stdio, popen) {
404 FILE* fp = popen("cat /proc/version", "r");
405 ASSERT_TRUE(fp != NULL);
406
407 char buf[16];
408 char* s = fgets(buf, sizeof(buf), fp);
409 buf[13] = '\0';
410 ASSERT_STREQ("Linux version", s);
411
412 ASSERT_EQ(0, pclose(fp));
413}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700414
415TEST(stdio, getc) {
416 FILE* fp = fopen("/proc/version", "r");
417 ASSERT_TRUE(fp != NULL);
418 ASSERT_EQ('L', getc(fp));
419 ASSERT_EQ('i', getc(fp));
420 ASSERT_EQ('n', getc(fp));
421 ASSERT_EQ('u', getc(fp));
422 ASSERT_EQ('x', getc(fp));
423 fclose(fp);
424}
425
426TEST(stdio, putc) {
427 FILE* fp = fopen("/proc/version", "r");
428 ASSERT_TRUE(fp != NULL);
429 ASSERT_EQ(EOF, putc('x', fp));
430 fclose(fp);
431}
Elliott Hughes603332f2014-03-12 17:10:41 -0700432
433TEST(stdio, sscanf) {
434 char s1[123];
435 int i1;
436 double d1;
437 char s2[123];
438 ASSERT_EQ(3, sscanf(" hello 123 1.23 ", "%s %i %lf %s", s1, &i1, &d1, s2));
439 ASSERT_STREQ("hello", s1);
440 ASSERT_EQ(123, i1);
Christopher Ferrisf171b342014-03-17 16:40:26 -0700441 ASSERT_DOUBLE_EQ(1.23, d1);
Elliott Hughes603332f2014-03-12 17:10:41 -0700442}
Elliott Hughes53b24382014-05-02 18:29:25 -0700443
444TEST(stdio, cantwrite_EBADF) {
445 // If we open a file read-only...
446 FILE* fp = fopen("/proc/version", "r");
447
448 // ...all attempts to write to that file should return failure.
449
450 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
451 // glibc gets the wide-character functions wrong.
452
453 errno = 0;
454 EXPECT_EQ(EOF, putc('x', fp));
455 EXPECT_EQ(EBADF, errno);
456
457 errno = 0;
458 EXPECT_EQ(EOF, fprintf(fp, "hello"));
459 EXPECT_EQ(EBADF, errno);
460
461 errno = 0;
462 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -0700463#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -0700464 EXPECT_EQ(EBADF, errno);
465#endif
466
467 errno = 0;
468 EXPECT_EQ(EOF, putw(1234, fp));
469 EXPECT_EQ(EBADF, errno);
470
471 errno = 0;
472 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
473 EXPECT_EQ(EBADF, errno);
474
475 errno = 0;
476 EXPECT_EQ(EOF, fputs("hello", fp));
477 EXPECT_EQ(EBADF, errno);
478
479 errno = 0;
480 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -0700481#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -0700482 EXPECT_EQ(EBADF, errno);
483#endif
484}
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100485
486// Tests that we can only have a consistent and correct fpos_t when using
487// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
488TEST(stdio, consistent_fpos_t) {
489 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
490 uselocale(LC_GLOBAL_LOCALE);
491
492 FILE* fp = tmpfile();
493 ASSERT_TRUE(fp != NULL);
494
495 wchar_t mb_one_bytes = L'h';
496 wchar_t mb_two_bytes = 0x00a2;
497 wchar_t mb_three_bytes = 0x20ac;
498 wchar_t mb_four_bytes = 0x24b62;
499
500 // Write to file.
501 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
502 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
503 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
504 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
505
506 rewind(fp);
507
508 // Record each character position.
509 fpos_t pos1;
510 fpos_t pos2;
511 fpos_t pos3;
512 fpos_t pos4;
513 fpos_t pos5;
514 EXPECT_EQ(0, fgetpos(fp, &pos1));
515 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
516 EXPECT_EQ(0, fgetpos(fp, &pos2));
517 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
518 EXPECT_EQ(0, fgetpos(fp, &pos3));
519 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
520 EXPECT_EQ(0, fgetpos(fp, &pos4));
521 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
522 EXPECT_EQ(0, fgetpos(fp, &pos5));
523
Elliott Hughes063525c2014-05-13 11:19:57 -0700524#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100525 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
526 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
527 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
528 // structure.
529 ASSERT_EQ(0, static_cast<off_t>(pos1));
530 ASSERT_EQ(1, static_cast<off_t>(pos2));
531 ASSERT_EQ(3, static_cast<off_t>(pos3));
532 ASSERT_EQ(6, static_cast<off_t>(pos4));
533 ASSERT_EQ(10, static_cast<off_t>(pos5));
534#endif
535
536 // Exercise back and forth movements of the position.
537 ASSERT_EQ(0, fsetpos(fp, &pos2));
538 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
539 ASSERT_EQ(0, fsetpos(fp, &pos1));
540 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
541 ASSERT_EQ(0, fsetpos(fp, &pos4));
542 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
543 ASSERT_EQ(0, fsetpos(fp, &pos3));
544 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
545 ASSERT_EQ(0, fsetpos(fp, &pos5));
546 ASSERT_EQ(WEOF, fgetwc(fp));
547
548 fclose(fp);
549}
550
551// Exercise the interaction between fpos and seek.
552TEST(stdio, fpos_t_and_seek) {
553 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
554 uselocale(LC_GLOBAL_LOCALE);
555
556 // For glibc we need to close and re-open the file in order for fseek to work
557 // after using setlocale(LC_CTYPE, "C.UTF-8") and fputwc.
558 // TODO: find out if this is expected or a bug in glibc.
559 TemporaryFile tf;
560 FILE* fp = fdopen(tf.fd, "w+");
561 ASSERT_TRUE(fp != NULL);
562
563 wchar_t mb_two_bytes = 0x00a2;
564 wchar_t mb_three_bytes = 0x20ac;
565 wchar_t mb_four_bytes = 0x24b62;
566
567 // Write to file.
568 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
569 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
570 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
571
572 fflush(fp);
573 fclose(fp);
574
575 fp = fopen(tf.filename, "r");
576 ASSERT_TRUE(fp != NULL);
577
578 // Store a valid position.
579 fpos_t mb_two_bytes_pos;
580 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
581
582 // Move inside mb_four_bytes with fseek.
583 long offset_inside_mb = 6;
584 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
585
586 // Store the "inside multi byte" position.
587 fpos_t pos_inside_mb;
588 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -0700589#if defined(__BIONIC__)
590 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
591#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +0100592
593 // Reading from within a byte should produce an error.
594 ASSERT_EQ(WEOF, fgetwc(fp));
595 ASSERT_EQ(EILSEQ, errno);
596
597 // Reverting to a valid position should work.
598 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
599 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
600
601 // Moving withing a multi byte with fsetpos should work but reading should
602 // produce an error.
603 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
604 ASSERT_EQ(WEOF, fgetwc(fp));
605 ASSERT_EQ(EILSEQ, errno);
606
607 fclose(fp);
608}