|
13 | 13 |
|
14 | 14 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
15 | 15 |
|
| 16 | +#include <kunit/test.h> |
16 | 17 | #include <linux/siphash.h>
|
17 | 18 | #include <linux/kernel.h>
|
18 | 19 | #include <linux/string.h>
|
@@ -109,114 +110,88 @@ static const u32 test_vectors_hsiphash[64] = {
|
109 | 110 | };
|
110 | 111 | #endif
|
111 | 112 |
|
112 |
| -static int __init siphash_test_init(void) |
| 113 | +#define chk(hash, vector, fmt...) \ |
| 114 | + KUNIT_EXPECT_EQ_MSG(test, hash, vector, fmt) |
| 115 | + |
| 116 | +static void siphash_test(struct kunit *test) |
113 | 117 | {
|
114 | 118 | u8 in[64] __aligned(SIPHASH_ALIGNMENT);
|
115 | 119 | u8 in_unaligned[65] __aligned(SIPHASH_ALIGNMENT);
|
116 | 120 | u8 i;
|
117 |
| - int ret = 0; |
118 | 121 |
|
119 | 122 | for (i = 0; i < 64; ++i) {
|
120 | 123 | in[i] = i;
|
121 | 124 | in_unaligned[i + 1] = i;
|
122 |
| - if (siphash(in, i, &test_key_siphash) != |
123 |
| - test_vectors_siphash[i]) { |
124 |
| - pr_info("siphash self-test aligned %u: FAIL\n", i + 1); |
125 |
| - ret = -EINVAL; |
126 |
| - } |
127 |
| - if (siphash(in_unaligned + 1, i, &test_key_siphash) != |
128 |
| - test_vectors_siphash[i]) { |
129 |
| - pr_info("siphash self-test unaligned %u: FAIL\n", i + 1); |
130 |
| - ret = -EINVAL; |
131 |
| - } |
132 |
| - if (hsiphash(in, i, &test_key_hsiphash) != |
133 |
| - test_vectors_hsiphash[i]) { |
134 |
| - pr_info("hsiphash self-test aligned %u: FAIL\n", i + 1); |
135 |
| - ret = -EINVAL; |
136 |
| - } |
137 |
| - if (hsiphash(in_unaligned + 1, i, &test_key_hsiphash) != |
138 |
| - test_vectors_hsiphash[i]) { |
139 |
| - pr_info("hsiphash self-test unaligned %u: FAIL\n", i + 1); |
140 |
| - ret = -EINVAL; |
141 |
| - } |
142 |
| - } |
143 |
| - if (siphash_1u64(0x0706050403020100ULL, &test_key_siphash) != |
144 |
| - test_vectors_siphash[8]) { |
145 |
| - pr_info("siphash self-test 1u64: FAIL\n"); |
146 |
| - ret = -EINVAL; |
147 |
| - } |
148 |
| - if (siphash_2u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL, |
149 |
| - &test_key_siphash) != test_vectors_siphash[16]) { |
150 |
| - pr_info("siphash self-test 2u64: FAIL\n"); |
151 |
| - ret = -EINVAL; |
| 125 | + chk(siphash(in, i, &test_key_siphash), |
| 126 | + test_vectors_siphash[i], |
| 127 | + "siphash self-test aligned %u: FAIL", i + 1); |
| 128 | + chk(siphash(in_unaligned + 1, i, &test_key_siphash), |
| 129 | + test_vectors_siphash[i], |
| 130 | + "siphash self-test unaligned %u: FAIL", i + 1); |
| 131 | + chk(hsiphash(in, i, &test_key_hsiphash), |
| 132 | + test_vectors_hsiphash[i], |
| 133 | + "hsiphash self-test aligned %u: FAIL", i + 1); |
| 134 | + chk(hsiphash(in_unaligned + 1, i, &test_key_hsiphash), |
| 135 | + test_vectors_hsiphash[i], |
| 136 | + "hsiphash self-test unaligned %u: FAIL", i + 1); |
152 | 137 | }
|
153 |
| - if (siphash_3u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL, |
154 |
| - 0x1716151413121110ULL, &test_key_siphash) != |
155 |
| - test_vectors_siphash[24]) { |
156 |
| - pr_info("siphash self-test 3u64: FAIL\n"); |
157 |
| - ret = -EINVAL; |
158 |
| - } |
159 |
| - if (siphash_4u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL, |
| 138 | + chk(siphash_1u64(0x0706050403020100ULL, &test_key_siphash), |
| 139 | + test_vectors_siphash[8], |
| 140 | + "siphash self-test 1u64: FAIL"); |
| 141 | + chk(siphash_2u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL, |
| 142 | + &test_key_siphash), |
| 143 | + test_vectors_siphash[16], |
| 144 | + "siphash self-test 2u64: FAIL"); |
| 145 | + chk(siphash_3u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL, |
| 146 | + 0x1716151413121110ULL, &test_key_siphash), |
| 147 | + test_vectors_siphash[24], |
| 148 | + "siphash self-test 3u64: FAIL"); |
| 149 | + chk(siphash_4u64(0x0706050403020100ULL, 0x0f0e0d0c0b0a0908ULL, |
160 | 150 | 0x1716151413121110ULL, 0x1f1e1d1c1b1a1918ULL,
|
161 |
| - &test_key_siphash) != test_vectors_siphash[32]) { |
162 |
| - pr_info("siphash self-test 4u64: FAIL\n"); |
163 |
| - ret = -EINVAL; |
164 |
| - } |
165 |
| - if (siphash_1u32(0x03020100U, &test_key_siphash) != |
166 |
| - test_vectors_siphash[4]) { |
167 |
| - pr_info("siphash self-test 1u32: FAIL\n"); |
168 |
| - ret = -EINVAL; |
169 |
| - } |
170 |
| - if (siphash_2u32(0x03020100U, 0x07060504U, &test_key_siphash) != |
171 |
| - test_vectors_siphash[8]) { |
172 |
| - pr_info("siphash self-test 2u32: FAIL\n"); |
173 |
| - ret = -EINVAL; |
174 |
| - } |
175 |
| - if (siphash_3u32(0x03020100U, 0x07060504U, |
176 |
| - 0x0b0a0908U, &test_key_siphash) != |
177 |
| - test_vectors_siphash[12]) { |
178 |
| - pr_info("siphash self-test 3u32: FAIL\n"); |
179 |
| - ret = -EINVAL; |
180 |
| - } |
181 |
| - if (siphash_4u32(0x03020100U, 0x07060504U, |
182 |
| - 0x0b0a0908U, 0x0f0e0d0cU, &test_key_siphash) != |
183 |
| - test_vectors_siphash[16]) { |
184 |
| - pr_info("siphash self-test 4u32: FAIL\n"); |
185 |
| - ret = -EINVAL; |
186 |
| - } |
187 |
| - if (hsiphash_1u32(0x03020100U, &test_key_hsiphash) != |
188 |
| - test_vectors_hsiphash[4]) { |
189 |
| - pr_info("hsiphash self-test 1u32: FAIL\n"); |
190 |
| - ret = -EINVAL; |
191 |
| - } |
192 |
| - if (hsiphash_2u32(0x03020100U, 0x07060504U, &test_key_hsiphash) != |
193 |
| - test_vectors_hsiphash[8]) { |
194 |
| - pr_info("hsiphash self-test 2u32: FAIL\n"); |
195 |
| - ret = -EINVAL; |
196 |
| - } |
197 |
| - if (hsiphash_3u32(0x03020100U, 0x07060504U, |
198 |
| - 0x0b0a0908U, &test_key_hsiphash) != |
199 |
| - test_vectors_hsiphash[12]) { |
200 |
| - pr_info("hsiphash self-test 3u32: FAIL\n"); |
201 |
| - ret = -EINVAL; |
202 |
| - } |
203 |
| - if (hsiphash_4u32(0x03020100U, 0x07060504U, |
204 |
| - 0x0b0a0908U, 0x0f0e0d0cU, &test_key_hsiphash) != |
205 |
| - test_vectors_hsiphash[16]) { |
206 |
| - pr_info("hsiphash self-test 4u32: FAIL\n"); |
207 |
| - ret = -EINVAL; |
208 |
| - } |
209 |
| - if (!ret) |
210 |
| - pr_info("self-tests: pass\n"); |
211 |
| - return ret; |
| 151 | + &test_key_siphash), |
| 152 | + test_vectors_siphash[32], |
| 153 | + "siphash self-test 4u64: FAIL"); |
| 154 | + chk(siphash_1u32(0x03020100U, &test_key_siphash), |
| 155 | + test_vectors_siphash[4], |
| 156 | + "siphash self-test 1u32: FAIL"); |
| 157 | + chk(siphash_2u32(0x03020100U, 0x07060504U, &test_key_siphash), |
| 158 | + test_vectors_siphash[8], |
| 159 | + "siphash self-test 2u32: FAIL"); |
| 160 | + chk(siphash_3u32(0x03020100U, 0x07060504U, |
| 161 | + 0x0b0a0908U, &test_key_siphash), |
| 162 | + test_vectors_siphash[12], |
| 163 | + "siphash self-test 3u32: FAIL"); |
| 164 | + chk(siphash_4u32(0x03020100U, 0x07060504U, |
| 165 | + 0x0b0a0908U, 0x0f0e0d0cU, &test_key_siphash), |
| 166 | + test_vectors_siphash[16], |
| 167 | + "siphash self-test 4u32: FAIL"); |
| 168 | + chk(hsiphash_1u32(0x03020100U, &test_key_hsiphash), |
| 169 | + test_vectors_hsiphash[4], |
| 170 | + "hsiphash self-test 1u32: FAIL"); |
| 171 | + chk(hsiphash_2u32(0x03020100U, 0x07060504U, &test_key_hsiphash), |
| 172 | + test_vectors_hsiphash[8], |
| 173 | + "hsiphash self-test 2u32: FAIL"); |
| 174 | + chk(hsiphash_3u32(0x03020100U, 0x07060504U, |
| 175 | + 0x0b0a0908U, &test_key_hsiphash), |
| 176 | + test_vectors_hsiphash[12], |
| 177 | + "hsiphash self-test 3u32: FAIL"); |
| 178 | + chk(hsiphash_4u32(0x03020100U, 0x07060504U, |
| 179 | + 0x0b0a0908U, 0x0f0e0d0cU, &test_key_hsiphash), |
| 180 | + test_vectors_hsiphash[16], |
| 181 | + "hsiphash self-test 4u32: FAIL"); |
212 | 182 | }
|
213 | 183 |
|
214 |
| -static void __exit siphash_test_exit(void) |
215 |
| -{ |
216 |
| -} |
| 184 | +static struct kunit_case siphash_test_cases[] = { |
| 185 | + KUNIT_CASE(siphash_test), |
| 186 | + {} |
| 187 | +}; |
| 188 | + |
| 189 | +static struct kunit_suite siphash_test_suite = { |
| 190 | + .name = "siphash", |
| 191 | + .test_cases = siphash_test_cases, |
| 192 | +}; |
217 | 193 |
|
218 |
| -module_init(siphash_test_init); |
219 |
| -module_exit(siphash_test_exit); |
| 194 | +kunit_test_suite(siphash_test_suite); |
220 | 195 |
|
221 | 196 | MODULE_AUTHOR( "Jason A. Donenfeld <[email protected]>");
|
222 | 197 | MODULE_LICENSE("Dual BSD/GPL");
|
0 commit comments