test-fileio.c revision 039f0e70a0fcd71dcf7cc2f3ba2cea2e3b186a60
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek This file is part of systemd.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Copyright 2013 Lennart Poettering
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek (at your option) any later version.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
74df0fca09b3c31ed19e14ba80f996fdff772417Lennart Poetteringstatic void test_parse_env_file(void) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p[] = "/tmp/test-fileio-out-XXXXXX";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_strv_free_ char **a = NULL, **b = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = mkostemp_safe(p, O_RDWR|O_CLOEXEC);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "invalid line\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "invalid line #comment\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "three = \"333\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "four = \'44\\\"44\'\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "five = \'55\\\'55\' \"FIVE\" cinco \n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "six = seis sechs\\\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "seven=\"sevenval\" #nocomment\n"
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk "eight=eightval #nocomment\n"
7f1ad696a273703789b624fe0b209fb63e953016Lennart Poettering "export nine=nineval\n"
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq_ptr(a[0], "one=BAR"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq_ptr(a[1], "two=bar"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq_ptr(a[2], "three=333\nxxxx"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq_ptr(a[3], "four=44\"44"));
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack assert_se(streq_ptr(a[4], "five=55\'55FIVEcinco"));
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack assert_se(streq_ptr(a[5], "six=seis sechs sis"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq_ptr(a[6], "seven=sevenval#nocomment"));
2c5859afecee81e345fc9526b1083bf79990ffb8Daniel Mack assert_se(streq_ptr(a[7], "eight=eightval #nocomment"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq_ptr(a[8], "export nine=nineval"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq_ptr(a[9], "ten="));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("one=[%s]", strna(one));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("two=[%s]", strna(two));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("three=[%s]", strna(three));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("four=[%s]", strna(four));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("five=[%s]", strna(five));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("six=[%s]", strna(six));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("seven=[%s]", strna(seven));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("eight=[%s]", strna(eight));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("export nine=[%s]", strna(nine));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("ten=[%s]", strna(nine));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq(three, "333\nxxxx"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq(four, "44\"44"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq(five, "55\'55FIVEcinco"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq(six, "seis sechs sis"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq(seven, "sevenval#nocomment"));
db91ea32aa223d1b087d99811226a9c59a1bb281Zbigniew Jędrzejewski-Szmek assert_se(streq(eight, "eightval #nocomment"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq(nine, "nineval"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_parse_multiline_env_file(void) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char t[] = "/tmp/test-fileio-in-XXXXXX",
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p[] = "/tmp/test-fileio-out-XXXXXX";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_strv_free_ char **a = NULL, **b = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = mkostemp_safe(p, O_RDWR|O_CLOEXEC);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fd = mkostemp_safe(t, O_RDWR|O_CLOEXEC);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek "tri=\"bar \\\n"
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek "\tgar \"\n", f);
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek r = load_env_file(NULL, t, NULL, &a);
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek assert_se(streq_ptr(a[0], "one=BAR VAR\tGAR"));
348ced909724a1331b85d57aede80a102a00e428Zbigniew Jędrzejewski-Szmek assert_se(streq_ptr(a[1], "two=bar var\tgar"));
670b110c3b59dfa335ac43065b2038400d1d04a9Zbigniew Jędrzejewski-Szmek assert_se(streq_ptr(a[2], "tri=bar var \tgar "));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = load_env_file(NULL, p, NULL, &b);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_executable_is_script(void) {
4608af4333d0f7f5f8e3bc632801b04ef07d246dLennart Poettering char t[] = "/tmp/test-executable-XXXXXX";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek fputs("#! /bin/script -a -b \ngoo goo", f);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = executable_is_script(t, &command);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(streq(command, "/bin/script"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = executable_is_script("/bin/sh", &command);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = executable_is_script("/usr/bin/yum", &command);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(r > 0 || r == -ENOENT);
23ad4dd8844c582929115a11ed2830a1371568d6Jan Alexander Steffens (heftig) assert_se(startswith(command, "/"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_status_field(void) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ char *t = NULL, *p = NULL, *s = NULL, *z = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek unsigned long long total = 0, buffers = 0;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(get_status_field("/proc/self/status", "\nThreads:", &t) == 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = get_status_field("/proc/meminfo", "MemTotal:", &p);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(safe_atollu(p, &total) == 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = get_status_field("/proc/meminfo", "\nBuffers:", &s);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(safe_atollu(s, &buffers) == 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek /* Seccomp should be a good test for field full of zeros. */
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek r = get_status_field("/proc/meminfo", "\nSeccomp:", &z);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(safe_atollu(z, &buffers) == 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_capeff(void) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_free_ char *capeff = NULL;
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek log_info("capeff: '%s' (r=%d)", capeff, r);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (r == -ENOENT || r == -EPERM)
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek p = capeff[strspn(capeff, DIGITS "abcdefABCDEF")];
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekstatic void test_write_string_stream(void) {
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek char fn[] = "/tmp/test-write_string_stream-XXXXXX";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek _cleanup_fclose_ FILE *f = NULL;
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert_se(write_string_stream(f, "boohoo") < 0);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert_se(write_string_stream(f, "boohoo") == 0);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert_se(streq(buf, "boohoo\n"));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmekstatic void test_write_string_file(void) {
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek char fn[] = "/tmp/test-write_string_file-XXXXXX";
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(write_string_file(fn, "boohoo") == 0);
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek assert_se(read(fd, buf, sizeof(buf)) == 7);
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmek assert_se(streq(buf, "boohoo\n"));
fc55baee9964a118afbddbf82b8e667a0ad80b99Zbigniew Jędrzejewski-Szmekstatic void test_write_string_file_no_create(void) {
43cf8388ea4ffed1801468d4b650d6e48eefce9eMichal Schmidt char fn[] = "/tmp/test-write_string_file_no_create-XXXXXX";
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert_se(write_string_file_no_create("/a/file/which/does/not/exists/i/guess", "boohoo") < 0);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert_se(write_string_file_no_create(fn, "boohoo") == 0);
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk assert_se(read(fd, buf, sizeof(buf)) == strlen("boohoo\n"));
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidtstatic void test_load_env_file_pairs(void) {
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk char fn[] = "/tmp/test-load_env_file_pairs-XXXXXX";
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt char **k, **v;
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt "NAME=\"Arch Linux\"\n"
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk "PRETTY_NAME=\"Arch Linux\"\n"
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk "ANSI_COLOR=\"0;36\"\n"
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk "HOME_URL=\"https://www.archlinux.org/\"\n"
26687bf8a907009dedcff79346860ed41511405eOleksii Shevchuk "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n"
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek r = load_env_file_pairs(f, fn, NULL, &l);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek assert_se(strv_length(l) == 14);
63c8666b824e8762ffb73647e1caee165dfbc868Zbigniew Jędrzejewski-Szmek assert_se(STR_IN_SET(*k, "NAME", "ID", "PRETTY_NAME", "ANSI_COLOR", "HOME_URL", "SUPPORT_URL", "BUG_REPORT_URL"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq(*k, "NAME")) assert_se(streq(*v, "Arch Linux"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq(*k, "ID")) assert_se(streq(*v, "arch"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq(*k, "PRETTY_NAME")) assert_se(streq(*v, "Arch Linux"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq(*k, "ANSI_COLOR")) assert_se(streq(*v, "0;36"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq(*k, "HOME_URL")) assert_se(streq(*v, "https://www.archlinux.org/"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq(*k, "SUPPORT_URL")) assert_se(streq(*v, "https://bbs.archlinux.org/"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmek if (streq(*k, "BUG_REPORT_URL")) assert_se(streq(*v, "https://bugs.archlinux.org/"));
d025f1e4dca8fc1436aff76f9e6185fe3e728daaZbigniew Jędrzejewski-Szmekint main(int argc, char *argv[]) {