chan.c 4.9 KB

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