Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

wifi: cfg80211: add first kunit tests, for element defrag

Add a couple of tests for element defragmentation, to
see that the function works correctly.

Signed-off-by: Johannes Berg <johannes.berg@intel.com>
Signed-off-by: Gregory Greenman <gregory.greenman@intel.com>
Link: https://lore.kernel.org/r/20230827135854.e2a5cead1816.I09f0edc19d162b54ee330991c728c1e9aa42ebf6@changeid
Signed-off-by: Johannes Berg <johannes.berg@intel.com>

+182
+11
net/wireless/Kconfig
··· 201 201 Drivers should select this option if they require cfg80211's 202 202 wext compatibility symbols to be exported. 203 203 204 + config CFG80211_KUNIT_TEST 205 + tristate "KUnit tests for cfg80211" if !KUNIT_ALL_TESTS 206 + depends on KUNIT 207 + depends on CFG80211 208 + default KUNIT_ALL_TESTS 209 + depends on !KERNEL_6_2 210 + help 211 + Enable this option to test cfg80211 functions with kunit. 212 + 213 + If unsure, say N. 214 + 204 215 endif # CFG80211 205 216 206 217 config LIB80211
+1
net/wireless/Makefile
··· 4 4 obj-$(CONFIG_LIB80211_CRYPT_WEP) += lib80211_crypt_wep.o 5 5 obj-$(CONFIG_LIB80211_CRYPT_CCMP) += lib80211_crypt_ccmp.o 6 6 obj-$(CONFIG_LIB80211_CRYPT_TKIP) += lib80211_crypt_tkip.o 7 + obj-y += tests/ 7 8 8 9 obj-$(CONFIG_WEXT_CORE) += wext-core.o 9 10 obj-$(CONFIG_WEXT_PROC) += wext-proc.o
+3
net/wireless/tests/Makefile
··· 1 + cfg80211-tests-y += module.o fragmentation.o 2 + 3 + obj-$(CONFIG_CFG80211_KUNIT_TEST) += cfg80211-tests.o
+157
net/wireless/tests/fragmentation.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * KUnit tests for element fragmentation 4 + * 5 + * Copyright (C) 2023 Intel Corporation 6 + */ 7 + #include <linux/ieee80211.h> 8 + #include <net/cfg80211.h> 9 + #include <kunit/test.h> 10 + 11 + static void defragment_0(struct kunit *test) 12 + { 13 + ssize_t ret; 14 + static const u8 input[] = { 15 + [0] = WLAN_EID_EXTENSION, 16 + [1] = 254, 17 + [2] = WLAN_EID_EXT_EHT_MULTI_LINK, 18 + [27] = 27, 19 + [123] = 123, 20 + [254 + 2] = WLAN_EID_FRAGMENT, 21 + [254 + 3] = 7, 22 + [254 + 3 + 7] = 0, /* for size */ 23 + }; 24 + u8 *data = kunit_kzalloc(test, sizeof(input), GFP_KERNEL); 25 + 26 + KUNIT_ASSERT_NOT_NULL(test, data); 27 + 28 + ret = cfg80211_defragment_element((void *)input, 29 + input, sizeof(input), 30 + data, sizeof(input), 31 + WLAN_EID_FRAGMENT); 32 + KUNIT_EXPECT_EQ(test, ret, 253); 33 + KUNIT_EXPECT_MEMEQ(test, data, input + 3, 253); 34 + } 35 + 36 + static void defragment_1(struct kunit *test) 37 + { 38 + ssize_t ret; 39 + static const u8 input[] = { 40 + [0] = WLAN_EID_EXTENSION, 41 + [1] = 255, 42 + [2] = WLAN_EID_EXT_EHT_MULTI_LINK, 43 + [27] = 27, 44 + [123] = 123, 45 + [255 + 2] = WLAN_EID_FRAGMENT, 46 + [255 + 3] = 7, 47 + [255 + 3 + 1] = 0xaa, 48 + [255 + 3 + 8] = WLAN_EID_FRAGMENT, /* not used */ 49 + [255 + 3 + 9] = 1, 50 + [255 + 3 + 10] = 0, /* for size */ 51 + }; 52 + u8 *data = kunit_kzalloc(test, sizeof(input), GFP_KERNEL); 53 + const struct element *elem; 54 + int count = 0; 55 + 56 + KUNIT_ASSERT_NOT_NULL(test, data); 57 + 58 + for_each_element(elem, input, sizeof(input)) 59 + count++; 60 + 61 + /* check the elements are right */ 62 + KUNIT_ASSERT_EQ(test, count, 3); 63 + 64 + ret = cfg80211_defragment_element((void *)input, 65 + input, sizeof(input), 66 + data, sizeof(input), 67 + WLAN_EID_FRAGMENT); 68 + /* this means the last fragment was not used */ 69 + KUNIT_EXPECT_EQ(test, ret, 254 + 7); 70 + KUNIT_EXPECT_MEMEQ(test, data, input + 3, 254); 71 + KUNIT_EXPECT_MEMEQ(test, data + 254, input + 255 + 4, 7); 72 + } 73 + 74 + static void defragment_2(struct kunit *test) 75 + { 76 + ssize_t ret; 77 + static const u8 input[] = { 78 + [0] = WLAN_EID_EXTENSION, 79 + [1] = 255, 80 + [2] = WLAN_EID_EXT_EHT_MULTI_LINK, 81 + [27] = 27, 82 + [123] = 123, 83 + 84 + [257 + 0] = WLAN_EID_FRAGMENT, 85 + [257 + 1] = 255, 86 + [257 + 20] = 0xaa, 87 + 88 + [2 * 257 + 0] = WLAN_EID_FRAGMENT, 89 + [2 * 257 + 1] = 1, 90 + [2 * 257 + 2] = 0xcc, 91 + [2 * 257 + 3] = WLAN_EID_FRAGMENT, /* not used */ 92 + [2 * 257 + 4] = 1, 93 + [2 * 257 + 5] = 0, /* for size */ 94 + }; 95 + u8 *data = kunit_kzalloc(test, sizeof(input), GFP_KERNEL); 96 + const struct element *elem; 97 + int count = 0; 98 + 99 + KUNIT_ASSERT_NOT_NULL(test, data); 100 + 101 + for_each_element(elem, input, sizeof(input)) 102 + count++; 103 + 104 + /* check the elements are right */ 105 + KUNIT_ASSERT_EQ(test, count, 4); 106 + 107 + ret = cfg80211_defragment_element((void *)input, 108 + input, sizeof(input), 109 + data, sizeof(input), 110 + WLAN_EID_FRAGMENT); 111 + /* this means the last fragment was not used */ 112 + KUNIT_EXPECT_EQ(test, ret, 254 + 255 + 1); 113 + KUNIT_EXPECT_MEMEQ(test, data, input + 3, 254); 114 + KUNIT_EXPECT_MEMEQ(test, data + 254, input + 257 + 2, 255); 115 + KUNIT_EXPECT_MEMEQ(test, data + 254 + 255, input + 2 * 257 + 2, 1); 116 + } 117 + 118 + static void defragment_at_end(struct kunit *test) 119 + { 120 + ssize_t ret; 121 + static const u8 input[] = { 122 + [0] = WLAN_EID_EXTENSION, 123 + [1] = 255, 124 + [2] = WLAN_EID_EXT_EHT_MULTI_LINK, 125 + [27] = 27, 126 + [123] = 123, 127 + [255 + 2] = WLAN_EID_FRAGMENT, 128 + [255 + 3] = 7, 129 + [255 + 3 + 7] = 0, /* for size */ 130 + }; 131 + u8 *data = kunit_kzalloc(test, sizeof(input), GFP_KERNEL); 132 + 133 + KUNIT_ASSERT_NOT_NULL(test, data); 134 + 135 + ret = cfg80211_defragment_element((void *)input, 136 + input, sizeof(input), 137 + data, sizeof(input), 138 + WLAN_EID_FRAGMENT); 139 + KUNIT_EXPECT_EQ(test, ret, 254 + 7); 140 + KUNIT_EXPECT_MEMEQ(test, data, input + 3, 254); 141 + KUNIT_EXPECT_MEMEQ(test, data + 254, input + 255 + 4, 7); 142 + } 143 + 144 + static struct kunit_case element_fragmentation_test_cases[] = { 145 + KUNIT_CASE(defragment_0), 146 + KUNIT_CASE(defragment_1), 147 + KUNIT_CASE(defragment_2), 148 + KUNIT_CASE(defragment_at_end), 149 + {} 150 + }; 151 + 152 + static struct kunit_suite element_fragmentation = { 153 + .name = "cfg80211-element-defragmentation", 154 + .test_cases = element_fragmentation_test_cases, 155 + }; 156 + 157 + kunit_test_suite(element_fragmentation);
+10
net/wireless/tests/module.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * This is just module boilerplate for the cfg80211 kunit module. 4 + * 5 + * Copyright (C) 2023 Intel Corporation 6 + */ 7 + #include <linux/module.h> 8 + 9 + MODULE_LICENSE("GPL"); 10 + MODULE_DESCRIPTION("tests for cfg80211");