mctp: tests: Add key state tests
authorJeremy Kerr <jk@codeconstruct.com.au>
Wed, 9 Feb 2022 04:05:54 +0000 (12:05 +0800)
committerDavid S. Miller <davem@davemloft.net>
Wed, 9 Feb 2022 12:00:11 +0000 (12:00 +0000)
This change adds a few more tests to check the key/tag lookups on route
input. We add a specific entry to the keys lists, route a packet with
specific header values, and check for key match/mismatch.

Signed-off-by: Jeremy Kerr <jk@codeconstruct.com.au>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/mctp/test/route-test.c

index 5862f7f..dad6bff 100644 (file)
@@ -370,6 +370,7 @@ static void mctp_test_route_input_sk(struct kunit *test)
 #define FL_S   (MCTP_HDR_FLAG_SOM)
 #define FL_E   (MCTP_HDR_FLAG_EOM)
 #define FL_TO  (MCTP_HDR_FLAG_TO)
+#define FL_T(t)        ((t) & MCTP_HDR_TAG_MASK)
 
 static const struct mctp_route_input_sk_test mctp_route_input_sk_tests[] = {
        { .hdr = RX_HDR(1, 10, 8, FL_S | FL_E | FL_TO), .type = 0, .deliver = true },
@@ -522,12 +523,148 @@ static void mctp_route_input_sk_reasm_to_desc(
 KUNIT_ARRAY_PARAM(mctp_route_input_sk_reasm, mctp_route_input_sk_reasm_tests,
                  mctp_route_input_sk_reasm_to_desc);
 
+struct mctp_route_input_sk_keys_test {
+       const char      *name;
+       mctp_eid_t      key_peer_addr;
+       mctp_eid_t      key_local_addr;
+       u8              key_tag;
+       struct mctp_hdr hdr;
+       bool            deliver;
+};
+
+/* test packet rx in the presence of various key configurations */
+static void mctp_test_route_input_sk_keys(struct kunit *test)
+{
+       const struct mctp_route_input_sk_keys_test *params;
+       struct mctp_test_route *rt;
+       struct sk_buff *skb, *skb2;
+       struct mctp_test_dev *dev;
+       struct mctp_sk_key *key;
+       struct netns_mctp *mns;
+       struct mctp_sock *msk;
+       struct socket *sock;
+       unsigned long flags;
+       int rc;
+       u8 c;
+
+       params = test->param_value;
+
+       dev = mctp_test_create_dev();
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, dev);
+
+       rt = mctp_test_create_route(&init_net, dev->mdev, 8, 68);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, rt);
+
+       rc = sock_create_kern(&init_net, AF_MCTP, SOCK_DGRAM, 0, &sock);
+       KUNIT_ASSERT_EQ(test, rc, 0);
+
+       msk = container_of(sock->sk, struct mctp_sock, sk);
+       mns = &sock_net(sock->sk)->mctp;
+
+       /* set the incoming tag according to test params */
+       key = mctp_key_alloc(msk, params->key_local_addr, params->key_peer_addr,
+                            params->key_tag, GFP_KERNEL);
+
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, key);
+
+       spin_lock_irqsave(&mns->keys_lock, flags);
+       mctp_reserve_tag(&init_net, key, msk);
+       spin_unlock_irqrestore(&mns->keys_lock, flags);
+
+       /* create packet and route */
+       c = 0;
+       skb = mctp_test_create_skb_data(&params->hdr, &c);
+       KUNIT_ASSERT_NOT_ERR_OR_NULL(test, skb);
+
+       skb->dev = dev->ndev;
+       __mctp_cb(skb);
+
+       rc = mctp_route_input(&rt->rt, skb);
+
+       /* (potentially) receive message */
+       skb2 = skb_recv_datagram(sock->sk, 0, 1, &rc);
+
+       if (params->deliver)
+               KUNIT_EXPECT_NOT_ERR_OR_NULL(test, skb2);
+       else
+               KUNIT_EXPECT_PTR_EQ(test, skb2, NULL);
+
+       if (skb2)
+               skb_free_datagram(sock->sk, skb2);
+
+       mctp_key_unref(key);
+       __mctp_route_test_fini(test, dev, rt, sock);
+}
+
+static const struct mctp_route_input_sk_keys_test mctp_route_input_sk_keys_tests[] = {
+       {
+               .name = "direct match",
+               .key_peer_addr = 9,
+               .key_local_addr = 8,
+               .key_tag = 1,
+               .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1)),
+               .deliver = true,
+       },
+       {
+               .name = "flipped src/dest",
+               .key_peer_addr = 8,
+               .key_local_addr = 9,
+               .key_tag = 1,
+               .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1)),
+               .deliver = false,
+       },
+       {
+               .name = "peer addr mismatch",
+               .key_peer_addr = 9,
+               .key_local_addr = 8,
+               .key_tag = 1,
+               .hdr = RX_HDR(1, 10, 8, FL_S | FL_E | FL_T(1)),
+               .deliver = false,
+       },
+       {
+               .name = "tag value mismatch",
+               .key_peer_addr = 9,
+               .key_local_addr = 8,
+               .key_tag = 1,
+               .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(2)),
+               .deliver = false,
+       },
+       {
+               .name = "TO mismatch",
+               .key_peer_addr = 9,
+               .key_local_addr = 8,
+               .key_tag = 1,
+               .hdr = RX_HDR(1, 9, 8, FL_S | FL_E | FL_T(1) | FL_TO),
+               .deliver = false,
+       },
+       {
+               .name = "broadcast response",
+               .key_peer_addr = MCTP_ADDR_ANY,
+               .key_local_addr = 8,
+               .key_tag = 1,
+               .hdr = RX_HDR(1, 11, 8, FL_S | FL_E | FL_T(1)),
+               .deliver = true,
+       },
+};
+
+static void mctp_route_input_sk_keys_to_desc(
+                               const struct mctp_route_input_sk_keys_test *t,
+                               char *desc)
+{
+       sprintf(desc, "%s", t->name);
+}
+
+KUNIT_ARRAY_PARAM(mctp_route_input_sk_keys, mctp_route_input_sk_keys_tests,
+                 mctp_route_input_sk_keys_to_desc);
+
 static struct kunit_case mctp_test_cases[] = {
        KUNIT_CASE_PARAM(mctp_test_fragment, mctp_frag_gen_params),
        KUNIT_CASE_PARAM(mctp_test_rx_input, mctp_rx_input_gen_params),
        KUNIT_CASE_PARAM(mctp_test_route_input_sk, mctp_route_input_sk_gen_params),
        KUNIT_CASE_PARAM(mctp_test_route_input_sk_reasm,
                         mctp_route_input_sk_reasm_gen_params),
+       KUNIT_CASE_PARAM(mctp_test_route_input_sk_keys,
+                        mctp_route_input_sk_keys_gen_params),
        {}
 };