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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
|
// SPDX-License-Identifier: GPL-2.0-only
/*
* Test cases for string functions.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <kunit/test.h>
#include <linux/ktime.h>
#include <linux/math64.h>
#include <linux/minmax.h>
#include <linux/mm.h>
#include <linux/module.h>
#include <linux/prandom.h>
#include <linux/printk.h>
#include <linux/slab.h>
#include <linux/string.h>
#include <linux/time64.h>
#include <linux/units.h>
#include <linux/vmalloc.h>
#define STRCMP_LARGE_BUF_LEN 2048
#define STRCMP_CHANGE_POINT 1337
#define STRCMP_TEST_EXPECT_EQUAL(test, fn, ...) KUNIT_EXPECT_EQ(test, fn(__VA_ARGS__), 0)
#define STRCMP_TEST_EXPECT_LOWER(test, fn, ...) KUNIT_EXPECT_LT(test, fn(__VA_ARGS__), 0)
#define STRCMP_TEST_EXPECT_GREATER(test, fn, ...) KUNIT_EXPECT_GT(test, fn(__VA_ARGS__), 0)
#define STRING_TEST_MAX_LEN 128
#define STRING_TEST_MAX_OFFSET 16
#define STRING_BENCH_SEED 888
#define STRING_BENCH_WORKLOAD (1 * MEGA)
static void string_test_memset16(struct kunit *test)
{
unsigned i, j, k;
u16 v, *p;
p = kunit_kzalloc(test, 256 * 2 * 2, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p);
for (i = 0; i < 256; i++) {
for (j = 0; j < 256; j++) {
memset(p, 0xa1, 256 * 2 * sizeof(v));
memset16(p + i, 0xb1b2, j);
for (k = 0; k < 512; k++) {
v = p[k];
if (k < i) {
KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1,
"i:%d j:%d k:%d", i, j, k);
} else if (k < i + j) {
KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2,
"i:%d j:%d k:%d", i, j, k);
} else {
KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1,
"i:%d j:%d k:%d", i, j, k);
}
}
}
}
}
static void string_test_memset32(struct kunit *test)
{
unsigned i, j, k;
u32 v, *p;
p = kunit_kzalloc(test, 256 * 2 * 4, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p);
for (i = 0; i < 256; i++) {
for (j = 0; j < 256; j++) {
memset(p, 0xa1, 256 * 2 * sizeof(v));
memset32(p + i, 0xb1b2b3b4, j);
for (k = 0; k < 512; k++) {
v = p[k];
if (k < i) {
KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1,
"i:%d j:%d k:%d", i, j, k);
} else if (k < i + j) {
KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2b3b4,
"i:%d j:%d k:%d", i, j, k);
} else {
KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1,
"i:%d j:%d k:%d", i, j, k);
}
}
}
}
}
static void string_test_memset64(struct kunit *test)
{
unsigned i, j, k;
u64 v, *p;
p = kunit_kzalloc(test, 256 * 2 * 8, GFP_KERNEL);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, p);
for (i = 0; i < 256; i++) {
for (j = 0; j < 256; j++) {
memset(p, 0xa1, 256 * 2 * sizeof(v));
memset64(p + i, 0xb1b2b3b4b5b6b7b8ULL, j);
for (k = 0; k < 512; k++) {
v = p[k];
if (k < i) {
KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1a1a1a1a1ULL,
"i:%d j:%d k:%d", i, j, k);
} else if (k < i + j) {
KUNIT_ASSERT_EQ_MSG(test, v, 0xb1b2b3b4b5b6b7b8ULL,
"i:%d j:%d k:%d", i, j, k);
} else {
KUNIT_ASSERT_EQ_MSG(test, v, 0xa1a1a1a1a1a1a1a1ULL,
"i:%d j:%d k:%d", i, j, k);
}
}
}
}
}
static void string_test_strlen(struct kunit *test)
{
size_t buf_size;
char *buf, *s;
buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);
buf = vmalloc(buf_size);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
memset(buf, 'A', buf_size);
for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {
s = buf + buf_size - 1 - offset - len;
s[len] = '\0';
KUNIT_EXPECT_EQ_MSG(test, strlen(s), len,
"offset:%zu len:%zu", offset, len);
s[len] = 'A';
}
}
vfree(buf);
}
static void string_test_strnlen(struct kunit *test)
{
size_t buf_size;
char *buf, *s;
buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);
buf = vmalloc(buf_size);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
memset(buf, 'A', buf_size);
for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {
s = buf + buf_size - 1 - offset - len;
s[len] = '\0';
if (len > 0)
KUNIT_EXPECT_EQ(test, strnlen(s, len - 1), len - 1);
if (len > 1)
KUNIT_EXPECT_EQ(test, strnlen(s, len - 2), len - 2);
KUNIT_EXPECT_EQ(test, strnlen(s, len), len);
KUNIT_EXPECT_EQ(test, strnlen(s, len + 1), len);
KUNIT_EXPECT_EQ(test, strnlen(s, len + 2), len);
KUNIT_EXPECT_EQ(test, strnlen(s, len + 10), len);
s[len] = 'A';
}
}
vfree(buf);
}
static void string_test_strchr(struct kunit *test)
{
const char *test_string = "abcdefghijkl";
const char *empty_string = "";
char *result;
int i;
for (i = 0; i < strlen(test_string) + 1; i++) {
result = strchr(test_string, test_string[i]);
KUNIT_ASSERT_EQ_MSG(test, result - test_string, i,
"char:%c", 'a' + i);
}
result = strchr(empty_string, '\0');
KUNIT_ASSERT_PTR_EQ(test, result, empty_string);
result = strchr(empty_string, 'a');
KUNIT_ASSERT_NULL(test, result);
result = strchr(test_string, 'z');
KUNIT_ASSERT_NULL(test, result);
}
static void string_test_strrchr(struct kunit *test)
{
size_t buf_size;
char *buf, *s;
buf_size = PAGE_ALIGN(STRING_TEST_MAX_LEN + STRING_TEST_MAX_OFFSET + 1);
buf = vmalloc(buf_size);
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buf);
memset(buf, 'A', buf_size);
for (size_t offset = 0; offset < STRING_TEST_MAX_OFFSET; offset++) {
for (size_t len = 0; len <= STRING_TEST_MAX_LEN; len++) {
s = buf + buf_size - 1 - offset - len;
s[len] = '\0';
KUNIT_EXPECT_PTR_EQ(test, strrchr(s, 'Z'), NULL);
if (len > 0)
KUNIT_EXPECT_PTR_EQ(test, strrchr(s, 'A'), s + len - 1);
else
KUNIT_EXPECT_PTR_EQ(test, strrchr(s, 'A'), NULL);
s[len] = 'A';
}
}
vfree(buf);
}
static void string_test_strnchr(struct kunit *test)
{
const char *test_string = "abcdefghijkl";
const char *empty_string = "";
char *result;
int i, j;
for (i = 0; i < strlen(test_string) + 1; i++) {
for (j = 0; j < strlen(test_string) + 2; j++) {
result = strnchr(test_string, j, test_string[i]);
if (j <= i) {
KUNIT_ASSERT_NULL_MSG(test, result,
"char:%c i:%d j:%d", 'a' + i, i, j);
} else {
KUNIT_ASSERT_EQ_MSG(test, result - test_string, i,
"char:%c i:%d j:%d", 'a' + i, i, j);
}
}
}
result = strnchr(empty_string, 0, '\0');
KUNIT_ASSERT_NULL(test, result);
result = strnchr(empty_string, 1, '\0');
KUNIT_ASSERT_PTR_EQ(test, result, empty_string);
result = strnchr(empty_string, 1, 'a');
KUNIT_ASSERT_NULL(test, result);
result = strnchr(NULL, 0, '\0');
KUNIT_ASSERT_NULL(test, result);
}
static void string_test_strspn(struct kunit *test)
{
static const struct strspn_test {
const char str[16];
const char accept[16];
const char reject[16];
unsigned a;
unsigned r;
} tests[] = {
{ "foobar", "", "", 0, 6 },
{ "abba", "abc", "ABBA", 4, 4 },
{ "abba", "a", "b", 1, 1 },
{ "", "abc", "abc", 0, 0},
};
const struct strspn_test *s = tests;
size_t i;
for (i = 0; i < ARRAY_SIZE(tests); ++i, ++s) {
KUNIT_ASSERT_EQ_MSG(test, s->a, strspn(s->str, s->accept),
"i:%zu", i);
KUNIT_ASSERT_EQ_MSG(test, s->r, strcspn(s->str, s->reject),
"i:%zu", i);
}
}
static char strcmp_buffer1[STRCMP_LARGE_BUF_LEN];
static char strcmp_buffer2[STRCMP_LARGE_BUF_LEN];
static void strcmp_fill_buffers(char fill1, char fill2)
{
memset(strcmp_buffer1, fill1, STRCMP_LARGE_BUF_LEN);
memset(strcmp_buffer2, fill2, STRCMP_LARGE_BUF_LEN);
strcmp_buffer1[STRCMP_LARGE_BUF_LEN - 1] = 0;
strcmp_buffer2[STRCMP_LARGE_BUF_LEN - 1] = 0;
}
static void string_test_strcmp(struct kunit *test)
{
/* Equal strings */
STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "Hello, Kernel!", "Hello, Kernel!");
/* First string is lexicographically less than the second */
STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Hello, KUnit!", "Hello, Kernel!");
/* First string is lexicographically larger than the second */
STRCMP_TEST_EXPECT_GREATER(test, strcmp, "Hello, Kernel!", "Hello, KUnit!");
/* Empty string is always lexicographically less than any non-empty string */
STRCMP_TEST_EXPECT_LOWER(test, strcmp, "", "Non-empty string");
/* Two empty strings should be equal */
STRCMP_TEST_EXPECT_EQUAL(test, strcmp, "", "");
/* Compare two strings which have only one char difference */
STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Abacaba", "Abadaba");
/* Compare two strings which have the same prefix*/
STRCMP_TEST_EXPECT_LOWER(test, strcmp, "Just a string", "Just a string and something else");
}
static void string_test_strcmp_long_strings(struct kunit *test)
{
strcmp_fill_buffers('B', 'B');
STRCMP_TEST_EXPECT_EQUAL(test, strcmp, strcmp_buffer1, strcmp_buffer2);
strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A';
STRCMP_TEST_EXPECT_LOWER(test, strcmp, strcmp_buffer1, strcmp_buffer2);
strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';
STRCMP_TEST_EXPECT_GREATER(test, strcmp, strcmp_buffer1, strcmp_buffer2);
}
static void string_test_strncmp(struct kunit *test)
{
/* Equal strings */
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, KUnit!", "Hello, KUnit!", 13);
/* First string is lexicographically less than the second */
STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Hello, KUnit!", "Hello, Kernel!", 13);
/* Result is always 'equal' when count = 0 */
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Hello, Kernel!", "Hello, KUnit!", 0);
/* Strings with common prefix are equal if count = length of prefix */
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Abacaba", "Abadaba", 3);
/* Strings with common prefix are not equal when count = length of prefix + 1 */
STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Abacaba", "Abadaba", 4);
/* If one string is a prefix of another, the shorter string is lexicographically smaller */
STRCMP_TEST_EXPECT_LOWER(test, strncmp, "Just a string", "Just a string and something else",
strlen("Just a string and something else"));
/*
* If one string is a prefix of another, and we check first length
* of prefix chars, the result is 'equal'
*/
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, "Just a string", "Just a string and something else",
strlen("Just a string"));
}
static void string_test_strncmp_long_strings(struct kunit *test)
{
strcmp_fill_buffers('B', 'B');
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
strcmp_buffer1[STRCMP_CHANGE_POINT] = 'A';
STRCMP_TEST_EXPECT_LOWER(test, strncmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';
STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
/* the strings are equal up to STRCMP_CHANGE_POINT */
STRCMP_TEST_EXPECT_EQUAL(test, strncmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_CHANGE_POINT);
STRCMP_TEST_EXPECT_GREATER(test, strncmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_CHANGE_POINT + 1);
}
static void string_test_strcasecmp(struct kunit *test)
{
/* Same strings in different case should be equal */
STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "Hello, Kernel!", "HeLLO, KErNeL!");
/* Empty strings should be equal */
STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "", "");
/* Despite ascii code for 'a' is larger than ascii code for 'B', 'a' < 'B' */
STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, "a", "B");
STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, "B", "a");
/* Special symbols and numbers should be processed correctly */
STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, "-+**.1230ghTTT~^", "-+**.1230Ghttt~^");
}
static void string_test_strcasecmp_long_strings(struct kunit *test)
{
strcmp_fill_buffers('b', 'B');
STRCMP_TEST_EXPECT_EQUAL(test, strcasecmp, strcmp_buffer1, strcmp_buffer2);
strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a';
STRCMP_TEST_EXPECT_LOWER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2);
strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';
STRCMP_TEST_EXPECT_GREATER(test, strcasecmp, strcmp_buffer1, strcmp_buffer2);
}
static void string_test_strncasecmp(struct kunit *test)
{
/* Same strings in different case should be equal */
STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbAcAbA", "Abacaba", strlen("Abacaba"));
/* strncasecmp should check 'count' chars only */
STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "AbaCaBa", "abaCaDa", 5);
STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, "a", "B", 1);
STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, "B", "a", 1);
/* Result is always 'equal' when count = 0 */
STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, "Abacaba", "Not abacaba", 0);
}
static void string_test_strncasecmp_long_strings(struct kunit *test)
{
strcmp_fill_buffers('b', 'B');
STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
strcmp_buffer1[STRCMP_CHANGE_POINT] = 'a';
STRCMP_TEST_EXPECT_LOWER(test, strncasecmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
strcmp_buffer1[STRCMP_CHANGE_POINT] = 'C';
STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_LARGE_BUF_LEN);
STRCMP_TEST_EXPECT_EQUAL(test, strncasecmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_CHANGE_POINT);
STRCMP_TEST_EXPECT_GREATER(test, strncasecmp, strcmp_buffer1,
strcmp_buffer2, STRCMP_CHANGE_POINT + 1);
}
/**
* strscpy_check() - Run a specific test case.
* @test: KUnit test context pointer
* @src: Source string, argument to strscpy_pad()
* @count: Size of destination buffer, argument to strscpy_pad()
* @expected: Expected return value from call to strscpy_pad()
* @chars: Number of characters from the src string expected to be
* written to the dst buffer.
* @terminator: 1 if there should be a terminating null byte 0 otherwise.
* @pad: Number of pad characters expected (in the tail of dst buffer).
* (@pad does not include the null terminator byte.)
*
* Calls strscpy_pad() and verifies the return value and state of the
* destination buffer after the call returns.
*/
static void strscpy_check(struct kunit *test, char *src, int count,
int expected, int chars, int terminator, int pad)
{
int nr_bytes_poison;
int max_expected;
int max_count;
int written;
char buf[6];
int index, i;
const char POISON = 'z';
KUNIT_ASSERT_TRUE_MSG(test, src != NULL,
"null source string not supported");
memset(buf, POISON, sizeof(buf));
/* Future proofing test suite, validate args */
max_count = sizeof(buf) - 2; /* Space for null and to verify overflow */
max_expected = count - 1; /* Space for the null */
KUNIT_ASSERT_LE_MSG(test, count, max_count,
"count (%d) is too big (%d) ... aborting", count, max_count);
KUNIT_EXPECT_LE_MSG(test, expected, max_expected,
"expected (%d) is bigger than can possibly be returned (%d)",
expected, max_expected);
written = strscpy_pad(buf, src, count);
KUNIT_ASSERT_EQ(test, written, expected);
if (count && written == -E2BIG) {
KUNIT_ASSERT_EQ_MSG(test, 0, strncmp(buf, src, count - 1),
"buffer state invalid for -E2BIG");
KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0',
"too big string is not null terminated correctly");
}
for (i = 0; i < chars; i++)
KUNIT_ASSERT_EQ_MSG(test, buf[i], src[i],
"buf[i]==%c != src[i]==%c", buf[i], src[i]);
if (terminator)
KUNIT_ASSERT_EQ_MSG(test, buf[count - 1], '\0',
"string is not null terminated correctly");
for (i = 0; i < pad; i++) {
index = chars + terminator + i;
KUNIT_ASSERT_EQ_MSG(test, buf[index], '\0',
"padding missing at index: %d", i);
}
nr_bytes_poison = sizeof(buf) - chars - terminator - pad;
for (i = 0; i < nr_bytes_poison; i++) {
index = sizeof(buf) - 1 - i; /* Check from the end back */
KUNIT_ASSERT_EQ_MSG(test, buf[index], POISON,
"poison value missing at index: %d", i);
}
}
static void string_test_strscpy(struct kunit *test)
{
char dest[8];
/*
* strscpy_check() uses a destination buffer of size 6 and needs at
* least 2 characters spare (one for null and one to check for
* overflow). This means we should only call tc() with
* strings up to a maximum of 4 characters long and 'count'
* should not exceed 4. To test with longer strings increase
* the buffer size in tc().
*/
/* strscpy_check(test, src, count, expected, chars, terminator, pad) */
strscpy_check(test, "a", 0, -E2BIG, 0, 0, 0);
strscpy_check(test, "", 0, -E2BIG, 0, 0, 0);
strscpy_check(test, "a", 1, -E2BIG, 0, 1, 0);
strscpy_check(test, "", 1, 0, 0, 1, 0);
strscpy_check(test, "ab", 2, -E2BIG, 1, 1, 0);
strscpy_check(test, "a", 2, 1, 1, 1, 0);
strscpy_check(test, "", 2, 0, 0, 1, 1);
strscpy_check(test, "abc", 3, -E2BIG, 2, 1, 0);
strscpy_check(test, "ab", 3, 2, 2, 1, 0);
strscpy_check(test, "a", 3, 1, 1, 1, 1);
strscpy_check(test, "", 3, 0, 0, 1, 2);
strscpy_check(test, "abcd", 4, -E2BIG, 3, 1, 0);
strscpy_check(test, "abc", 4, 3, 3, 1, 0);
strscpy_check(test, "ab", 4, 2, 2, 1, 1);
strscpy_check(test, "a", 4, 1, 1, 1, 2);
strscpy_check(test, "", 4, 0, 0, 1, 3);
/* Compile-time-known source strings. */
KUNIT_EXPECT_EQ(test, strscpy(dest, "", ARRAY_SIZE(dest)), 0);
KUNIT_EXPECT_EQ(test, strscpy(dest, "", 3), 0);
KUNIT_EXPECT_EQ(test, strscpy(dest, "", 1), 0);
KUNIT_EXPECT_EQ(test, strscpy(dest, "", 0), -E2BIG);
KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", ARRAY_SIZE(dest)), 5);
KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 3), -E2BIG);
KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 1), -E2BIG);
KUNIT_EXPECT_EQ(test, strscpy(dest, "Fixed", 0), -E2BIG);
KUNIT_EXPECT_EQ(test, strscpy(dest, "This is too long", ARRAY_SIZE(dest)), -E2BIG);
}
static volatile int unconst;
static void string_test_strcat(struct kunit *test)
{
char dest[8];
/* Destination is terminated. */
memset(dest, 0, sizeof(dest));
KUNIT_EXPECT_EQ(test, strlen(dest), 0);
/* Empty copy does nothing. */
KUNIT_EXPECT_TRUE(test, strcat(dest, "") == dest);
KUNIT_EXPECT_STREQ(test, dest, "");
/* 4 characters copied in, stops at %NUL. */
KUNIT_EXPECT_TRUE(test, strcat(dest, "four\000123") == dest);
KUNIT_EXPECT_STREQ(test, dest, "four");
KUNIT_EXPECT_EQ(test, dest[5], '\0');
/* 2 more characters copied in okay. */
KUNIT_EXPECT_TRUE(test, strcat(dest, "AB") == dest);
KUNIT_EXPECT_STREQ(test, dest, "fourAB");
}
static void string_test_strncat(struct kunit *test)
{
char dest[8];
/* Destination is terminated. */
memset(dest, 0, sizeof(dest));
KUNIT_EXPECT_EQ(test, strlen(dest), 0);
/* Empty copy of size 0 does nothing. */
KUNIT_EXPECT_TRUE(test, strncat(dest, "", 0 + unconst) == dest);
KUNIT_EXPECT_STREQ(test, dest, "");
/* Empty copy of size 1 does nothing too. */
KUNIT_EXPECT_TRUE(test, strncat(dest, "", 1 + unconst) == dest);
KUNIT_EXPECT_STREQ(test, dest, "");
/* Copy of max 0 characters should do nothing. */
KUNIT_EXPECT_TRUE(test, strncat(dest, "asdf", 0 + unconst) == dest);
KUNIT_EXPECT_STREQ(test, dest, "");
/* 4 characters copied in, even if max is 8. */
KUNIT_EXPECT_TRUE(test, strncat(dest, "four\000123", 8 + unconst) == dest);
KUNIT_EXPECT_STREQ(test, dest, "four");
KUNIT_EXPECT_EQ(test, dest[5], '\0');
KUNIT_EXPECT_EQ(test, dest[6], '\0');
/* 2 characters copied in okay, 2 ignored. */
KUNIT_EXPECT_TRUE(test, strncat(dest, "ABCD", 2 + unconst) == dest);
KUNIT_EXPECT_STREQ(test, dest, "fourAB");
}
static void string_test_strlcat(struct kunit *test)
{
char dest[8] = "";
int len = sizeof(dest) + unconst;
/* Destination is terminated. */
KUNIT_EXPECT_EQ(test, strlen(dest), 0);
/* Empty copy is size 0. */
KUNIT_EXPECT_EQ(test, strlcat(dest, "", len), 0);
KUNIT_EXPECT_STREQ(test, dest, "");
/* Size 1 should keep buffer terminated, report size of source only. */
KUNIT_EXPECT_EQ(test, strlcat(dest, "four", 1 + unconst), 4);
KUNIT_EXPECT_STREQ(test, dest, "");
/* 4 characters copied in. */
KUNIT_EXPECT_EQ(test, strlcat(dest, "four", len), 4);
KUNIT_EXPECT_STREQ(test, dest, "four");
/* 2 characters copied in okay, gets to 6 total. */
KUNIT_EXPECT_EQ(test, strlcat(dest, "AB", len), 6);
KUNIT_EXPECT_STREQ(test, dest, "fourAB");
/* 2 characters ignored if max size (7) reached. */
KUNIT_EXPECT_EQ(test, strlcat(dest, "CD", 7 + unconst), 8);
KUNIT_EXPECT_STREQ(test, dest, "fourAB");
/* 1 of 2 characters skipped, now at true max size. */
KUNIT_EXPECT_EQ(test, strlcat(dest, "EFG", len), 9);
KUNIT_EXPECT_STREQ(test, dest, "fourABE");
/* Everything else ignored, now at full size. */
KUNIT_EXPECT_EQ(test, strlcat(dest, "1234", len), 11);
KUNIT_EXPECT_STREQ(test, dest, "fourABE");
}
static void string_test_strtomem(struct kunit *test)
{
static const char input[sizeof(unsigned long)] = "hi";
static const char truncate[] = "this is too long";
struct {
unsigned long canary1;
unsigned char output[sizeof(unsigned long)] __nonstring;
unsigned long canary2;
} wrap;
memset(&wrap, 0xFF, sizeof(wrap));
KUNIT_EXPECT_EQ_MSG(test, wrap.canary1, ULONG_MAX,
"bad initial canary value");
KUNIT_EXPECT_EQ_MSG(test, wrap.canary2, ULONG_MAX,
"bad initial canary value");
/* Check unpadded copy leaves surroundings untouched. */
strtomem(wrap.output, input);
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
for (size_t i = 2; i < sizeof(wrap.output); i++)
KUNIT_EXPECT_EQ(test, wrap.output[i], 0xFF);
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
/* Check truncated copy leaves surroundings untouched. */
memset(&wrap, 0xFF, sizeof(wrap));
strtomem(wrap.output, truncate);
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
for (size_t i = 0; i < sizeof(wrap.output); i++)
KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
/* Check padded copy leaves only string padded. */
memset(&wrap, 0xFF, sizeof(wrap));
strtomem_pad(wrap.output, input, 0xAA);
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
KUNIT_EXPECT_EQ(test, wrap.output[0], input[0]);
KUNIT_EXPECT_EQ(test, wrap.output[1], input[1]);
for (size_t i = 2; i < sizeof(wrap.output); i++)
KUNIT_EXPECT_EQ(test, wrap.output[i], 0xAA);
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
/* Check truncated padded copy has no padding. */
memset(&wrap, 0xFF, sizeof(wrap));
strtomem(wrap.output, truncate);
KUNIT_EXPECT_EQ(test, wrap.canary1, ULONG_MAX);
for (size_t i = 0; i < sizeof(wrap.output); i++)
KUNIT_EXPECT_EQ(test, wrap.output[i], truncate[i]);
KUNIT_EXPECT_EQ(test, wrap.canary2, ULONG_MAX);
}
static void string_test_memtostr(struct kunit *test)
{
char nonstring[7] __nonstring = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };
char nonstring_small[3] __nonstring = { 'a', 'b', 'c' };
char dest[sizeof(nonstring) + 1];
/* Copy in a non-NUL-terminated string into exactly right-sized dest. */
KUNIT_EXPECT_EQ(test, sizeof(dest), sizeof(nonstring) + 1);
memset(dest, 'X', sizeof(dest));
memtostr(dest, nonstring);
KUNIT_EXPECT_STREQ(test, dest, "abcdefg");
memset(dest, 'X', sizeof(dest));
memtostr(dest, nonstring_small);
KUNIT_EXPECT_STREQ(test, dest, "abc");
KUNIT_EXPECT_EQ(test, dest[7], 'X');
memset(dest, 'X', sizeof(dest));
memtostr_pad(dest, nonstring);
KUNIT_EXPECT_STREQ(test, dest, "abcdefg");
memset(dest, 'X', sizeof(dest));
memtostr_pad(dest, nonstring_small);
KUNIT_EXPECT_STREQ(test, dest, "abc");
KUNIT_EXPECT_EQ(test, dest[7], '\0');
}
static void string_test_strends(struct kunit *test)
{
KUNIT_EXPECT_TRUE(test, strends("foo-bar", "bar"));
KUNIT_EXPECT_TRUE(test, strends("foo-bar", "-bar"));
KUNIT_EXPECT_TRUE(test, strends("foobar", "foobar"));
KUNIT_EXPECT_TRUE(test, strends("foobar", ""));
KUNIT_EXPECT_FALSE(test, strends("bar", "foobar"));
KUNIT_EXPECT_FALSE(test, strends("", "foo"));
KUNIT_EXPECT_FALSE(test, strends("foobar", "ba"));
KUNIT_EXPECT_TRUE(test, strends("", ""));
}
#if IS_ENABLED(CONFIG_STRING_KUNIT_BENCH)
/* Target string lengths for benchmarking */
static const size_t bench_lens[] = {
0, 1, 7, 8, 16, 31, 64, 127, 512, 1024, 3173, 4096,
};
/**
* alloc_max_bench_buffer() - Allocate buffer for the max test case.
* @test: KUnit context for managed allocation.
* @lens: Array of lengths used in the benchmark cases.
* @count: Number of elements in the @lens array.
* @buf_len: [out] Pointer to store the actually allocated buffer
* size (including NUL character).
*
* Return: Pointer to the allocated memory, or NULL on failure.
*/
static void *alloc_max_bench_buffer(struct kunit *test, const size_t *lens,
size_t count, size_t *buf_len)
{
size_t max_len = 0;
void *buf;
for (size_t i = 0; i < count; i++)
max_len = max(lens[i], max_len);
/* Add space for NUL character */
max_len += 1;
buf = kunit_kzalloc(test, max_len, GFP_KERNEL);
if (!buf)
return NULL;
if (buf_len)
*buf_len = max_len;
return buf;
}
/**
* fill_random_string() - Populate a buffer with a random NUL-terminated string.
* @buf: Buffer to fill.
* @len: Length of the buffer in bytes.
*
* Fills the buffer with random non-NUL bytes and ensures the string is
* properly NUL-terminated.
*/
static void fill_random_string(char *buf, size_t len)
{
struct rnd_state state;
if (!buf || !len)
return;
/* Use a fixed seed to ensure deterministic benchmark results */
prandom_seed_state(&state, STRING_BENCH_SEED);
prandom_bytes_state(&state, buf, len);
/* Replace NUL characters to avoid early string termination */
for (size_t i = 0; i < len; i++) {
if (buf[i] == '\0')
buf[i] = 0x01;
}
buf[len - 1] = '\0';
}
/**
* STRING_BENCH() - Benchmark string functions.
* @iters: Number of iterations to run.
* @func: Function to benchmark.
* @...: Variable arguments passed to @func.
*
* Disables preemption and measures the total time in nanoseconds to execute
* @func(@__VA_ARGS__) for @iters times, including a small warm-up phase.
*
* Context: Disables preemption during measurement.
* Return: Total execution time in nanoseconds (u64).
*/
#define STRING_BENCH(iters, func, ...) \
({ \
/* Volatile function pointer prevents dead code elimination */ \
typeof(func) (* volatile __func) = (func); \
size_t __bn_iters = (iters); \
size_t __bn_warm_iters; \
u64 __bn_t; \
\
/* Use 10% of the given iterations (maximum 50) to warm up */ \
__bn_warm_iters = max(__bn_iters / 10, 50U); \
\
for (size_t __bn_i = 0; __bn_i < __bn_warm_iters; __bn_i++) \
(void)__func(__VA_ARGS__); \
\
preempt_disable(); \
__bn_t = ktime_get_ns(); \
for (size_t __bn_i = 0; __bn_i < __bn_iters; __bn_i++) \
(void)__func(__VA_ARGS__); \
__bn_t = ktime_get_ns() - __bn_t; \
preempt_enable(); \
__bn_t; \
})
/**
* STRING_BENCH_BUF() - Benchmark harness for single-buffer functions.
* @test: KUnit context.
* @buf_name: Local char * variable name to be defined.
* @buf_size: Local size_t variable name to be defined.
* @func: Function to benchmark.
* @...: Extra arguments for @func.
*
* Prepares a randomized, NUL-terminated buffer and iterates through lengths
* in bench_lens, defining @buf_name and @buf_size in each loop.
*/
#define STRING_BENCH_BUF(test, buf_name, buf_size, func, ...) \
do { \
size_t _bn_i, _bn_iters, _bn_size = 0; \
u64 _bn_t, _bn_mbps = 0, _bn_lat = 0; \
char *_bn_buf; \
\
_bn_buf = alloc_max_bench_buffer(test, bench_lens, \
ARRAY_SIZE(bench_lens), &_bn_size); \
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, _bn_buf); \
\
fill_random_string(_bn_buf, _bn_size); \
\
for (_bn_i = 0; _bn_i < ARRAY_SIZE(bench_lens); _bn_i++) { \
size_t buf_size = bench_lens[_bn_i]; \
char *buf_name = _bn_buf + _bn_size - buf_size - 1; \
_bn_iters = STRING_BENCH_WORKLOAD / max(buf_size, 1U); \
\
_bn_t = STRING_BENCH(_bn_iters, func, ##__VA_ARGS__); \
if (_bn_t > 0) { \
_bn_mbps = (u64)(buf_size) * _bn_iters * \
(NSEC_PER_SEC / MEGA); \
_bn_mbps = div64_u64(_bn_mbps, _bn_t); \
_bn_lat = div64_u64(_bn_t, _bn_iters); \
} \
kunit_info(test, "len=%zu: %llu MB/s (%llu ns/call)\n", \
buf_size, _bn_mbps, _bn_lat); \
} \
} while (0)
#else
#define STRING_BENCH_BUF(test, buf_name, buf_size, func, ...) \
kunit_skip(test, "not enabled")
#endif /* IS_ENABLED(CONFIG_STRING_KUNIT_BENCH) */
static void string_bench_strlen(struct kunit *test)
{
STRING_BENCH_BUF(test, buf, len, strlen, buf);
}
static void string_bench_strnlen(struct kunit *test)
{
STRING_BENCH_BUF(test, buf, len, strnlen, buf, len);
}
static void string_bench_strchr(struct kunit *test)
{
STRING_BENCH_BUF(test, buf, len, strchr, buf, '\0');
}
static void string_bench_strrchr(struct kunit *test)
{
STRING_BENCH_BUF(test, buf, len, strrchr, buf, '\0');
}
static struct kunit_case string_test_cases[] = {
KUNIT_CASE(string_test_memset16),
KUNIT_CASE(string_test_memset32),
KUNIT_CASE(string_test_memset64),
KUNIT_CASE(string_test_strlen),
KUNIT_CASE(string_test_strnlen),
KUNIT_CASE(string_test_strchr),
KUNIT_CASE(string_test_strnchr),
KUNIT_CASE(string_test_strrchr),
KUNIT_CASE(string_test_strspn),
KUNIT_CASE(string_test_strcmp),
KUNIT_CASE(string_test_strcmp_long_strings),
KUNIT_CASE(string_test_strncmp),
KUNIT_CASE(string_test_strncmp_long_strings),
KUNIT_CASE(string_test_strcasecmp),
KUNIT_CASE(string_test_strcasecmp_long_strings),
KUNIT_CASE(string_test_strncasecmp),
KUNIT_CASE(string_test_strncasecmp_long_strings),
KUNIT_CASE(string_test_strscpy),
KUNIT_CASE(string_test_strcat),
KUNIT_CASE(string_test_strncat),
KUNIT_CASE(string_test_strlcat),
KUNIT_CASE(string_test_strtomem),
KUNIT_CASE(string_test_memtostr),
KUNIT_CASE(string_test_strends),
KUNIT_CASE(string_bench_strlen),
KUNIT_CASE(string_bench_strnlen),
KUNIT_CASE(string_bench_strchr),
KUNIT_CASE(string_bench_strrchr),
{}
};
static struct kunit_suite string_test_suite = {
.name = "string",
.test_cases = string_test_cases,
};
kunit_test_suites(&string_test_suite);
MODULE_DESCRIPTION("Test cases for string functions");
MODULE_LICENSE("GPL v2");
|