1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <errno.h>
17 #include <limits.h>
18 #include <signal.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/wait.h>
22 #include <sigchain.h>
23 #include "fortify_test.h"
24 #include "functionalext.h"
25 #include "test.h"
26 #include "../../../../porting/linux/user/include/fortify/fortify.h"
27
28 #define SIZE_1 1
29 #define SIZE_5 5
30 #define SIZE_7 7
31 #define SIZE_10 10
32 #define SIZE_11 11
33 #define SIZE_15 15
34 #define SIZE_20 20
35 #define EQ_0 '0'
36 #define EQ_1 '1'
37 #define EQ_5 '5'
38 #define EQ_8 '8'
39 #define EQ_9 '9'
40 #define EQ_10 "10"
41 #define EQ_11 "11"
42 #define STRLEN_4 "1234"
43 #define STRLEN_5 "01234"
44 #define STRLEN_9 "123456789"
45 #define STRLEN_10 "0123456789"
46 #define STRLEN_14 "01234567890123"
47
48 /**
49 * @tc.name : test_strcat_0010
50 * @tc.desc : After adding fortify, test the normal strcat of the function.
51 * @tc.level : Level 0
52 */
test_strcat_0010()53 static void test_strcat_0010()
54 {
55 char src[SIZE_15];
56 strcpy(src, STRLEN_10);
57 char dst[SIZE_20];
58 memset(dst, 0, SIZE_20);
59 strcat(dst, src);
60 TEST(dst[0] == EQ_0);
61 }
62
63 /**
64 * @tc.name : test_strcat_0020
65 * @tc.desc : Ability to test the strcat Fortify runtime
66 * @tc.level : Level 2
67 */
test_strcat_0020()68 static void test_strcat_0020()
69 {
70 struct sigaction sigabrt = {
71 .sa_handler = SignalHandler,
72 };
73 sigaction(SIGABRT, &sigabrt, NULL);
74
75 char src[SIZE_15];
76 strcpy(src, STRLEN_10);
77 char dst[SIZE_5];
78 memset(dst, 0, SIZE_5);
79 int status;
80 int pid = fork();
81 switch (pid) {
82 case -1:
83 t_error("fork failed: %s\n", strerror(errno));
84 break;
85 case 0:
86 strcat(dst, src);
87 exit(0);
88 default:
89 waitpid(pid, &status, WUNTRACED);
90 TEST(WIFEXITED(status) == 0);
91 TEST(WIFSTOPPED(status) == 1);
92 TEST(WSTOPSIG(status) == SIGSTOP);
93 kill(pid, SIGCONT);
94 break;
95 }
96 return;
97 }
98
99 /**
100 * @tc.name : test_strcat_0010
101 * @tc.desc : After adding fortify, test the normal strcat of the function.
102 * @tc.level : Level 0
103 */
test_strncat_0010()104 static void test_strncat_0010()
105 {
106 char src[SIZE_15];
107 strcpy(src, STRLEN_10);
108 char dst[SIZE_20];
109 memset(dst, 0, SIZE_20);
110 strncat(dst, src, strlen(src));
111 TEST(dst[0] == EQ_0);
112 }
113
114 /**
115 * @tc.name : test_strcat_0020
116 * @tc.desc : Ability to test the strcat Fortify runtime
117 * @tc.level : Level 2
118 */
test_strncat_0020()119 static void test_strncat_0020()
120 {
121 struct sigaction sigabrt = {
122 .sa_handler = SignalHandler,
123 };
124 sigaction(SIGABRT, &sigabrt, NULL);
125
126 char src[SIZE_15];
127 strcpy(src, STRLEN_10);
128 char dst[SIZE_5];
129 memset(dst, 0, SIZE_5);
130 int status;
131 int pid = fork();
132 switch (pid) {
133 case -1:
134 t_error("fork failed: %s\n", strerror(errno));
135 break;
136 case 0:
137 strncat(dst, src, strlen(src));
138 exit(0);
139 default:
140 waitpid(pid, &status, WUNTRACED);
141 TEST(WIFEXITED(status) == 0);
142 TEST(WIFSTOPPED(status) == 1);
143 TEST(WSTOPSIG(status) == SIGSTOP);
144 kill(pid, SIGCONT);
145 break;
146 }
147 return;
148 }
149
150
151 /**
152 * @tc.name : test_stpcpy_0010
153 * @tc.desc : After adding fortify, test the normal stpcpy of the function.
154 * @tc.level : Level 0
155 */
test_stpcpy_0010()156 static void test_stpcpy_0010()
157 {
158 char *src = "abcdefg";
159 char dst[SIZE_15];
160 stpcpy(dst, src);
161 TEST(dst[0] == 'a');
162 }
163
164 /**
165 * @tc.name : test_stpcpy_0020
166 * @tc.desc : Ability to test the stpcpy Fortify runtime
167 * @tc.level : Level 2
168 */
test_stpcpy_0020()169 static void test_stpcpy_0020()
170 {
171 struct sigaction sigabrt = {
172 .sa_handler = SignalHandler,
173 };
174 sigaction(SIGABRT, &sigabrt, NULL);
175
176 char *src = STRLEN_9;
177 char dst[SIZE_5];
178 int status;
179 int pid = fork();
180 switch (pid) {
181 case -1:
182 t_error("fork failed: %s\n", strerror(errno));
183 break;
184 case 0:
185 stpcpy(dst, src);
186 exit(0);
187 default:
188 waitpid(pid, &status, WUNTRACED);
189 TEST(WIFEXITED(status) == 0);
190 TEST(WIFSTOPPED(status) == 1);
191 TEST(WSTOPSIG(status) == SIGSTOP);
192 kill(pid, SIGCONT);
193 break;
194 }
195 return;
196 }
197
198 /**
199 * @tc.name : test_stpncpy_0010
200 * @tc.desc : After adding fortify, test the normal strcpy of the function.
201 * @tc.level : Level 0
202 */
test_stpncpy_0010()203 static void test_stpncpy_0010()
204 {
205 char src[SIZE_10];
206 char dst[SIZE_15];
207 strcpy(src, STRLEN_4);
208 size_t n = strlen(src);
209 stpncpy(dst, src, n);
210 TEST(dst[0] == EQ_1);
211 }
212
213 /**
214 * @tc.name : test_stpncpy_0020
215 * @tc.desc : Ability to test the strcpy Fortify runtime
216 * @tc.level : Level 2
217 */
test_stpncpy_0020()218 static void test_stpncpy_0020()
219 {
220 struct sigaction sigabrt = {
221 .sa_handler = SignalHandler,
222 };
223 sigaction(SIGABRT, &sigabrt, NULL);
224
225 char src[SIZE_15];
226 char dst[SIZE_5];
227 strcpy(src, STRLEN_10);
228 size_t n = strlen(src);
229 int status;
230 int pid = fork();
231 switch (pid) {
232 case -1:
233 t_error("fork failed: %s\n", strerror(errno));
234 break;
235 case 0:
236 stpncpy(dst, src, n);
237 exit(0);
238 default:
239 waitpid(pid, &status, WUNTRACED);
240 TEST(WIFEXITED(status) == 0);
241 TEST(WIFSTOPPED(status) == 1);
242 TEST(WSTOPSIG(status) == SIGSTOP);
243 kill(pid, SIGCONT);
244 break;
245 }
246 return;
247 }
248
249 /**
250 * @tc.name : test_strncpy_0010
251 * @tc.desc : After adding fortify, test the normal strncpy of the function.
252 * @tc.level : Level 0
253 */
test_strncpy_0010()254 static void test_strncpy_0010()
255 {
256 char src[SIZE_10] = STRLEN_10;
257 char dst[SIZE_15];
258 strncpy(dst, src, SIZE_5);
259 TEST(src != dst);
260 }
261
262 /**
263 * @tc.name : test_strncpy_0020
264 * @tc.desc : Ability to test the strncpy Fortify runtime
265 * @tc.level : Level 2
266 */
test_strncpy_0020()267 static void test_strncpy_0020()
268 {
269 struct sigaction sigabrt = {
270 .sa_handler = SignalHandler,
271 };
272 sigaction(SIGABRT, &sigabrt, NULL);
273
274 char src[SIZE_15];
275 char dst[SIZE_10];
276 strcpy(src, STRLEN_14);
277 size_t n = strlen(src);
278
279 int status;
280 int pid = fork();
281 switch (pid) {
282 case -1:
283 t_error("fork failed: %s\n", strerror(errno));
284 break;
285 case 0:
286 strncpy(dst, src, n);
287 exit(0);
288 default:
289 waitpid(pid, &status, WUNTRACED);
290 TEST(WIFEXITED(status) == 0);
291 TEST(WIFSTOPPED(status) == 1);
292 TEST(WSTOPSIG(status) == SIGSTOP);
293 kill(pid, SIGCONT);
294 break;
295 }
296 return;
297 }
298
299 /**
300 * @tc.name : test_memchr_0010
301 * @tc.desc : After adding fortify, test the normal memchr of the function.
302 * @tc.level : Level 0
303 */
test_memchr_0010()304 static void test_memchr_0010()
305 {
306 const char str[] = STRLEN_9;
307 const char ch = EQ_9;
308 char *ret = (char*)memchr(str, ch, strlen(str));
309 TEST(*ret == EQ_9);
310 }
311
312 /**
313 * @tc.name : test_memchr_0020
314 * @tc.desc : Ability to test the memchr Fortify runtime
315 * @tc.level : Level 2
316 */
test_memchr_0020()317 static void test_memchr_0020()
318 {
319 struct sigaction sigabrt = {
320 .sa_handler = SignalHandler,
321 };
322 sigaction(SIGABRT, &sigabrt, NULL);
323
324 const char s[SIZE_10] = STRLEN_9;
325 const char c = EQ_5;
326 int status;
327 int pid = fork();
328 switch (pid) {
329 case -1:
330 t_error("fork failed: %s\n", strerror(errno));
331 break;
332 case 0:
333 memchr(s, c, SIZE_20);
334 exit(0);
335 default:
336 waitpid(pid, &status, WUNTRACED);
337 TEST(WIFEXITED(status) == 0);
338 TEST(WIFSTOPPED(status) == 1);
339 TEST(WSTOPSIG(status) == SIGSTOP);
340 kill(pid, SIGCONT);
341 break;
342 }
343 return;
344 }
345
346 /**
347 * @tc.name : test_memrchr_0010
348 * @tc.desc : After adding fortify, test the normal memrchr of the function.
349 * @tc.level : Level 0
350 */
test_memrchr_0010()351 static void test_memrchr_0010()
352 {
353 const char str[] = STRLEN_9;
354 const char ch = EQ_9;
355 char *ret = (char*)memrchr(str, ch, strlen(str));
356 TEST(*ret == EQ_9);
357 }
358
359 /**
360 * @tc.name : test_memrchr_0020
361 * @tc.desc : Ability to test the memrchr Fortify runtime
362 * @tc.level : Level 2
363 */
test_memrchr_0020()364 static void test_memrchr_0020()
365 {
366 struct sigaction sigabrt = {
367 .sa_handler = SignalHandler,
368 };
369 sigaction(SIGABRT, &sigabrt, NULL);
370
371 const char s[SIZE_10] = STRLEN_9;
372 const char c = EQ_5;
373 int status;
374 int pid = fork();
375 switch (pid) {
376 case -1:
377 t_error("fork failed: %s\n", strerror(errno));
378 break;
379 case 0:
380 memrchr(s, c, SIZE_20);
381 exit(0);
382 default:
383 waitpid(pid, &status, WUNTRACED);
384 TEST(WIFEXITED(status) == 0);
385 TEST(WIFSTOPPED(status) == 1);
386 TEST(WSTOPSIG(status) == SIGSTOP);
387 kill(pid, SIGCONT);
388 break;
389 }
390 return;
391 }
392
393 /**
394 * @tc.name : test_strchr_0010
395 * @tc.desc : After adding fortify, test the normal strchr of the function.
396 * @tc.level : Level 0
397 */
test_strchr_0010()398 static void test_strchr_0010()
399 {
400 const char str[] = STRLEN_9;
401 const char ch = EQ_9;
402 char *ret = strchr(str, ch);
403 TEST(*ret == EQ_9);
404 }
405
406 /**
407 * @tc.name : test_strchr_0020
408 * @tc.desc : Ability to test the strchr Fortify runtime
409 * @tc.level : Level 2
410 */
test_strchr_0020()411 static void test_strchr_0020()
412 {
413 struct sigaction sigabrt = {
414 .sa_handler = SignalHandler,
415 };
416 sigaction(SIGABRT, &sigabrt, NULL);
417
418 char str[0];
419 int status;
420 int pid = fork();
421 switch (pid) {
422 case -1:
423 t_error("fork failed: %s\n", strerror(errno));
424 break;
425 case 0:
426 strchr(str, 'a');
427 exit(0);
428 default:
429 waitpid(pid, &status, WUNTRACED);
430 TEST(WIFEXITED(status) == 0);
431 TEST(WIFSTOPPED(status) == 1);
432 TEST(WSTOPSIG(status) == SIGSTOP);
433 kill(pid, SIGCONT);
434 break;
435 }
436 return;
437 }
438
439
440 /**
441 * @tc.name : test_strrchr_0010
442 * @tc.desc : After adding fortify, test the normal strrchr of the function.
443 * @tc.level : Level 0
444 */
test_strrchr_0010()445 static void test_strrchr_0010()
446 {
447 int len;
448 const char str[] = STRLEN_9;
449 const char ch = EQ_9;
450 char *ret = strrchr(str, ch);
451 TEST(*ret == EQ_9);
452 }
453
454 /**
455 * @tc.name : test_strrchr_0020
456 * @tc.desc : Ability to test the strrchr Fortify runtime
457 * @tc.level : Level 2
458 */
test_strrchr_0020()459 static void test_strrchr_0020()
460 {
461 struct sigaction sigabrt = {
462 .sa_handler = SignalHandler,
463 };
464 sigaction(SIGABRT, &sigabrt, NULL);
465
466 char str[0];
467 int status;
468 int pid = fork();
469 switch (pid) {
470 case -1:
471 t_error("fork failed: %s\n", strerror(errno));
472 break;
473 case 0:
474 strrchr(str, 'a');
475 exit(0);
476 default:
477 waitpid(pid, &status, WUNTRACED);
478 TEST(WIFEXITED(status) == 0);
479 TEST(WIFSTOPPED(status) == 1);
480 TEST(WSTOPSIG(status) == SIGSTOP);
481 kill(pid, SIGCONT);
482 break;
483 }
484 return;
485 }
486
487 /**
488 * @tc.name : test_strlcat_0010
489 * @tc.desc : After adding fortify, test the normal strlcat of the function.
490 * @tc.level : Level 0
491 */
492 #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
test_strlcat_0010()493 static void test_strlcat_0010()
494 {
495 char dst[SIZE_10];
496 char src[SIZE_5] = STRLEN_4;
497 memset(dst, 0, SIZE_10);
498 strlcat(dst, src, strlen(src));
499 TEST(dst[0] == EQ_1);
500 }
501
502 /**
503 * @tc.name : test_strlcat_0020
504 * @tc.desc : Ability to test the strlcat Fortify runtime
505 * @tc.level : Level 2
506 */
test_strlcat_0020()507 static void test_strlcat_0020()
508 {
509 struct sigaction sigabrt = {
510 .sa_handler = SignalHandler,
511 };
512 sigaction(SIGABRT, &sigabrt, NULL);
513
514 char dst[SIZE_5] = STRLEN_4;
515 char src[SIZE_10] = STRLEN_9;
516 int status;
517 int pid = fork();
518 switch (pid) {
519 case -1:
520 t_error("fork failed: %s\n", strerror(errno));
521 break;
522 case 0:
523 strlcat(dst, src, strlen(src));
524 exit(0);
525 default:
526 waitpid(pid, &status, WUNTRACED);
527 TEST(WIFEXITED(status) == 0);
528 TEST(WIFSTOPPED(status) == 1);
529 TEST(WSTOPSIG(status) == SIGSTOP);
530 kill(pid, SIGCONT);
531 break;
532 }
533 return;
534 }
535
536 /**
537 * @tc.name : test_strlcpy_0010
538 * @tc.desc : After adding fortify, test the normal strcpy of the function.
539 * @tc.level : Level 0
540 */
test_strlcpy_0010()541 static void test_strlcpy_0010()
542 {
543 char src[SIZE_10];
544 char dst[SIZE_15];
545 memset(dst, 0, SIZE_15);
546 strcpy(src, STRLEN_4);
547 size_t n = strlen(src);
548 strlcpy(dst, src, n);
549 TEST(dst[0] == EQ_1);
550 }
551
552 /**
553 * @tc.name : test_strlcpy_0020
554 * @tc.desc : Ability to test the strcpy Fortify runtime
555 * @tc.level : Level 2
556 */
test_strlcpy_0020()557 static void test_strlcpy_0020()
558 {
559 struct sigaction sigabrt = {
560 .sa_handler = SignalHandler,
561 };
562 sigaction(SIGABRT, &sigabrt, NULL);
563
564 char src[SIZE_15];
565 char dst[SIZE_10];
566 strcpy(src, STRLEN_14);
567 size_t n = strlen(src);
568 int status;
569 int pid = fork();
570 switch (pid) {
571 case -1:
572 t_error("fork failed: %s\n", strerror(errno));
573 break;
574 case 0:
575 strlcpy(dst, src, n);
576 exit(0);
577 default:
578 waitpid(pid, &status, WUNTRACED);
579 TEST(WIFEXITED(status) == 0);
580 TEST(WIFSTOPPED(status) == 1);
581 TEST(WSTOPSIG(status) == SIGSTOP);
582 kill(pid, SIGCONT);
583 break;
584 }
585 return;
586 }
587 #endif
588
589 /**
590 * @tc.name : test_mempcpy_0010
591 * @tc.desc : After adding fortify, test the normal mempcpy of the function.
592 * @tc.level : Level 0
593 */
test_mempcpy_0010()594 static void test_mempcpy_0010()
595 {
596 char dst[SIZE_20];
597 char src[SIZE_15] = STRLEN_10;
598 mempcpy(dst, src, strlen(src));
599 TEST(dst[0] == EQ_0);
600 }
601
602 /**
603 * @tc.name : test_mempcpy_0020
604 * @tc.desc : Ability to test the mempcpy Fortify runtime
605 * @tc.level : Level 2
606 */
test_mempcpy_0020()607 static void test_mempcpy_0020()
608 {
609 struct sigaction sigabrt = {
610 .sa_handler = SignalHandler,
611 };
612 sigaction(SIGABRT, &sigabrt, NULL);
613
614 char dst[SIZE_5] = STRLEN_4;
615 char src[SIZE_20] = STRLEN_10;
616 int status;
617 int pid = fork();
618 switch (pid) {
619 case -1:
620 t_error("fork failed: %s\n", strerror(errno));
621 break;
622 case 0:
623 mempcpy(dst, src, strlen(src));
624 exit(0);
625 default:
626 waitpid(pid, &status, WUNTRACED);
627 TEST(WIFEXITED(status) == 0);
628 TEST(WIFSTOPPED(status) == 1);
629 TEST(WSTOPSIG(status) == SIGSTOP);
630 kill(pid, SIGCONT);
631 break;
632 }
633 return;
634 }
635
636 /**
637 * @tc.name : test_mempcpy_0030
638 * @tc.desc : Ability to test the mempcpy Fortify runtime
639 * @tc.level : Level 2
640 */
test_mempcpy_0030()641 static void test_mempcpy_0030()
642 {
643 struct sigaction sigabrt = {
644 .sa_handler = SignalHandler,
645 };
646 sigaction(SIGABRT, &sigabrt, NULL);
647
648 char dst[SIZE_5] = STRLEN_4;
649 char src[SIZE_20] = STRLEN_10;
650 size_t n = strlen(src) + SSIZE_MAX;
651 int status;
652 int pid = fork();
653 switch (pid) {
654 case -1:
655 t_error("fork failed: %s\n", strerror(errno));
656 break;
657 case 0:
658 mempcpy(dst, src, n);
659 exit(0);
660 default:
661 waitpid(pid, &status, WUNTRACED);
662 TEST(WIFEXITED(status) == 0);
663 TEST(WIFSTOPPED(status) == 1);
664 TEST(WSTOPSIG(status) == SIGSTOP);
665 kill(pid, SIGCONT);
666 break;
667 }
668 return;
669 }
670
671 /**
672 * @tc.name : test_strcpy_0010
673 * @tc.desc : After adding fortify, test the normal strcpy of the function.
674 * @tc.level : Level 0
675 */
test_strcpy_0010()676 static void test_strcpy_0010()
677 {
678 char dst[SIZE_10];
679 char src[SIZE_10] = {STRLEN_4};
680 strcpy(dst, src);
681 TEST(dst[0] == EQ_1);
682 }
683
684 /**
685 * @tc.name : test_strcpy_0020
686 * @tc.desc : Ability to test the strcpy Fortify runtime
687 * @tc.level : Level 2
688 */
test_strcpy_0020()689 static void test_strcpy_0020()
690 {
691 struct sigaction sigabrt = {
692 .sa_handler = SignalHandler,
693 };
694 sigaction(SIGABRT, &sigabrt, NULL);
695
696 char dst[2], src[SIZE_5] = {STRLEN_4};
697 int status;
698 int pid = fork();
699 switch (pid) {
700 case -1:
701 t_error("fork failed: %s\n", strerror(errno));
702 break;
703 case 0:
704 strcpy(dst, src);
705 exit(0);
706 default:
707 waitpid(pid, &status, WUNTRACED);
708 TEST(WIFEXITED(status) == 0);
709 TEST(WIFSTOPPED(status) == 1);
710 TEST(WSTOPSIG(status) == SIGSTOP);
711 kill(pid, SIGCONT);
712 break;
713 }
714 return;
715 }
716
717 /**
718 * @tc.name : test_memmove_0010
719 * @tc.desc : After adding fortify, test the normal memmove of the function.
720 * @tc.level : Level 0
721 */
test_memmove_0010()722 static void test_memmove_0010()
723 {
724 char s[] = STRLEN_9;
725 memmove(s, s+SIZE_7, strlen(s)+1-SIZE_7);
726 TEST(s[0] == EQ_8);
727 }
728
729 /**
730 * @tc.name : test_memmove_0020
731 * @tc.desc : Ability to test the memmove Fortify runtime
732 * @tc.level : Level 2
733 */
test_memmove_0020()734 static void test_memmove_0020()
735 {
736 struct sigaction sigabrt = {
737 .sa_handler = SignalHandler,
738 };
739 sigaction(SIGABRT, &sigabrt, NULL);
740
741 char dst[SIZE_20];
742 strcpy(dst, STRLEN_10);
743 size_t n = atoi(EQ_10);
744 int status;
745 int pid = fork();
746 switch (pid) {
747 case -1:
748 t_error("fork failed: %s\n", strerror(errno));
749 break;
750 case 0:
751 memmove(dst + SIZE_11, dst, n);
752 exit(0);
753 default:
754 waitpid(pid, &status, WUNTRACED);
755 TEST(WIFEXITED(status) == 0);
756 TEST(WIFSTOPPED(status) == 1);
757 TEST(WSTOPSIG(status) == SIGSTOP);
758 kill(pid, SIGCONT);
759 break;
760 }
761 return;
762 }
763
764 /**
765 * @tc.name : test_memcpy_0010
766 * @tc.desc : After adding fortify, test the normal memcpy of the function.
767 * @tc.level : Level 0
768 */
test_memcpy_0010()769 static void test_memcpy_0010()
770 {
771 char dst[SIZE_15];
772 memcpy(dst, STRLEN_10, SIZE_10);
773 TEST(dst[0] == EQ_0);
774 }
775
776 /**
777 * @tc.name : test_memcpy_0020
778 * @tc.desc : Ability to test the memcpy Fortify runtime
779 * @tc.level : Level 2
780 */
test_memcpy_0020()781 static void test_memcpy_0020()
782 {
783 struct sigaction sigabrt = {
784 .sa_handler = SignalHandler,
785 };
786 sigaction(SIGABRT, &sigabrt, NULL);
787
788
789 int status;
790 char dst[SIZE_10];
791 int pid = fork();
792 switch (pid) {
793 case -1:
794 t_error("fork failed: %s\n", strerror(errno));
795 break;
796 case 0:
797 memcpy(dst, STRLEN_14, SIZE_15);
798 exit(0);
799 default:
800 waitpid(pid, &status, WUNTRACED);
801 TEST(WIFEXITED(status) == 0);
802 TEST(WIFSTOPPED(status) == 1);
803 TEST(WSTOPSIG(status) == SIGSTOP);
804 kill(pid, SIGCONT);
805 break;
806 }
807 return;
808 }
809
810 /**
811 * @tc.name : test_memset_0010
812 * @tc.desc : After adding fortify, test the normal memset of the function.
813 * @tc.level : Level 0
814 */
test_memset_0010()815 static void test_memset_0010()
816 {
817 char src[SIZE_5] = STRLEN_5;
818 char dst[SIZE_5] = STRLEN_5;
819 memset(dst, 0, SIZE_5);
820 TEST(dst != src);
821 }
822
823 /**
824 * @tc.name : test_memset_0020
825 * @tc.desc : Ability to test the memset Fortify runtime
826 * @tc.level : Level 2
827 */
test_memset_0020()828 static void test_memset_0020()
829 {
830 struct sigaction sigabrt = {
831 .sa_handler = SignalHandler,
832 };
833 sigaction(SIGABRT, &sigabrt, NULL);
834
835 char buf[SIZE_10];
836 size_t n = atoi(EQ_11);
837 int status;
838 int pid = fork();
839 switch (pid) {
840 case -1:
841 t_error("fork failed: %s\n", strerror(errno));
842 break;
843 case 0:
844 memset(buf, 0, n);
845 exit(0);
846 default:
847 waitpid(pid, &status, WUNTRACED);
848 TEST(WIFEXITED(status) == 0);
849 TEST(WIFSTOPPED(status) == 1);
850 TEST(WSTOPSIG(status) == SIGSTOP);
851 kill(pid, SIGCONT);
852 break;
853 }
854 return;
855 }
856
857 /**
858 * @tc.name : test_strlen_0010
859 * @tc.desc : Ability to test the strlen normal condition
860 * @tc.level : Level 0
861 */
test_strlen_0010()862 static void test_strlen_0010()
863 {
864 struct sigaction sigabrt = {
865 .sa_handler = SignalHandler,
866 };
867 sigaction(SIGABRT, &sigabrt, NULL);
868
869 char buf[SIZE_10];
870 memcpy(buf, STRLEN_10, sizeof(buf));
871 int status;
872 int pid = fork();
873 switch (pid) {
874 case -1:
875 t_error("fork failed: %s\n", strerror(errno));
876 break;
877 case 0:
878 strlen(buf);
879 exit(0);
880 default:
881 waitpid(pid, &status, WUNTRACED);
882 TEST(WIFEXITED(status) == 0);
883 TEST(WIFSTOPPED(status) == 1);
884 TEST(WSTOPSIG(status) == SIGSTOP);
885 kill(pid, SIGCONT);
886 break;
887 }
888 return;
889 }
890
891 /**
892 * @tc.name : test_strlen_0020
893 * @tc.desc : Ability to test the strlen with NULL
894 * @tc.level : Level 2
895 */
test_strlen_0020()896 static void test_strlen_0020()
897 {
898 struct sigaction sigabrt = {
899 .sa_handler = SignalHandler,
900 };
901 sigaction(SIGABRT, &sigabrt, NULL);
902
903 int status;
904 int pid = fork();
905 switch (pid) {
906 case -1:
907 t_error("fork failed: %s\n", strerror(errno));
908 break;
909 case 0:
910 strlen(NULL);
911 exit(0);
912 default:
913 waitpid(pid, &status, WUNTRACED);
914 EXPECT_EQ(test_strlen_0020, WIFEXITED(status), 0);
915 kill(pid, SIGCONT);
916 break;
917 }
918 return;
919 }
920
main(int argc,char * argv[])921 int main(int argc, char *argv[]) {
922 remove_all_special_handler(SIGABRT);
923 test_strcat_0010();
924 test_strcat_0020();
925 test_strncat_0010();
926 test_strncat_0020();
927 test_strchr_0010();
928 test_strchr_0020();
929 test_strncpy_0010();
930 test_strncpy_0020();
931 test_stpcpy_0010();
932 test_stpcpy_0020();
933 test_stpncpy_0010();
934 test_stpncpy_0020();
935 test_memchr_0010();
936 test_memchr_0020();
937 test_strrchr_0010();
938 test_strrchr_0020();
939 test_strcpy_0010();
940 test_strcpy_0020();
941 test_memmove_0010();
942 test_memmove_0020();
943 test_memset_0010();
944 test_memset_0020();
945 test_memcpy_0010();
946 test_memcpy_0020();
947 test_strlen_0010();
948 test_strlen_0020();
949
950 #ifdef _GNU_SOURCE
951 test_mempcpy_0010();
952 test_mempcpy_0020();
953 test_mempcpy_0030();
954 test_memrchr_0010();
955 test_memrchr_0020();
956 #endif
957
958 #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
959 test_strlcat_0010();
960 test_strlcat_0020();
961 test_strlcpy_0010();
962 test_strlcpy_0020();
963 #endif
964 return t_status;
965 }