p[] = "/tmp/test-fileio-out-XXXXXX";
FILE *f;
_cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
- *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL;
+ *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL,
+ *eleven = NULL, *twelve = NULL, *thirteen = NULL;
_cleanup_strv_free_ char **a = NULL, **b = NULL;
char **i;
unsigned k;
"three = \"333\n"
"xxxx\"\n"
"four = \'44\\\"44\'\n"
- "five = \'55\\\'55\' \"FIVE\" cinco \n"
+ "five = \"55\\\"55\" \"FIVE\" cinco \n"
"six = seis sechs\\\n"
" sis\n"
"seven=\"sevenval\" #nocomment\n"
"export nine=nineval\n"
"ten=ignored\n"
"ten=ignored\n"
- "ten=", f);
+ "ten=\n"
+ "eleven=\\value\n"
+ "twelve=\"\\value\"\n"
+ "thirteen='\\value'", f);
fflush(f);
fclose(f);
assert_se(streq_ptr(a[0], "one=BAR"));
assert_se(streq_ptr(a[1], "two=bar"));
assert_se(streq_ptr(a[2], "three=333\nxxxx"));
- assert_se(streq_ptr(a[3], "four=44\"44"));
- assert_se(streq_ptr(a[4], "five=55\'55FIVEcinco"));
+ assert_se(streq_ptr(a[3], "four=44\\\"44"));
+ assert_se(streq_ptr(a[4], "five=55\"55FIVEcinco"));
assert_se(streq_ptr(a[5], "six=seis sechs sis"));
assert_se(streq_ptr(a[6], "seven=sevenval#nocomment"));
assert_se(streq_ptr(a[7], "eight=eightval #nocomment"));
assert_se(streq_ptr(a[8], "export nine=nineval"));
assert_se(streq_ptr(a[9], "ten="));
- assert_se(a[10] == NULL);
+ assert_se(streq_ptr(a[10], "eleven=value"));
+ assert_se(streq_ptr(a[11], "twelve=\\value"));
+ assert_se(streq_ptr(a[12], "thirteen=\\value"));
+ assert_se(a[13] == NULL);
strv_env_clean(a);
"seven", &seven,
"eight", &eight,
"export nine", &nine,
- "ten", &ten);
+ "ten", &ten,
+ "eleven", &eleven,
+ "twelve", &twelve,
+ "thirteen", &thirteen);
assert_se(r >= 0);
log_info("eight=[%s]", strna(eight));
log_info("export nine=[%s]", strna(nine));
log_info("ten=[%s]", strna(nine));
+ log_info("eleven=[%s]", strna(eleven));
+ log_info("twelve=[%s]", strna(twelve));
+ log_info("thirteen=[%s]", strna(thirteen));
assert_se(streq(one, "BAR"));
assert_se(streq(two, "bar"));
assert_se(streq(three, "333\nxxxx"));
- assert_se(streq(four, "44\"44"));
- assert_se(streq(five, "55\'55FIVEcinco"));
+ assert_se(streq(four, "44\\\"44"));
+ assert_se(streq(five, "55\"55FIVEcinco"));
assert_se(streq(six, "seis sechs sis"));
assert_se(streq(seven, "sevenval#nocomment"));
assert_se(streq(eight, "eightval #nocomment"));
assert_se(streq(nine, "nineval"));
assert_se(ten == NULL);
+ assert_se(streq(eleven, "value"));
+ assert_se(streq(twelve, "\\value"));
+ assert_se(streq(thirteen, "\\value"));
{
/* prepare a temporary file to write the environment to */
free(ret);
}
+static const char chars[] =
+ "Aąę„”\n루\377";
+
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wtype-limits"
+
+static void test_fgetc(void) {
+ _cleanup_fclose_ FILE *f = NULL;
+ char c;
+
+ f = fmemopen_unlocked((void*) chars, sizeof(chars), "re");
+ assert_se(f);
+
+ for (unsigned i = 0; i < sizeof(chars); i++) {
+ assert_se(safe_fgetc(f, &c) == 1);
+ assert_se(c == chars[i]);
+
+ /* EOF is -1, and hence we can't push value 255 in this way if char is signed */
+ assert_se(ungetc(c, f) != EOF || c == EOF);
+ assert_se(c == EOF || safe_fgetc(f, &c) == 1);
+ assert_se(c == chars[i]);
+
+ /* But it works when we push it properly cast */
+ assert_se(ungetc((unsigned char) c, f) != EOF);
+ assert_se(safe_fgetc(f, &c) == 1);
+ assert_se(c == chars[i]);
+ }
+
+ assert_se(safe_fgetc(f, &c) == 0);
+}
+
+#pragma GCC diagnostic pop
+
static const char buffer[] =
"Some test data\n"
+ "루Non-ascii chars: ąę„”\n"
+ "terminators\r\n"
+ "and even more\n\r"
+ "now the same with a NUL\n\0"
+ "and more\r\0"
+ "and even more\r\n\0"
+ "and yet even more\n\r\0"
"With newlines, and a NUL byte\0"
"\n"
"an empty line\n"
assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "Some test data"));
line = mfree(line);
+ assert_se(read_line(f, (size_t) -1, &line) > 0 && streq(line, "루Non-ascii chars: ąę„”"));
+ line = mfree(line);
+
+ assert_se(read_line(f, (size_t) -1, &line) == 13 && streq(line, "terminators"));
+ line = mfree(line);
+
+ assert_se(read_line(f, (size_t) -1, &line) == 15 && streq(line, "and even more"));
+ line = mfree(line);
+
+ assert_se(read_line(f, (size_t) -1, &line) == 25 && streq(line, "now the same with a NUL"));
+ line = mfree(line);
+
+ assert_se(read_line(f, (size_t) -1, &line) == 10 && streq(line, "and more"));
+ line = mfree(line);
+
+ assert_se(read_line(f, (size_t) -1, &line) == 16 && streq(line, "and even more"));
+ line = mfree(line);
+
+ assert_se(read_line(f, (size_t) -1, &line) == 20 && streq(line, "and yet even more"));
+ line = mfree(line);
+
assert_se(read_line(f, 1024, &line) == 30 && streq(line, "With newlines, and a NUL byte"));
line = mfree(line);
line = mfree(line);
/* read_line() stopped when it hit the limit, that means when we continue reading we'll read at the first
- * character after the previous limit. Let's make use of tha to continue our test. */
- assert_se(read_line(f, 1024, &line) == 61 && streq(line, "line that is supposed to be truncated, because it is so long"));
- line = mfree(line);
-
- assert_se(read_line(f, 1024, &line) == 1 && streq(line, ""));
+ * character after the previous limit. Let's make use of that to continue our test. */
+ assert_se(read_line(f, 1024, &line) == 62 && streq(line, "line that is supposed to be truncated, because it is so long"));
line = mfree(line);
assert_se(read_line(f, 1024, &line) == 0 && streq(line, ""));
static void test_read_line(void) {
_cleanup_fclose_ FILE *f = NULL;
- f = fmemopen((void*) buffer, sizeof(buffer), "re");
+ f = fmemopen_unlocked((void*) buffer, sizeof(buffer), "re");
assert_se(f);
test_read_line_one_file(f);
assert_se(f);
r = read_line(f, LINE_MAX, &line);
- assert_se((size_t) r == strlen(line) + 1);
+ assert_se(r >= 0);
+ if (r == 0)
+ assert_se(line && isempty(line));
+ else
+ assert_se((size_t) r == strlen(line) + 1);
assert_se(read_line(f, LINE_MAX, NULL) == 0);
}
+static void test_read_line4(void) {
+ static const struct {
+ size_t length;
+ const char *string;
+ } eof_endings[] = {
+ /* Each of these will be followed by EOF and should generate the one same single string */
+ { 3, "foo" },
+ { 4, "foo\n" },
+ { 4, "foo\r" },
+ { 4, "foo\0" },
+ { 5, "foo\n\0" },
+ { 5, "foo\r\0" },
+ { 5, "foo\r\n" },
+ { 5, "foo\n\r" },
+ { 6, "foo\r\n\0" },
+ { 6, "foo\n\r\0" },
+ };
+
+ size_t i;
+ int r;
+
+ for (i = 0; i < ELEMENTSOF(eof_endings); i++) {
+ _cleanup_fclose_ FILE *f = NULL;
+ _cleanup_free_ char *s = NULL;
+
+ assert_se(f = fmemopen_unlocked((void*) eof_endings[i].string, eof_endings[i].length, "r"));
+
+ r = read_line(f, (size_t) -1, &s);
+ assert_se((size_t) r == eof_endings[i].length);
+ assert_se(streq_ptr(s, "foo"));
+
+ assert_se(read_line(f, (size_t) -1, NULL) == 0); /* Ensure we hit EOF */
+ }
+}
+
+static void test_read_nul_string(void) {
+ static const char test[] = "string nr. 1\0"
+ "string nr. 2\n\0"
+ "\377empty string follows\0"
+ "\0"
+ "final string\n is empty\0"
+ "\0";
+
+ _cleanup_fclose_ FILE *f = NULL;
+ _cleanup_free_ char *s = NULL;
+
+ assert_se(f = fmemopen_unlocked((void*) test, sizeof(test)-1, "r"));
+
+ assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 13 && streq_ptr(s, "string nr. 1"));
+ s = mfree(s);
+
+ assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 14 && streq_ptr(s, "string nr. 2\n"));
+ s = mfree(s);
+
+ assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 22 && streq_ptr(s, "\377empty string follows"));
+ s = mfree(s);
+
+ assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
+ s = mfree(s);
+
+ assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 23 && streq_ptr(s, "final string\n is empty"));
+ s = mfree(s);
+
+ assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 1 && streq_ptr(s, ""));
+ s = mfree(s);
+
+ assert_se(read_nul_string(f, LONG_LINE_MAX, &s) == 0 && streq_ptr(s, ""));
+}
+
int main(int argc, char *argv[]) {
test_setup_logging(LOG_DEBUG);
test_search_and_fopen_nulstr();
test_writing_tmpfile();
test_tempfn();
+ test_fgetc();
test_read_line();
test_read_line2();
test_read_line3();
+ test_read_line4();
+ test_read_nul_string();
return 0;
}