daandemeyer / rpms / systemd

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