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