xref: /linux/net/wireless/tests/chan.c (revision dbcedec3a31119d7594baacc743300d127c99c56)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * KUnit tests for channel helper functions
4  *
5  * Copyright (C) 2023-2024 Intel Corporation
6  */
7 #include <net/cfg80211.h>
8 #include <kunit/test.h>
9 
10 MODULE_IMPORT_NS(EXPORTED_FOR_KUNIT_TESTING);
11 
12 static struct ieee80211_channel chan_6ghz_1 = {
13 	.band = NL80211_BAND_6GHZ,
14 	.center_freq = 5955,
15 };
16 
17 static struct ieee80211_channel chan_6ghz_5 = {
18 	.band = NL80211_BAND_6GHZ,
19 	.center_freq = 5975,
20 };
21 
22 static struct ieee80211_channel chan_6ghz_105 = {
23 	.band = NL80211_BAND_6GHZ,
24 	.center_freq = 6475,
25 };
26 
27 static const struct chandef_compat_case {
28 	const char *desc;
29 	/* leave c1 empty for tests for identical */
30 	struct cfg80211_chan_def c1, c2;
31 	/* we test both ways around, so c2 should always be the compat one */
32 	bool compat;
33 } chandef_compat_cases[] = {
34 	{
35 		.desc = "identical non-HT",
36 		.c2 = {
37 			.width = NL80211_CHAN_WIDTH_20_NOHT,
38 			.chan = &chan_6ghz_1,
39 			.center_freq1 = 5955,
40 		},
41 		.compat = true,
42 	},
43 	{
44 		.desc = "identical 20 MHz",
45 		.c2 = {
46 			.width = NL80211_CHAN_WIDTH_20,
47 			.chan = &chan_6ghz_1,
48 			.center_freq1 = 5955,
49 		},
50 		.compat = true,
51 	},
52 	{
53 		.desc = "identical 40 MHz",
54 		.c2 = {
55 			.width = NL80211_CHAN_WIDTH_40,
56 			.chan = &chan_6ghz_1,
57 			.center_freq1 = 5955 + 10,
58 		},
59 		.compat = true,
60 	},
61 	{
62 		.desc = "identical 80 MHz",
63 		.c2 = {
64 			.width = NL80211_CHAN_WIDTH_80,
65 			.chan = &chan_6ghz_1,
66 			.center_freq1 = 5955 + 10 + 20,
67 		},
68 		.compat = true,
69 	},
70 	{
71 		.desc = "identical 160 MHz",
72 		.c2 = {
73 			.width = NL80211_CHAN_WIDTH_160,
74 			.chan = &chan_6ghz_1,
75 			.center_freq1 = 5955 + 10 + 20 + 40,
76 		},
77 		.compat = true,
78 	},
79 	{
80 		.desc = "identical 320 MHz",
81 		.c2 = {
82 			.width = NL80211_CHAN_WIDTH_320,
83 			.chan = &chan_6ghz_1,
84 			.center_freq1 = 5955 + 10 + 20 + 40 + 80,
85 		},
86 		.compat = true,
87 	},
88 	{
89 		.desc = "20 MHz in 320 MHz\n",
90 		.c1 = {
91 			.width = NL80211_CHAN_WIDTH_20,
92 			.chan = &chan_6ghz_1,
93 			.center_freq1 = 5955,
94 		},
95 		.c2 = {
96 			.width = NL80211_CHAN_WIDTH_320,
97 			.chan = &chan_6ghz_1,
98 			.center_freq1 = 5955 + 10 + 20 + 40 + 80,
99 		},
100 		.compat = true,
101 	},
102 	{
103 		.desc = "different 20 MHz",
104 		.c1 = {
105 			.width = NL80211_CHAN_WIDTH_20,
106 			.chan = &chan_6ghz_1,
107 			.center_freq1 = 5955,
108 		},
109 		.c2 = {
110 			.width = NL80211_CHAN_WIDTH_20,
111 			.chan = &chan_6ghz_5,
112 			.center_freq1 = 5975,
113 		},
114 	},
115 	{
116 		.desc = "different primary 160 MHz",
117 		.c1 = {
118 			.width = NL80211_CHAN_WIDTH_320,
119 			.chan = &chan_6ghz_105,
120 			.center_freq1 = 6475 + 150,
121 		},
122 		.c2 = {
123 			.width = NL80211_CHAN_WIDTH_320,
124 			.chan = &chan_6ghz_105,
125 			.center_freq1 = 6475 - 10,
126 		},
127 	},
128 	{
129 		/* similar to previous test but one has lower BW */
130 		.desc = "matching primary 160 MHz",
131 		.c1 = {
132 			.width = NL80211_CHAN_WIDTH_160,
133 			.chan = &chan_6ghz_105,
134 			.center_freq1 = 6475 + 70,
135 		},
136 		.c2 = {
137 			.width = NL80211_CHAN_WIDTH_320,
138 			.chan = &chan_6ghz_105,
139 			.center_freq1 = 6475 - 10,
140 		},
141 		.compat = true,
142 	},
143 	{
144 		.desc = "matching primary 160 MHz & punctured secondary 160 Mhz",
145 		.c1 = {
146 			.width = NL80211_CHAN_WIDTH_160,
147 			.chan = &chan_6ghz_105,
148 			.center_freq1 = 6475 + 70,
149 		},
150 		.c2 = {
151 			.width = NL80211_CHAN_WIDTH_320,
152 			.chan = &chan_6ghz_105,
153 			.center_freq1 = 6475 - 10,
154 			.punctured = 0xf,
155 		},
156 		.compat = true,
157 	},
158 	{
159 		.desc = "matching primary 160 MHz & punctured matching",
160 		.c1 = {
161 			.width = NL80211_CHAN_WIDTH_160,
162 			.chan = &chan_6ghz_105,
163 			.center_freq1 = 6475 + 70,
164 			.punctured = 0xc0,
165 		},
166 		.c2 = {
167 			.width = NL80211_CHAN_WIDTH_320,
168 			.chan = &chan_6ghz_105,
169 			.center_freq1 = 6475 - 10,
170 			.punctured = 0xc000,
171 		},
172 		.compat = true,
173 	},
174 	{
175 		.desc = "matching primary 160 MHz & punctured not matching",
176 		.c1 = {
177 			.width = NL80211_CHAN_WIDTH_160,
178 			.chan = &chan_6ghz_105,
179 			.center_freq1 = 6475 + 70,
180 			.punctured = 0x80,
181 		},
182 		.c2 = {
183 			.width = NL80211_CHAN_WIDTH_320,
184 			.chan = &chan_6ghz_105,
185 			.center_freq1 = 6475 - 10,
186 			.punctured = 0xc000,
187 		},
188 	},
189 };
190 
191 KUNIT_ARRAY_PARAM_DESC(chandef_compat, chandef_compat_cases, desc)
192 
193 static void test_chandef_compat(struct kunit *test)
194 {
195 	const struct chandef_compat_case *params = test->param_value;
196 	const struct cfg80211_chan_def *ret, *expect;
197 	struct cfg80211_chan_def c1 = params->c1;
198 
199 	/* tests with identical ones */
200 	if (!params->c1.chan)
201 		c1 = params->c2;
202 
203 	KUNIT_EXPECT_EQ(test, cfg80211_chandef_valid(&c1), true);
204 	KUNIT_EXPECT_EQ(test, cfg80211_chandef_valid(&params->c2), true);
205 
206 	expect = params->compat ? &params->c2 : NULL;
207 
208 	ret = cfg80211_chandef_compatible(&c1, &params->c2);
209 	KUNIT_EXPECT_PTR_EQ(test, ret, expect);
210 
211 	if (!params->c1.chan)
212 		expect = &c1;
213 
214 	ret = cfg80211_chandef_compatible(&params->c2, &c1);
215 	KUNIT_EXPECT_PTR_EQ(test, ret, expect);
216 }
217 
218 static struct kunit_case chandef_compat_test_cases[] = {
219 	KUNIT_CASE_PARAM(test_chandef_compat, chandef_compat_gen_params),
220 	{}
221 };
222 
223 static struct kunit_suite chandef_compat = {
224 	.name = "cfg80211-chandef-compat",
225 	.test_cases = chandef_compat_test_cases,
226 };
227 
228 kunit_test_suite(chandef_compat);
229