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