From da8ba7219aefc174da48b21047d8236e9d0bfe66 Mon Sep 17 00:00:00 2001 From: Daniel Mack Date: Thu, 30 Jul 2015 21:07:25 +0200 Subject: test: add test for static lists Test af-list and arphdr-list. --- src/test/test-af-list.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/test/test-arphrd-list.c | 48 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/test/test-af-list.c create mode 100644 src/test/test-arphrd-list.c (limited to 'src') diff --git a/src/test/test-af-list.c b/src/test/test-af-list.c new file mode 100644 index 0000000000..d69104f540 --- /dev/null +++ b/src/test/test-af-list.c @@ -0,0 +1,48 @@ +/*** + This file is part of systemd + + Copyright 2015 Daniel Mack + + 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 . +***/ + +#include +#include + +#include "macro.h" +#include "util.h" + +static const struct af_name* lookup_af(register const char *str, register unsigned int len); + +#include "af-list.h" +#include "af-to-name.h" +#include "af-from-name.h" + +int main(int argc, const char *argv[]) { + + unsigned int i; + + for (i = 0; i < ELEMENTSOF(af_names); i++) { + if (af_names[i]) { + assert_se(streq(af_to_name(i), af_names[i])); + assert_se(af_from_name(af_names[i]) == (int) i); + } + } + + assert_se(af_to_name(af_max()) == NULL); + assert_se(af_to_name(-1) == NULL); + assert_se(af_from_name("huddlduddl") == AF_UNSPEC); + + return 0; +} \ No newline at end of file diff --git a/src/test/test-arphrd-list.c b/src/test/test-arphrd-list.c new file mode 100644 index 0000000000..d7c8eaa4a9 --- /dev/null +++ b/src/test/test-arphrd-list.c @@ -0,0 +1,48 @@ +/*** + This file is part of systemd + + Copyright 2015 Daniel Mack + + 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 . +***/ + +#include +#include + +#include "macro.h" +#include "util.h" + +static const struct arphrd_name* lookup_arphrd(register const char *str, register unsigned int len); + +#include "arphrd-list.h" +#include "arphrd-to-name.h" +#include "arphrd-from-name.h" + +int main(int argc, const char *argv[]) { + + unsigned int i; + + for (i = 1; i < ELEMENTSOF(arphrd_names); i++) { + if (arphrd_names[i]) { + assert_se(streq(arphrd_to_name(i), arphrd_names[i])); + assert_se(arphrd_from_name(arphrd_names[i]) == (int) i); + } + } + + assert_se(arphrd_to_name(arphrd_max()) == NULL); + assert_se(arphrd_to_name(0) == NULL); + assert_se(arphrd_from_name("huddlduddl") == 0); + + return 0; +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf From ff9265d0898a707cb19cbc631289ff4f41974415 Mon Sep 17 00:00:00 2001 From: Daniel Mack Date: Thu, 30 Jul 2015 22:10:05 +0200 Subject: test-util: add more tests Add tests for safe_ato[iu]16() and some more unbase32hexmem() torture. --- src/test/test-util.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'src') diff --git a/src/test/test-util.c b/src/test/test-util.c index 7a1a4b6df2..f4002aa86e 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -280,6 +280,39 @@ static void test_parse_uid(void) { r = parse_uid("100", &uid); assert_se(r == 0); assert_se(uid == 100); + + r = parse_uid("65535", &uid); + assert_se(r == -ENXIO); +} + +static void test_safe_atou16(void) { + int r; + uint16_t l; + + r = safe_atou16("12345", &l); + assert_se(r == 0); + assert_se(l == 12345); + + r = safe_atou16("123456", &l); + assert_se(r == -ERANGE); + + r = safe_atou16("junk", &l); + assert_se(r == -EINVAL); +} + +static void test_safe_atoi16(void) { + int r; + int16_t l; + + r = safe_atoi16("-12345", &l); + assert_se(r == 0); + assert_se(l == -12345); + + r = safe_atoi16("36536", &l); + assert_se(r == -ERANGE); + + r = safe_atoi16("junk", &l); + assert_se(r == -EINVAL); } static void test_safe_atolli(void) { @@ -583,6 +616,15 @@ static void test_unbase32hexmem(void) { assert_se(unbase32hexmem("AAAAB===", strlen("AAAAB==="), true, &mem, &len) == -EINVAL); assert_se(unbase32hexmem("AAAAAAB=", strlen("AAAAAAB="), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("XPNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CXNMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPXMUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPNXUOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPNMXOJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPNMUXJ1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPNMUOX1", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("CPNMUOJX", strlen("CPNMUOJ1"), true, &mem, &len) == -EINVAL); + assert_se(unbase32hexmem("", strlen(""), false, &mem, &len) == 0); assert_se(streq(strndupa(mem, len), "")); free(mem); @@ -1260,6 +1302,16 @@ static void test_endswith(void) { assert_se(!endswith("foobar", "foobarfoofoo")); } +static void test_endswith_no_case(void) { + assert_se(endswith_no_case("fooBAR", "bar")); + assert_se(endswith_no_case("foobar", "")); + assert_se(endswith_no_case("foobar", "FOOBAR")); + assert_se(endswith_no_case("", "")); + + assert_se(!endswith_no_case("foobar", "FOO")); + assert_se(!endswith_no_case("foobar", "FOOBARFOOFOO")); +} + static void test_close_nointr(void) { char name[] = "/tmp/test-test-close_nointr.XXXXXX"; int fd; @@ -2110,6 +2162,8 @@ int main(int argc, char *argv[]) { test_parse_boolean(); test_parse_pid(); test_parse_uid(); + test_safe_atou16(); + test_safe_atoi16(); test_safe_atolli(); test_safe_atod(); test_strappend(); @@ -2159,6 +2213,7 @@ int main(int argc, char *argv[]) { test_is_valid_documentation_url(); test_file_in_same_dir(); test_endswith(); + test_endswith_no_case(); test_close_nointr(); test_unlink_noerrno(); test_readlink_and_make_absolute(); -- cgit v1.2.3-54-g00ecf From e76398868a154258c3620be2bdecdd9fa7cdf093 Mon Sep 17 00:00:00 2001 From: Daniel Mack Date: Thu, 30 Jul 2015 22:29:59 +0200 Subject: test-socket-util: add test for in_addr_is_null() --- src/test/test-socket-util.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src') diff --git a/src/test/test-socket-util.c b/src/test/test-socket-util.c index f257af445a..2c18090ae5 100644 --- a/src/test/test-socket-util.c +++ b/src/test/test-socket-util.c @@ -158,6 +158,20 @@ static void test_socket_address_is_netlink(void) { assert_se(!socket_address_is_netlink(&a, "route 1")); } +static void test_in_addr_is_null(void) { + + union in_addr_union i = {}; + + assert_se(in_addr_is_null(AF_INET, &i) == true); + assert_se(in_addr_is_null(AF_INET6, &i) == true); + + i.in.s_addr = 0x1000000; + assert_se(in_addr_is_null(AF_INET, &i) == false); + assert_se(in_addr_is_null(AF_INET6, &i) == false); + + assert_se(in_addr_is_null(-1, &i) == -EAFNOSUPPORT); +} + 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; @@ -340,6 +354,7 @@ int main(int argc, char *argv[]) { test_socket_address_is(); test_socket_address_is_netlink(); + test_in_addr_is_null(); test_in_addr_prefix_intersect(); test_in_addr_prefix_next(); test_in_addr_to_string(); -- cgit v1.2.3-54-g00ecf