richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
698723
From aff5197c5f06617b1ca0291614a0fb02c0c2e948 Mon Sep 17 00:00:00 2001
698723
From: Yu Watanabe <watanabe.yu+github@gmail.com>
698723
Date: Sun, 15 Dec 2019 21:48:12 +0900
698723
Subject: [PATCH] test: add a test for sd_netlink_message_{append,read}_strv()
698723
698723
(cherry picked from commit d08d92d5ee508a80e35d6b95b962bd09527fb5f2)
698723
698723
Related: #1850986
698723
---
698723
 src/libsystemd/sd-netlink/test-netlink.c | 33 ++++++++++++++++++++++++
698723
 1 file changed, 33 insertions(+)
698723
698723
diff --git a/src/libsystemd/sd-netlink/test-netlink.c b/src/libsystemd/sd-netlink/test-netlink.c
698723
index 03773fb936..8ee6551385 100644
698723
--- a/src/libsystemd/sd-netlink/test-netlink.c
698723
+++ b/src/libsystemd/sd-netlink/test-netlink.c
698723
@@ -10,7 +10,9 @@
698723
 #include "missing.h"
698723
 #include "netlink-util.h"
698723
 #include "socket-util.h"
698723
+#include "stdio-util.h"
698723
 #include "string-util.h"
698723
+#include "strv.h"
698723
 #include "util.h"
698723
 
698723
 static void test_message_link_bridge(sd_netlink *rtnl) {
698723
@@ -357,6 +359,36 @@ static void test_message(sd_netlink *rtnl) {
698723
         assert_se(sd_netlink_message_get_errno(m) == -ETIMEDOUT);
698723
 }
698723
 
698723
+static void test_strv(sd_netlink *rtnl) {
698723
+        _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
698723
+        _cleanup_strv_free_ char **names_in = NULL, **names_out;
698723
+        const char *p;
698723
+
698723
+        assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINKPROP, 1) >= 0);
698723
+
698723
+        for (unsigned i = 0; i < 10; i++) {
698723
+                char name[STRLEN("hoge") + DECIMAL_STR_MAX(uint32_t)];
698723
+
698723
+                xsprintf(name, "hoge%" PRIu32, i + 1000);
698723
+                assert_se(strv_extend(&names_in, name) >= 0);
698723
+        }
698723
+
698723
+        assert_se(sd_netlink_message_open_container(m, IFLA_PROP_LIST) >= 0);
698723
+        assert_se(sd_netlink_message_append_strv(m, IFLA_ALT_IFNAME, names_in) >= 0);
698723
+        assert_se(sd_netlink_message_close_container(m) >= 0);
698723
+
698723
+        rtnl_message_seal(m);
698723
+        assert_se(sd_netlink_message_rewind(m) >= 0);
698723
+
698723
+        assert_se(sd_netlink_message_read_strv(m, IFLA_PROP_LIST, IFLA_ALT_IFNAME, &names_out) >= 0);
698723
+        assert_se(strv_equal(names_in, names_out));
698723
+
698723
+        assert_se(sd_netlink_message_enter_container(m, IFLA_PROP_LIST) >= 0);
698723
+        assert_se(sd_netlink_message_read_string(m, IFLA_ALT_IFNAME, &p) >= 0);
698723
+        assert_se(streq(p, "hoge1009"));
698723
+        assert_se(sd_netlink_message_exit_container(m) >= 0);
698723
+}
698723
+
698723
 int main(void) {
698723
         sd_netlink *rtnl;
698723
         sd_netlink_message *m;
698723
@@ -377,6 +409,7 @@ int main(void) {
698723
         test_message(rtnl);
698723
 
698723
         test_container(rtnl);
698723
+        test_strv(rtnl);
698723
 
698723
         if_loopback = (int) if_nametoindex("lo");
698723
         assert_se(if_loopback > 0);