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);
17 static void mptcp_token_test_req_basic(struct kunit *test)
19 struct mptcp_subflow_request_sock *req = build_req_sock(test);
20 struct mptcp_sock *null_msk = NULL;
22 KUNIT_ASSERT_EQ(test, 0,
23 mptcp_token_new_request((struct request_sock *)req));
24 KUNIT_EXPECT_NE(test, 0, (int)req->token);
25 KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(req->token));
28 mptcp_token_destroy_request((struct request_sock *)req);
31 static struct inet_connection_sock *build_icsk(struct kunit *test)
33 struct inet_connection_sock *icsk;
35 icsk = kunit_kzalloc(test, sizeof(struct inet_connection_sock),
37 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, icsk);
41 static struct mptcp_subflow_context *build_ctx(struct kunit *test)
43 struct mptcp_subflow_context *ctx;
45 ctx = kunit_kzalloc(test, sizeof(struct mptcp_subflow_context),
47 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, ctx);
51 static struct mptcp_sock *build_msk(struct kunit *test)
53 struct mptcp_sock *msk;
55 msk = kunit_kzalloc(test, sizeof(struct mptcp_sock), GFP_USER);
56 KUNIT_EXPECT_NOT_ERR_OR_NULL(test, msk);
57 refcount_set(&((struct sock *)msk)->sk_refcnt, 1);
61 static void mptcp_token_test_msk_basic(struct kunit *test)
63 struct inet_connection_sock *icsk = build_icsk(test);
64 struct mptcp_subflow_context *ctx = build_ctx(test);
65 struct mptcp_sock *msk = build_msk(test);
66 struct mptcp_sock *null_msk = NULL;
69 rcu_assign_pointer(icsk->icsk_ulp_data, ctx);
70 ctx->conn = (struct sock *)msk;
71 sk = (struct sock *)msk;
73 KUNIT_ASSERT_EQ(test, 0,
74 mptcp_token_new_connect((struct sock *)icsk));
75 KUNIT_EXPECT_NE(test, 0, (int)ctx->token);
76 KUNIT_EXPECT_EQ(test, ctx->token, msk->token);
77 KUNIT_EXPECT_PTR_EQ(test, msk, mptcp_token_get_sock(ctx->token));
78 KUNIT_EXPECT_EQ(test, 2, (int)refcount_read(&sk->sk_refcnt));
80 mptcp_token_destroy(msk);
81 KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(ctx->token));
84 static void mptcp_token_test_accept(struct kunit *test)
86 struct mptcp_subflow_request_sock *req = build_req_sock(test);
87 struct mptcp_sock *msk = build_msk(test);
89 KUNIT_ASSERT_EQ(test, 0,
90 mptcp_token_new_request((struct request_sock *)req));
91 msk->token = req->token;
92 mptcp_token_accept(req, msk);
93 KUNIT_EXPECT_PTR_EQ(test, msk, mptcp_token_get_sock(msk->token));
95 /* this is now a no-op */
96 mptcp_token_destroy_request((struct request_sock *)req);
97 KUNIT_EXPECT_PTR_EQ(test, msk, mptcp_token_get_sock(msk->token));
100 mptcp_token_destroy(msk);
103 static void mptcp_token_test_destroyed(struct kunit *test)
105 struct mptcp_subflow_request_sock *req = build_req_sock(test);
106 struct mptcp_sock *msk = build_msk(test);
107 struct mptcp_sock *null_msk = NULL;
110 sk = (struct sock *)msk;
112 KUNIT_ASSERT_EQ(test, 0,
113 mptcp_token_new_request((struct request_sock *)req));
114 msk->token = req->token;
115 mptcp_token_accept(req, msk);
117 /* simulate race on removal */
118 refcount_set(&sk->sk_refcnt, 0);
119 KUNIT_EXPECT_PTR_EQ(test, null_msk, mptcp_token_get_sock(msk->token));
122 mptcp_token_destroy(msk);
125 static struct kunit_case mptcp_token_test_cases[] = {
126 KUNIT_CASE(mptcp_token_test_req_basic),
127 KUNIT_CASE(mptcp_token_test_msk_basic),
128 KUNIT_CASE(mptcp_token_test_accept),
129 KUNIT_CASE(mptcp_token_test_destroyed),
133 static struct kunit_suite mptcp_token_suite = {
134 .name = "mptcp-token",
135 .test_cases = mptcp_token_test_cases,
138 kunit_test_suite(mptcp_token_suite);
140 MODULE_LICENSE("GPL");