LCOV - code coverage report
Current view: top level - mptcp/token_test.c (source / functions) Coverage Total Hit
Test: export-net Lines: 100.0 % 77 77
Test Date: 2024-10-18 11:14:13 Functions: 100.0 % 8 8
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 50.0 % 44 22

             Branch data     Line data    Source code
       1                 :             : // SPDX-License-Identifier: GPL-2.0
       2                 :             : #include <kunit/test.h>
       3                 :             : 
       4                 :             : #include "protocol.h"
       5                 :             : 
       6                 :           6 : static struct mptcp_subflow_request_sock *build_req_sock(struct kunit *test)
       7                 :             : {
       8                 :           6 :         struct mptcp_subflow_request_sock *req;
       9                 :             : 
      10                 :           6 :         req = kunit_kzalloc(test, sizeof(struct mptcp_subflow_request_sock),
      11                 :             :                             GFP_USER);
      12   [ +  -  -  + ]:          12 :         KUNIT_EXPECT_NOT_ERR_OR_NULL(test, req);
      13                 :           6 :         mptcp_token_init_request((struct request_sock *)req);
      14                 :           6 :         sock_net_set((struct sock *)req, &init_net);
      15                 :           6 :         return req;
      16                 :             : }
      17                 :             : 
      18                 :           2 : static void mptcp_token_test_req_basic(struct kunit *test)
      19                 :             : {
      20                 :           2 :         struct mptcp_subflow_request_sock *req = build_req_sock(test);
      21                 :           2 :         struct mptcp_sock *null_msk = NULL;
      22                 :             : 
      23         [ -  + ]:           2 :         KUNIT_ASSERT_EQ(test, 0,
      24                 :             :                         mptcp_token_new_request((struct request_sock *)req));
      25         [ -  + ]:           2 :         KUNIT_EXPECT_NE(test, 0, (int)req->token);
      26         [ -  + ]:           2 :         KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(&init_net, req->token));
      27                 :             : 
      28                 :             :         /* cleanup */
      29                 :           2 :         mptcp_token_destroy_request((struct request_sock *)req);
      30                 :           2 : }
      31                 :             : 
      32                 :           2 : static struct inet_connection_sock *build_icsk(struct kunit *test)
      33                 :             : {
      34                 :           2 :         struct inet_connection_sock *icsk;
      35                 :             : 
      36                 :           2 :         icsk = kunit_kzalloc(test, sizeof(struct inet_connection_sock),
      37                 :             :                              GFP_USER);
      38   [ +  -  -  + ]:           4 :         KUNIT_EXPECT_NOT_ERR_OR_NULL(test, icsk);
      39                 :           2 :         return icsk;
      40                 :             : }
      41                 :             : 
      42                 :           2 : static struct mptcp_subflow_context *build_ctx(struct kunit *test)
      43                 :             : {
      44                 :           2 :         struct mptcp_subflow_context *ctx;
      45                 :             : 
      46                 :           2 :         ctx = kunit_kzalloc(test, sizeof(struct mptcp_subflow_context),
      47                 :             :                             GFP_USER);
      48   [ +  -  -  + ]:           4 :         KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ctx);
      49                 :           2 :         return ctx;
      50                 :             : }
      51                 :             : 
      52                 :           6 : static struct mptcp_sock *build_msk(struct kunit *test)
      53                 :             : {
      54                 :           6 :         struct mptcp_sock *msk;
      55                 :           6 :         struct sock *sk;
      56                 :             : 
      57                 :           6 :         msk = kunit_kzalloc(test, sizeof(struct mptcp_sock), GFP_USER);
      58   [ +  -  -  + ]:          12 :         KUNIT_EXPECT_NOT_ERR_OR_NULL(test, msk);
      59                 :           6 :         refcount_set(&((struct sock *)msk)->sk_refcnt, 1);
      60                 :           6 :         sock_net_set((struct sock *)msk, &init_net);
      61                 :             : 
      62                 :           6 :         sk = (struct sock *)msk;
      63                 :             : 
      64                 :             :         /* be sure the token helpers can dereference sk->sk_prot */
      65                 :           6 :         sk->sk_prot = &tcp_prot;
      66                 :           6 :         sk->sk_protocol = IPPROTO_MPTCP;
      67                 :             : 
      68                 :           6 :         return msk;
      69                 :             : }
      70                 :             : 
      71                 :           2 : static void mptcp_token_test_msk_basic(struct kunit *test)
      72                 :             : {
      73                 :           2 :         struct inet_connection_sock *icsk = build_icsk(test);
      74                 :           2 :         struct mptcp_subflow_context *ctx = build_ctx(test);
      75                 :           2 :         struct mptcp_sock *msk = build_msk(test);
      76                 :           2 :         struct mptcp_sock *null_msk = NULL;
      77                 :           2 :         struct sock *sk;
      78                 :             : 
      79                 :           2 :         rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
      80                 :           2 :         ctx->conn = (struct sock *)msk;
      81                 :           2 :         sk = (struct sock *)msk;
      82                 :             : 
      83         [ -  + ]:           2 :         KUNIT_ASSERT_EQ(test, 0,
      84                 :             :                         mptcp_token_new_connect((struct sock *)icsk));
      85         [ -  + ]:           2 :         KUNIT_EXPECT_NE(test, 0, (int)ctx->token);
      86         [ -  + ]:           2 :         KUNIT_EXPECT_EQ(test, ctx->token, msk->token);
      87         [ -  + ]:           2 :         KUNIT_EXPECT_PTR_EQ(test, msk, mptcp_token_get_sock(&init_net, ctx->token));
      88         [ -  + ]:           2 :         KUNIT_EXPECT_EQ(test, 2, (int)refcount_read(&sk->sk_refcnt));
      89                 :             : 
      90                 :           2 :         mptcp_token_destroy(msk);
      91         [ -  + ]:           2 :         KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(&init_net, ctx->token));
      92                 :           2 : }
      93                 :             : 
      94                 :           2 : static void mptcp_token_test_accept(struct kunit *test)
      95                 :             : {
      96                 :           2 :         struct mptcp_subflow_request_sock *req = build_req_sock(test);
      97                 :           2 :         struct mptcp_sock *msk = build_msk(test);
      98                 :             : 
      99         [ -  + ]:           2 :         KUNIT_ASSERT_EQ(test, 0,
     100                 :             :                         mptcp_token_new_request((struct request_sock *)req));
     101                 :           2 :         msk->token = req->token;
     102                 :           2 :         mptcp_token_accept(req, msk);
     103         [ -  + ]:           2 :         KUNIT_EXPECT_PTR_EQ(test, msk, mptcp_token_get_sock(&init_net, msk->token));
     104                 :             : 
     105                 :             :         /* this is now a no-op */
     106                 :           2 :         mptcp_token_destroy_request((struct request_sock *)req);
     107         [ -  + ]:           2 :         KUNIT_EXPECT_PTR_EQ(test, msk, mptcp_token_get_sock(&init_net, msk->token));
     108                 :             : 
     109                 :             :         /* cleanup */
     110                 :           2 :         mptcp_token_destroy(msk);
     111                 :           2 : }
     112                 :             : 
     113                 :           2 : static void mptcp_token_test_destroyed(struct kunit *test)
     114                 :             : {
     115                 :           2 :         struct mptcp_subflow_request_sock *req = build_req_sock(test);
     116                 :           2 :         struct mptcp_sock *msk = build_msk(test);
     117                 :           2 :         struct mptcp_sock *null_msk = NULL;
     118                 :           2 :         struct sock *sk;
     119                 :             : 
     120                 :           2 :         sk = (struct sock *)msk;
     121                 :             : 
     122         [ -  + ]:           2 :         KUNIT_ASSERT_EQ(test, 0,
     123                 :             :                         mptcp_token_new_request((struct request_sock *)req));
     124                 :           2 :         msk->token = req->token;
     125                 :           2 :         mptcp_token_accept(req, msk);
     126                 :             : 
     127                 :             :         /* simulate race on removal */
     128                 :           2 :         refcount_set(&sk->sk_refcnt, 0);
     129         [ -  + ]:           2 :         KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(&init_net, msk->token));
     130                 :             : 
     131                 :             :         /* cleanup */
     132                 :           2 :         mptcp_token_destroy(msk);
     133                 :           2 : }
     134                 :             : 
     135                 :             : static struct kunit_case mptcp_token_test_cases[] = {
     136                 :             :         KUNIT_CASE(mptcp_token_test_req_basic),
     137                 :             :         KUNIT_CASE(mptcp_token_test_msk_basic),
     138                 :             :         KUNIT_CASE(mptcp_token_test_accept),
     139                 :             :         KUNIT_CASE(mptcp_token_test_destroyed),
     140                 :             :         {}
     141                 :             : };
     142                 :             : 
     143                 :             : static struct kunit_suite mptcp_token_suite = {
     144                 :             :         .name = "mptcp-token",
     145                 :             :         .test_cases = mptcp_token_test_cases,
     146                 :             : };
     147                 :             : 
     148                 :             : kunit_test_suite(mptcp_token_suite);
     149                 :             : 
     150                 :             : MODULE_LICENSE("GPL");
     151                 :             : MODULE_DESCRIPTION("KUnit tests for MPTCP Token");
        

Generated by: LCOV version 2.0-1