]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/test/test-fileio.c
util-lib: split our string related calls from util.[ch] into its own file string...
[thirdparty/systemd.git] / src / test / test-fileio.c
index 4c31b776bd97ea35e640a616a0e53c39d2d8d26a..a72e84bc66799cd8be7a76b290c497b201efd686 100644 (file)
@@ -30,6 +30,7 @@
 #include "env-util.h"
 #include "def.h"
 #include "ctype.h"
+#include "string-util.h"
 
 static void test_parse_env_file(void) {
         char    t[] = "/tmp/test-fileio-in-XXXXXX",
@@ -241,18 +242,18 @@ static void test_status_field(void) {
         unsigned long long total = 0, buffers = 0;
         int r;
 
-        assert_se(get_status_field("/proc/self/status", "\nThreads:", &t) == 0);
+        assert_se(get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t) == 0);
         puts(t);
         assert_se(streq(t, "1"));
 
-        r = get_status_field("/proc/meminfo", "MemTotal:", &p);
+        r = get_proc_field("/proc/meminfo", "MemTotal", WHITESPACE, &p);
         if (r != -ENOENT) {
                 assert_se(r == 0);
                 puts(p);
                 assert_se(safe_atollu(p, &total) == 0);
         }
 
-        r = get_status_field("/proc/meminfo", "\nBuffers:", &s);
+        r = get_proc_field("/proc/meminfo", "Buffers", WHITESPACE, &s);
         if (r != -ENOENT) {
                 assert_se(r == 0);
                 puts(s);
@@ -263,7 +264,7 @@ static void test_status_field(void) {
                 assert_se(buffers < total);
 
         /* Seccomp should be a good test for field full of zeros. */
-        r = get_status_field("/proc/meminfo", "\nSeccomp:", &z);
+        r = get_proc_field("/proc/meminfo", "Seccomp", WHITESPACE, &z);
         if (r != -ENOENT) {
                 assert_se(r == 0);
                 puts(z);
@@ -302,17 +303,27 @@ static void test_write_string_stream(void) {
 
         f = fdopen(fd, "r");
         assert_se(f);
-        assert_se(write_string_stream(f, "boohoo") < 0);
+        assert_se(write_string_stream(f, "boohoo", true) < 0);
 
         f = freopen(fn, "r+", f);
         assert_se(f);
 
-        assert_se(write_string_stream(f, "boohoo") == 0);
+        assert_se(write_string_stream(f, "boohoo", true) == 0);
         rewind(f);
 
         assert_se(fgets(buf, sizeof(buf), f));
         assert_se(streq(buf, "boohoo\n"));
 
+        f = freopen(fn, "w+", f);
+        assert_se(f);
+
+        assert_se(write_string_stream(f, "boohoo", false) == 0);
+        rewind(f);
+
+        assert_se(fgets(buf, sizeof(buf), f));
+        printf(">%s<", buf);
+        assert_se(streq(buf, "boohoo"));
+
         unlink(fn);
 }
 
@@ -324,7 +335,7 @@ static void test_write_string_file(void) {
         fd = mkostemp_safe(fn, O_RDWR);
         assert_se(fd >= 0);
 
-        assert_se(write_string_file(fn, "boohoo") == 0);
+        assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0);
 
         assert_se(read(fd, buf, sizeof(buf)) == 7);
         assert_se(streq(buf, "boohoo\n"));
@@ -340,8 +351,8 @@ static void test_write_string_file_no_create(void) {
         fd = mkostemp_safe(fn, O_RDWR);
         assert_se(fd >= 0);
 
-        assert_se(write_string_file_no_create("/a/file/which/does/not/exists/i/guess", "boohoo") < 0);
-        assert_se(write_string_file_no_create(fn, "boohoo") == 0);
+        assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0);
+        assert_se(write_string_file(fn, "boohoo", 0) == 0);
 
         assert_se(read(fd, buf, sizeof(buf)) == strlen("boohoo\n"));
         assert_se(streq(buf, "boohoo\n"));
@@ -367,8 +378,8 @@ static void test_load_env_file_pairs(void) {
                         "ANSI_COLOR=\"0;36\"\n"
                         "HOME_URL=\"https://www.archlinux.org/\"\n"
                         "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
-                        "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n"
-                        );
+                        "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n",
+                        WRITE_STRING_FILE_CREATE);
         assert_se(r == 0);
 
         f = fdopen(fd, "r");