Skip to content

Commit b7e2812

Browse files
mkjPaolo Abeni
authored andcommitted
net: mctp: Test conflicts of connect() with bind()
The addition of connect() adds new conflict cases to test. Signed-off-by: Matt Johnston <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent 3549eb0 commit b7e2812

File tree

3 files changed

+59
-4
lines changed

3 files changed

+59
-4
lines changed

net/mctp/test/sock-test.c

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ static const struct mctp_test_bind_setup bind_addrany_net2_type2 = {
245245
.bind_addr = MCTP_ADDR_ANY, .bind_net = 2, .bind_type = 2,
246246
};
247247

248+
static const struct mctp_test_bind_setup bind_addrany_net2_type1_peer9 = {
249+
.bind_addr = MCTP_ADDR_ANY, .bind_net = 2, .bind_type = 1,
250+
.have_peer = true, .peer_addr = 9, .peer_net = 2,
251+
};
252+
248253
struct mctp_bind_pair_test {
249254
const struct mctp_test_bind_setup *bind1;
250255
const struct mctp_test_bind_setup *bind2;
@@ -278,19 +283,50 @@ static const struct mctp_bind_pair_test mctp_bind_pair_tests[] = {
278283
* vs ADDR_ANY, explicit default net 1, OK
279284
*/
280285
{ &bind_addrany_netdefault_type1, &bind_addrany_net1_type1, 0 },
286+
287+
/* specific remote peer doesn't conflict with any-peer bind */
288+
{ &bind_addrany_net2_type1_peer9, &bind_addrany_net2_type1, 0 },
289+
290+
/* bind() NET_ANY is allowed with a connect() net */
291+
{ &bind_addrany_net2_type1_peer9, &bind_addrany_netdefault_type1, 0 },
281292
};
282293

283294
static void mctp_bind_pair_desc(const struct mctp_bind_pair_test *t, char *desc)
284295
{
296+
char peer1[25] = {0}, peer2[25] = {0};
297+
298+
if (t->bind1->have_peer)
299+
snprintf(peer1, sizeof(peer1), ", peer %d net %d",
300+
t->bind1->peer_addr, t->bind1->peer_net);
301+
if (t->bind2->have_peer)
302+
snprintf(peer2, sizeof(peer2), ", peer %d net %d",
303+
t->bind2->peer_addr, t->bind2->peer_net);
304+
285305
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);
306+
"{bind(addr %d, type %d, net %d%s)} {bind(addr %d, type %d, net %d%s)} -> error %d",
307+
t->bind1->bind_addr, t->bind1->bind_type,
308+
t->bind1->bind_net, peer1,
309+
t->bind2->bind_addr, t->bind2->bind_type,
310+
t->bind2->bind_net, peer2, t->error);
290311
}
291312

292313
KUNIT_ARRAY_PARAM(mctp_bind_pair, mctp_bind_pair_tests, mctp_bind_pair_desc);
293314

315+
static void mctp_test_bind_invalid(struct kunit *test)
316+
{
317+
struct socket *sock;
318+
int rc;
319+
320+
/* bind() fails if the bind() vs connect() networks mismatch. */
321+
const struct mctp_test_bind_setup bind_connect_net_mismatch = {
322+
.bind_addr = MCTP_ADDR_ANY, .bind_net = 1, .bind_type = 1,
323+
.have_peer = true, .peer_addr = 9, .peer_net = 2,
324+
};
325+
mctp_test_bind_run(test, &bind_connect_net_mismatch, &rc, &sock);
326+
KUNIT_EXPECT_EQ(test, -rc, EINVAL);
327+
sock_release(sock);
328+
}
329+
294330
static int
295331
mctp_test_bind_conflicts_inner(struct kunit *test,
296332
const struct mctp_test_bind_setup *bind1,
@@ -348,6 +384,7 @@ static struct kunit_case mctp_test_cases[] = {
348384
KUNIT_CASE(mctp_test_sock_sendmsg_extaddr),
349385
KUNIT_CASE(mctp_test_sock_recvmsg_extaddr),
350386
KUNIT_CASE_PARAM(mctp_test_bind_conflicts, mctp_bind_pair_gen_params),
387+
KUNIT_CASE(mctp_test_bind_invalid),
351388
{}
352389
};
353390

net/mctp/test/utils.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,20 @@ void mctp_test_bind_run(struct kunit *test,
271271
rc = sock_create_kern(&init_net, AF_MCTP, SOCK_DGRAM, 0, sock);
272272
KUNIT_ASSERT_EQ(test, rc, 0);
273273

274+
/* connect() if requested */
275+
if (setup->have_peer) {
276+
memset(&addr, 0x0, sizeof(addr));
277+
addr.smctp_family = AF_MCTP;
278+
addr.smctp_network = setup->peer_net;
279+
addr.smctp_addr.s_addr = setup->peer_addr;
280+
/* connect() type must match bind() type */
281+
addr.smctp_type = setup->bind_type;
282+
rc = kernel_connect(*sock, (struct sockaddr *)&addr,
283+
sizeof(addr), 0);
284+
KUNIT_EXPECT_EQ(test, rc, 0);
285+
}
286+
287+
/* bind() */
274288
memset(&addr, 0x0, sizeof(addr));
275289
addr.smctp_family = AF_MCTP;
276290
addr.smctp_network = setup->bind_net;

net/mctp/test/utils.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ struct mctp_test_bind_setup {
3535
mctp_eid_t bind_addr;
3636
int bind_net;
3737
u8 bind_type;
38+
39+
bool have_peer;
40+
mctp_eid_t peer_addr;
41+
int peer_net;
3842
};
3943

4044
struct mctp_test_dev *mctp_test_create_dev(void);

0 commit comments

Comments
 (0)