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 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
|
/* Test setvbuf under various conditions.
Copyright (C) 2025 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
/* This file is used twice, once as the test itself (where do_test
is defined) and once as a subprocess we spawn to test stdin et all
(where main is defined). INDEPENDENT_PART is defined for the
latter.
Note also that the purpose of this test is to test setvbuf, not the
underlying buffering code. */
#include <stdbool.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <libio.h>
#include <termios.h>
#include <support/support.h>
#include <support/check.h>
#include <support/temp_file.h>
#include <support/xstdio.h>
#include <support/xunistd.h>
#include <support/xthread.h>
#include <support/tty.h>
/* Dear future developer: If you are reading this, you are likely
trying to change or understand this test. In that case, these
debug/dump macros will be helpful. */
#if 0
# define debug printf ("\033[3%dm%s:%d\033[0m\n", \
(__LINE__ % 6) + 1, __FUNCTION__, __LINE__);
static void
dumpfp (FILE *fp)
{
char f[10], *p=f;
if (fp->_flags & _IO_UNBUFFERED)
*p++ = 'N';
if (fp->_flags & _IO_LINE_BUF)
*p++ = 'L';
if (p == f)
*p++ = 'B';
*p = 0;
printf ("FILE %p flags %s"
" read %p \033[%dm%+ld \033[%dm%+ld\033[0m"
" write %p \033[%dm%+ld \033[%dm%+ld\033[0m %ld"
" buf %p \033[%dm%+ld\033[0m sz %ld pend %ld\n",
fp, f,
fp->_IO_read_base,
fp->_IO_read_ptr == fp->_IO_read_base ? 33 : 32,
fp->_IO_read_ptr - fp->_IO_read_base,
fp->_IO_read_end == fp->_IO_read_base ? 33 : 36,
fp->_IO_read_end - fp->_IO_read_base,
fp->_IO_write_base,
fp->_IO_write_ptr == fp->_IO_write_base ? 33 : 32,
fp->_IO_write_ptr - fp->_IO_write_base,
fp->_IO_write_end == fp->_IO_write_base ? 33 : 36,
fp->_IO_write_end - fp->_IO_write_base,
fp->_IO_write_end - fp->_IO_write_base,
fp->_IO_buf_base,
fp->_IO_buf_end == fp->_IO_buf_base ? 33 : 35,
fp->_IO_buf_end - fp->_IO_buf_base,
__fbufsize (fp), __fpending (fp)
);
}
#else
# define debug
# define dumpfp(FP)
#endif
#ifndef INDEPENDENT_PART
/* st_blksize value for that file, or BUFSIZ if out of range. */
static int blksize = BUFSIZ;
#endif
/* Our test buffer. */
#define TEST_BUFSIZE 42
static int bufsize = TEST_BUFSIZE < BUFSIZ ? TEST_BUFSIZE : BUFSIZ;
static char *buffer;
/* Test data, both written to that file and used as an in-memory
stream. */
char test_data[2 * BUFSIZ];
#define TEST_STRING "abcdef\n"
enum test_source_case
{
test_source_file,
test_source_pipe,
test_source_fifo,
test_source_pseudo_terminal,
test_source_dev_null,
test_source_count,
};
static const char *const test_source_name[test_source_count] =
{
"regular file",
"pipe",
"fifo",
"pseudo_terminal",
"dev_null"
};
enum test_stream_case
{
test_stream_stdin,
test_stream_stdout,
test_stream_stderr,
test_stream_fopen_r,
test_stream_fdopen_r,
test_stream_fopen_w,
test_stream_fdopen_w,
test_stream_count
};
static bool test_stream_reads[test_stream_count] =
{
true,
false,
false,
true,
true,
false,
false
};
static const char *const test_stream_name[test_stream_count] =
{
"stdin",
"stdout",
"stderr",
"fopen (read)",
"fdopen (read)",
"fopen (write)",
"fdopen (write)"
};
enum test_config_case
{
test_config_none,
test_config_unbuffered,
test_config_line,
test_config_fully,
test_config_count
};
static const char *const test_config_name[test_config_count] =
{
"no change",
"unbuffered",
"line buffered",
"fully buffered"
};
FILE *test_stream;
char *test_file_name = NULL;
int pty_fd;
char *test_pipe_name = NULL;
int test_pipe[2];
/* This is either -1 or represents a pre-opened file descriptor for
the test as returned by prepare_test_file. */
int test_fd;
/*------------------------------------------------------------*/
/* Note that throughout this test we reopen, remove, and change
to/from a fifo, the test file. This would normally cause a race
condition, except that we're in a test container. No other process
can run in the test container simultaneously. */
void
prepare_test_data (void)
{
buffer = (char *) xmalloc (bufsize);
#ifndef INDEPENDENT_PART
/* Both file and pipe need this. */
if (test_file_name == NULL)
{
debug;
int fd = create_temp_file ("tst-setvbuf2", &test_file_name);
TEST_VERIFY_EXIT (fd != -1);
struct stat64 st;
xfstat64 (fd, &st);
if (st.st_blksize > 0 && st.st_blksize < BUFSIZ)
blksize = st.st_blksize;
xclose (fd);
}
#endif
for (size_t i = 0; i < 2 * BUFSIZ; i++)
{
unsigned char c = TEST_STRING[i % strlen (TEST_STRING)];
test_data[i] = c;
}
}
#ifndef INDEPENDENT_PART
/* These functions provide a source/sink for the "other" side of any
pipe-style descriptor we're using for test. */
static pthread_t writer_thread_tid = 0;
static pthread_t reader_thread_tid = 0;
typedef struct {
int fd;
const char *fname;
} ThreadData;
/* It's OK if this is static, we only run one at a time. */
ThreadData thread_data;
static void
end_thread (pthread_t *ptid)
{
if (*ptid)
{
pthread_cancel (*ptid);
xpthread_join (*ptid);
/* The descriptor was passed in, or the helper thread made
sufficient progress and opened the file. */
if (thread_data.fd >= 0)
xclose (thread_data.fd);
*ptid = 0;
}
}
static void *
writer_thread_proc (void *closure)
{
ThreadData *td = (ThreadData *) closure;
int fd;
int i;
ssize_t wn;
debug;
if (td->fname)
td->fd = xopen (td->fname, O_WRONLY, 0777);
fd = td->fd;
while (1)
{
i = 0;
while (i < BUFSIZ)
{
wn = write (fd, test_data + i, BUFSIZ - i);
if (wn <= 0)
break;
i += wn;
}
}
return NULL;
}
static void *
reader_thread_proc (void *closure)
{
ThreadData *td = (ThreadData *) closure;
int fd;
ssize_t rn;
int n = 0;
debug;
if (td->fname)
td->fd = xopen (td->fname, O_RDONLY, 0777);
fd = td->fd;
while (1)
{
char buf[BUFSIZ];
rn = read (fd, buf, BUFSIZ);
if (rn <= 0)
break;
TEST_COMPARE_BLOB (buf, rn, test_data+n, rn);
n += rn;
}
return NULL;
}
static void
start_writer_thread (int fd)
{
debug;
thread_data.fd = fd;
thread_data.fname = NULL;
writer_thread_tid = xpthread_create (NULL, writer_thread_proc,
(void *)&thread_data);
}
static void
start_writer_thread_n (const char *fname)
{
debug;
thread_data.fd = -1;
thread_data.fname = fname;
writer_thread_tid = xpthread_create (NULL, writer_thread_proc,
(void *)&thread_data);
}
static void
end_writer_thread (void)
{
debug;
end_thread (&writer_thread_tid);
}
static void
start_reader_thread (int fd)
{
debug;
thread_data.fd = fd;
thread_data.fname = NULL;
reader_thread_tid = xpthread_create (NULL, reader_thread_proc,
(void *)&thread_data);
}
static void
start_reader_thread_n (const char *fname)
{
debug;
thread_data.fd = -1;
thread_data.fname = fname;
reader_thread_tid = xpthread_create (NULL, reader_thread_proc,
(void *)&thread_data);
}
static void
end_reader_thread (void)
{
debug;
end_thread (&reader_thread_tid);
}
/*------------------------------------------------------------*/
/* These two functions are reponsible for choosing a file to be tested
against, typically by returning a filename but in a few cases also
providing a file descriptor (i.e. for fdopen). */
static const char *
prepare_test_file (enum test_source_case f, enum test_stream_case s)
{
debug;
test_fd = -1;
switch (f)
{
case test_source_file:
{
if (test_stream_reads[f])
{
debug;
FILE *fp = xfopen (test_file_name, "w");
TEST_VERIFY_EXIT (fwrite (test_data, 1, 2 * BUFSIZ, fp)
== 2 * BUFSIZ);
xfclose (fp);
}
debug;
return test_file_name;
}
case test_source_pipe:
{
debug;
xpipe (test_pipe);
if (test_stream_reads[s])
{
start_writer_thread (test_pipe[1]);
test_fd = test_pipe[0];
}
else
{
start_reader_thread (test_pipe[0]);
test_fd = test_pipe[1];
}
test_pipe_name = xasprintf ("/proc/self/fd/%d", test_fd);
debug;
return test_pipe_name;
}
case test_source_fifo:
{
/* We do not want to fail/exit if the file doesn't exist. */
unlink (test_file_name);
xmkfifo (test_file_name, 0600);
debug;
if (test_stream_reads[s])
start_writer_thread_n (test_file_name);
else
start_reader_thread_n (test_file_name);
debug;
return test_file_name;
}
case test_source_pseudo_terminal:
{
support_openpty (&pty_fd, &test_fd, &test_pipe_name, NULL, NULL);
debug;
if (test_stream_reads[s])
start_writer_thread (pty_fd);
else
start_reader_thread (pty_fd);
debug;
return test_pipe_name;
}
case test_source_dev_null:
debug;
return "/dev/null";
default:
abort ();
}
}
static void
unprepare_test_file (FILE *fp,
enum test_source_case f,
enum test_stream_case s)
{
debug;
switch (f)
{
case test_source_file:
break;
case test_source_pipe:
free (test_pipe_name);
if (test_stream_reads[s])
end_writer_thread ();
else
end_reader_thread ();
break;
case test_source_fifo:
if (test_stream_reads[s])
end_writer_thread ();
else
end_reader_thread ();
unlink (test_file_name);
break;
case test_source_pseudo_terminal:
free (test_pipe_name);
if (test_stream_reads[s])
end_writer_thread ();
else
end_reader_thread ();
break;
case test_source_dev_null:
break;
default:
abort ();
}
debug;
}
/*------------------------------------------------------------*/
/* This function takes a filename and returns a file descriptor,
opened according to the method requested. */
static FILE *
open_test_stream (enum test_source_case f, enum test_stream_case s)
{
int fd;
FILE *fp;
const char *fname;
debug;
fname = prepare_test_file (f, s);
if (fname == NULL)
return NULL;
switch (s)
{
case test_stream_stdin:
fp = xfopen (fname, "r");
break;
case test_stream_stdout:
fp = xfopen (fname, "w");
break;
case test_stream_stderr:
fp = xfopen (fname, "w");
break;
case test_stream_fopen_r:
fp = xfopen (fname, "r");
break;
case test_stream_fdopen_r:
if (test_fd == -1)
fd = xopen (fname, O_RDONLY, 0);
else
fd = test_fd;
fp = fdopen (fd, "r");
break;
case test_stream_fopen_w:
fp = xfopen (fname, "w");
break;
case test_stream_fdopen_w:
fd = xopen (fname, O_WRONLY|O_CREAT|O_TRUNC, 0777);
fp = fdopen (fd, "w");
break;
default:
abort ();
}
TEST_VERIFY_EXIT (fp != NULL);
if (f == test_source_pseudo_terminal)
{
struct termios t;
/* We disable the NL to CR-LF conversion so that we can compare
data without having to remove the extra CRs. */
if (tcgetattr (fileno (fp), &t) < 0)
FAIL_EXIT1 ("tcgetattr failed: %m");
t.c_oflag &= ~ONLCR;
if (tcsetattr (fileno (fp), TCSANOW, &t) < 0)
FAIL_EXIT1 ("tcsetattr failed: %m");
}
debug;
printf ("source %s stream %s file %s fd %d\n",
test_source_name[f],
test_stream_name[s], fname, fileno (fp));
return fp;
}
#endif
/*------------------------------------------------------------*/
/* These functions do the actual testing - setting various buffering
options and verifying that they buffer as expected. */
static void
test_put_string (FILE *fp, const char *s, int count)
{
while (*s && count--)
{
fputc (*s++, fp);
TEST_VERIFY_EXIT (!ferror (fp));
}
}
int
verify_fully_buffered (FILE *fp,
enum test_source_case f,
enum test_stream_case s,
enum test_config_case c)
{
debug;
if (test_stream_reads[s])
{
char buf[10];
dumpfp (fp);
size_t fc = fread (buf, 1, 10 - 1, fp);
dumpfp (fp);
ssize_t count = fp->_IO_read_ptr - fp->_IO_read_base;
TEST_VERIFY (fp->_IO_read_base != NULL);
if (f == test_source_dev_null)
{
TEST_VERIFY (fc == 0);
TEST_VERIFY (count == 0);
}
else if (f == test_source_pseudo_terminal)
{
TEST_VERIFY (fc == 9);
TEST_VERIFY (count == 3 || count == 10);
}
else
{
TEST_VERIFY (fc == 9);
TEST_VERIFY (count == 10);
}
/* We already checked for the first character being 'a'. */
if (count > 1)
{
TEST_COMPARE_BLOB (buf, count - 1, test_data + 1, count - 1);
TEST_COMPARE_BLOB (fp->_IO_read_base, count, test_data, count);
}
}
else
{
dumpfp (fp);
test_put_string (fp, test_data + 1, 10 - 1);
dumpfp (fp);
TEST_COMPARE (fp->_IO_write_ptr - fp->_IO_write_base, 10);
TEST_COMPARE_BLOB (fp->_IO_write_base, 10, test_data, 10);
}
TEST_COMPARE ((fp->_flags & (_IO_UNBUFFERED | _IO_LINE_BUF)), 0);
if (c != test_config_none)
TEST_COMPARE (__fbufsize (fp), bufsize);
return 0;
}
int
verify_line_buffered (FILE *fp,
enum test_source_case f,
enum test_stream_case s,
enum test_config_case c)
{
debug;
/* "line buffered" for inputs is not really defined; what you really
want here is to control the device providing input. For GLIBC a
line-buffered input is treated as fully buffered. */
if (test_stream_reads[s])
{
char buf[10];
dumpfp (fp);
size_t fc = fread (buf, 1, 10 - 1, fp);
dumpfp (fp);
ssize_t count = fp->_IO_read_ptr - fp->_IO_read_base;
TEST_VERIFY (fp->_IO_read_base != NULL);
if (f == test_source_dev_null)
{
TEST_VERIFY (fc == 0);
TEST_VERIFY (count == 0);
}
else if (f == test_source_pseudo_terminal)
{
TEST_VERIFY (fc == 9);
TEST_VERIFY (count == 3 || count == 10);
}
else
{
TEST_VERIFY (fc == 9);
TEST_VERIFY (count == 10);
}
/* We already checked for the first character being 'a'. */
if (count > 1)
{
TEST_COMPARE_BLOB (buf, count - 1, test_data + 1, count - 1);
TEST_COMPARE_BLOB (fp->_IO_read_base, count, test_data, count);
}
}
else
{
dumpfp (fp);
test_put_string (fp, test_data + 1, 10 - 1);
dumpfp (fp);
TEST_COMPARE (fp->_IO_write_ptr - fp->_IO_write_base, 3);
/* The first "abcdef\n" got flushed, leaving "abc". */
TEST_COMPARE_BLOB (fp->_IO_write_base, 3, test_data + 7, 3);
}
TEST_COMPARE ((fp->_flags & (_IO_UNBUFFERED | _IO_LINE_BUF)), _IO_LINE_BUF);
if (c != test_config_none)
TEST_COMPARE (__fbufsize (fp), bufsize);
return 0;
}
int
verify_unbuffered (FILE *fp,
enum test_source_case f,
enum test_stream_case s,
enum test_config_case c)
{
debug;
if (test_stream_reads[s])
{
/* We've already read one byte. */
dumpfp (fp);
TEST_VERIFY (fp->_IO_read_base != NULL);
if (f == test_source_dev_null)
TEST_COMPARE (fp->_IO_read_ptr - fp->_IO_read_base, 0);
else
{
TEST_COMPARE (fp->_IO_read_ptr - fp->_IO_read_base, 1);
TEST_COMPARE (fp->_IO_read_base[0], test_data[0]);
TEST_VERIFY (fp->_IO_read_ptr == fp->_IO_read_end);
}
}
else
{
dumpfp (fp);
fputc (test_data[1], fp);
dumpfp (fp);
TEST_COMPARE (fp->_IO_write_ptr - fp->_IO_write_base, 0);
TEST_COMPARE (fp->_IO_write_base[0], test_data[1]);
TEST_VERIFY (fp->_IO_write_end == fp->_IO_write_base);
}
TEST_COMPARE ((fp->_flags & (_IO_UNBUFFERED | _IO_LINE_BUF)),
_IO_UNBUFFERED);
TEST_COMPARE (__fbufsize (fp), 1);
return 0;
}
static int
do_setvbuf (FILE *fp, void *buf, int flags, int size,
enum test_stream_case s)
{
if (s != test_stream_stdout)
printf ("SETVBUF %p %p %s %d\n",
fp, buf,
flags == _IONBF ? "_IONBF"
: flags == _IOLBF ? "_IOLBF"
: flags == _IOFBF ? "_IOFBF"
: "???", size);
if (setvbuf (fp, buf, flags, size))
{
perror ("setvbuf");
return 1;
}
return 0;
}
int
do_second_part (FILE *fp,
enum test_source_case f,
enum test_stream_case s,
enum test_config_case c)
{
/* At this point, FP is the stream to test according to the other
parameters. */
int rv = 0;
int flags_before;
int flags_after;
debug;
flags_before = fp->_flags & (_IO_UNBUFFERED | _IO_LINE_BUF);
/* This is where we do the thing we're testing for. */
switch (c)
{
case test_config_none:
/* Buffering is unchanged. */
break;
case test_config_unbuffered:
do_setvbuf (fp, NULL, _IONBF, 0, s);
break;
case test_config_line:
do_setvbuf (fp, buffer, _IOLBF, bufsize, s);
break;
case test_config_fully:
do_setvbuf (fp, buffer, _IOFBF, bufsize, s);
break;
default:
abort ();
}
flags_after = fp->_flags & (_IO_UNBUFFERED | _IO_LINE_BUF);
/* Check the buffer mode after we touch it, if we touched it. */
switch (c)
{
case test_config_none:
/* Buffering is unchanged, but may change on the first read/write. */
TEST_COMPARE (flags_after, flags_before);
break;
case test_config_unbuffered:
TEST_COMPARE (flags_after, _IO_UNBUFFERED);
break;
case test_config_line:
TEST_COMPARE (flags_after, _IO_LINE_BUF);
break;
case test_config_fully:
TEST_COMPARE (flags_after, 0);
break;
default:
abort ();
}
/* Glibc defers calculating the appropriate buffering mechanism
until it reads from or writes to the device. So we read one
character here, and account for that in the tests. */
if (test_stream_reads[s])
{
dumpfp (fp);
int c = fgetc (fp);
if (c != TEST_STRING[0] && f != test_source_dev_null)
FAIL ("first char read is %c not %c", c, TEST_STRING[0]);
dumpfp (fp);
}
else
{
dumpfp (fp);
fputc (TEST_STRING[0], fp);
dumpfp (fp);
}
switch (fp->_flags & (_IO_UNBUFFERED | _IO_LINE_BUF))
{
case _IO_LINE_BUF:
rv += verify_line_buffered (fp, f, s, c);
break;
case _IO_UNBUFFERED:
rv += verify_unbuffered (fp, f, s, c);
break;
case 0: /* Fully buffered. */
rv += verify_fully_buffered (fp, f, s, c);
break;
default:
abort ();
}
xfclose (fp);
return rv;
}
/*------------------------------------------------------------*/
#ifdef INDEPENDENT_PART
/* This part is the independent sub-process we call to test stdin et
al. */
int
main (int argc, char **argv)
{
/* This is one of the subprocesses we created to test stdin et
al. */
FILE *fp;
/* If we're called as a regular test, instead of as a sub-process,
don't complain. */
if (argc == 1)
return 0;
if (argc != 4)
{
int i;
for (i = 0; i <= argc; i ++)
printf ("argv[%d] = `%s'\n", i, argv[i] ?: "(null)");
FAIL_EXIT1 ("sub-process called wrong");
}
prepare_test_data ();
enum test_source_case f = atoi (argv[1]);
enum test_stream_case s = atoi (argv[2]);
enum test_config_case c = atoi (argv[3]);
if (s != test_stream_stdout)
printf ("\n\033[41mRunning test %s : %s : %s\033[0m\n",
test_source_name[f],
test_stream_name[s],
test_config_name[c]);
switch (s)
{
case test_stream_stdin:
fp = stdin;
break;
case test_stream_stdout:
fp = stdout;
break;
case test_stream_stderr:
fp = stderr;
break;
default:
abort ();
}
return do_second_part (fp, f, s, c);
}
#else
/* This part is the standard test process. */
/* Spawn an independent sub-process with std* redirected. */
int
recurse (FILE *fp,
enum test_source_case f,
enum test_stream_case s,
enum test_config_case c)
{
/* We need to test stdin, stdout, or stderr, which means creating a
subprocess with one of those redirected from FP. */
debug;
pid_t pid;
int status;
pid = fork ();
switch (pid)
{
case -1: /* error */
perror ("fork");
return 1;
break;
default: /* parent */
xfclose (fp);
xwaitpid (pid, &status, 0);
if (WIFEXITED (status)
&& WEXITSTATUS (status) == 0)
return 0;
return 1;
case 0: /* child */
switch (s)
{
case test_stream_stdin:
xclose (0);
dup2 (fileno (fp), 0);
break;
case test_stream_stdout:
xclose (1);
dup2 (fileno (fp), 1);
break;
case test_stream_stderr:
xclose (2);
dup2 (fileno (fp), 2);
break;
default:
abort ();
}
fclose (fp);
/* At this point, we have to run a program... which is tricky to
properly support for remote targets or crosses, because of
glibc versions etc. Hence we run in a test-container. */
char fs[10], ss[10], cs[10];
sprintf (fs, "%d", f);
sprintf (ss, "%d", s);
sprintf (cs, "%d", c);
execl (IND_PROC, IND_PROC, fs, ss, cs, NULL);
if (s == test_stream_stdout)
fprintf (stderr, "execl (%s) failed, ", IND_PROC);
else
printf ("execl (%s) failed, ", IND_PROC);
perror ("The error was");
exit (1);
break;
}
return 0;
}
int
do_test (void)
{
int rv = 0;
signal (SIGPIPE, SIG_IGN);
prepare_test_data ();
for (enum test_source_case f = 0; f < test_source_count; ++f)
for (enum test_stream_case s = 0; s < test_stream_count; ++s)
for (enum test_config_case c = 0; c < test_config_count; ++c)
{
printf ("\n\033[43mRunning test %s : %s : %s\033[0m\n",
test_source_name[f],
test_stream_name[s],
test_config_name[c]);
FILE *fp = open_test_stream (f, s);
if (fp)
{
if (s <= test_stream_stderr)
rv += recurse (fp, f, s, c);
else
rv += do_second_part (fp, f, s, c);
unprepare_test_file (fp, f, s);
}
}
free (buffer);
printf ("return %d\n", rv);
return rv;
}
# include <support/test-driver.c>
#endif
|