summaryrefslogtreecommitdiff
path: root/lib/string_helpers_kunit.c
blob: c853046183d2452fb0f765058db194372b582cfc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/*
 * Test cases for lib/string_helpers.c module.
 */
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

#include <kunit/test.h>
#include <linux/array_size.h>
#include <linux/kernel.h>
#include <linux/random.h>
#include <linux/string.h>
#include <linux/string_helpers.h>

static void test_string_check_buf(struct kunit *test,
				  const char *name, unsigned int flags,
				  char *in, size_t p,
				  char *out_real, size_t q_real,
				  char *out_test, size_t q_test)
{
	KUNIT_ASSERT_EQ_MSG(test, q_real, q_test, "name:%s", name);
	KUNIT_EXPECT_MEMEQ_MSG(test, out_test, out_real, q_test,
			       "name:%s", name);
}

struct test_string {
	const char *in;
	const char *out;
	unsigned int flags;
};

static const struct test_string strings[] = {
	{
		.in = "\\f\\ \\n\\r\\t\\v",
		.out = "\f\\ \n\r\t\v",
		.flags = UNESCAPE_SPACE,
	},
	{
		.in = "\\40\\1\\387\\0064\\05\\040\\8a\\110\\777",
		.out = " \001\00387\0064\005 \\8aH?7",
		.flags = UNESCAPE_OCTAL,
	},
	{
		.in = "\\xv\\xa\\x2c\\xD\\x6f2",
		.out = "\\xv\n,\ro2",
		.flags = UNESCAPE_HEX,
	},
	{
		.in = "\\h\\\\\\\"\\a\\e\\",
		.out = "\\h\\\"\a\e\\",
		.flags = UNESCAPE_SPECIAL,
	},
};

static void test_string_unescape(struct kunit *test,
				 const char *name, unsigned int flags,
				 bool inplace)
{
	int q_real = 256;
	char *in = kunit_kzalloc(test, q_real, GFP_KERNEL);
	char *out_test = kunit_kzalloc(test, q_real, GFP_KERNEL);
	char *out_real = kunit_kzalloc(test, q_real, GFP_KERNEL);
	int i, p = 0, q_test = 0;

	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real);

	for (i = 0; i < ARRAY_SIZE(strings); i++) {
		const char *s = strings[i].in;
		int len = strlen(strings[i].in);

		/* Copy string to in buffer */
		memcpy(&in[p], s, len);
		p += len;

		/* Copy expected result for given flags */
		if (flags & strings[i].flags) {
			s = strings[i].out;
			len = strlen(strings[i].out);
		}
		memcpy(&out_test[q_test], s, len);
		q_test += len;
	}
	in[p++] = '\0';

	/* Call string_unescape and compare result */
	if (inplace) {
		memcpy(out_real, in, p);
		if (flags == UNESCAPE_ANY)
			q_real = string_unescape_any_inplace(out_real);
		else
			q_real = string_unescape_inplace(out_real, flags);
	} else if (flags == UNESCAPE_ANY) {
		q_real = string_unescape_any(in, out_real, q_real);
	} else {
		q_real = string_unescape(in, out_real, q_real, flags);
	}

	test_string_check_buf(test, name, flags, in, p - 1, out_real, q_real,
			      out_test, q_test);
}

struct test_string_1 {
	const char *out;
	unsigned int flags;
};

#define	TEST_STRING_2_MAX_S1		32
struct test_string_2 {
	const char *in;
	struct test_string_1 s1[TEST_STRING_2_MAX_S1];
};

#define	TEST_STRING_2_DICT_0		NULL
static const struct test_string_2 escape0[] = {{
	.in = "\f\\ \n\r\t\v",
	.s1 = {{
		.out = "\\f\\ \\n\\r\\t\\v",
		.flags = ESCAPE_SPACE,
	},{
		.out = "\\f\\134\\040\\n\\r\\t\\v",
		.flags = ESCAPE_SPACE | ESCAPE_OCTAL,
	},{
		.out = "\\f\\x5c\\x20\\n\\r\\t\\v",
		.flags = ESCAPE_SPACE | ESCAPE_HEX,
	},{
		/* terminator */
	}}
},{
	.in = "\\h\\\"\a\e\\",
	.s1 = {{
		.out = "\\\\h\\\\\\\"\\a\\e\\\\",
		.flags = ESCAPE_SPECIAL,
	},{
		.out = "\\\\\\150\\\\\\\"\\a\\e\\\\",
		.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
	},{
		.out = "\\\\\\x68\\\\\\\"\\a\\e\\\\",
		.flags = ESCAPE_SPECIAL | ESCAPE_HEX,
	},{
		/* terminator */
	}}
},{
	.in = "\eb \\C\007\"\x90\r]",
	.s1 = {{
		.out = "\eb \\C\007\"\x90\\r]",
		.flags = ESCAPE_SPACE,
	},{
		.out = "\\eb \\\\C\\a\\\"\x90\r]",
		.flags = ESCAPE_SPECIAL,
	},{
		.out = "\\eb \\\\C\\a\\\"\x90\\r]",
		.flags = ESCAPE_SPACE | ESCAPE_SPECIAL,
	},{
		.out = "\\033\\142\\040\\134\\103\\007\\042\\220\\015\\135",
		.flags = ESCAPE_OCTAL,
	},{
		.out = "\\033\\142\\040\\134\\103\\007\\042\\220\\r\\135",
		.flags = ESCAPE_SPACE | ESCAPE_OCTAL,
	},{
		.out = "\\e\\142\\040\\\\\\103\\a\\\"\\220\\015\\135",
		.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
	},{
		.out = "\\e\\142\\040\\\\\\103\\a\\\"\\220\\r\\135",
		.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_OCTAL,
	},{
		.out = "\eb \\C\007\"\x90\r]",
		.flags = ESCAPE_NP,
	},{
		.out = "\eb \\C\007\"\x90\\r]",
		.flags = ESCAPE_SPACE | ESCAPE_NP,
	},{
		.out = "\\eb \\C\\a\"\x90\r]",
		.flags = ESCAPE_SPECIAL | ESCAPE_NP,
	},{
		.out = "\\eb \\C\\a\"\x90\\r]",
		.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_NP,
	},{
		.out = "\\033b \\C\\007\"\\220\\015]",
		.flags = ESCAPE_OCTAL | ESCAPE_NP,
	},{
		.out = "\\033b \\C\\007\"\\220\\r]",
		.flags = ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_NP,
	},{
		.out = "\\eb \\C\\a\"\\220\\r]",
		.flags = ESCAPE_SPECIAL | ESCAPE_SPACE | ESCAPE_OCTAL |
			 ESCAPE_NP,
	},{
		.out = "\\x1bb \\C\\x07\"\\x90\\x0d]",
		.flags = ESCAPE_NP | ESCAPE_HEX,
	},{
		/* terminator */
	}}
},{
	.in = "\007 \eb\"\x90\xCF\r",
	.s1 = {{
		.out = "\007 \eb\"\\220\\317\r",
		.flags = ESCAPE_OCTAL | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\\x90\\xcf\r",
		.flags = ESCAPE_HEX | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\xCF\r",
		.flags = ESCAPE_NA,
	},{
		/* terminator */
	}}
},{
	/* terminator */
}};

#define	TEST_STRING_2_DICT_1		"b\\ \t\r\xCF"
static const struct test_string_2 escape1[] = {{
	.in = "\f\\ \n\r\t\v",
	.s1 = {{
		.out = "\f\\134\\040\n\\015\\011\v",
		.flags = ESCAPE_OCTAL,
	},{
		.out = "\f\\x5c\\x20\n\\x0d\\x09\v",
		.flags = ESCAPE_HEX,
	},{
		.out = "\f\\134\\040\n\\015\\011\v",
		.flags = ESCAPE_ANY | ESCAPE_APPEND,
	},{
		.out = "\\014\\134\\040\\012\\015\\011\\013",
		.flags = ESCAPE_OCTAL | ESCAPE_APPEND | ESCAPE_NAP,
	},{
		.out = "\\x0c\\x5c\\x20\\x0a\\x0d\\x09\\x0b",
		.flags = ESCAPE_HEX | ESCAPE_APPEND | ESCAPE_NAP,
	},{
		.out = "\f\\134\\040\n\\015\\011\v",
		.flags = ESCAPE_OCTAL | ESCAPE_APPEND | ESCAPE_NA,
	},{
		.out = "\f\\x5c\\x20\n\\x0d\\x09\v",
		.flags = ESCAPE_HEX | ESCAPE_APPEND | ESCAPE_NA,
	},{
		/* terminator */
	}}
},{
	.in = "\\h\\\"\a\xCF\e\\",
	.s1 = {{
		.out = "\\134h\\134\"\a\\317\e\\134",
		.flags = ESCAPE_OCTAL,
	},{
		.out = "\\134h\\134\"\a\\317\e\\134",
		.flags = ESCAPE_ANY | ESCAPE_APPEND,
	},{
		.out = "\\134h\\134\"\\007\\317\\033\\134",
		.flags = ESCAPE_OCTAL | ESCAPE_APPEND | ESCAPE_NAP,
	},{
		.out = "\\134h\\134\"\a\\317\e\\134",
		.flags = ESCAPE_OCTAL | ESCAPE_APPEND | ESCAPE_NA,
	},{
		/* terminator */
	}}
},{
	.in = "\eb \\C\007\"\x90\r]",
	.s1 = {{
		.out = "\e\\142\\040\\134C\007\"\x90\\015]",
		.flags = ESCAPE_OCTAL,
	},{
		/* terminator */
	}}
},{
	.in = "\007 \eb\"\x90\xCF\r",
	.s1 = {{
		.out = "\007 \eb\"\x90\xCF\r",
		.flags = ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\xCF\r",
		.flags = ESCAPE_SPACE | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\xCF\r",
		.flags = ESCAPE_SPECIAL | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\xCF\r",
		.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\\317\r",
		.flags = ESCAPE_OCTAL | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\\317\r",
		.flags = ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\\317\r",
		.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\\317\r",
		.flags = ESCAPE_ANY | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\\xcf\r",
		.flags = ESCAPE_HEX | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\\xcf\r",
		.flags = ESCAPE_SPACE | ESCAPE_HEX | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\\xcf\r",
		.flags = ESCAPE_SPECIAL | ESCAPE_HEX | ESCAPE_NA,
	},{
		.out = "\007 \eb\"\x90\\xcf\r",
		.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_HEX | ESCAPE_NA,
	},{
		/* terminator */
	}}
},{
	.in = "\007 \eb\"\x90\xCF\r",
	.s1 = {{
		.out = "\007 \eb\"\x90\xCF\r",
		.flags = ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\xCF\\r",
		.flags = ESCAPE_SPACE | ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\xCF\r",
		.flags = ESCAPE_SPECIAL | ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\xCF\\r",
		.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\\317\\015",
		.flags = ESCAPE_OCTAL | ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\\317\\r",
		.flags = ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\\317\\015",
		.flags = ESCAPE_SPECIAL | ESCAPE_OCTAL | ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\\317\r",
		.flags = ESCAPE_ANY | ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\\xcf\\x0d",
		.flags = ESCAPE_HEX | ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\\xcf\\r",
		.flags = ESCAPE_SPACE | ESCAPE_HEX | ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\\xcf\\x0d",
		.flags = ESCAPE_SPECIAL | ESCAPE_HEX | ESCAPE_NAP,
	},{
		.out = "\007 \eb\"\x90\\xcf\\r",
		.flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_HEX | ESCAPE_NAP,
	},{
		/* terminator */
	}}
},{
	/* terminator */
}};

static const struct test_string strings_upper[] = {
	{
		.in = "abcdefgh1234567890test",
		.out = "ABCDEFGH1234567890TEST",
	},
	{
		.in = "abCdeFgH1234567890TesT",
		.out = "ABCDEFGH1234567890TEST",
	},
};

static const struct test_string strings_lower[] = {
	{
		.in = "ABCDEFGH1234567890TEST",
		.out = "abcdefgh1234567890test",
	},
	{
		.in = "abCdeFgH1234567890TesT",
		.out = "abcdefgh1234567890test",
	},
};

static const char *test_string_find_match(const struct test_string_2 *s2,
					  unsigned int flags)
{
	const struct test_string_1 *s1 = s2->s1;
	unsigned int i;

	if (!flags)
		return s2->in;

	/* Test cases are NULL-aware */
	flags &= ~ESCAPE_NULL;

	/* ESCAPE_OCTAL has a higher priority */
	if (flags & ESCAPE_OCTAL)
		flags &= ~ESCAPE_HEX;

	for (i = 0; i < TEST_STRING_2_MAX_S1 && s1->out; i++, s1++)
		if (s1->flags == flags)
			return s1->out;
	return NULL;
}

static void
test_string_escape_overflow(struct kunit *test,
			    const char *in, int p, unsigned int flags, const char *esc,
			    int q_test, const char *name)
{
	int q_real;

	q_real = string_escape_mem(in, p, NULL, 0, flags, esc);
	KUNIT_EXPECT_EQ_MSG(test, q_real, q_test, "name:%s: flags:%#x", name, flags);
}

static void test_string_escape(struct kunit *test, const char *name,
			       const struct test_string_2 *s2,
			       unsigned int flags, const char *esc)
{
	size_t out_size = 512;
	char *out_test = kunit_kzalloc(test, out_size, GFP_KERNEL);
	char *out_real = kunit_kzalloc(test, out_size, GFP_KERNEL);
	char *in = kunit_kzalloc(test, 256, GFP_KERNEL);
	int p = 0, q_test = 0;
	int q_real;

	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_test);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, out_real);
	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, in);

	for (; s2->in; s2++) {
		const char *out;
		int len;

		/* NULL injection */
		if (flags & ESCAPE_NULL) {
			in[p++] = '\0';
			/* '\0' passes isascii() test */
			if (flags & ESCAPE_NA && !(flags & ESCAPE_APPEND && esc)) {
				out_test[q_test++] = '\0';
			} else {
				out_test[q_test++] = '\\';
				out_test[q_test++] = '0';
			}
		}

		/* Don't try strings that have no output */
		out = test_string_find_match(s2, flags);
		if (!out)
			continue;

		/* Copy string to in buffer */
		len = strlen(s2->in);
		memcpy(&in[p], s2->in, len);
		p += len;

		/* Copy expected result for given flags */
		len = strlen(out);
		memcpy(&out_test[q_test], out, len);
		q_test += len;
	}

	q_real = string_escape_mem(in, p, out_real, out_size, flags, esc);

	test_string_check_buf(test, name, flags, in, p, out_real, q_real, out_test,
			      q_test);

	test_string_escape_overflow(test, in, p, flags, esc, q_test, name);
}

#define string_get_size_maxbuf 16
#define test_string_get_size_one(size, blk_size, exp_result10, exp_result2)      \
	do {                                                                     \
		BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf);    \
		BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf);     \
		__test_string_get_size(test, (size), (blk_size), (exp_result10), \
				       (exp_result2));                           \
	} while (0)


static void test_string_get_size_check(struct kunit *test,
				       const char *units,
				       const char *exp,
				       char *res,
				       const u64 size,
				       const u64 blk_size)
{
	KUNIT_EXPECT_MEMEQ_MSG(test, res, exp, strlen(exp) + 1,
		"string_get_size(size = %llu, blk_size = %llu, units = %s)",
		size, blk_size, units);
}

static void __strchrcut(char *dst, const char *src, const char *cut)
{
	const char *from = src;
	size_t len;

	do {
		len = strcspn(from, cut);
		memcpy(dst, from, len);
		dst += len;
		from += len;
	} while (*from++);
	*dst = '\0';
}

static void __test_string_get_size_one(struct kunit *test,
				       const u64 size, const u64 blk_size,
				       const char *exp_result10,
				       const char *exp_result2,
				       enum string_size_units units,
				       const char *cut)
{
	char buf10[string_get_size_maxbuf];
	char buf2[string_get_size_maxbuf];
	char exp10[string_get_size_maxbuf];
	char exp2[string_get_size_maxbuf];
	char prefix10[64];
	char prefix2[64];

	sprintf(prefix10, "STRING_UNITS_10 [%s]", cut);
	sprintf(prefix2, "STRING_UNITS_2 [%s]", cut);

	__strchrcut(exp10, exp_result10, cut);
	__strchrcut(exp2, exp_result2, cut);

	string_get_size(size, blk_size, STRING_UNITS_10 | units, buf10, sizeof(buf10));
	string_get_size(size, blk_size, STRING_UNITS_2 | units, buf2, sizeof(buf2));

	test_string_get_size_check(test, prefix10, exp10, buf10, size, blk_size);
	test_string_get_size_check(test, prefix2, exp2, buf2, size, blk_size);
}

static void __test_string_get_size(struct kunit *test,
				   const u64 size, const u64 blk_size,
				   const char *exp_result10,
				   const char *exp_result2)
{
	struct {
		enum string_size_units units;
		const char *cut;
	} get_size_test_cases[] = {
		{ 0, "" },
		{ STRING_UNITS_NO_SPACE, " " },
		{ STRING_UNITS_NO_SPACE | STRING_UNITS_NO_BYTES, " B" },
		{ STRING_UNITS_NO_BYTES, "B" },
	};
	int i;

	for (i = 0; i < ARRAY_SIZE(get_size_test_cases); i++)
		__test_string_get_size_one(test, size, blk_size,
					   exp_result10, exp_result2,
					   get_size_test_cases[i].units,
					   get_size_test_cases[i].cut);
}

static void test_get_size(struct kunit *test)
{
	/* small values */
	test_string_get_size_one(0, 512, "0 B", "0 B");
	test_string_get_size_one(1, 512, "512 B", "512 B");
	test_string_get_size_one(1100, 1, "1.10 kB", "1.07 KiB");

	/* normal values */
	test_string_get_size_one(16384, 512, "8.39 MB", "8.00 MiB");
	test_string_get_size_one(500118192, 512, "256 GB", "238 GiB");
	test_string_get_size_one(8192, 4096, "33.6 MB", "32.0 MiB");

	/* weird block sizes */
	test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");

	/* huge values */
	test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB");
	test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
}

static void test_upper_lower(struct kunit *test)
{
	char *dst;
	int i;

	for (i = 0; i < ARRAY_SIZE(strings_upper); i++) {
		const char *s = strings_upper[i].in;
		int len = strlen(strings_upper[i].in) + 1;

		dst = kmalloc(len, GFP_KERNEL);
		KUNIT_ASSERT_NOT_NULL(test, dst);

		string_upper(dst, s);
		KUNIT_EXPECT_STREQ(test, dst, strings_upper[i].out);
		kfree(dst);
	}

	for (i = 0; i < ARRAY_SIZE(strings_lower); i++) {
		const char *s = strings_lower[i].in;
		int len = strlen(strings_lower[i].in) + 1;

		dst = kmalloc(len, GFP_KERNEL);
		KUNIT_ASSERT_NOT_NULL(test, dst);

		string_lower(dst, s);
		KUNIT_EXPECT_STREQ(test, dst, strings_lower[i].out);
		kfree(dst);
	}
}

static void test_unescape(struct kunit *test)
{
	unsigned int i;

	for (i = 0; i < UNESCAPE_ALL_MASK + 1; i++)
		test_string_unescape(test, "unescape", i, false);
	test_string_unescape(test, "unescape inplace",
			     get_random_u32_below(UNESCAPE_ALL_MASK + 1), true);

	/* Without dictionary */
	for (i = 0; i < ESCAPE_ALL_MASK + 1; i++)
		test_string_escape(test, "escape 0", escape0, i, TEST_STRING_2_DICT_0);

	/* With dictionary */
	for (i = 0; i < ESCAPE_ALL_MASK + 1; i++)
		test_string_escape(test, "escape 1", escape1, i, TEST_STRING_2_DICT_1);
}

static struct kunit_case string_helpers_test_cases[] = {
	KUNIT_CASE(test_get_size),
	KUNIT_CASE(test_upper_lower),
	KUNIT_CASE(test_unescape),
	{}
};

static struct kunit_suite string_helpers_test_suite = {
	.name = "string_helpers",
	.test_cases = string_helpers_test_cases,
};

kunit_test_suites(&string_helpers_test_suite);

MODULE_DESCRIPTION("Test cases for string helpers module");
MODULE_LICENSE("Dual BSD/GPL");