summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorYu Watanabe <watanabe.yu+github@gmail.com>2018-11-29 16:28:33 +0100
committerYu Watanabe <watanabe.yu+github@gmail.com>2018-12-02 06:13:47 +0100
commitdca2309108721ebda0512a974e4542f4b0cf5587 (patch)
treef9ca0952a4d551a043f7857ee3139848e1c5268e /src
parentutil: introduce parse_ip_protocol() (diff)
downloadsystemd-dca2309108721ebda0512a974e4542f4b0cf5587.tar.xz
systemd-dca2309108721ebda0512a974e4542f4b0cf5587.zip
test: add tests for ip_protocol_{from,to}_name()
Diffstat (limited to 'src')
-rw-r--r--src/shared/meson.build3
-rw-r--r--src/test/meson.build5
-rw-r--r--src/test/test-ip-protocol-list.c64
3 files changed, 71 insertions, 1 deletions
diff --git a/src/shared/meson.build b/src/shared/meson.build
index ea8f959129..1ad3de41e8 100644
--- a/src/shared/meson.build
+++ b/src/shared/meson.build
@@ -225,7 +225,8 @@ target2 = custom_target(
command : [awk, '-f', '@INPUT0@', '@INPUT1@'],
capture : true)
-shared_sources += [target1, target2]
+shared_generated_gperf_headers = [target1, target2]
+shared_sources += shared_generated_gperf_headers
libshared_name = 'systemd-shared-@0@'.format(meson.project_version())
diff --git a/src/test/meson.build b/src/test/meson.build
index e29e27d63c..f1115d7b64 100644
--- a/src/test/meson.build
+++ b/src/test/meson.build
@@ -646,6 +646,11 @@ tests += [
[],
[]],
+ [['src/test/test-ip-protocol-list.c',
+ shared_generated_gperf_headers],
+ [],
+ []],
+
[['src/test/test-journal-importer.c'],
[],
[]],
diff --git a/src/test/test-ip-protocol-list.c b/src/test/test-ip-protocol-list.c
new file mode 100644
index 0000000000..79390e5289
--- /dev/null
+++ b/src/test/test-ip-protocol-list.c
@@ -0,0 +1,64 @@
+/* SPDX-License-Identifier: LGPL-2.1+ */
+
+#include <netinet/in.h>
+
+#include "macro.h"
+#include "ip-protocol-list.h"
+#include "stdio-util.h"
+#include "string-util.h"
+
+static void test_int(int i) {
+ char str[DECIMAL_STR_MAX(int)];
+
+ assert_se(ip_protocol_from_name(ip_protocol_to_name(i)) == i);
+
+ xsprintf(str, "%i", i);
+ assert_se(ip_protocol_from_name(ip_protocol_to_name(parse_ip_protocol(str))) == i);
+}
+
+static void test_int_fail(int i) {
+ char str[DECIMAL_STR_MAX(int)];
+
+ assert_se(!ip_protocol_to_name(i));
+
+ xsprintf(str, "%i", i);
+ assert_se(parse_ip_protocol(str) == -EINVAL);
+}
+
+static void test_str(const char *s) {
+ assert_se(streq(ip_protocol_to_name(ip_protocol_from_name(s)), s));
+ assert_se(streq(ip_protocol_to_name(parse_ip_protocol(s)), s));
+}
+
+static void test_str_fail(const char *s) {
+ assert_se(ip_protocol_from_name(s) == -EINVAL);
+ assert_se(parse_ip_protocol(s) == -EINVAL);
+}
+
+static void test_parse_ip_protocol(const char *s, int expected) {
+ assert_se(parse_ip_protocol(s) == expected);
+}
+
+int main(int argc, const char *argv[]) {
+ test_int(IPPROTO_TCP);
+ test_int(IPPROTO_DCCP);
+ test_int_fail(-1);
+ test_int_fail(1024 * 1024);
+
+ test_str("sctp");
+ test_str("udp");
+ test_str_fail("hoge");
+ test_str_fail("-1");
+ test_str_fail("1000000000");
+
+ test_parse_ip_protocol("sctp", IPPROTO_SCTP);
+ test_parse_ip_protocol("ScTp", IPPROTO_SCTP);
+ test_parse_ip_protocol("ip", IPPROTO_IP);
+ test_parse_ip_protocol("", IPPROTO_IP);
+ test_parse_ip_protocol("1", 1);
+ test_parse_ip_protocol("0", 0);
+ test_parse_ip_protocol("-10", -EINVAL);
+ test_parse_ip_protocol("100000000", -EINVAL);
+
+ return 0;
+}