dcavalca / rpms / systemd

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