[systemd-commits] 8 commits - .gitignore Makefile.am src/test
Lennart Poettering
lennart at kemper.freedesktop.org
Mon Aug 18 09:45:12 PDT 2014
.gitignore | 1
Makefile.am | 11 ++
src/test/test-async.c | 2
src/test/test-condition-util.c | 107 ++++++++++++++++++++++++++
src/test/test-fdset.c | 29 +++++++
src/test/test-fileio.c | 69 +++++++++++++++++
src/test/test-socket-util.c | 50 ++++++++++++
src/test/test-tables.c | 1
src/test/test-time.c | 23 +++++
src/test/test-util.c | 165 +++++++++++++++++++++++++++++++++++++++++
10 files changed, 457 insertions(+), 1 deletion(-)
New commits:
commit 8aa209ee141b2314e9f1bb502a08c5129bcca3b1
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date: Sat Aug 16 14:19:12 2014 +0200
tests: add missing entry to test-tables
diff --git a/src/test/test-tables.c b/src/test/test-tables.c
index cb9185e..88e7d10 100644
--- a/src/test/test-tables.c
+++ b/src/test/test-tables.c
@@ -86,6 +86,7 @@ int main(int argc, char **argv) {
test_table(policy_item_type, POLICY_ITEM_TYPE);
test_table(protect_home, PROTECT_HOME);
test_table(protect_system, PROTECT_SYSTEM);
+ test_table(rlimit, RLIMIT);
test_table(scope_result, SCOPE_RESULT);
test_table(scope_state, SCOPE_STATE);
test_table(service_exec_command, SERVICE_EXEC_COMMAND);
commit 6accc7a24c76a2658129933ccda7ea1c6993f31e
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date: Sat Aug 16 14:19:11 2014 +0200
tests: add tests for time-util.c
add tests for:
- timezone_is_valid
- get_timezones
diff --git a/src/test/test-time.c b/src/test/test-time.c
index 7c29f96..87e7ae7 100644
--- a/src/test/test-time.c
+++ b/src/test/test-time.c
@@ -20,6 +20,7 @@
***/
#include "time-util.h"
+#include "strv.h"
static void test_parse_sec(void) {
usec_t u;
@@ -126,11 +127,33 @@ static void test_format_timespan(usec_t accuracy) {
test_format_timespan_one(9*USEC_PER_YEAR/5 - 23, accuracy);
}
+static void test_timezone_is_valid(void) {
+ assert_se(timezone_is_valid("Europe/Berlin"));
+ assert_se(timezone_is_valid("Australia/Sydney"));
+ assert_se(!timezone_is_valid("Europe/Do not exist"));
+}
+
+static void test_get_timezones(void) {
+ _cleanup_strv_free_ char **zones = NULL;
+ int r;
+ char **zone;
+
+ r = get_timezones(&zones);
+ assert_se(r == 0);
+
+ STRV_FOREACH(zone, zones) {
+ assert_se(timezone_is_valid(*zone));
+ }
+}
+
int main(int argc, char *argv[]) {
test_parse_sec();
test_parse_nsec();
test_format_timespan(1);
test_format_timespan(USEC_PER_MSEC);
test_format_timespan(USEC_PER_SEC);
+ test_timezone_is_valid();
+ test_get_timezones();
+
return 0;
}
commit b08f2be60aceb0c260fb232b9e8b950f0c871cb9
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date: Sat Aug 16 14:19:10 2014 +0200
tests: add test-condition-util
diff --git a/.gitignore b/.gitignore
index 6f90524..a3d8c4e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -153,6 +153,7 @@
/test-cgroup-util
/test-compress
/test-compress-benchmark
+/test-condition-util
/test-conf-files
/test-coredump-vacuum
/test-daemon
diff --git a/Makefile.am b/Makefile.am
index b9deaa6..3ef9c24 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1321,7 +1321,8 @@ tests += \
test-conf-files \
test-capability \
test-async \
- test-ratelimit
+ test-ratelimit \
+ test-condition-util
EXTRA_DIST += \
test/a.service \
@@ -1460,6 +1461,14 @@ test_async_SOURCES = \
test_async_LDADD = \
libsystemd-shared.la
+test_condition_util_SOURCES = \
+ src/test/test-condition-util.c
+
+test_condition_util_LDADD = \
+ libsystemd-shared.la \
+ libsystemd-internal.la
+
+
test_fdset_SOURCES = \
src/test/test-fdset.c
diff --git a/src/test/test-condition-util.c b/src/test/test-condition-util.c
new file mode 100644
index 0000000..4ee5600
--- /dev/null
+++ b/src/test/test-condition-util.c
@@ -0,0 +1,107 @@
+/***
+ This file is part of systemd
+
+ Copyright 2014 Ronny Chevalier
+
+ systemd is free software; you can redistribute it and/or modify it
+ 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 "condition-util.h"
+#include "macro.h"
+#include "util.h"
+#include "log.h"
+#include "architecture.h"
+#include "systemd/sd-id128.h"
+
+static void test_condition_test_ac_power(void) {
+ Condition *condition;
+
+ condition = condition_new(CONDITION_AC_POWER, "true", false, false);
+ assert_se(condition_test_ac_power(condition) == on_ac_power());
+ condition_free(condition);
+
+ condition = condition_new(CONDITION_AC_POWER, "false", false, false);
+ assert_se(condition_test_ac_power(condition) != on_ac_power());
+ condition_free(condition);
+
+ condition = condition_new(CONDITION_AC_POWER, "false", false, true);
+ assert_se(condition_test_ac_power(condition) == on_ac_power());
+ condition_free(condition);
+}
+
+static void test_condition_test_host(void) {
+ Condition *condition;
+ sd_id128_t id;
+ int r;
+ char sid[SD_ID128_STRING_MAX];
+ char *hostname;
+
+ r = sd_id128_get_machine(&id);
+ assert_se(r >= 0);
+ assert_se(sd_id128_to_string(id, sid));
+
+ condition = condition_new(CONDITION_HOST, sid, false, false);
+ assert_se(condition_test_host(condition));
+ condition_free(condition);
+
+ condition = condition_new(CONDITION_HOST, "garbage value jjjjjjjjjjjjjj", false, false);
+ assert_se(!condition_test_host(condition));
+ condition_free(condition);
+
+ condition = condition_new(CONDITION_HOST, sid, false, true);
+ assert_se(!condition_test_host(condition));
+ condition_free(condition);
+
+ hostname = gethostname_malloc();
+ assert_se(hostname);
+
+ condition = condition_new(CONDITION_HOST, hostname, false, false);
+ assert_se(condition_test_host(condition));
+ condition_free(condition);
+}
+
+static void test_condition_test_architecture(void) {
+ Condition *condition;
+ Architecture a;
+ const char *sa;
+
+ a = uname_architecture();
+ assert_se(a >= 0);
+
+ sa = architecture_to_string(a);
+ assert_se(sa);
+
+ condition = condition_new(CONDITION_ARCHITECTURE, sa, false, false);
+ assert_se(condition_test_architecture(condition));
+ condition_free(condition);
+
+ condition = condition_new(CONDITION_ARCHITECTURE, "garbage value", false, false);
+ assert_se(!condition_test_architecture(condition));
+ condition_free(condition);
+
+ condition = condition_new(CONDITION_ARCHITECTURE, sa, false, true);
+ assert_se(!condition_test_architecture(condition));
+ condition_free(condition);
+}
+
+int main(int argc, char *argv[]) {
+ log_parse_environment();
+ log_open();
+
+ test_condition_test_ac_power();
+ test_condition_test_host();
+ test_condition_test_architecture();
+
+ return 0;
+}
commit 8852362bfc39a3e3bcad9a3287a61ff6587369ef
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date: Sat Aug 16 14:19:09 2014 +0200
tests: add tests for util.c
add tests for:
- is_symlink
- pid_is_unwaited
- pid_is_alive
- search_and_fopen
- search_and_fopen_nulstr
- glob_exists
- execute_directory
diff --git a/src/test/test-util.c b/src/test/test-util.c
index a8fa48a..e876248 100644
--- a/src/test/test-util.c
+++ b/src/test/test-util.c
@@ -27,6 +27,7 @@
#include <errno.h>
#include <signal.h>
#include <math.h>
+#include <sys/wait.h>
#include "util.h"
#include "mkdir.h"
@@ -943,6 +944,163 @@ static void test_strappenda(void) {
assert_se(streq(actual, "foobarbaz"));
}
+static void test_is_symlink(void) {
+ char name[] = "/tmp/test-is_symlink.XXXXXX";
+ char name_link[] = "/tmp/test-is_symlink.link";
+ _cleanup_close_ int fd = -1;
+
+ fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
+ assert_se(fd >= 0);
+ assert_se(symlink(name, name_link) >= 0);
+
+ assert_se(is_symlink(name) == 0);
+ assert_se(is_symlink(name_link) == 1);
+ assert_se(is_symlink("/a/file/which/does/not/exist/i/guess") < 0);
+
+
+ unlink(name);
+ unlink(name_link);
+}
+
+static void test_pid_is_unwaited(void) {
+ pid_t pid;
+
+ pid = fork();
+ assert_se(pid >= 0);
+ if (pid == 0) {
+ _exit(EXIT_SUCCESS);
+ } else {
+ int status;
+
+ waitpid(pid, &status, 0);
+ assert_se(!pid_is_unwaited(pid));
+ }
+ assert_se(pid_is_unwaited(getpid()));
+ assert_se(!pid_is_unwaited(-1));
+}
+
+static void test_pid_is_alive(void) {
+ pid_t pid;
+
+ pid = fork();
+ assert_se(pid >= 0);
+ if (pid == 0) {
+ _exit(EXIT_SUCCESS);
+ } else {
+ int status;
+
+ waitpid(pid, &status, 0);
+ assert_se(!pid_is_alive(pid));
+ }
+ assert_se(pid_is_alive(getpid()));
+ assert_se(!pid_is_alive(-1));
+}
+
+static void test_search_and_fopen(void) {
+ const char *dirs[] = {"/tmp/foo/bar", "/tmp", NULL};
+ char name[] = "/tmp/test-search_and_fopen.XXXXXX";
+ int fd = -1;
+ int r;
+ FILE *f;
+
+ fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
+ assert_se(fd >= 0);
+ close(fd);
+
+ r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
+ assert_se(r >= 0);
+ fclose(f);
+
+ r = search_and_fopen(name, "r", NULL, dirs, &f);
+ assert_se(r >= 0);
+ fclose(f);
+
+ r = search_and_fopen(basename(name), "r", "/", dirs, &f);
+ assert_se(r >= 0);
+ fclose(f);
+
+ r = search_and_fopen("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
+ assert_se(r < 0);
+ r = search_and_fopen("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
+ assert_se(r < 0);
+
+ r = unlink(name);
+ assert_se(r == 0);
+
+ r = search_and_fopen(basename(name), "r", NULL, dirs, &f);
+ assert_se(r < 0);
+}
+
+
+static void test_search_and_fopen_nulstr(void) {
+ const char dirs[] = "/tmp/foo/bar\0/tmp\0";
+ char name[] = "/tmp/test-search_and_fopen.XXXXXX";
+ int fd = -1;
+ int r;
+ FILE *f;
+
+ fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
+ assert_se(fd >= 0);
+ close(fd);
+
+ r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
+ assert_se(r >= 0);
+ fclose(f);
+
+ r = search_and_fopen_nulstr(name, "r", NULL, dirs, &f);
+ assert_se(r >= 0);
+ fclose(f);
+
+ r = search_and_fopen_nulstr("/a/file/which/does/not/exist/i/guess", "r", NULL, dirs, &f);
+ assert_se(r < 0);
+ r = search_and_fopen_nulstr("afilewhichdoesnotexistiguess", "r", NULL, dirs, &f);
+ assert_se(r < 0);
+
+ r = unlink(name);
+ assert_se(r == 0);
+
+ r = search_and_fopen_nulstr(basename(name), "r", NULL, dirs, &f);
+ assert_se(r < 0);
+}
+
+static void test_glob_exists(void) {
+ char name[] = "/tmp/test-glob_exists.XXXXXX";
+ int fd = -1;
+ int r;
+
+ fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
+ assert_se(fd >= 0);
+ close(fd);
+
+ r = glob_exists("/tmp/test-glob_exists*");
+ assert_se(r == 1);
+
+ r = unlink(name);
+ assert_se(r == 0);
+ r = glob_exists("/tmp/test-glob_exists*");
+ assert_se(r == 0);
+}
+
+static void test_execute_directory(void) {
+ char name[] = "/tmp/test-execute_directory/script1";
+ char name2[] = "/tmp/test-execute_directory/script2";
+ char name3[] = "/tmp/test-execute_directory/useless";
+ char tempdir[] = "/tmp/test-execute_directory/";
+
+ assert_se(mkdir_safe(tempdir, 0755, getuid(), getgid()) >= 0);
+ assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch /tmp/test-execute_directory/it_works") == 0);
+ assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch /tmp/test-execute_directory/it_works2") == 0);
+ assert_se(chmod(name, 0755) == 0);
+ assert_se(chmod(name2, 0755) == 0);
+ assert_se(touch(name3) >= 0);
+
+ execute_directory(tempdir, NULL, DEFAULT_TIMEOUT_USEC, NULL);
+ assert_se(access("/tmp/test-execute_directory/it_works", F_OK) >= 0);
+ assert_se(access("/tmp/test-execute_directory/it_works2", F_OK) >= 0);
+
+ rm_rf_dangerous(tempdir, false, true, false);
+}
+
int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
@@ -1003,6 +1161,13 @@ int main(int argc, char *argv[]) {
test_ignore_signals();
test_strshorten();
test_strappenda();
+ test_is_symlink();
+ test_pid_is_unwaited();
+ test_pid_is_alive();
+ test_search_and_fopen();
+ test_search_and_fopen_nulstr();
+ test_glob_exists();
+ test_execute_directory();
return 0;
}
commit d7aeffea144c2c6bfee4e63131bb2b6c460de678
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date: Sat Aug 16 14:19:08 2014 +0200
tests: add test for fdset_iterate
diff --git a/src/test/test-fdset.c b/src/test/test-fdset.c
index 3b77415..91df7eb 100644
--- a/src/test/test-fdset.c
+++ b/src/test/test-fdset.c
@@ -126,12 +126,41 @@ static void test_fdset_remove(void) {
unlink(name);
}
+static void test_fdset_iterate(void) {
+ int fd = -1;
+ FDSet *fdset = NULL;
+ char name[] = "/tmp/test-fdset_iterate.XXXXXX";
+ Iterator i;
+ int c = 0;
+ int a;
+
+ fd = mkostemp_safe(name, O_RDWR|O_CLOEXEC);
+ assert_se(fd >= 0);
+
+ fdset = fdset_new();
+ assert_se(fdset);
+ assert_se(fdset_put(fdset, fd) >= 0);
+ assert_se(fdset_put(fdset, fd) >= 0);
+ assert_se(fdset_put(fdset, fd) >= 0);
+
+ FDSET_FOREACH(a, fdset, i) {
+ c++;
+ assert_se(a == fd);
+ }
+ assert_se(c == 1);
+
+ fdset_free(fdset);
+
+ unlink(name);
+}
+
int main(int argc, char *argv[]) {
test_fdset_new_fill();
test_fdset_put_dup();
test_fdset_cloexec();
test_fdset_close_others();
test_fdset_remove();
+ test_fdset_iterate();
return 0;
}
commit 0709b743749c42f54ffc0d9aeacf7bff45d65af6
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date: Sat Aug 16 14:19:07 2014 +0200
tests: add tests for fileio.c
add tests for:
- write_string_stream
- write_string_file
- sendfile_full
diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c
index 1de59fa..e69706c 100644
--- a/src/test/test-fileio.c
+++ b/src/test/test-fileio.c
@@ -290,6 +290,72 @@ static void test_capeff(void) {
}
}
+static void test_write_string_stream(void) {
+ char fn[] = "/tmp/test-write_string_stream-XXXXXX";
+ _cleanup_fclose_ FILE *f = NULL;
+ int fd;
+ char buf[64];
+
+ fd = mkostemp_safe(fn, O_RDWR);
+ assert_se(fd >= 0);
+
+ f = fdopen(fd, "r");
+ assert_se(f);
+ assert_se(write_string_stream(f, "boohoo") < 0);
+
+ f = fdopen(fd, "r+");
+ assert_se(f);
+
+ assert_se(write_string_stream(f, "boohoo") == 0);
+ rewind(f);
+
+ assert_se(fgets(buf, sizeof(buf), f));
+ assert_se(streq(buf, "boohoo\n"));
+
+ unlink(fn);
+}
+
+static void test_write_string_file(void) {
+ char fn[] = "/tmp/test-write_string_file-XXXXXX";
+ int fd;
+ char buf[64] = {0};
+
+ fd = mkostemp_safe(fn, O_RDWR);
+ assert_se(fd >= 0);
+
+ assert_se(write_string_file(fn, "boohoo") == 0);
+
+ assert_se(read(fd, buf, sizeof(buf)));
+ assert_se(streq(buf, "boohoo\n"));
+
+ unlink(fn);
+}
+
+static void test_sendfile_full(void) {
+ char in_fn[] = "/tmp/test-sendfile_full-XXXXXX";
+ char out_fn[] = "/tmp/test-sendfile_full-XXXXXX";
+ _cleanup_close_ int in_fd = -1;
+ int out_fd;
+ char text[] = "boohoo\nfoo\n\tbar\n";
+ char buf[64] = {0};
+
+ in_fd = mkostemp_safe(in_fn, O_RDWR);
+ assert_se(in_fd >= 0);
+ out_fd = mkostemp_safe(out_fn, O_RDWR);
+ assert_se(out_fd >= 0);
+
+ assert_se(write_string_file(in_fn, text) == 0);
+ assert_se(sendfile_full(out_fd, "/a/file/which/does/not/exist/i/guess") < 0);
+ assert_se(sendfile_full(out_fd, in_fn) == sizeof(text) - 1);
+ assert_se(lseek(out_fd, SEEK_SET, 0) == 0);
+
+ assert_se(read(out_fd, buf, sizeof(buf)) == sizeof(text) - 1);
+ assert_se(streq(buf, text));
+
+ unlink(in_fn);
+ unlink(out_fn);
+}
+
int main(int argc, char *argv[]) {
log_parse_environment();
log_open();
@@ -299,6 +365,9 @@ int main(int argc, char *argv[]) {
test_executable_is_script();
test_status_field();
test_capeff();
+ test_write_string_stream();
+ test_write_string_file();
+ test_sendfile_full();
return 0;
}
commit 1f532d7ef35210f11e75cfcab0535e65a37901f3
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date: Sat Aug 16 14:19:06 2014 +0200
tests: add missing unlink
diff --git a/src/test/test-async.c b/src/test/test-async.c
index c1c535b..401e685 100644
--- a/src/test/test-async.c
+++ b/src/test/test-async.c
@@ -46,5 +46,7 @@ int main(int argc, char *argv[]) {
assert_se(fcntl(fd, F_GETFD) == -1);
assert_se(test_async);
+ unlink(name);
+
return 0;
}
commit 43dc004336ae0c99cac45df20e32dc7aa2e7cd0c
Author: Ronny Chevalier <chevalier.ronny at gmail.com>
Date: Sat Aug 16 14:19:05 2014 +0200
tests: add tests for socket-util.c
add tests for:
- socket_address_is
- socket_address_is_netlink
- sockaddr_equal
diff --git a/src/test/test-socket-util.c b/src/test/test-socket-util.c
index 4e90590..17180db 100644
--- a/src/test/test-socket-util.c
+++ b/src/test/test-socket-util.c
@@ -140,6 +140,24 @@ static void test_socket_address_get_path(void) {
assert_se(streq(socket_address_get_path(&a), "/foo/bar"));
}
+static void test_socket_address_is(void) {
+ SocketAddress a;
+
+ assert_se(socket_address_parse(&a, "192.168.1.1:8888") >= 0);
+ assert_se(socket_address_is(&a, "192.168.1.1:8888", SOCK_STREAM));
+ assert_se(!socket_address_is(&a, "route", SOCK_STREAM));
+ assert_se(!socket_address_is(&a, "192.168.1.1:8888", SOCK_RAW));
+}
+
+static void test_socket_address_is_netlink(void) {
+ SocketAddress a;
+
+ assert_se(socket_address_parse_netlink(&a, "route 10") >= 0);
+ assert_se(socket_address_is_netlink(&a, "route 10"));
+ assert_se(!socket_address_is_netlink(&a, "192.168.1.1:8888"));
+ assert_se(!socket_address_is_netlink(&a, "route 1"));
+}
+
static void test_in_addr_prefix_intersect_one(unsigned f, const char *a, unsigned apl, const char *b, unsigned bpl, int result) {
union in_addr_union ua, ub;
@@ -265,6 +283,34 @@ static void test_nameinfo_pretty(void) {
assert(r == 0);
}
+static void test_sockaddr_equal(void) {
+ union sockaddr_union a = {
+ .in.sin_family = AF_INET,
+ .in.sin_port = 0,
+ .in.sin_addr.s_addr = htonl(INADDR_ANY),
+ };
+ union sockaddr_union b = {
+ .in.sin_family = AF_INET,
+ .in.sin_port = 0,
+ .in.sin_addr.s_addr = htonl(INADDR_ANY),
+ };
+ union sockaddr_union c = {
+ .in.sin_family = AF_INET,
+ .in.sin_port = 0,
+ .in.sin_addr.s_addr = htonl(1234),
+ };
+ union sockaddr_union d = {
+ .in6.sin6_family = AF_INET6,
+ .in6.sin6_port = 0,
+ .in6.sin6_addr = IN6ADDR_ANY_INIT,
+ };
+ assert_se(sockaddr_equal(&a, &a));
+ assert_se(sockaddr_equal(&a, &b));
+ assert_se(sockaddr_equal(&d, &d));
+ assert_se(!sockaddr_equal(&a, &c));
+ assert_se(!sockaddr_equal(&b, &c));
+}
+
int main(int argc, char *argv[]) {
log_set_max_level(LOG_DEBUG);
@@ -273,11 +319,15 @@ int main(int argc, char *argv[]) {
test_socket_address_parse_netlink();
test_socket_address_equal();
test_socket_address_get_path();
+ test_socket_address_is();
+ test_socket_address_is_netlink();
test_in_addr_prefix_intersect();
test_in_addr_prefix_next();
test_nameinfo_pretty();
+ test_sockaddr_equal();
+
return 0;
}
More information about the systemd-commits
mailing list