2 * Copyright (C) 2013 Peter Schiffer.
4 * This file is part of libpipeline.
6 * libpipeline is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * libpipeline is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with libpipeline; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
29 #include <sys/select.h>
33 #include "xvasprintf.h"
37 const char *program_name = "read";
39 /* Must be 8194 or bigger */
40 #define RANDOM_STR_LEN 9000
42 /* Unit test for bug: https://bugzilla.redhat.com/show_bug.cgi?id=876108 */
43 START_TEST (test_read_long_line)
45 /* Generate long random string */
46 static const char *alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
47 "abcdefghijklmnopqrstuvwxyz";
48 char random_string[RANDOM_STR_LEN] = "";
51 for (i = 0; i < RANDOM_STR_LEN - 2; i++) {
52 random_string[i] = alphanum[rand () % (strlen (alphanum) - 1)];
54 random_string[RANDOM_STR_LEN - 1] = '\0';
56 /* Write the random string to file */
57 char *testfile = xasprintf ("%s/test", temp_dir);
58 FILE *tfd = fopen (testfile, "w");
60 ck_abort_msg ("fopen failed: %s", strerror (errno));
63 fprintf (tfd, "%s\n", random_string);
66 char *expected_output = xasprintf ("%s\n", random_string);
70 char *read_result = NULL, *temp = NULL;
72 /* File must be read twice to reproduce the bug */
74 pipeline_want_infile (p, testfile);
75 pipeline_want_out (p, -1);
77 while ((line = pipeline_readline (p)) != NULL) {
80 read_result = xasprintf ("%s%s", read_result, line);
83 read_result = xasprintf ("%s", line);
87 ck_assert_str_eq (read_result, expected_output);
93 pipeline_want_infile (p, testfile);
94 pipeline_want_out (p, -1);
96 while ((line = pipeline_readline (p)) != NULL) {
99 read_result = xasprintf ("%s%s", read_result, line);
102 read_result = xasprintf ("%s", line);
106 ck_assert_str_eq (read_result, expected_output);
109 free (expected_output);
114 /* Write the first character of a string quickly, followed by the rest of it
117 static void slow_line_helper (void *data)
119 const char *s = data;
120 struct timeval timeout;
122 setbuf (stdout, NULL);
125 timeout.tv_usec = 100000;
126 select (0, NULL, NULL, NULL, &timeout);
127 fputs (s + 1, stdout);
130 START_TEST (test_read_readline_slow)
136 cmd = pipecmd_new_function ("slow_line_helper", slow_line_helper, free,
137 xstrdup ("a line\nsome more"));
138 pipeline_command (p, cmd);
139 pipeline_want_out (p, -1);
141 ck_assert_str_eq (pipeline_readline (p), "a line\n");
146 static Suite *read_suite (void)
148 Suite *s = suite_create ("Read");
150 TEST_CASE_WITH_FIXTURE (s, read, long_line, temp_dir_setup,
152 TEST_CASE (s, read, readline_slow);