1
0
mirror of https://github.com/systemd/systemd.git synced 2025-01-21 22:04:01 +03:00

Merge pull request #25986 from yuwata/test-netlink

test-netlink: cleanups
This commit is contained in:
Luca Boccassi 2023-01-10 21:21:24 +00:00 committed by GitHub
commit c32b049dfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -24,11 +24,12 @@
#include "strv.h"
#include "tests.h"
static void test_message_link_bridge(sd_netlink *rtnl) {
TEST(message_newlink_bridge) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
uint32_t cost;
log_debug("/* %s */", __func__);
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_NEWLINK, 1) >= 0);
assert_se(sd_rtnl_message_link_set_family(message, AF_BRIDGE) >= 0);
@ -44,99 +45,81 @@ static void test_message_link_bridge(sd_netlink *rtnl) {
assert_se(sd_netlink_message_exit_container(message) >= 0);
}
static void test_link_configure(sd_netlink *rtnl, int ifindex) {
TEST(message_getlink) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL, *reply = NULL;
uint32_t mtu_out;
const char *name_out;
struct ether_addr mac_out;
int ifindex;
uint8_t u8_data;
uint16_t u16_data;
uint32_t u32_data;
const char *str_data;
struct ether_addr eth_data;
log_debug("/* %s */", __func__);
assert_se(sd_netlink_open(&rtnl) >= 0);
ifindex = (int) if_nametoindex("lo");
/* we'd really like to test NEWLINK, but let's not mess with the running kernel */
assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_GETLINK, ifindex) >= 0);
assert_se(sd_netlink_call(rtnl, message, 0, &reply) == 1);
assert_se(sd_netlink_message_read_string(reply, IFLA_IFNAME, &name_out) >= 0);
assert_se(sd_netlink_message_read_ether_addr(reply, IFLA_ADDRESS, &mac_out) >= 0);
assert_se(sd_netlink_message_read_u32(reply, IFLA_MTU, &mtu_out) >= 0);
/* u8 */
assert_se(sd_netlink_message_read_u8(reply, IFLA_CARRIER, &u8_data) >= 0);
assert_se(sd_netlink_message_read_u8(reply, IFLA_OPERSTATE, &u8_data) >= 0);
assert_se(sd_netlink_message_read_u8(reply, IFLA_LINKMODE, &u8_data) >= 0);
/* u16 */
assert_se(sd_netlink_message_get_type(reply, &u16_data) >= 0);
assert_se(u16_data == RTM_NEWLINK);
/* u32 */
assert_se(sd_netlink_message_read_u32(reply, IFLA_MTU, &u32_data) >= 0);
assert_se(sd_netlink_message_read_u32(reply, IFLA_GROUP, &u32_data) >= 0);
assert_se(sd_netlink_message_read_u32(reply, IFLA_TXQLEN, &u32_data) >= 0);
assert_se(sd_netlink_message_read_u32(reply, IFLA_NUM_TX_QUEUES, &u32_data) >= 0);
assert_se(sd_netlink_message_read_u32(reply, IFLA_NUM_RX_QUEUES, &u32_data) >= 0);
/* string */
assert_se(sd_netlink_message_read_string(reply, IFLA_IFNAME, &str_data) >= 0);
/* ether_addr */
assert_se(sd_netlink_message_read_ether_addr(reply, IFLA_ADDRESS, &eth_data) >= 0);
}
static void test_link_get(sd_netlink *rtnl, int ifindex) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *r = NULL;
const char *str_data;
uint8_t u8_data;
uint32_t u32_data;
struct ether_addr eth_data;
log_debug("/* %s */", __func__);
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
assert_se(m);
assert_se(sd_netlink_call(rtnl, m, 0, &r) == 1);
assert_se(sd_netlink_message_read_string(r, IFLA_IFNAME, &str_data) == 0);
assert_se(sd_netlink_message_read_u8(r, IFLA_CARRIER, &u8_data) == 0);
assert_se(sd_netlink_message_read_u8(r, IFLA_OPERSTATE, &u8_data) == 0);
assert_se(sd_netlink_message_read_u8(r, IFLA_LINKMODE, &u8_data) == 0);
assert_se(sd_netlink_message_read_u32(r, IFLA_MTU, &u32_data) == 0);
assert_se(sd_netlink_message_read_u32(r, IFLA_GROUP, &u32_data) == 0);
assert_se(sd_netlink_message_read_u32(r, IFLA_TXQLEN, &u32_data) == 0);
assert_se(sd_netlink_message_read_u32(r, IFLA_NUM_TX_QUEUES, &u32_data) == 0);
assert_se(sd_netlink_message_read_u32(r, IFLA_NUM_RX_QUEUES, &u32_data) == 0);
assert_se(sd_netlink_message_read_ether_addr(r, IFLA_ADDRESS, &eth_data) == 0);
}
static void test_address_get(sd_netlink *rtnl, int ifindex) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *r = NULL;
TEST(message_address) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL, *reply = NULL;
int ifindex;
struct in_addr in_data;
struct ifa_cacheinfo cache;
const char *label;
log_debug("/* %s */", __func__);
assert_se(sd_netlink_open(&rtnl) >= 0);
ifindex = (int) if_nametoindex("lo");
assert_se(sd_rtnl_message_new_addr(rtnl, &m, RTM_GETADDR, ifindex, AF_INET) >= 0);
assert_se(m);
assert_se(sd_netlink_message_set_request_dump(m, true) >= 0);
assert_se(sd_netlink_call(rtnl, m, -1, &r) == 1);
assert_se(sd_rtnl_message_new_addr(rtnl, &message, RTM_GETADDR, ifindex, AF_INET) >= 0);
assert_se(sd_netlink_message_set_request_dump(message, true) >= 0);
assert_se(sd_netlink_call(rtnl, message, 0, &reply) == 1);
assert_se(sd_netlink_message_read_in_addr(r, IFA_LOCAL, &in_data) == 0);
assert_se(sd_netlink_message_read_in_addr(r, IFA_ADDRESS, &in_data) == 0);
assert_se(sd_netlink_message_read_string(r, IFA_LABEL, &label) == 0);
assert_se(sd_netlink_message_read_cache_info(r, IFA_CACHEINFO, &cache) == 0);
assert_se(sd_netlink_message_read_in_addr(reply, IFA_LOCAL, &in_data) >= 0);
assert_se(sd_netlink_message_read_in_addr(reply, IFA_ADDRESS, &in_data) >= 0);
assert_se(sd_netlink_message_read_string(reply, IFA_LABEL, &label) >= 0);
assert_se(sd_netlink_message_read_cache_info(reply, IFA_CACHEINFO, &cache) == 0);
}
static void test_route(sd_netlink *rtnl) {
TEST(message_route) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
struct in_addr addr, addr_data;
uint32_t index = 2, u32_data;
int r;
log_debug("/* %s */", __func__);
assert_se(sd_netlink_open(&rtnl) >= 0);
r = sd_rtnl_message_new_route(rtnl, &req, RTM_NEWROUTE, AF_INET, RTPROT_STATIC);
if (r < 0) {
log_error_errno(r, "Could not create RTM_NEWROUTE message: %m");
return;
}
assert_se(sd_rtnl_message_new_route(rtnl, &req, RTM_NEWROUTE, AF_INET, RTPROT_STATIC) >= 0);
addr.s_addr = htobe32(INADDR_LOOPBACK);
r = sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &addr);
if (r < 0) {
log_error_errno(r, "Could not append RTA_GATEWAY attribute: %m");
return;
}
r = sd_netlink_message_append_u32(req, RTA_OIF, index);
if (r < 0) {
log_error_errno(r, "Could not append RTA_OIF attribute: %m");
return;
}
assert_se(sd_netlink_message_append_in_addr(req, RTA_GATEWAY, &addr) >= 0);
assert_se(sd_netlink_message_append_u32(req, RTA_OIF, index) >= 0);
assert_se(sd_netlink_message_rewind(req, rtnl) >= 0);
@ -149,135 +132,94 @@ static void test_route(sd_netlink *rtnl) {
assert_se((req = sd_netlink_message_unref(req)) == NULL);
}
static void test_multiple(void) {
sd_netlink *rtnl1, *rtnl2;
log_debug("/* %s */", __func__);
assert_se(sd_netlink_open(&rtnl1) >= 0);
assert_se(sd_netlink_open(&rtnl2) >= 0);
rtnl1 = sd_netlink_unref(rtnl1);
rtnl2 = sd_netlink_unref(rtnl2);
}
static int link_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) {
char *ifname = userdata;
const char *data;
assert_se(rtnl);
assert_se(m);
assert_se(userdata);
log_info("%s: got link info about %s", __func__, ifname);
free(ifname);
assert_se(streq_ptr(userdata, "foo"));
assert_se(sd_netlink_message_read_string(m, IFLA_IFNAME, &data) >= 0);
assert_se(streq(data, "lo"));
log_info("%s: got link info about %s", __func__, data);
return 1;
}
static void test_event_loop(int ifindex) {
TEST(netlink_event_loop) {
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
char *ifname;
log_debug("/* %s */", __func__);
ifname = strdup("lo2");
assert_se(ifname);
_cleanup_free_ char *userdata = NULL;
int ifindex;
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
ifindex = (int) if_nametoindex("lo");
assert_se(sd_netlink_call_async(rtnl, NULL, m, link_handler, NULL, ifname, 0, NULL) >= 0);
assert_se(userdata = strdup("foo"));
assert_se(sd_event_default(&event) >= 0);
assert_se(sd_netlink_attach_event(rtnl, event, 0) >= 0);
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
assert_se(sd_netlink_call_async(rtnl, NULL, m, link_handler, NULL, userdata, 0, NULL) >= 0);
assert_se(sd_event_run(event, 0) >= 0);
assert_se(sd_netlink_detach_event(rtnl) >= 0);
assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
}
static void test_async_destroy(void *userdata) {
}
static void test_async(int ifindex) {
TEST(netlink_call_async) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *r = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *reply = NULL;
_cleanup_(sd_netlink_slot_unrefp) sd_netlink_slot *slot = NULL;
_cleanup_free_ char *userdata = NULL;
sd_netlink_destroy_t destroy_callback;
const char *description;
char *ifname;
log_debug("/* %s */", __func__);
ifname = strdup("lo");
assert_se(ifname);
int ifindex;
assert_se(sd_netlink_open(&rtnl) >= 0);
ifindex = (int) if_nametoindex("lo");
assert_se(userdata = strdup("foo"));
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
assert_se(sd_netlink_call_async(rtnl, &slot, m, link_handler, test_async_destroy, ifname, 0, "hogehoge") >= 0);
assert_se(sd_netlink_call_async(rtnl, &slot, m, link_handler, test_async_destroy, userdata, 0, "hogehoge") >= 0);
assert_se(sd_netlink_slot_get_netlink(slot) == rtnl);
assert_se(sd_netlink_slot_get_userdata(slot) == ifname);
assert_se(sd_netlink_slot_get_userdata(slot) == userdata);
assert_se(sd_netlink_slot_set_userdata(slot, NULL) == userdata);
assert_se(sd_netlink_slot_get_userdata(slot) == NULL);
assert_se(sd_netlink_slot_set_userdata(slot, userdata) == NULL);
assert_se(sd_netlink_slot_get_userdata(slot) == userdata);
assert_se(sd_netlink_slot_get_destroy_callback(slot, &destroy_callback) == 1);
assert_se(destroy_callback == test_async_destroy);
assert_se(sd_netlink_slot_get_floating(slot) == 0);
assert_se(sd_netlink_slot_get_description(slot, &description) == 1);
assert_se(streq(description, "hogehoge"));
assert_se(sd_netlink_wait(rtnl, 0) >= 0);
assert_se(sd_netlink_process(rtnl, &r) >= 0);
assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
}
static void test_slot_set(int ifindex) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *r = NULL;
_cleanup_(sd_netlink_slot_unrefp) sd_netlink_slot *slot = NULL;
sd_netlink_destroy_t destroy_callback;
const char *description;
char *ifname;
log_debug("/* %s */", __func__);
ifname = strdup("lo");
assert_se(ifname);
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
assert_se(sd_netlink_call_async(rtnl, &slot, m, link_handler, NULL, NULL, 0, NULL) >= 0);
assert_se(sd_netlink_slot_get_netlink(slot) == rtnl);
assert_se(!sd_netlink_slot_get_userdata(slot));
assert_se(!sd_netlink_slot_set_userdata(slot, ifname));
assert_se(sd_netlink_slot_get_userdata(slot) == ifname);
assert_se(sd_netlink_slot_get_destroy_callback(slot, NULL) == 0);
assert_se(sd_netlink_slot_set_destroy_callback(slot, NULL) >= 0);
assert_se(sd_netlink_slot_get_destroy_callback(slot, &destroy_callback) == 0);
assert_se(destroy_callback == NULL);
assert_se(sd_netlink_slot_set_destroy_callback(slot, test_async_destroy) >= 0);
assert_se(sd_netlink_slot_get_destroy_callback(slot, &destroy_callback) == 1);
assert_se(destroy_callback == test_async_destroy);
assert_se(sd_netlink_slot_get_floating(slot) == 0);
assert_se(sd_netlink_slot_set_floating(slot, 1) == 1);
assert_se(sd_netlink_slot_get_floating(slot) == 1);
assert_se(sd_netlink_slot_get_description(slot, NULL) == 0);
assert_se(sd_netlink_slot_set_description(slot, "hogehoge") >= 0);
assert_se(sd_netlink_slot_get_description(slot, &description) == 1);
assert_se(streq(description, "hogehoge"));
assert_se(sd_netlink_slot_set_description(slot, NULL) >= 0);
assert_se(sd_netlink_slot_get_description(slot, &description) == 0);
assert_se(description == NULL);
assert_se(sd_netlink_wait(rtnl, 0) >= 0);
assert_se(sd_netlink_process(rtnl, &r) >= 0);
assert_se(sd_netlink_process(rtnl, &reply) >= 0);
assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
}
@ -322,23 +264,21 @@ static void test_async_object_destroy(void *userdata) {
test_async_object_unref(t);
}
static void test_async_destroy_callback(int ifindex) {
TEST(async_destroy_callback) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *r = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL, *reply = NULL;
_cleanup_(test_async_object_unrefp) struct test_async_object *t = NULL;
_cleanup_(sd_netlink_slot_unrefp) sd_netlink_slot *slot = NULL;
char *ifname;
log_debug("/* %s */", __func__);
assert_se(t = new(struct test_async_object, 1));
assert_se(ifname = strdup("lo"));
*t = (struct test_async_object) {
.n_ref = 1,
.ifname = ifname,
};
int ifindex;
assert_se(sd_netlink_open(&rtnl) >= 0);
ifindex = (int) if_nametoindex("lo");
assert_se(t = new(struct test_async_object, 1));
*t = (struct test_async_object) {
.n_ref = 1,
};
assert_se(t->ifname = strdup("lo"));
/* destroy callback is called after processing message */
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, ifindex) >= 0);
@ -349,7 +289,7 @@ static void test_async_destroy_callback(int ifindex) {
assert_se(t->n_ref == 2);
assert_se(sd_netlink_wait(rtnl, 0) >= 0);
assert_se(sd_netlink_process(rtnl, &r) == 1);
assert_se(sd_netlink_process(rtnl, &reply) == 1);
assert_se(t->n_ref == 1);
assert_se(!sd_netlink_message_unref(m));
@ -394,14 +334,13 @@ static int pipe_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata)
return 1;
}
static void test_pipe(int ifindex) {
TEST(pipe) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m1 = NULL, *m2 = NULL;
int counter = 0;
log_debug("/* %s */", __func__);
int ifindex, counter = 0;
assert_se(sd_netlink_open(&rtnl) >= 0);
ifindex = (int) if_nametoindex("lo");
assert_se(sd_rtnl_message_new_link(rtnl, &m1, RTM_GETLINK, ifindex) >= 0);
assert_se(sd_rtnl_message_new_link(rtnl, &m2, RTM_GETLINK, ifindex) >= 0);
@ -420,13 +359,14 @@ static void test_pipe(int ifindex) {
assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
}
static void test_container(sd_netlink *rtnl) {
TEST(message_container) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
uint16_t u16_data;
uint32_t u32_data;
const char *string_data;
log_debug("/* %s */", __func__);
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINK, 0) >= 0);
@ -434,9 +374,7 @@ static void test_container(sd_netlink *rtnl) {
assert_se(sd_netlink_message_open_container_union(m, IFLA_INFO_DATA, "vlan") >= 0);
assert_se(sd_netlink_message_append_u16(m, IFLA_VLAN_ID, 100) >= 0);
assert_se(sd_netlink_message_close_container(m) >= 0);
assert_se(sd_netlink_message_append_string(m, IFLA_INFO_KIND, "vlan") >= 0);
assert_se(sd_netlink_message_close_container(m) >= 0);
assert_se(sd_netlink_message_close_container(m) == -EINVAL);
assert_se(sd_netlink_message_rewind(m, rtnl) >= 0);
@ -453,16 +391,12 @@ static void test_container(sd_netlink *rtnl) {
assert_se(sd_netlink_message_exit_container(m) >= 0);
assert_se(sd_netlink_message_read_u32(m, IFLA_LINKINFO, &u32_data) < 0);
assert_se(sd_netlink_message_exit_container(m) == -EINVAL);
}
static void test_match(void) {
TEST(sd_netlink_add_match) {
_cleanup_(sd_netlink_slot_unrefp) sd_netlink_slot *s1 = NULL, *s2 = NULL;
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
log_debug("/* %s */", __func__);
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(sd_netlink_add_match(rtnl, &s1, RTM_NEWLINK, link_handler, NULL, NULL, NULL) >= 0);
@ -475,17 +409,17 @@ static void test_match(void) {
assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
}
static void test_get_addresses(sd_netlink *rtnl) {
TEST(dump_addresses) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL, *reply = NULL;
sd_netlink_message *m;
log_debug("/* %s */", __func__);
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(sd_rtnl_message_new_addr(rtnl, &req, RTM_GETADDR, 0, AF_UNSPEC) >= 0);
assert_se(sd_netlink_message_set_request_dump(req, true) >= 0);
assert_se(sd_netlink_call(rtnl, req, 0, &reply) >= 0);
for (m = reply; m; m = sd_netlink_message_next(m)) {
for (sd_netlink_message *m = reply; m; m = sd_netlink_message_next(m)) {
uint16_t type;
unsigned char scope, flags;
int family, ifindex;
@ -505,21 +439,20 @@ static void test_get_addresses(sd_netlink *rtnl) {
}
}
static void test_message(sd_netlink *rtnl) {
TEST(sd_netlink_message_get_errno) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
log_debug("/* %s */", __func__);
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(message_new_synthetic_error(rtnl, -ETIMEDOUT, 1, &m) >= 0);
assert_se(sd_netlink_message_get_errno(m) == -ETIMEDOUT);
}
static void test_array(void) {
TEST(message_array) {
_cleanup_(sd_netlink_unrefp) sd_netlink *genl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
log_debug("/* %s */", __func__);
assert_se(sd_genl_socket_open(&genl) >= 0);
assert_se(sd_genl_message_new(genl, CTRL_GENL_NAME, CTRL_CMD_GETFAMILY, &m) >= 0);
@ -557,12 +490,13 @@ static void test_array(void) {
assert_se(sd_netlink_message_exit_container(m) >= 0);
}
static void test_strv(sd_netlink *rtnl) {
TEST(message_strv) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
_cleanup_strv_free_ char **names_in = NULL, **names_out;
const char *p;
log_debug("/* %s */", __func__);
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_NEWLINKPROP, 1) >= 0);
@ -624,7 +558,7 @@ static int genl_ctrl_match_callback(sd_netlink *genl, sd_netlink_message *m, voi
return 0;
}
static void test_genl(void) {
TEST(genl) {
_cleanup_(sd_event_unrefp) sd_event *event = NULL;
_cleanup_(sd_netlink_unrefp) sd_netlink *genl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *m = NULL;
@ -632,8 +566,6 @@ static void test_genl(void) {
uint8_t cmd;
int r;
log_debug("/* %s */", __func__);
assert_se(sd_genl_socket_open(&genl) >= 0);
assert_se(sd_event_default(&event) >= 0);
assert_se(sd_netlink_attach_event(genl, event, 0) >= 0);
@ -669,92 +601,81 @@ static void test_genl(void) {
}
}
static void test_rtnl_set_link_name(sd_netlink *rtnl, int ifindex) {
static void remove_dummy_interfacep(int *ifindex) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL;
if (!ifindex || *ifindex <= 0)
return;
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_DELLINK, *ifindex) >= 0);
assert_se(sd_netlink_call(rtnl, message, 0, NULL) == 1);
}
TEST(rtnl_set_link_name) {
_cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *message = NULL, *reply = NULL;
_cleanup_(remove_dummy_interfacep) int ifindex = 0;
_cleanup_strv_free_ char **alternative_names = NULL;
int r;
log_debug("/* %s */", __func__);
if (geteuid() != 0)
return (void) log_tests_skipped("not root");
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_NEWLINK, 0) >= 0);
assert_se(sd_netlink_message_append_string(message, IFLA_IFNAME, "test-netlink") >= 0);
assert_se(sd_netlink_message_open_container(message, IFLA_LINKINFO) >= 0);
assert_se(sd_netlink_message_append_string(message, IFLA_INFO_KIND, "dummy") >= 0);
r = sd_netlink_call(rtnl, message, 0, &reply);
if (r == -EPERM)
return (void) log_tests_skipped("missing required capabilities");
if (r == -EOPNOTSUPP)
return (void) log_tests_skipped("dummy network interface is not supported");
assert_se(r >= 0);
message = sd_netlink_message_unref(message);
reply = sd_netlink_message_unref(reply);
assert_se(sd_rtnl_message_new_link(rtnl, &message, RTM_GETLINK, 0) >= 0);
assert_se(sd_netlink_message_append_string(message, IFLA_IFNAME, "test-netlink") >= 0);
assert_se(sd_netlink_call(rtnl, message, 0, &reply) == 1);
assert_se(sd_rtnl_message_link_get_ifindex(reply, &ifindex) >= 0);
assert_se(ifindex > 0);
/* Test that the new name (which is currently an alternative name) is
* restored as an alternative name on error. Create an error by using
* an invalid device name, namely one that exceeds IFNAMSIZ
* (alternative names can exceed IFNAMSIZ, but not regular names). */
r = rtnl_set_link_alternative_names(&rtnl, ifindex, STRV_MAKE("testlongalternativename"));
r = rtnl_set_link_alternative_names(&rtnl, ifindex, STRV_MAKE("testlongalternativename", "test-shortname"));
if (r == -EPERM)
return (void) log_tests_skipped("missing required capabilities");
if (r == -EOPNOTSUPP)
return (void) log_tests_skipped("alternative name is not supported");
assert_se(r >= 0);
assert_se(rtnl_set_link_name(&rtnl, ifindex, "testlongalternativename") == -EINVAL);
assert_se(rtnl_get_link_alternative_names(&rtnl, ifindex, &alternative_names) >= 0);
assert_se(strv_contains(alternative_names, "testlongalternativename"));
assert_se(strv_contains(alternative_names, "test-shortname"));
assert_se(rtnl_set_link_name(&rtnl, ifindex, "testlongalternativename") == -EINVAL);
assert_se(rtnl_set_link_name(&rtnl, ifindex, "test-shortname") >= 0);
alternative_names = strv_free(alternative_names);
assert_se(rtnl_get_link_alternative_names(&rtnl, ifindex, &alternative_names) >= 0);
assert_se(strv_contains(alternative_names, "testlongalternativename"));
assert_se(!strv_contains(alternative_names, "test-shortname"));
assert_se(rtnl_delete_link_alternative_names(&rtnl, ifindex, STRV_MAKE("testlongalternativename")) >= 0);
alternative_names = strv_free(alternative_names);
assert_se(rtnl_get_link_alternative_names(&rtnl, ifindex, &alternative_names) >= 0);
assert_se(!strv_contains(alternative_names, "testlongalternativename"));
assert_se(!strv_contains(alternative_names, "test-shortname"));
}
int main(void) {
sd_netlink *rtnl;
sd_netlink_message *m;
sd_netlink_message *r;
const char *string_data;
int if_loopback;
uint16_t type;
test_setup_logging(LOG_DEBUG);
test_match();
test_multiple();
assert_se(sd_netlink_open(&rtnl) >= 0);
assert_se(rtnl);
test_route(rtnl);
test_message(rtnl);
test_container(rtnl);
test_array();
test_strv(rtnl);
if_loopback = (int) if_nametoindex("lo");
assert_se(if_loopback > 0);
test_async(if_loopback);
test_slot_set(if_loopback);
test_async_destroy_callback(if_loopback);
test_pipe(if_loopback);
test_event_loop(if_loopback);
test_link_configure(rtnl, if_loopback);
test_rtnl_set_link_name(rtnl, if_loopback);
test_get_addresses(rtnl);
test_message_link_bridge(rtnl);
assert_se(sd_rtnl_message_new_link(rtnl, &m, RTM_GETLINK, if_loopback) >= 0);
assert_se(m);
assert_se(sd_netlink_message_get_type(m, &type) >= 0);
assert_se(type == RTM_GETLINK);
assert_se(sd_netlink_message_read_string(m, IFLA_IFNAME, &string_data) == -EPERM);
assert_se(sd_netlink_call(rtnl, m, 0, &r) == 1);
assert_se(sd_netlink_message_get_type(r, &type) >= 0);
assert_se(type == RTM_NEWLINK);
assert_se((r = sd_netlink_message_unref(r)) == NULL);
assert_se(sd_netlink_call(rtnl, m, -1, &r) == -EPERM);
assert_se((m = sd_netlink_message_unref(m)) == NULL);
assert_se((r = sd_netlink_message_unref(r)) == NULL);
test_link_get(rtnl, if_loopback);
test_address_get(rtnl, if_loopback);
assert_se((m = sd_netlink_message_unref(m)) == NULL);
assert_se((r = sd_netlink_message_unref(r)) == NULL);
assert_se((rtnl = sd_netlink_unref(rtnl)) == NULL);
test_genl();
return EXIT_SUCCESS;
}
DEFINE_TEST_MAIN(LOG_DEBUG);