test-fileio.c revision 69ab80881552d5f79ca95f6b3be48ad122ab1ec2
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2013 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include "util.h"
#include "fileio.h"
#include "strv.h"
#include "env-util.h"
static void test_parse_env_file(void) {
char t[] = "/tmp/test-fileio-in-XXXXXX",
p[] = "/tmp/test-fileio-out-XXXXXX";
int fd, r;
FILE *f;
char **i;
unsigned k;
assert_se(f);
fputs("one=BAR \n"
"# comment\n"
" # comment \n"
" ; comment \n"
" two = bar \n"
"invalid line\n"
"invalid line #comment\n"
"three = \"333\n"
"xxxx\"\n"
"four = \'44\\\"44\'\n"
"five = \'55\\\'55\' \"FIVE\" cinco \n"
"six = seis sechs\\\n"
" sis\n"
"seven=\"sevenval\" #nocomment\n"
"eight=eightval #nocomment\n"
"export nine=nineval\n"
"ten=", f);
fflush(f);
fclose(f);
r = load_env_file(t, NULL, &a);
assert_se(r >= 0);
STRV_FOREACH(i, a)
log_info("Got: <%s>", *i);
strv_env_clean_log(a, "test");
k = 0;
STRV_FOREACH(i, b) {
log_info("Got2: <%s>", *i);
}
r = parse_env_file(
t, NULL,
"one", &one,
"two", &two,
"three", &three,
"four", &four,
"five", &five,
"six", &six,
"seven", &seven,
"eight", &eight,
"export nine", &nine,
"ten", &ten,
NULL);
assert_se(r >= 0);
r = write_env_file(p, a);
assert_se(r >= 0);
r = load_env_file(p, NULL, &b);
assert_se(r >= 0);
unlink(t);
unlink(p);
}
static void test_parse_multiline_env_file(void) {
char t[] = "/tmp/test-fileio-in-XXXXXX",
p[] = "/tmp/test-fileio-out-XXXXXX";
int fd, r;
FILE *f;
char **i;
assert_se(f);
fputs("one=BAR\\\n"
" VAR\\\n"
"\tGAR\n"
"#comment\n"
"two=\"bar\\\n"
" var\\\n"
"\tgar\"\n"
"#comment\n"
"tri=\"bar \\\n"
" var \\\n"
"\tgar \"\n", f);
fflush(f);
fclose(f);
r = load_env_file(t, NULL, &a);
assert_se(r >= 0);
STRV_FOREACH(i, a)
log_info("Got: <%s>", *i);
r = write_env_file(p, a);
assert_se(r >= 0);
r = load_env_file(p, NULL, &b);
assert_se(r >= 0);
unlink(t);
unlink(p);
}
static void test_executable_is_script(void) {
char t[] = "/tmp/test-executable-XXXXXX";
int fd, r;
FILE *f;
char *command;
assert_se(f);
fflush(f);
r = executable_is_script(t, &command);
assert_se(r > 0);
assert_se(r == 0);
if (r > 0) {
}
fclose(f);
unlink(t);
}
static void test_status_field(void) {
puts(t);
puts(p);
puts(s);
}
return 0;
}