1 // SPDX-License-Identifier: GPL-2.0
2 #include <kunit/test.h>
6 static struct mptcp_subflow_request_sock *build_req_sock(struct kunit *test)
8 struct mptcp_subflow_request_sock *req;
10 req = kunit_kzalloc(test, sizeof(struct mptcp_subflow_request_sock),
12 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, req);
13 mptcp_token_init_request((struct request_sock *)req);
14 sock_net_set((struct sock *)req, &init_net);
18 static void mptcp_token_test_req_basic(struct kunit *test)
20 struct mptcp_subflow_request_sock *req = build_req_sock(test);
21 struct mptcp_sock *null_msk = NULL;
23 KUNIT_ASSERT_EQ(test, 0,
24 mptcp_token_new_request((struct request_sock *)req));
25 KUNIT_EXPECT_NE(test, 0, (int)req->token);
26 KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(&init_net, req->token));
29 mptcp_token_destroy_request((struct request_sock *)req);
32 static struct inet_connection_sock *build_icsk(struct kunit *test)
34 struct inet_connection_sock *icsk;
36 icsk = kunit_kzalloc(test, sizeof(struct inet_connection_sock),
38 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, icsk);
42 static struct mptcp_subflow_context *build_ctx(struct kunit *test)
44 struct mptcp_subflow_context *ctx;
46 ctx = kunit_kzalloc(test, sizeof(struct mptcp_subflow_context),
48 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ctx);
52 static struct mptcp_sock *build_msk(struct kunit *test)
54 struct mptcp_sock *msk;
56 msk = kunit_kzalloc(test, sizeof(struct mptcp_sock), GFP_USER);
57 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, msk);
58 refcount_set(&((struct sock *)msk)->sk_refcnt, 1);
59 sock_net_set((struct sock *)msk, &init_net);
61 /* be sure the token helpers can dereference sk->sk_prot */
62 ((struct sock *)msk)->sk_prot = &tcp_prot;
66 static void mptcp_token_test_msk_basic(struct kunit *test)
68 struct inet_connection_sock *icsk = build_icsk(test);
69 struct mptcp_subflow_context *ctx = build_ctx(test);
70 struct mptcp_sock *msk = build_msk(test);
71 struct mptcp_sock *null_msk = NULL;
74 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
75 ctx->conn = (struct sock *)msk;
76 sk = (struct sock *)msk;
78 KUNIT_ASSERT_EQ(test, 0,
79 mptcp_token_new_connect((struct sock *)icsk));
80 KUNIT_EXPECT_NE(test, 0, (int)ctx->token);
81 KUNIT_EXPECT_EQ(test, ctx->token, msk->token);
82 KUNIT_EXPECT_PTR_EQ(test, msk, mptcp_token_get_sock(&init_net, ctx->token));
83 KUNIT_EXPECT_EQ(test, 2, (int)refcount_read(&sk->sk_refcnt));
85 mptcp_token_destroy(msk);
86 KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(&init_net, ctx->token));
89 static void mptcp_token_test_accept(struct kunit *test)
91 struct mptcp_subflow_request_sock *req = build_req_sock(test);
92 struct mptcp_sock *msk = build_msk(test);
94 KUNIT_ASSERT_EQ(test, 0,
95 mptcp_token_new_request((struct request_sock *)req));
96 msk->token = req->token;
97 mptcp_token_accept(req, msk);
98 KUNIT_EXPECT_PTR_EQ(test, msk, mptcp_token_get_sock(&init_net, msk->token));
100 /* this is now a no-op */
101 mptcp_token_destroy_request((struct request_sock *)req);
102 KUNIT_EXPECT_PTR_EQ(test, msk, mptcp_token_get_sock(&init_net, msk->token));
105 mptcp_token_destroy(msk);
108 static void mptcp_token_test_destroyed(struct kunit *test)
110 struct mptcp_subflow_request_sock *req = build_req_sock(test);
111 struct mptcp_sock *msk = build_msk(test);
112 struct mptcp_sock *null_msk = NULL;
115 sk = (struct sock *)msk;
117 KUNIT_ASSERT_EQ(test, 0,
118 mptcp_token_new_request((struct request_sock *)req));
119 msk->token = req->token;
120 mptcp_token_accept(req, msk);
122 /* simulate race on removal */
123 refcount_set(&sk->sk_refcnt, 0);
124 KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(&init_net, msk->token));
127 mptcp_token_destroy(msk);
130 static struct kunit_case mptcp_token_test_cases[] = {
131 KUNIT_CASE(mptcp_token_test_req_basic),
132 KUNIT_CASE(mptcp_token_test_msk_basic),
133 KUNIT_CASE(mptcp_token_test_accept),
134 KUNIT_CASE(mptcp_token_test_destroyed),
138 static struct kunit_suite mptcp_token_suite = {
139 .name = "mptcp-token",
140 .test_cases = mptcp_token_test_cases,
143 kunit_test_suite(mptcp_token_suite);
145 MODULE_LICENSE("GPL");