test-fileio.c revision 98f59e59e0c31ffcb953d3a7dba0da5e6f2f55f7
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek/***
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek This file is part of systemd.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Copyright 2013 Lennart Poettering
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek (at your option) any later version.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek***/
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include <stdio.h>
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include <fcntl.h>
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include <unistd.h>
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include "util.h"
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include "fileio.h"
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include "strv.h"
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek#include "env-util.h"
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmekstatic void test_parse_env_file(void) {
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek char t[] = "/tmp/test-parse-env-file-XXXXXX";
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek int fd, r;
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek FILE *f;
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *one = NULL, *two = NULL, *three = NULL, *four = NULL, *five = NULL,
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek *six = NULL, *seven = NULL, *eight = NULL, *nine = NULL, *ten = NULL;
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek _cleanup_strv_free_ char **a = NULL, **b = NULL;
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek char **i;
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek unsigned k;
799a8f39d8eb9ea725e85a598c0f5dbd658c8ba7Zbigniew Jędrzejewski-Szmek
50a0b0717563d08c027a16a896bff8d7754eab9eZbigniew Jędrzejewski-Szmek fd = mkostemp(t, O_CLOEXEC);
29fc0ddcd737af906986d4029579d4dfe838ba02Zbigniew Jędrzejewski-Szmek assert_se(fd >= 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek f = fdopen(fd, "w");
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_se(f);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek fputs("one=BAR \n"
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek "# comment\n"
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek " # comment \n"
7449bc1f34c206e3ff8e274cd74e2db950d492a1Zbigniew Jędrzejewski-Szmek " ; comment \n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek " two = bar \n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "invalid line\n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "invalid line #comment\n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "three = \"333\n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "xxxx\"\n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "four = \'44\\\"44\'\n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "five = \'55\\\'55\' \"FIVE\" cinco \n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "six = seis sechs\\\n"
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek " sis\n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "seven=\"sevenval\" #nocomment\n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "eight=eightval #nocomment\n"
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "export nine=nineval\n"
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek "ten=", f);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek fflush(f);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek fclose(f);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek r = load_env_file(t, NULL, &a);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_se(r >= 0);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek STRV_FOREACH(i, a)
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek log_info("Got: <%s>", *i);
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_se(streq(a[0], "one=BAR"));
3d090cc6f34e5970765dd1e7ee5e648a056d180dZbigniew Jędrzejewski-Szmek assert_se(streq(a[1], "two=bar"));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(streq(a[2], "three=333\nxxxx"));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(streq(a[3], "four=44\"44"));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(streq(a[4], "five=55\'55FIVEcinco"));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(streq(a[5], "six=seis sechs sis"));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(streq(a[6], "seven=sevenval#nocomment"));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(streq(a[7], "eight=eightval #nocomment"));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(streq(a[8], "export nine=nineval"));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(streq(a[9], "ten="));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(a[10] == NULL);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek strv_env_clean_log(a, "/tmp/test-fileio");
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek k = 0;
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek STRV_FOREACH(i, b) {
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek log_info("Got2: <%s>", *i);
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek assert_se(streq(*i, a[k++]));
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek }
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek r = parse_env_file(
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek t, NULL,
eacbb4d33e2bb5c54311544851140efe3dd0f774Zbigniew Jędrzejewski-Szmek "one", &one,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek "two", &two,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek "three", &three,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek "four", &four,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek "five", &five,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek "six", &six,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek "seven", &seven,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek "eight", &eight,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek "export nine", &nine,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek "ten", &ten,
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek NULL);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek assert_se(r >= 0);
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek log_info("one=[%s]", strna(one));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek log_info("two=[%s]", strna(two));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek log_info("three=[%s]", strna(three));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek log_info("four=[%s]", strna(four));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek log_info("five=[%s]", strna(five));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek log_info("six=[%s]", strna(six));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek log_info("seven=[%s]", strna(seven));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek log_info("eight=[%s]", strna(eight));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek log_info("export nine=[%s]", strna(nine));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek log_info("ten=[%s]", strna(nine));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek assert_se(streq(one, "BAR"));
d71839afd88589247d8dd42b2b09d024f521749dZbigniew Jędrzejewski-Szmek assert_se(streq(two, "bar"));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(streq(three, "333\nxxxx"));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(streq(four, "44\"44"));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(streq(five, "55\'55FIVEcinco"));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(streq(six, "seis sechs sis"));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(streq(seven, "sevenval#nocomment"));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(streq(eight, "eightval #nocomment"));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(streq(nine, "nineval"));
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(ten == NULL);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek r = write_env_file("/tmp/test-fileio", a);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(r >= 0);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek r = load_env_file("/tmp/test-fileio", NULL, &b);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek assert_se(r >= 0);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek unlink(t);
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek unlink("/tmp/test-fileio");
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek}
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmekint main(int argc, char *argv[]) {
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek test_parse_env_file();
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek return 0;
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek}
722b6795655149a68277b3cffeba711e1d440e5aZbigniew Jędrzejewski-Szmek