summaryrefslogtreecommitdiffstats
path: root/net/wireless/tests/fragmentation.c
blob: 49a339ca888078544ace329f04b2484e2f060b0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-License-Identifier: GPL-2.0-only
/*
 * KUnit tests for element fragmentation
 *
 * Copyright (C) 2023 Intel Corporation
 */
#include <linux/ieee80211.h>
#include <net/cfg80211.h>
#include <kunit/test.h>

static void defragment_0(struct kunit *test)
{
	ssize_t ret;
	static const u8 input[] = {
		[0] = WLAN_EID_EXTENSION,
		[1] = 254,
		[2] = WLAN_EID_EXT_EHT_MULTI_LINK,
		[27] = 27,
		[123] = 123,
		[254 + 2] = WLAN_EID_FRAGMENT,
		[254 + 3] = 7,
		[254 + 3 + 7] = 0, /* for size */
	};
	u8 *data = kunit_kzalloc(test, sizeof(input), GFP_KERNEL);

	KUNIT_ASSERT_NOT_NULL(test, data);

	ret = cfg80211_defragment_element((void *)input,
					  input, sizeof(input),
					  data, sizeof(input),
					  WLAN_EID_FRAGMENT);
	KUNIT_EXPECT_EQ(test, ret, 253);
	KUNIT_EXPECT_MEMEQ(test, data, input + 3, 253);
}

static void defragment_1(struct kunit *test)
{
	ssize_t ret;
	static const u8 input[] = {
		[0] = WLAN_EID_EXTENSION,
		[1] = 255,
		[2] = WLAN_EID_EXT_EHT_MULTI_LINK,
		[27] = 27,
		[123] = 123,
		[255 + 2] = WLAN_EID_FRAGMENT,
		[255 + 3] = 7,
		[255 + 3 + 1] = 0xaa,
		[255 + 3 + 8] = WLAN_EID_FRAGMENT, /* not used */
		[255 + 3 + 9] = 1,
		[255 + 3 + 10] = 0, /* for size */
	};
	u8 *data = kunit_kzalloc(test, sizeof(input), GFP_KERNEL);
	const struct element *elem;
	int count = 0;

	KUNIT_ASSERT_NOT_NULL(test, data);

	for_each_element(elem, input, sizeof(input))
		count++;

	/* check the elements are right */
	KUNIT_ASSERT_EQ(test, count, 3);

	ret = cfg80211_defragment_element((void *)input,
					  input, sizeof(input),
					  data, sizeof(input),
					  WLAN_EID_FRAGMENT);
	/* this means the last fragment was not used */
	KUNIT_EXPECT_EQ(test, ret, 254 + 7);
	KUNIT_EXPECT_MEMEQ(test, data, input + 3, 254);
	KUNIT_EXPECT_MEMEQ(test, data + 254, input + 255 + 4, 7);
}

static void defragment_2(struct kunit *test)
{
	ssize_t ret;
	static const u8 input[] = {
		[0] = WLAN_EID_EXTENSION,
		[1] = 255,
		[2] = WLAN_EID_EXT_EHT_MULTI_LINK,
		[27] = 27,
		[123] = 123,

		[257 + 0] = WLAN_EID_FRAGMENT,
		[257 + 1] = 255,
		[257 + 20] = 0xaa,

		[2 * 257 + 0] = WLAN_EID_FRAGMENT,
		[2 * 257 + 1] = 1,
		[2 * 257 + 2] = 0xcc,
		[2 * 257 + 3] = WLAN_EID_FRAGMENT, /* not used */
		[2 * 257 + 4] = 1,
		[2 * 257 + 5] = 0, /* for size */
	};
	u8 *data = kunit_kzalloc(test, sizeof(input), GFP_KERNEL);
	const struct element *elem;
	int count = 0;

	KUNIT_ASSERT_NOT_NULL(test, data);

	for_each_element(elem, input, sizeof(input))
		count++;

	/* check the elements are right */
	KUNIT_ASSERT_EQ(test, count, 4);

	ret = cfg80211_defragment_element((void *)input,
					  input, sizeof(input),
					  data, sizeof(input),
					  WLAN_EID_FRAGMENT);
	/* this means the last fragment was not used */
	KUNIT_EXPECT_EQ(test, ret, 254 + 255 + 1);
	KUNIT_EXPECT_MEMEQ(test, data, input + 3, 254);
	KUNIT_EXPECT_MEMEQ(test, data + 254, input + 257 + 2, 255);
	KUNIT_EXPECT_MEMEQ(test, data + 254 + 255, input + 2 * 257 + 2, 1);
}

static void defragment_at_end(struct kunit *test)
{
	ssize_t ret;
	static const u8 input[] = {
		[0] = WLAN_EID_EXTENSION,
		[1] = 255,
		[2] = WLAN_EID_EXT_EHT_MULTI_LINK,
		[27] = 27,
		[123] = 123,
		[255 + 2] = WLAN_EID_FRAGMENT,
		[255 + 3] = 7,
		[255 + 3 + 7] = 0, /* for size */
	};
	u8 *data = kunit_kzalloc(test, sizeof(input), GFP_KERNEL);

	KUNIT_ASSERT_NOT_NULL(test, data);

	ret = cfg80211_defragment_element((void *)input,
					  input, sizeof(input),
					  data, sizeof(input),
					  WLAN_EID_FRAGMENT);
	KUNIT_EXPECT_EQ(test, ret, 254 + 7);
	KUNIT_EXPECT_MEMEQ(test, data, input + 3, 254);
	KUNIT_EXPECT_MEMEQ(test, data + 254, input + 255 + 4, 7);
}

static struct kunit_case element_fragmentation_test_cases[] = {
	KUNIT_CASE(defragment_0),
	KUNIT_CASE(defragment_1),
	KUNIT_CASE(defragment_2),
	KUNIT_CASE(defragment_at_end),
	{}
};

static struct kunit_suite element_fragmentation = {
	.name = "cfg80211-element-defragmentation",
	.test_cases = element_fragmentation_test_cases,
};

kunit_test_suite(element_fragmentation);