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); 158