Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

net: mctp: Add test for conflicting bind()s

Test pairwise combinations of bind addresses and types.

Signed-off-by: Matt Johnston <matt@codeconstruct.com.au>
Link: https://patch.msgid.link/20250710-mctp-bind-v4-4-8ec2f6460c56@codeconstruct.com.au
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Matt Johnston and committed by
Paolo Abeni
4ec4b7fc 5000268c

+162
+130
net/mctp/test/sock-test.c
··· 215 215 __mctp_sock_test_fini(test, dev, rt, sock); 216 216 } 217 217 218 + static const struct mctp_test_bind_setup bind_addrany_netdefault_type1 = { 219 + .bind_addr = MCTP_ADDR_ANY, .bind_net = MCTP_NET_ANY, .bind_type = 1, 220 + }; 221 + 222 + static const struct mctp_test_bind_setup bind_addrany_net2_type1 = { 223 + .bind_addr = MCTP_ADDR_ANY, .bind_net = 2, .bind_type = 1, 224 + }; 225 + 226 + /* 1 is default net */ 227 + static const struct mctp_test_bind_setup bind_addr8_net1_type1 = { 228 + .bind_addr = 8, .bind_net = 1, .bind_type = 1, 229 + }; 230 + 231 + static const struct mctp_test_bind_setup bind_addrany_net1_type1 = { 232 + .bind_addr = MCTP_ADDR_ANY, .bind_net = 1, .bind_type = 1, 233 + }; 234 + 235 + /* 2 is an arbitrary net */ 236 + static const struct mctp_test_bind_setup bind_addr8_net2_type1 = { 237 + .bind_addr = 8, .bind_net = 2, .bind_type = 1, 238 + }; 239 + 240 + static const struct mctp_test_bind_setup bind_addr8_netdefault_type1 = { 241 + .bind_addr = 8, .bind_net = MCTP_NET_ANY, .bind_type = 1, 242 + }; 243 + 244 + static const struct mctp_test_bind_setup bind_addrany_net2_type2 = { 245 + .bind_addr = MCTP_ADDR_ANY, .bind_net = 2, .bind_type = 2, 246 + }; 247 + 248 + struct mctp_bind_pair_test { 249 + const struct mctp_test_bind_setup *bind1; 250 + const struct mctp_test_bind_setup *bind2; 251 + int error; 252 + }; 253 + 254 + /* Pairs of binds and whether they will conflict */ 255 + static const struct mctp_bind_pair_test mctp_bind_pair_tests[] = { 256 + /* Both ADDR_ANY, conflict */ 257 + { &bind_addrany_netdefault_type1, &bind_addrany_netdefault_type1, 258 + EADDRINUSE }, 259 + /* Same specific EID, conflict */ 260 + { &bind_addr8_netdefault_type1, &bind_addr8_netdefault_type1, 261 + EADDRINUSE }, 262 + /* ADDR_ANY vs specific EID, OK */ 263 + { &bind_addrany_netdefault_type1, &bind_addr8_netdefault_type1, 0 }, 264 + /* ADDR_ANY different types, OK */ 265 + { &bind_addrany_net2_type2, &bind_addrany_net2_type1, 0 }, 266 + /* ADDR_ANY different nets, OK */ 267 + { &bind_addrany_net2_type1, &bind_addrany_netdefault_type1, 0 }, 268 + 269 + /* specific EID, NET_ANY (resolves to default) 270 + * vs specific EID, explicit default net 1, conflict 271 + */ 272 + { &bind_addr8_netdefault_type1, &bind_addr8_net1_type1, EADDRINUSE }, 273 + 274 + /* specific EID, net 1 vs specific EID, net 2, ok */ 275 + { &bind_addr8_net1_type1, &bind_addr8_net2_type1, 0 }, 276 + 277 + /* ANY_ADDR, NET_ANY (doesn't resolve to default) 278 + * vs ADDR_ANY, explicit default net 1, OK 279 + */ 280 + { &bind_addrany_netdefault_type1, &bind_addrany_net1_type1, 0 }, 281 + }; 282 + 283 + static void mctp_bind_pair_desc(const struct mctp_bind_pair_test *t, char *desc) 284 + { 285 + snprintf(desc, KUNIT_PARAM_DESC_SIZE, 286 + "{bind(addr %d, type %d, net %d)} {bind(addr %d, type %d, net %d)} -> error %d", 287 + t->bind1->bind_addr, t->bind1->bind_type, t->bind1->bind_net, 288 + t->bind2->bind_addr, t->bind2->bind_type, t->bind2->bind_net, 289 + t->error); 290 + } 291 + 292 + KUNIT_ARRAY_PARAM(mctp_bind_pair, mctp_bind_pair_tests, mctp_bind_pair_desc); 293 + 294 + static int 295 + mctp_test_bind_conflicts_inner(struct kunit *test, 296 + const struct mctp_test_bind_setup *bind1, 297 + const struct mctp_test_bind_setup *bind2) 298 + { 299 + struct socket *sock1 = NULL, *sock2 = NULL, *sock3 = NULL; 300 + int bind_errno; 301 + 302 + /* Bind to first address, always succeeds */ 303 + mctp_test_bind_run(test, bind1, &bind_errno, &sock1); 304 + KUNIT_EXPECT_EQ(test, bind_errno, 0); 305 + 306 + /* A second identical bind always fails */ 307 + mctp_test_bind_run(test, bind1, &bind_errno, &sock2); 308 + KUNIT_EXPECT_EQ(test, -bind_errno, EADDRINUSE); 309 + 310 + /* A different bind, result is returned */ 311 + mctp_test_bind_run(test, bind2, &bind_errno, &sock3); 312 + 313 + if (sock1) 314 + sock_release(sock1); 315 + if (sock2) 316 + sock_release(sock2); 317 + if (sock3) 318 + sock_release(sock3); 319 + 320 + return bind_errno; 321 + } 322 + 323 + static void mctp_test_bind_conflicts(struct kunit *test) 324 + { 325 + const struct mctp_bind_pair_test *pair; 326 + int bind_errno; 327 + 328 + pair = test->param_value; 329 + 330 + bind_errno = 331 + mctp_test_bind_conflicts_inner(test, pair->bind1, pair->bind2); 332 + KUNIT_EXPECT_EQ(test, -bind_errno, pair->error); 333 + 334 + /* swapping the calls, the second bind should still fail */ 335 + bind_errno = 336 + mctp_test_bind_conflicts_inner(test, pair->bind2, pair->bind1); 337 + KUNIT_EXPECT_EQ(test, -bind_errno, pair->error); 338 + } 339 + 340 + static void mctp_test_assumptions(struct kunit *test) 341 + { 342 + /* check assumption of default net from bind_addr8_net1_type1 */ 343 + KUNIT_ASSERT_EQ(test, mctp_default_net(&init_net), 1); 344 + } 345 + 218 346 static struct kunit_case mctp_test_cases[] = { 347 + KUNIT_CASE(mctp_test_assumptions), 219 348 KUNIT_CASE(mctp_test_sock_sendmsg_extaddr), 220 349 KUNIT_CASE(mctp_test_sock_recvmsg_extaddr), 350 + KUNIT_CASE_PARAM(mctp_test_bind_conflicts, mctp_bind_pair_gen_params), 221 351 {} 222 352 }; 223 353
+22
net/mctp/test/utils.c
··· 258 258 259 259 return skb; 260 260 } 261 + 262 + void mctp_test_bind_run(struct kunit *test, 263 + const struct mctp_test_bind_setup *setup, 264 + int *ret_bind_errno, struct socket **sock) 265 + { 266 + struct sockaddr_mctp addr; 267 + int rc; 268 + 269 + *ret_bind_errno = -EIO; 270 + 271 + rc = sock_create_kern(&init_net, AF_MCTP, SOCK_DGRAM, 0, sock); 272 + KUNIT_ASSERT_EQ(test, rc, 0); 273 + 274 + memset(&addr, 0x0, sizeof(addr)); 275 + addr.smctp_family = AF_MCTP; 276 + addr.smctp_network = setup->bind_net; 277 + addr.smctp_addr.s_addr = setup->bind_addr; 278 + addr.smctp_type = setup->bind_type; 279 + 280 + *ret_bind_errno = 281 + kernel_bind(*sock, (struct sockaddr *)&addr, sizeof(addr)); 282 + }
+10
net/mctp/test/utils.h
··· 31 31 struct sk_buff_head pkts; 32 32 }; 33 33 34 + struct mctp_test_bind_setup { 35 + mctp_eid_t bind_addr; 36 + int bind_net; 37 + u8 bind_type; 38 + }; 39 + 34 40 struct mctp_test_dev *mctp_test_create_dev(void); 35 41 struct mctp_test_dev *mctp_test_create_dev_lladdr(unsigned short lladdr_len, 36 42 const unsigned char *lladdr); ··· 66 60 67 61 #define mctp_test_create_skb_data(h, d) \ 68 62 __mctp_test_create_skb_data(h, d, sizeof(*d)) 63 + 64 + void mctp_test_bind_run(struct kunit *test, 65 + const struct mctp_test_bind_setup *setup, 66 + int *ret_bind_errno, struct socket **sock); 69 67 70 68 #endif /* __NET_MCTP_TEST_UTILS_H */