1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
|
/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <gtest/gtest.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <wchar.h>
#include <locale.h>
#include "TemporaryFile.h"
TEST(stdio, tmpfile_fileno_fprintf_rewind_fgets) {
FILE* fp = tmpfile();
ASSERT_TRUE(fp != NULL);
int fd = fileno(fp);
ASSERT_NE(fd, -1);
struct stat sb;
int rc = fstat(fd, &sb);
ASSERT_NE(rc, -1);
ASSERT_EQ(sb.st_mode & 0777, 0600U);
rc = fprintf(fp, "hello\n");
ASSERT_EQ(rc, 6);
rewind(fp);
char buf[16];
char* s = fgets(buf, sizeof(buf), fp);
ASSERT_TRUE(s != NULL);
ASSERT_STREQ("hello\n", s);
fclose(fp);
}
TEST(stdio, dprintf) {
TemporaryFile tf;
int rc = dprintf(tf.fd, "hello\n");
ASSERT_EQ(rc, 6);
lseek(tf.fd, SEEK_SET, 0);
FILE* tfile = fdopen(tf.fd, "r");
ASSERT_TRUE(tfile != NULL);
char buf[7];
ASSERT_EQ(buf, fgets(buf, sizeof(buf), tfile));
ASSERT_STREQ("hello\n", buf);
// Make sure there isn't anything else in the file.
ASSERT_EQ(NULL, fgets(buf, sizeof(buf), tfile));
fclose(tfile);
}
TEST(stdio, getdelim) {
FILE* fp = tmpfile();
ASSERT_TRUE(fp != NULL);
const char* line_written = "This is a test";
int rc = fprintf(fp, "%s", line_written);
ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
rewind(fp);
char* word_read = NULL;
size_t allocated_length = 0;
const char* expected[] = { "This ", " ", "is ", "a ", "test" };
for (size_t i = 0; i < 5; ++i) {
ASSERT_FALSE(feof(fp));
ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
ASSERT_GE(allocated_length, strlen(expected[i]));
ASSERT_STREQ(word_read, expected[i]);
}
// The last read should have set the end-of-file indicator for the stream.
ASSERT_TRUE(feof(fp));
clearerr(fp);
// getdelim returns -1 but doesn't set errno if we're already at EOF.
// It should set the end-of-file indicator for the stream, though.
errno = 0;
ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
ASSERT_EQ(0, errno);
ASSERT_TRUE(feof(fp));
free(word_read);
fclose(fp);
}
TEST(stdio, getdelim_invalid) {
FILE* fp = tmpfile();
ASSERT_TRUE(fp != NULL);
char* buffer = NULL;
size_t buffer_length = 0;
// The first argument can't be NULL.
errno = 0;
ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
ASSERT_EQ(EINVAL, errno);
// The second argument can't be NULL.
errno = 0;
ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
ASSERT_EQ(EINVAL, errno);
// The underlying fd can't be closed.
ASSERT_EQ(0, close(fileno(fp)));
errno = 0;
ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
ASSERT_EQ(EBADF, errno);
fclose(fp);
}
TEST(stdio, getline) {
FILE* fp = tmpfile();
ASSERT_TRUE(fp != NULL);
const char* line_written = "This is a test for getline\n";
const size_t line_count = 5;
for (size_t i = 0; i < line_count; ++i) {
int rc = fprintf(fp, "%s", line_written);
ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
}
rewind(fp);
char* line_read = NULL;
size_t allocated_length = 0;
size_t read_line_count = 0;
ssize_t read_char_count;
while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
ASSERT_GE(allocated_length, strlen(line_written));
ASSERT_STREQ(line_read, line_written);
++read_line_count;
}
ASSERT_EQ(read_line_count, line_count);
// The last read should have set the end-of-file indicator for the stream.
ASSERT_TRUE(feof(fp));
clearerr(fp);
// getline returns -1 but doesn't set errno if we're already at EOF.
// It should set the end-of-file indicator for the stream, though.
errno = 0;
ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
ASSERT_EQ(0, errno);
ASSERT_TRUE(feof(fp));
free(line_read);
fclose(fp);
}
TEST(stdio, getline_invalid) {
FILE* fp = tmpfile();
ASSERT_TRUE(fp != NULL);
char* buffer = NULL;
size_t buffer_length = 0;
// The first argument can't be NULL.
errno = 0;
ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
ASSERT_EQ(EINVAL, errno);
// The second argument can't be NULL.
errno = 0;
ASSERT_EQ(getline(&buffer, NULL, fp), -1);
ASSERT_EQ(EINVAL, errno);
// The underlying fd can't be closed.
ASSERT_EQ(0, close(fileno(fp)));
errno = 0;
ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
ASSERT_EQ(EBADF, errno);
fclose(fp);
}
TEST(stdio, printf_ssize_t) {
// http://b/8253769
ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
// For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
// error: format '%zd' expects argument of type 'signed size_t',
// but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
ssize_t v = 1;
char buf[32];
snprintf(buf, sizeof(buf), "%zd", v);
}
// https://code.google.com/p/android/issues/detail?id=64886
TEST(stdio, snprintf_a) {
char buf[BUFSIZ];
EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
}
TEST(stdio, snprintf_lc) {
char buf[BUFSIZ];
wint_t wc = L'a';
EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
EXPECT_STREQ("<a>", buf);
}
TEST(stdio, snprintf_ls) {
char buf[BUFSIZ];
wchar_t* ws = NULL;
EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
EXPECT_STREQ("<(null)>", buf);
wchar_t chars[] = { L'h', L'i', 0 };
ws = chars;
EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
EXPECT_STREQ("<hi>", buf);
}
TEST(stdio, snprintf_n) {
#if defined(__BIONIC__)
// http://b/14492135
char buf[32];
int i = 1234;
EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
EXPECT_EQ(1234, i);
EXPECT_STREQ("a n b", buf);
#else
GTEST_LOG_(INFO) << "This test does nothing.\n";
#endif
}
TEST(stdio, snprintf_smoke) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "a");
EXPECT_STREQ("a", buf);
snprintf(buf, sizeof(buf), "%%");
EXPECT_STREQ("%", buf);
snprintf(buf, sizeof(buf), "01234");
EXPECT_STREQ("01234", buf);
snprintf(buf, sizeof(buf), "a%sb", "01234");
EXPECT_STREQ("a01234b", buf);
char* s = NULL;
snprintf(buf, sizeof(buf), "a%sb", s);
EXPECT_STREQ("a(null)b", buf);
snprintf(buf, sizeof(buf), "aa%scc", "bb");
EXPECT_STREQ("aabbcc", buf);
snprintf(buf, sizeof(buf), "a%cc", 'b');
EXPECT_STREQ("abc", buf);
snprintf(buf, sizeof(buf), "a%db", 1234);
EXPECT_STREQ("a1234b", buf);
snprintf(buf, sizeof(buf), "a%db", -8123);
EXPECT_STREQ("a-8123b", buf);
snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
EXPECT_STREQ("a16b", buf);
snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
EXPECT_STREQ("a16b", buf);
snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
EXPECT_STREQ("a68719476736b", buf);
snprintf(buf, sizeof(buf), "a%ldb", 70000L);
EXPECT_STREQ("a70000b", buf);
snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
EXPECT_STREQ("a0xb0001234b", buf);
snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
EXPECT_STREQ("a12abz", buf);
snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
EXPECT_STREQ("a12ABz", buf);
snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
EXPECT_STREQ("a00123456z", buf);
snprintf(buf, sizeof(buf), "a%5dz", 1234);
EXPECT_STREQ("a 1234z", buf);
snprintf(buf, sizeof(buf), "a%05dz", 1234);
EXPECT_STREQ("a01234z", buf);
snprintf(buf, sizeof(buf), "a%8dz", 1234);
EXPECT_STREQ("a 1234z", buf);
snprintf(buf, sizeof(buf), "a%-8dz", 1234);
EXPECT_STREQ("a1234 z", buf);
snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
EXPECT_STREQ("Aabcdef Z", buf);
snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
EXPECT_STREQ("Ahello:1234Z", buf);
snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
EXPECT_STREQ("a005:5:05z", buf);
void* p = NULL;
snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
#if defined(__BIONIC__)
EXPECT_STREQ("a5,0x0z", buf);
#else // __BIONIC__
EXPECT_STREQ("a5,(nil)z", buf);
#endif // __BIONIC__
snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
EXPECT_STREQ("a68719476736,6,7,8z", buf);
snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
EXPECT_STREQ("a_1.230000_b", buf);
snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
EXPECT_STREQ("a_3.14_b", buf);
snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
EXPECT_STREQ("print_me_twice print_me_twice", buf);
}
TEST(stdio, snprintf_f_special) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%f", nanf(""));
EXPECT_STRCASEEQ("NaN", buf);
snprintf(buf, sizeof(buf), "%f", HUGE_VALF);
EXPECT_STRCASEEQ("Inf", buf);
}
TEST(stdio, snprintf_g_special) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%g", nan(""));
EXPECT_STRCASEEQ("NaN", buf);
snprintf(buf, sizeof(buf), "%g", HUGE_VAL);
EXPECT_STRCASEEQ("Inf", buf);
}
TEST(stdio, snprintf_d_INT_MAX) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%d", INT_MAX);
EXPECT_STREQ("2147483647", buf);
}
TEST(stdio, snprintf_d_INT_MIN) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%d", INT_MIN);
EXPECT_STREQ("-2147483648", buf);
}
TEST(stdio, snprintf_ld_LONG_MAX) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
#if __LP64__
EXPECT_STREQ("9223372036854775807", buf);
#else
EXPECT_STREQ("2147483647", buf);
#endif
}
TEST(stdio, snprintf_ld_LONG_MIN) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
#if __LP64__
EXPECT_STREQ("-9223372036854775808", buf);
#else
EXPECT_STREQ("-2147483648", buf);
#endif
}
TEST(stdio, snprintf_lld_LLONG_MAX) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
EXPECT_STREQ("9223372036854775807", buf);
}
TEST(stdio, snprintf_lld_LLONG_MIN) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
EXPECT_STREQ("-9223372036854775808", buf);
}
TEST(stdio, snprintf_e) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%e", 1.5);
EXPECT_STREQ("1.500000e+00", buf);
snprintf(buf, sizeof(buf), "%Le", 1.5l);
EXPECT_STREQ("1.500000e+00", buf);
}
TEST(stdio, snprintf_negative_zero_5084292) {
char buf[BUFSIZ];
snprintf(buf, sizeof(buf), "%f", -0.0);
EXPECT_STREQ("-0.000000", buf);
}
TEST(stdio, snprintf_utf8_15439554) {
locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
locale_t old_locale = uselocale(cloc);
// http://b/15439554
char buf[BUFSIZ];
// 1-byte character.
snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
EXPECT_STREQ("1x2", buf);
// 2-byte character.
snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
EXPECT_STREQ("1¢2", buf);
// 3-byte character.
snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
EXPECT_STREQ("1€2", buf);
// 4-byte character.
snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
EXPECT_STREQ("1𤭢2", buf);
uselocale(old_locale);
freelocale(cloc);
}
TEST(stdio, fprintf_failures_7229520) {
// http://b/7229520
FILE* fp;
// Unbuffered case where the fprintf(3) itself fails.
ASSERT_NE(nullptr, fp = tmpfile());
setbuf(fp, NULL);
ASSERT_EQ(4, fprintf(fp, "epic"));
ASSERT_EQ(0, close(fileno(fp)));
ASSERT_EQ(-1, fprintf(fp, "fail"));
ASSERT_EQ(-1, fclose(fp));
// Buffered case where we won't notice until the fclose(3).
// It's likely this is what was actually seen in http://b/7229520,
// and that expecting fprintf to fail is setting yourself up for
// disappointment. Remember to check fclose(3)'s return value, kids!
ASSERT_NE(nullptr, fp = tmpfile());
ASSERT_EQ(4, fprintf(fp, "epic"));
ASSERT_EQ(0, close(fileno(fp)));
ASSERT_EQ(4, fprintf(fp, "fail"));
ASSERT_EQ(-1, fclose(fp));
}
TEST(stdio, popen) {
FILE* fp = popen("cat /proc/version", "r");
ASSERT_TRUE(fp != NULL);
char buf[16];
char* s = fgets(buf, sizeof(buf), fp);
buf[13] = '\0';
ASSERT_STREQ("Linux version", s);
ASSERT_EQ(0, pclose(fp));
}
TEST(stdio, getc) {
FILE* fp = fopen("/proc/version", "r");
ASSERT_TRUE(fp != NULL);
ASSERT_EQ('L', getc(fp));
ASSERT_EQ('i', getc(fp));
ASSERT_EQ('n', getc(fp));
ASSERT_EQ('u', getc(fp));
ASSERT_EQ('x', getc(fp));
fclose(fp);
}
TEST(stdio, putc) {
FILE* fp = fopen("/proc/version", "r");
ASSERT_TRUE(fp != NULL);
ASSERT_EQ(EOF, putc('x', fp));
fclose(fp);
}
TEST(stdio, sscanf) {
char s1[123];
int i1;
double d1;
char s2[123];
ASSERT_EQ(3, sscanf(" hello 123 1.23 ", "%s %i %lf %s", s1, &i1, &d1, s2));
ASSERT_STREQ("hello", s1);
ASSERT_EQ(123, i1);
ASSERT_DOUBLE_EQ(1.23, d1);
}
TEST(stdio, cantwrite_EBADF) {
// If we open a file read-only...
FILE* fp = fopen("/proc/version", "r");
// ...all attempts to write to that file should return failure.
// They should also set errno to EBADF. This isn't POSIX, but it's traditional.
// glibc gets the wide-character functions wrong.
errno = 0;
EXPECT_EQ(EOF, putc('x', fp));
EXPECT_EQ(EBADF, errno);
errno = 0;
EXPECT_EQ(EOF, fprintf(fp, "hello"));
EXPECT_EQ(EBADF, errno);
errno = 0;
EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
#if defined(__BIONIC__)
EXPECT_EQ(EBADF, errno);
#endif
errno = 0;
EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
EXPECT_EQ(EBADF, errno);
errno = 0;
EXPECT_EQ(EOF, fputs("hello", fp));
EXPECT_EQ(EBADF, errno);
errno = 0;
EXPECT_EQ(WEOF, fputwc(L'x', fp));
#if defined(__BIONIC__)
EXPECT_EQ(EBADF, errno);
#endif
}
// Tests that we can only have a consistent and correct fpos_t when using
// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
TEST(stdio, consistent_fpos_t) {
ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
uselocale(LC_GLOBAL_LOCALE);
FILE* fp = tmpfile();
ASSERT_TRUE(fp != NULL);
wchar_t mb_one_bytes = L'h';
wchar_t mb_two_bytes = 0x00a2;
wchar_t mb_three_bytes = 0x20ac;
wchar_t mb_four_bytes = 0x24b62;
// Write to file.
ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
rewind(fp);
// Record each character position.
fpos_t pos1;
fpos_t pos2;
fpos_t pos3;
fpos_t pos4;
fpos_t pos5;
EXPECT_EQ(0, fgetpos(fp, &pos1));
ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
EXPECT_EQ(0, fgetpos(fp, &pos2));
ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
EXPECT_EQ(0, fgetpos(fp, &pos3));
ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
EXPECT_EQ(0, fgetpos(fp, &pos4));
ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
EXPECT_EQ(0, fgetpos(fp, &pos5));
#if defined(__BIONIC__)
// Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
// upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
// Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
// structure.
ASSERT_EQ(0, static_cast<off_t>(pos1));
ASSERT_EQ(1, static_cast<off_t>(pos2));
ASSERT_EQ(3, static_cast<off_t>(pos3));
ASSERT_EQ(6, static_cast<off_t>(pos4));
ASSERT_EQ(10, static_cast<off_t>(pos5));
#endif
// Exercise back and forth movements of the position.
ASSERT_EQ(0, fsetpos(fp, &pos2));
ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
ASSERT_EQ(0, fsetpos(fp, &pos1));
ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
ASSERT_EQ(0, fsetpos(fp, &pos4));
ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
ASSERT_EQ(0, fsetpos(fp, &pos3));
ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
ASSERT_EQ(0, fsetpos(fp, &pos5));
ASSERT_EQ(WEOF, fgetwc(fp));
fclose(fp);
}
// Exercise the interaction between fpos and seek.
TEST(stdio, fpos_t_and_seek) {
ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
uselocale(LC_GLOBAL_LOCALE);
// In glibc-2.16 fseek doesn't work properly in wide mode
// (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
// to close and re-open the file. We do it in order to make the test pass
// with all glibcs.
TemporaryFile tf;
FILE* fp = fdopen(tf.fd, "w+");
ASSERT_TRUE(fp != NULL);
wchar_t mb_two_bytes = 0x00a2;
wchar_t mb_three_bytes = 0x20ac;
wchar_t mb_four_bytes = 0x24b62;
// Write to file.
ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
fflush(fp);
fclose(fp);
fp = fopen(tf.filename, "r");
ASSERT_TRUE(fp != NULL);
// Store a valid position.
fpos_t mb_two_bytes_pos;
ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
// Move inside mb_four_bytes with fseek.
long offset_inside_mb = 6;
ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
// Store the "inside multi byte" position.
fpos_t pos_inside_mb;
ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
#if defined(__BIONIC__)
ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
#endif
// Reading from within a byte should produce an error.
ASSERT_EQ(WEOF, fgetwc(fp));
ASSERT_EQ(EILSEQ, errno);
// Reverting to a valid position should work.
ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
// Moving withing a multi byte with fsetpos should work but reading should
// produce an error.
ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
ASSERT_EQ(WEOF, fgetwc(fp));
ASSERT_EQ(EILSEQ, errno);
fclose(fp);
}
|