1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <errno.h>
17570af302Sopenharmony_ci#include <limits.h>
18570af302Sopenharmony_ci#include <signal.h>
19570af302Sopenharmony_ci#include <stdlib.h>
20570af302Sopenharmony_ci#include <string.h>
21570af302Sopenharmony_ci#include <sys/wait.h>
22570af302Sopenharmony_ci#include <sigchain.h>
23570af302Sopenharmony_ci#include "fortify_test.h"
24570af302Sopenharmony_ci#include "functionalext.h"
25570af302Sopenharmony_ci#include "test.h"
26570af302Sopenharmony_ci#include "../../../../include/fortify/linux/fortify.h"
27570af302Sopenharmony_ci
28570af302Sopenharmony_ci#define SIZE_1 1
29570af302Sopenharmony_ci#define SIZE_5 5
30570af302Sopenharmony_ci#define SIZE_7 7
31570af302Sopenharmony_ci#define SIZE_10 10
32570af302Sopenharmony_ci#define SIZE_11 11
33570af302Sopenharmony_ci#define SIZE_15 15
34570af302Sopenharmony_ci#define SIZE_20 20
35570af302Sopenharmony_ci#define EQ_0 '0'
36570af302Sopenharmony_ci#define EQ_1 '1'
37570af302Sopenharmony_ci#define EQ_5 '5'
38570af302Sopenharmony_ci#define EQ_8 '8'
39570af302Sopenharmony_ci#define EQ_9 '9'
40570af302Sopenharmony_ci#define EQ_10 "10"
41570af302Sopenharmony_ci#define EQ_11 "11"
42570af302Sopenharmony_ci#define STRLEN_4 "1234"
43570af302Sopenharmony_ci#define STRLEN_5 "01234"
44570af302Sopenharmony_ci#define STRLEN_9 "123456789"
45570af302Sopenharmony_ci#define STRLEN_10 "0123456789"
46570af302Sopenharmony_ci#define STRLEN_14 "01234567890123"
47570af302Sopenharmony_ci
48570af302Sopenharmony_ci/**
49570af302Sopenharmony_ci * @tc.name     : test_strcat_0010
50570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal strcat of the function.
51570af302Sopenharmony_ci * @tc.level    : Level 0
52570af302Sopenharmony_ci */
53570af302Sopenharmony_cistatic void test_strcat_0010()
54570af302Sopenharmony_ci{
55570af302Sopenharmony_ci    char src[SIZE_15];
56570af302Sopenharmony_ci    strcpy(src, STRLEN_10);
57570af302Sopenharmony_ci    char dst[SIZE_20];
58570af302Sopenharmony_ci    memset(dst, 0, SIZE_20);
59570af302Sopenharmony_ci    strcat(dst, src);
60570af302Sopenharmony_ci    TEST(dst[0] == EQ_0);
61570af302Sopenharmony_ci}
62570af302Sopenharmony_ci
63570af302Sopenharmony_ci/**
64570af302Sopenharmony_ci * @tc.name     : test_strcat_0020
65570af302Sopenharmony_ci * @tc.desc     : Ability to test the strcat Fortify runtime
66570af302Sopenharmony_ci * @tc.level    : Level 2
67570af302Sopenharmony_ci */
68570af302Sopenharmony_cistatic void test_strcat_0020()
69570af302Sopenharmony_ci{
70570af302Sopenharmony_ci    struct sigaction sigabrt = {
71570af302Sopenharmony_ci        .sa_handler = SignalHandler,
72570af302Sopenharmony_ci    };
73570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
74570af302Sopenharmony_ci
75570af302Sopenharmony_ci    char src[SIZE_15];
76570af302Sopenharmony_ci    strcpy(src, STRLEN_10);
77570af302Sopenharmony_ci    char dst[SIZE_5];
78570af302Sopenharmony_ci    memset(dst, 0, SIZE_5);
79570af302Sopenharmony_ci    int status;
80570af302Sopenharmony_ci    int pid = fork();
81570af302Sopenharmony_ci    switch (pid) {
82570af302Sopenharmony_ci        case -1:
83570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
84570af302Sopenharmony_ci            break;
85570af302Sopenharmony_ci        case 0:
86570af302Sopenharmony_ci            strcat(dst, src);
87570af302Sopenharmony_ci            exit(0);
88570af302Sopenharmony_ci        default:
89570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
90570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
91570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
92570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
93570af302Sopenharmony_ci            kill(pid, SIGCONT);
94570af302Sopenharmony_ci            break;
95570af302Sopenharmony_ci    }
96570af302Sopenharmony_ci    return;
97570af302Sopenharmony_ci}
98570af302Sopenharmony_ci
99570af302Sopenharmony_ci/**
100570af302Sopenharmony_ci * @tc.name     : test_strcat_0010
101570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal strcat of the function.
102570af302Sopenharmony_ci * @tc.level    : Level 0
103570af302Sopenharmony_ci */
104570af302Sopenharmony_cistatic void test_strncat_0010()
105570af302Sopenharmony_ci{
106570af302Sopenharmony_ci    char src[SIZE_15];
107570af302Sopenharmony_ci    strcpy(src, STRLEN_10);
108570af302Sopenharmony_ci    char dst[SIZE_20];
109570af302Sopenharmony_ci    memset(dst, 0, SIZE_20);
110570af302Sopenharmony_ci    strncat(dst, src, strlen(src));
111570af302Sopenharmony_ci    TEST(dst[0] == EQ_0);
112570af302Sopenharmony_ci}
113570af302Sopenharmony_ci
114570af302Sopenharmony_ci/**
115570af302Sopenharmony_ci * @tc.name     : test_strcat_0020
116570af302Sopenharmony_ci * @tc.desc     : Ability to test the strcat Fortify runtime
117570af302Sopenharmony_ci * @tc.level    : Level 2
118570af302Sopenharmony_ci */
119570af302Sopenharmony_cistatic void test_strncat_0020()
120570af302Sopenharmony_ci{
121570af302Sopenharmony_ci    struct sigaction sigabrt = {
122570af302Sopenharmony_ci        .sa_handler = SignalHandler,
123570af302Sopenharmony_ci    };
124570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
125570af302Sopenharmony_ci
126570af302Sopenharmony_ci    char src[SIZE_15];
127570af302Sopenharmony_ci    strcpy(src, STRLEN_10);
128570af302Sopenharmony_ci    char dst[SIZE_5];
129570af302Sopenharmony_ci    memset(dst, 0, SIZE_5);
130570af302Sopenharmony_ci    int status;
131570af302Sopenharmony_ci    int pid = fork();
132570af302Sopenharmony_ci    switch (pid) {
133570af302Sopenharmony_ci        case -1:
134570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
135570af302Sopenharmony_ci            break;
136570af302Sopenharmony_ci        case 0:
137570af302Sopenharmony_ci            strncat(dst, src, strlen(src));
138570af302Sopenharmony_ci            exit(0);
139570af302Sopenharmony_ci        default:
140570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
141570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
142570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
143570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
144570af302Sopenharmony_ci            kill(pid, SIGCONT);
145570af302Sopenharmony_ci            break;
146570af302Sopenharmony_ci    }
147570af302Sopenharmony_ci    return;
148570af302Sopenharmony_ci}
149570af302Sopenharmony_ci
150570af302Sopenharmony_ci
151570af302Sopenharmony_ci/**
152570af302Sopenharmony_ci * @tc.name     : test_stpcpy_0010
153570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal stpcpy of the function.
154570af302Sopenharmony_ci * @tc.level    : Level 0
155570af302Sopenharmony_ci */
156570af302Sopenharmony_cistatic void test_stpcpy_0010()
157570af302Sopenharmony_ci{
158570af302Sopenharmony_ci    char *src = "abcdefg";
159570af302Sopenharmony_ci    char dst[SIZE_15];
160570af302Sopenharmony_ci    stpcpy(dst, src);
161570af302Sopenharmony_ci    TEST(dst[0] == 'a');
162570af302Sopenharmony_ci}
163570af302Sopenharmony_ci
164570af302Sopenharmony_ci/**
165570af302Sopenharmony_ci * @tc.name     : test_stpcpy_0020
166570af302Sopenharmony_ci * @tc.desc     : Ability to test the stpcpy Fortify runtime
167570af302Sopenharmony_ci * @tc.level    : Level 2
168570af302Sopenharmony_ci */
169570af302Sopenharmony_cistatic void test_stpcpy_0020()
170570af302Sopenharmony_ci{
171570af302Sopenharmony_ci    struct sigaction sigabrt = {
172570af302Sopenharmony_ci        .sa_handler = SignalHandler,
173570af302Sopenharmony_ci    };
174570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
175570af302Sopenharmony_ci
176570af302Sopenharmony_ci    char *src = STRLEN_9;
177570af302Sopenharmony_ci    char dst[SIZE_5];
178570af302Sopenharmony_ci    int status;
179570af302Sopenharmony_ci    int pid = fork();
180570af302Sopenharmony_ci    switch (pid) {
181570af302Sopenharmony_ci        case -1:
182570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
183570af302Sopenharmony_ci            break;
184570af302Sopenharmony_ci        case 0:
185570af302Sopenharmony_ci            stpcpy(dst, src);
186570af302Sopenharmony_ci            exit(0);
187570af302Sopenharmony_ci        default:
188570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
189570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
190570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
191570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
192570af302Sopenharmony_ci            kill(pid, SIGCONT);
193570af302Sopenharmony_ci            break;
194570af302Sopenharmony_ci    }
195570af302Sopenharmony_ci    return;
196570af302Sopenharmony_ci}
197570af302Sopenharmony_ci
198570af302Sopenharmony_ci/**
199570af302Sopenharmony_ci * @tc.name     : test_stpncpy_0010
200570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal strcpy of the function.
201570af302Sopenharmony_ci * @tc.level    : Level 0
202570af302Sopenharmony_ci */
203570af302Sopenharmony_cistatic void test_stpncpy_0010()
204570af302Sopenharmony_ci{
205570af302Sopenharmony_ci    char src[SIZE_10];
206570af302Sopenharmony_ci    char dst[SIZE_15];
207570af302Sopenharmony_ci    strcpy(src, STRLEN_4);
208570af302Sopenharmony_ci    size_t n = strlen(src);
209570af302Sopenharmony_ci    stpncpy(dst, src, n);
210570af302Sopenharmony_ci    TEST(dst[0] == EQ_1);
211570af302Sopenharmony_ci}
212570af302Sopenharmony_ci
213570af302Sopenharmony_ci/**
214570af302Sopenharmony_ci * @tc.name     : test_stpncpy_0020
215570af302Sopenharmony_ci * @tc.desc     : Ability to test the strcpy Fortify runtime
216570af302Sopenharmony_ci * @tc.level    : Level 2
217570af302Sopenharmony_ci */
218570af302Sopenharmony_cistatic void test_stpncpy_0020()
219570af302Sopenharmony_ci{
220570af302Sopenharmony_ci    struct sigaction sigabrt = {
221570af302Sopenharmony_ci        .sa_handler = SignalHandler,
222570af302Sopenharmony_ci    };
223570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
224570af302Sopenharmony_ci
225570af302Sopenharmony_ci    char src[SIZE_15];
226570af302Sopenharmony_ci    char dst[SIZE_5];
227570af302Sopenharmony_ci    strcpy(src, STRLEN_10);
228570af302Sopenharmony_ci    size_t n = strlen(src);
229570af302Sopenharmony_ci    int status;
230570af302Sopenharmony_ci    int pid = fork();
231570af302Sopenharmony_ci    switch (pid) {
232570af302Sopenharmony_ci        case -1:
233570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
234570af302Sopenharmony_ci            break;
235570af302Sopenharmony_ci        case 0:
236570af302Sopenharmony_ci            stpncpy(dst, src, n);
237570af302Sopenharmony_ci            exit(0);
238570af302Sopenharmony_ci        default:
239570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
240570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
241570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
242570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
243570af302Sopenharmony_ci            kill(pid, SIGCONT);
244570af302Sopenharmony_ci            break;
245570af302Sopenharmony_ci    }
246570af302Sopenharmony_ci    return;
247570af302Sopenharmony_ci}
248570af302Sopenharmony_ci
249570af302Sopenharmony_ci/**
250570af302Sopenharmony_ci * @tc.name     : test_strncpy_0010
251570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal strncpy of the function.
252570af302Sopenharmony_ci * @tc.level    : Level 0
253570af302Sopenharmony_ci */
254570af302Sopenharmony_cistatic void test_strncpy_0010()
255570af302Sopenharmony_ci{
256570af302Sopenharmony_ci    char src[SIZE_10] = STRLEN_10;
257570af302Sopenharmony_ci    char dst[SIZE_15];
258570af302Sopenharmony_ci    strncpy(dst, src, SIZE_5);
259570af302Sopenharmony_ci    TEST(src != dst);
260570af302Sopenharmony_ci}
261570af302Sopenharmony_ci
262570af302Sopenharmony_ci/**
263570af302Sopenharmony_ci * @tc.name     : test_strncpy_0020
264570af302Sopenharmony_ci * @tc.desc     : Ability to test the strncpy Fortify runtime
265570af302Sopenharmony_ci * @tc.level    : Level 2
266570af302Sopenharmony_ci */
267570af302Sopenharmony_cistatic void test_strncpy_0020()
268570af302Sopenharmony_ci{
269570af302Sopenharmony_ci    struct sigaction sigabrt = {
270570af302Sopenharmony_ci        .sa_handler = SignalHandler,
271570af302Sopenharmony_ci    };
272570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
273570af302Sopenharmony_ci
274570af302Sopenharmony_ci    char src[SIZE_15];
275570af302Sopenharmony_ci    char dst[SIZE_10];
276570af302Sopenharmony_ci    strcpy(src, STRLEN_14);
277570af302Sopenharmony_ci    size_t n = strlen(src);
278570af302Sopenharmony_ci
279570af302Sopenharmony_ci    int status;
280570af302Sopenharmony_ci    int pid = fork();
281570af302Sopenharmony_ci    switch (pid) {
282570af302Sopenharmony_ci        case -1:
283570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
284570af302Sopenharmony_ci            break;
285570af302Sopenharmony_ci        case 0:
286570af302Sopenharmony_ci            strncpy(dst, src, n);
287570af302Sopenharmony_ci            exit(0);
288570af302Sopenharmony_ci        default:
289570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
290570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
291570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
292570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
293570af302Sopenharmony_ci            kill(pid, SIGCONT);
294570af302Sopenharmony_ci            break;
295570af302Sopenharmony_ci    }
296570af302Sopenharmony_ci    return;
297570af302Sopenharmony_ci}
298570af302Sopenharmony_ci
299570af302Sopenharmony_ci/**
300570af302Sopenharmony_ci * @tc.name     : test_memchr_0010
301570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal memchr of the function.
302570af302Sopenharmony_ci * @tc.level    : Level 0
303570af302Sopenharmony_ci */
304570af302Sopenharmony_cistatic void test_memchr_0010()
305570af302Sopenharmony_ci{
306570af302Sopenharmony_ci    const char str[] = STRLEN_9;
307570af302Sopenharmony_ci    const char ch = EQ_9;
308570af302Sopenharmony_ci    char *ret = (char*)memchr(str, ch, strlen(str));
309570af302Sopenharmony_ci    TEST(*ret == EQ_9);
310570af302Sopenharmony_ci}
311570af302Sopenharmony_ci
312570af302Sopenharmony_ci/**
313570af302Sopenharmony_ci * @tc.name     : test_memchr_0020
314570af302Sopenharmony_ci * @tc.desc     : Ability to test the memchr Fortify runtime
315570af302Sopenharmony_ci * @tc.level    : Level 2
316570af302Sopenharmony_ci */
317570af302Sopenharmony_cistatic void test_memchr_0020()
318570af302Sopenharmony_ci{
319570af302Sopenharmony_ci    struct sigaction sigabrt = {
320570af302Sopenharmony_ci        .sa_handler = SignalHandler,
321570af302Sopenharmony_ci    };
322570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
323570af302Sopenharmony_ci
324570af302Sopenharmony_ci    const char s[SIZE_10] = STRLEN_9;
325570af302Sopenharmony_ci    const char c = EQ_5;
326570af302Sopenharmony_ci    int status;
327570af302Sopenharmony_ci    int pid = fork();
328570af302Sopenharmony_ci    switch (pid) {
329570af302Sopenharmony_ci        case -1:
330570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
331570af302Sopenharmony_ci            break;
332570af302Sopenharmony_ci        case 0:
333570af302Sopenharmony_ci            memchr(s, c, SIZE_20);
334570af302Sopenharmony_ci            exit(0);
335570af302Sopenharmony_ci        default:
336570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
337570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
338570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
339570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
340570af302Sopenharmony_ci            kill(pid, SIGCONT);
341570af302Sopenharmony_ci            break;
342570af302Sopenharmony_ci    }
343570af302Sopenharmony_ci    return;
344570af302Sopenharmony_ci}
345570af302Sopenharmony_ci
346570af302Sopenharmony_ci/**
347570af302Sopenharmony_ci * @tc.name     : test_memrchr_0010
348570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal memrchr of the function.
349570af302Sopenharmony_ci * @tc.level    : Level 0
350570af302Sopenharmony_ci */
351570af302Sopenharmony_cistatic void test_memrchr_0010()
352570af302Sopenharmony_ci{
353570af302Sopenharmony_ci    const char str[] = STRLEN_9;
354570af302Sopenharmony_ci    const char ch = EQ_9;
355570af302Sopenharmony_ci    char *ret = (char*)memrchr(str, ch, strlen(str));
356570af302Sopenharmony_ci    TEST(*ret == EQ_9);
357570af302Sopenharmony_ci}
358570af302Sopenharmony_ci
359570af302Sopenharmony_ci/**
360570af302Sopenharmony_ci * @tc.name     : test_memrchr_0020
361570af302Sopenharmony_ci * @tc.desc     : Ability to test the memrchr Fortify runtime
362570af302Sopenharmony_ci * @tc.level    : Level 2
363570af302Sopenharmony_ci */
364570af302Sopenharmony_cistatic void test_memrchr_0020()
365570af302Sopenharmony_ci{
366570af302Sopenharmony_ci    struct sigaction sigabrt = {
367570af302Sopenharmony_ci        .sa_handler = SignalHandler,
368570af302Sopenharmony_ci    };
369570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
370570af302Sopenharmony_ci
371570af302Sopenharmony_ci    const char s[SIZE_10] = STRLEN_9;
372570af302Sopenharmony_ci    const char c = EQ_5;
373570af302Sopenharmony_ci    int status;
374570af302Sopenharmony_ci    int pid = fork();
375570af302Sopenharmony_ci    switch (pid) {
376570af302Sopenharmony_ci        case -1:
377570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
378570af302Sopenharmony_ci            break;
379570af302Sopenharmony_ci        case 0:
380570af302Sopenharmony_ci            memrchr(s, c, SIZE_20);
381570af302Sopenharmony_ci            exit(0);
382570af302Sopenharmony_ci        default:
383570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
384570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
385570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
386570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
387570af302Sopenharmony_ci            kill(pid, SIGCONT);
388570af302Sopenharmony_ci            break;
389570af302Sopenharmony_ci    }
390570af302Sopenharmony_ci    return;
391570af302Sopenharmony_ci}
392570af302Sopenharmony_ci
393570af302Sopenharmony_ci/**
394570af302Sopenharmony_ci * @tc.name     : test_strchr_0010
395570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal strchr of the function.
396570af302Sopenharmony_ci * @tc.level    : Level 0
397570af302Sopenharmony_ci */
398570af302Sopenharmony_cistatic void test_strchr_0010()
399570af302Sopenharmony_ci{
400570af302Sopenharmony_ci    const char str[] = STRLEN_9;
401570af302Sopenharmony_ci    const char ch = EQ_9;
402570af302Sopenharmony_ci    char *ret = strchr(str, ch);
403570af302Sopenharmony_ci    TEST(*ret == EQ_9);
404570af302Sopenharmony_ci}
405570af302Sopenharmony_ci
406570af302Sopenharmony_ci/**
407570af302Sopenharmony_ci * @tc.name     : test_strchr_0020
408570af302Sopenharmony_ci * @tc.desc     : Ability to test the strchr Fortify runtime
409570af302Sopenharmony_ci * @tc.level    : Level 2
410570af302Sopenharmony_ci */
411570af302Sopenharmony_cistatic void test_strchr_0020()
412570af302Sopenharmony_ci{
413570af302Sopenharmony_ci    struct sigaction sigabrt = {
414570af302Sopenharmony_ci        .sa_handler = SignalHandler,
415570af302Sopenharmony_ci    };
416570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
417570af302Sopenharmony_ci
418570af302Sopenharmony_ci    char str[0];
419570af302Sopenharmony_ci    int status;
420570af302Sopenharmony_ci    int pid = fork();
421570af302Sopenharmony_ci    switch (pid) {
422570af302Sopenharmony_ci        case -1:
423570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
424570af302Sopenharmony_ci            break;
425570af302Sopenharmony_ci        case 0:
426570af302Sopenharmony_ci            strchr(str, 'a');
427570af302Sopenharmony_ci            exit(0);
428570af302Sopenharmony_ci        default:
429570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
430570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
431570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
432570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
433570af302Sopenharmony_ci            kill(pid, SIGCONT);
434570af302Sopenharmony_ci            break;
435570af302Sopenharmony_ci    }
436570af302Sopenharmony_ci    return;
437570af302Sopenharmony_ci}
438570af302Sopenharmony_ci
439570af302Sopenharmony_ci
440570af302Sopenharmony_ci/**
441570af302Sopenharmony_ci * @tc.name     : test_strrchr_0010
442570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal strrchr of the function.
443570af302Sopenharmony_ci * @tc.level    : Level 0
444570af302Sopenharmony_ci */
445570af302Sopenharmony_cistatic void test_strrchr_0010()
446570af302Sopenharmony_ci{
447570af302Sopenharmony_ci    int len;
448570af302Sopenharmony_ci    const char str[] = STRLEN_9;
449570af302Sopenharmony_ci    const char ch = EQ_9;
450570af302Sopenharmony_ci    char *ret = strrchr(str, ch);
451570af302Sopenharmony_ci    TEST(*ret == EQ_9);
452570af302Sopenharmony_ci}
453570af302Sopenharmony_ci
454570af302Sopenharmony_ci/**
455570af302Sopenharmony_ci * @tc.name     : test_strrchr_0020
456570af302Sopenharmony_ci * @tc.desc     : Ability to test the strrchr Fortify runtime
457570af302Sopenharmony_ci * @tc.level    : Level 2
458570af302Sopenharmony_ci */
459570af302Sopenharmony_cistatic void test_strrchr_0020()
460570af302Sopenharmony_ci{
461570af302Sopenharmony_ci    struct sigaction sigabrt = {
462570af302Sopenharmony_ci        .sa_handler = SignalHandler,
463570af302Sopenharmony_ci    };
464570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
465570af302Sopenharmony_ci
466570af302Sopenharmony_ci    char str[0];
467570af302Sopenharmony_ci    int status;
468570af302Sopenharmony_ci    int pid = fork();
469570af302Sopenharmony_ci    switch (pid) {
470570af302Sopenharmony_ci        case -1:
471570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
472570af302Sopenharmony_ci            break;
473570af302Sopenharmony_ci        case 0:
474570af302Sopenharmony_ci            strrchr(str, 'a');
475570af302Sopenharmony_ci            exit(0);
476570af302Sopenharmony_ci        default:
477570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
478570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
479570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
480570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
481570af302Sopenharmony_ci            kill(pid, SIGCONT);
482570af302Sopenharmony_ci            break;
483570af302Sopenharmony_ci    }
484570af302Sopenharmony_ci    return;
485570af302Sopenharmony_ci}
486570af302Sopenharmony_ci
487570af302Sopenharmony_ci/**
488570af302Sopenharmony_ci * @tc.name     : test_strlcat_0010
489570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal strlcat of the function.
490570af302Sopenharmony_ci * @tc.level    : Level 0
491570af302Sopenharmony_ci */
492570af302Sopenharmony_ci#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
493570af302Sopenharmony_cistatic void test_strlcat_0010()
494570af302Sopenharmony_ci{
495570af302Sopenharmony_ci    char dst[SIZE_10];
496570af302Sopenharmony_ci    char src[SIZE_5] = STRLEN_4;
497570af302Sopenharmony_ci    memset(dst, 0, SIZE_10);
498570af302Sopenharmony_ci    strlcat(dst, src, strlen(src));
499570af302Sopenharmony_ci    TEST(dst[0] == EQ_1);
500570af302Sopenharmony_ci}
501570af302Sopenharmony_ci
502570af302Sopenharmony_ci/**
503570af302Sopenharmony_ci * @tc.name     : test_strlcat_0020
504570af302Sopenharmony_ci * @tc.desc     : Ability to test the strlcat Fortify runtime
505570af302Sopenharmony_ci * @tc.level    : Level 2
506570af302Sopenharmony_ci */
507570af302Sopenharmony_cistatic void test_strlcat_0020()
508570af302Sopenharmony_ci{
509570af302Sopenharmony_ci    struct sigaction sigabrt = {
510570af302Sopenharmony_ci        .sa_handler = SignalHandler,
511570af302Sopenharmony_ci    };
512570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
513570af302Sopenharmony_ci
514570af302Sopenharmony_ci    char dst[SIZE_5] = STRLEN_4;
515570af302Sopenharmony_ci    char src[SIZE_10] = STRLEN_9;
516570af302Sopenharmony_ci    int status;
517570af302Sopenharmony_ci    int pid = fork();
518570af302Sopenharmony_ci    switch (pid) {
519570af302Sopenharmony_ci        case -1:
520570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
521570af302Sopenharmony_ci            break;
522570af302Sopenharmony_ci        case 0:
523570af302Sopenharmony_ci            strlcat(dst, src, strlen(src));
524570af302Sopenharmony_ci            exit(0);
525570af302Sopenharmony_ci        default:
526570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
527570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
528570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
529570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
530570af302Sopenharmony_ci            kill(pid, SIGCONT);
531570af302Sopenharmony_ci            break;
532570af302Sopenharmony_ci    }
533570af302Sopenharmony_ci    return;
534570af302Sopenharmony_ci}
535570af302Sopenharmony_ci
536570af302Sopenharmony_ci/**
537570af302Sopenharmony_ci * @tc.name     : test_strlcpy_0010
538570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal strcpy of the function.
539570af302Sopenharmony_ci * @tc.level    : Level 0
540570af302Sopenharmony_ci */
541570af302Sopenharmony_cistatic void test_strlcpy_0010()
542570af302Sopenharmony_ci{
543570af302Sopenharmony_ci    char src[SIZE_10];
544570af302Sopenharmony_ci    char dst[SIZE_15];
545570af302Sopenharmony_ci    memset(dst, 0, SIZE_15);
546570af302Sopenharmony_ci    strcpy(src, STRLEN_4);
547570af302Sopenharmony_ci    size_t n = strlen(src);
548570af302Sopenharmony_ci    strlcpy(dst, src, n);
549570af302Sopenharmony_ci    TEST(dst[0] == EQ_1);
550570af302Sopenharmony_ci}
551570af302Sopenharmony_ci
552570af302Sopenharmony_ci/**
553570af302Sopenharmony_ci * @tc.name     : test_strlcpy_0020
554570af302Sopenharmony_ci * @tc.desc     : Ability to test the strcpy Fortify runtime
555570af302Sopenharmony_ci * @tc.level    : Level 2
556570af302Sopenharmony_ci */
557570af302Sopenharmony_cistatic void test_strlcpy_0020()
558570af302Sopenharmony_ci{
559570af302Sopenharmony_ci    struct sigaction sigabrt = {
560570af302Sopenharmony_ci        .sa_handler = SignalHandler,
561570af302Sopenharmony_ci    };
562570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
563570af302Sopenharmony_ci
564570af302Sopenharmony_ci    char src[SIZE_15];
565570af302Sopenharmony_ci    char dst[SIZE_10];
566570af302Sopenharmony_ci    strcpy(src, STRLEN_14);
567570af302Sopenharmony_ci    size_t n = strlen(src);
568570af302Sopenharmony_ci    int status;
569570af302Sopenharmony_ci    int pid = fork();
570570af302Sopenharmony_ci    switch (pid) {
571570af302Sopenharmony_ci        case -1:
572570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
573570af302Sopenharmony_ci            break;
574570af302Sopenharmony_ci        case 0:
575570af302Sopenharmony_ci            strlcpy(dst, src, n);
576570af302Sopenharmony_ci            exit(0);
577570af302Sopenharmony_ci        default:
578570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
579570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
580570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
581570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
582570af302Sopenharmony_ci            kill(pid, SIGCONT);
583570af302Sopenharmony_ci            break;
584570af302Sopenharmony_ci    }
585570af302Sopenharmony_ci    return;
586570af302Sopenharmony_ci}
587570af302Sopenharmony_ci#endif
588570af302Sopenharmony_ci
589570af302Sopenharmony_ci/**
590570af302Sopenharmony_ci * @tc.name     : test_mempcpy_0010
591570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal mempcpy of the function.
592570af302Sopenharmony_ci * @tc.level    : Level 0
593570af302Sopenharmony_ci */
594570af302Sopenharmony_cistatic void test_mempcpy_0010()
595570af302Sopenharmony_ci{
596570af302Sopenharmony_ci    char dst[SIZE_20];
597570af302Sopenharmony_ci    char src[SIZE_15] = STRLEN_10;
598570af302Sopenharmony_ci    mempcpy(dst, src, strlen(src));
599570af302Sopenharmony_ci    TEST(dst[0] == EQ_0);
600570af302Sopenharmony_ci}
601570af302Sopenharmony_ci
602570af302Sopenharmony_ci/**
603570af302Sopenharmony_ci * @tc.name     : test_mempcpy_0020
604570af302Sopenharmony_ci * @tc.desc     : Ability to test the mempcpy Fortify runtime
605570af302Sopenharmony_ci * @tc.level    : Level 2
606570af302Sopenharmony_ci */
607570af302Sopenharmony_cistatic void test_mempcpy_0020()
608570af302Sopenharmony_ci{
609570af302Sopenharmony_ci    struct sigaction sigabrt = {
610570af302Sopenharmony_ci        .sa_handler = SignalHandler,
611570af302Sopenharmony_ci    };
612570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
613570af302Sopenharmony_ci
614570af302Sopenharmony_ci    char dst[SIZE_5] = STRLEN_4;
615570af302Sopenharmony_ci    char src[SIZE_20] = STRLEN_10;
616570af302Sopenharmony_ci    int status;
617570af302Sopenharmony_ci    int pid = fork();
618570af302Sopenharmony_ci    switch (pid) {
619570af302Sopenharmony_ci        case -1:
620570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
621570af302Sopenharmony_ci            break;
622570af302Sopenharmony_ci        case 0:
623570af302Sopenharmony_ci            mempcpy(dst, src, strlen(src));
624570af302Sopenharmony_ci            exit(0);
625570af302Sopenharmony_ci        default:
626570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
627570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
628570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
629570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
630570af302Sopenharmony_ci            kill(pid, SIGCONT);
631570af302Sopenharmony_ci            break;
632570af302Sopenharmony_ci    }
633570af302Sopenharmony_ci    return;
634570af302Sopenharmony_ci}
635570af302Sopenharmony_ci
636570af302Sopenharmony_ci/**
637570af302Sopenharmony_ci * @tc.name     : test_mempcpy_0030
638570af302Sopenharmony_ci * @tc.desc     : Ability to test the mempcpy Fortify runtime
639570af302Sopenharmony_ci * @tc.level    : Level 2
640570af302Sopenharmony_ci */
641570af302Sopenharmony_cistatic void test_mempcpy_0030()
642570af302Sopenharmony_ci{
643570af302Sopenharmony_ci    struct sigaction sigabrt = {
644570af302Sopenharmony_ci        .sa_handler = SignalHandler,
645570af302Sopenharmony_ci    };
646570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
647570af302Sopenharmony_ci
648570af302Sopenharmony_ci    char dst[SIZE_5] = STRLEN_4;
649570af302Sopenharmony_ci    char src[SIZE_20] = STRLEN_10;
650570af302Sopenharmony_ci    size_t n = strlen(src) + SSIZE_MAX;
651570af302Sopenharmony_ci    int status;
652570af302Sopenharmony_ci    int pid = fork();
653570af302Sopenharmony_ci    switch (pid) {
654570af302Sopenharmony_ci        case -1:
655570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
656570af302Sopenharmony_ci            break;
657570af302Sopenharmony_ci        case 0:
658570af302Sopenharmony_ci            mempcpy(dst, src, n);
659570af302Sopenharmony_ci            exit(0);
660570af302Sopenharmony_ci        default:
661570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
662570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
663570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
664570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
665570af302Sopenharmony_ci            kill(pid, SIGCONT);
666570af302Sopenharmony_ci            break;
667570af302Sopenharmony_ci    }
668570af302Sopenharmony_ci    return;
669570af302Sopenharmony_ci}
670570af302Sopenharmony_ci
671570af302Sopenharmony_ci/**
672570af302Sopenharmony_ci * @tc.name     : test_strcpy_0010
673570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal strcpy of the function.
674570af302Sopenharmony_ci * @tc.level    : Level 0
675570af302Sopenharmony_ci */
676570af302Sopenharmony_cistatic void test_strcpy_0010()
677570af302Sopenharmony_ci{
678570af302Sopenharmony_ci    char dst[SIZE_10];
679570af302Sopenharmony_ci    char src[SIZE_10] = {STRLEN_4};
680570af302Sopenharmony_ci    strcpy(dst, src);
681570af302Sopenharmony_ci    TEST(dst[0] == EQ_1);
682570af302Sopenharmony_ci}
683570af302Sopenharmony_ci
684570af302Sopenharmony_ci/**
685570af302Sopenharmony_ci * @tc.name     : test_strcpy_0020
686570af302Sopenharmony_ci * @tc.desc     : Ability to test the strcpy Fortify runtime
687570af302Sopenharmony_ci * @tc.level    : Level 2
688570af302Sopenharmony_ci */
689570af302Sopenharmony_cistatic void test_strcpy_0020()
690570af302Sopenharmony_ci{
691570af302Sopenharmony_ci    struct sigaction sigabrt = {
692570af302Sopenharmony_ci        .sa_handler = SignalHandler,
693570af302Sopenharmony_ci    };
694570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
695570af302Sopenharmony_ci
696570af302Sopenharmony_ci    char dst[2], src[SIZE_5] = {STRLEN_4};
697570af302Sopenharmony_ci    int status;
698570af302Sopenharmony_ci    int pid = fork();
699570af302Sopenharmony_ci    switch (pid) {
700570af302Sopenharmony_ci        case -1:
701570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
702570af302Sopenharmony_ci            break;
703570af302Sopenharmony_ci        case 0:
704570af302Sopenharmony_ci            strcpy(dst, src);
705570af302Sopenharmony_ci            exit(0);
706570af302Sopenharmony_ci        default:
707570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
708570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
709570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
710570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
711570af302Sopenharmony_ci            kill(pid, SIGCONT);
712570af302Sopenharmony_ci            break;
713570af302Sopenharmony_ci    }
714570af302Sopenharmony_ci    return;
715570af302Sopenharmony_ci}
716570af302Sopenharmony_ci
717570af302Sopenharmony_ci/**
718570af302Sopenharmony_ci * @tc.name     : test_memmove_0010
719570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal memmove of the function.
720570af302Sopenharmony_ci * @tc.level    : Level 0
721570af302Sopenharmony_ci */
722570af302Sopenharmony_cistatic void test_memmove_0010()
723570af302Sopenharmony_ci{
724570af302Sopenharmony_ci    char s[] = STRLEN_9;
725570af302Sopenharmony_ci    memmove(s, s+SIZE_7, strlen(s)+1-SIZE_7);
726570af302Sopenharmony_ci    TEST(s[0] == EQ_8);
727570af302Sopenharmony_ci}
728570af302Sopenharmony_ci
729570af302Sopenharmony_ci/**
730570af302Sopenharmony_ci * @tc.name     : test_memmove_0020
731570af302Sopenharmony_ci * @tc.desc     : Ability to test the memmove Fortify runtime
732570af302Sopenharmony_ci * @tc.level    : Level 2
733570af302Sopenharmony_ci */
734570af302Sopenharmony_cistatic void test_memmove_0020()
735570af302Sopenharmony_ci{
736570af302Sopenharmony_ci    struct sigaction sigabrt = {
737570af302Sopenharmony_ci        .sa_handler = SignalHandler,
738570af302Sopenharmony_ci    };
739570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
740570af302Sopenharmony_ci
741570af302Sopenharmony_ci    char dst[SIZE_20];
742570af302Sopenharmony_ci    strcpy(dst, STRLEN_10);
743570af302Sopenharmony_ci    size_t n = atoi(EQ_10);
744570af302Sopenharmony_ci    int status;
745570af302Sopenharmony_ci    int pid = fork();
746570af302Sopenharmony_ci    switch (pid) {
747570af302Sopenharmony_ci        case -1:
748570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
749570af302Sopenharmony_ci            break;
750570af302Sopenharmony_ci        case 0:
751570af302Sopenharmony_ci            memmove(dst + SIZE_11, dst, n);
752570af302Sopenharmony_ci            exit(0);
753570af302Sopenharmony_ci        default:
754570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
755570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
756570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
757570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
758570af302Sopenharmony_ci            kill(pid, SIGCONT);
759570af302Sopenharmony_ci            break;
760570af302Sopenharmony_ci    }
761570af302Sopenharmony_ci    return;
762570af302Sopenharmony_ci}
763570af302Sopenharmony_ci
764570af302Sopenharmony_ci/**
765570af302Sopenharmony_ci * @tc.name     : test_memcpy_0010
766570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal memcpy of the function.
767570af302Sopenharmony_ci * @tc.level    : Level 0
768570af302Sopenharmony_ci */
769570af302Sopenharmony_cistatic void test_memcpy_0010()
770570af302Sopenharmony_ci{
771570af302Sopenharmony_ci    char dst[SIZE_15];
772570af302Sopenharmony_ci    memcpy(dst, STRLEN_10, SIZE_10);
773570af302Sopenharmony_ci    TEST(dst[0] == EQ_0);
774570af302Sopenharmony_ci}
775570af302Sopenharmony_ci
776570af302Sopenharmony_ci/**
777570af302Sopenharmony_ci * @tc.name     : test_memcpy_0020
778570af302Sopenharmony_ci * @tc.desc     : Ability to test the memcpy Fortify runtime
779570af302Sopenharmony_ci * @tc.level    : Level 2
780570af302Sopenharmony_ci */
781570af302Sopenharmony_cistatic void test_memcpy_0020()
782570af302Sopenharmony_ci{
783570af302Sopenharmony_ci    struct sigaction sigabrt = {
784570af302Sopenharmony_ci        .sa_handler = SignalHandler,
785570af302Sopenharmony_ci    };
786570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
787570af302Sopenharmony_ci
788570af302Sopenharmony_ci
789570af302Sopenharmony_ci    int status;
790570af302Sopenharmony_ci    char dst[SIZE_10];
791570af302Sopenharmony_ci    int pid = fork();
792570af302Sopenharmony_ci    switch (pid) {
793570af302Sopenharmony_ci        case -1:
794570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
795570af302Sopenharmony_ci            break;
796570af302Sopenharmony_ci        case 0:
797570af302Sopenharmony_ci            memcpy(dst, STRLEN_14, SIZE_15);
798570af302Sopenharmony_ci            exit(0);
799570af302Sopenharmony_ci        default:
800570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
801570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
802570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
803570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
804570af302Sopenharmony_ci            kill(pid, SIGCONT);
805570af302Sopenharmony_ci            break;
806570af302Sopenharmony_ci    }
807570af302Sopenharmony_ci    return;
808570af302Sopenharmony_ci}
809570af302Sopenharmony_ci
810570af302Sopenharmony_ci/**
811570af302Sopenharmony_ci * @tc.name     : test_memset_0010
812570af302Sopenharmony_ci * @tc.desc     : After adding fortify, test the normal memset of the function.
813570af302Sopenharmony_ci * @tc.level    : Level 0
814570af302Sopenharmony_ci */
815570af302Sopenharmony_cistatic void test_memset_0010()
816570af302Sopenharmony_ci{
817570af302Sopenharmony_ci    char src[SIZE_5] = STRLEN_5;
818570af302Sopenharmony_ci    char dst[SIZE_5] = STRLEN_5;
819570af302Sopenharmony_ci    memset(dst, 0, SIZE_5);
820570af302Sopenharmony_ci    TEST(dst != src);
821570af302Sopenharmony_ci}
822570af302Sopenharmony_ci
823570af302Sopenharmony_ci/**
824570af302Sopenharmony_ci * @tc.name     : test_memset_0020
825570af302Sopenharmony_ci * @tc.desc     : Ability to test the memset Fortify runtime
826570af302Sopenharmony_ci * @tc.level    : Level 2
827570af302Sopenharmony_ci */
828570af302Sopenharmony_cistatic void test_memset_0020()
829570af302Sopenharmony_ci{
830570af302Sopenharmony_ci    struct sigaction sigabrt = {
831570af302Sopenharmony_ci        .sa_handler = SignalHandler,
832570af302Sopenharmony_ci    };
833570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
834570af302Sopenharmony_ci
835570af302Sopenharmony_ci    char buf[SIZE_10];
836570af302Sopenharmony_ci    size_t n = atoi(EQ_11);
837570af302Sopenharmony_ci    int status;
838570af302Sopenharmony_ci    int pid = fork();
839570af302Sopenharmony_ci    switch (pid) {
840570af302Sopenharmony_ci        case -1:
841570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
842570af302Sopenharmony_ci            break;
843570af302Sopenharmony_ci        case 0:
844570af302Sopenharmony_ci            memset(buf, 0, n);
845570af302Sopenharmony_ci            exit(0);
846570af302Sopenharmony_ci        default:
847570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
848570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
849570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
850570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
851570af302Sopenharmony_ci            kill(pid, SIGCONT);
852570af302Sopenharmony_ci            break;
853570af302Sopenharmony_ci    }
854570af302Sopenharmony_ci    return;
855570af302Sopenharmony_ci}
856570af302Sopenharmony_ci
857570af302Sopenharmony_ci/**
858570af302Sopenharmony_ci * @tc.name     : test_strlen_0010
859570af302Sopenharmony_ci * @tc.desc     : Ability to test the strlen normal condition
860570af302Sopenharmony_ci * @tc.level    : Level 0
861570af302Sopenharmony_ci */
862570af302Sopenharmony_cistatic void test_strlen_0010()
863570af302Sopenharmony_ci{
864570af302Sopenharmony_ci    struct sigaction sigabrt = {
865570af302Sopenharmony_ci        .sa_handler = SignalHandler,
866570af302Sopenharmony_ci    };
867570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
868570af302Sopenharmony_ci
869570af302Sopenharmony_ci    char buf[SIZE_10];
870570af302Sopenharmony_ci    memcpy(buf, STRLEN_10, sizeof(buf));
871570af302Sopenharmony_ci    int status;
872570af302Sopenharmony_ci    int pid = fork();
873570af302Sopenharmony_ci    switch (pid) {
874570af302Sopenharmony_ci        case -1:
875570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
876570af302Sopenharmony_ci            break;
877570af302Sopenharmony_ci        case 0:
878570af302Sopenharmony_ci            strlen(buf);
879570af302Sopenharmony_ci            exit(0);
880570af302Sopenharmony_ci        default:
881570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
882570af302Sopenharmony_ci            TEST(WIFEXITED(status) == 0);
883570af302Sopenharmony_ci            TEST(WIFSTOPPED(status) == 1);
884570af302Sopenharmony_ci            TEST(WSTOPSIG(status) == SIGSTOP);
885570af302Sopenharmony_ci            kill(pid, SIGCONT);
886570af302Sopenharmony_ci            break;
887570af302Sopenharmony_ci    }
888570af302Sopenharmony_ci    return;
889570af302Sopenharmony_ci}
890570af302Sopenharmony_ci
891570af302Sopenharmony_ci/**
892570af302Sopenharmony_ci * @tc.name     : test_strlen_0020
893570af302Sopenharmony_ci * @tc.desc     : Ability to test the strlen with NULL
894570af302Sopenharmony_ci * @tc.level    : Level 2
895570af302Sopenharmony_ci */
896570af302Sopenharmony_cistatic void test_strlen_0020()
897570af302Sopenharmony_ci{
898570af302Sopenharmony_ci    struct sigaction sigabrt = {
899570af302Sopenharmony_ci        .sa_handler = SignalHandler,
900570af302Sopenharmony_ci    };
901570af302Sopenharmony_ci    sigaction(SIGABRT, &sigabrt, NULL);
902570af302Sopenharmony_ci
903570af302Sopenharmony_ci    int status;
904570af302Sopenharmony_ci    int pid = fork();
905570af302Sopenharmony_ci    switch (pid) {
906570af302Sopenharmony_ci        case -1:
907570af302Sopenharmony_ci            t_error("fork failed: %s\n", strerror(errno));
908570af302Sopenharmony_ci            break;
909570af302Sopenharmony_ci        case 0:
910570af302Sopenharmony_ci            strlen(NULL);
911570af302Sopenharmony_ci            exit(0);
912570af302Sopenharmony_ci        default:
913570af302Sopenharmony_ci            waitpid(pid, &status, WUNTRACED);
914570af302Sopenharmony_ci            EXPECT_EQ(test_strlen_0020, WIFEXITED(status), 0);
915570af302Sopenharmony_ci            kill(pid, SIGCONT);
916570af302Sopenharmony_ci            break;
917570af302Sopenharmony_ci    }
918570af302Sopenharmony_ci    return;
919570af302Sopenharmony_ci}
920570af302Sopenharmony_ci
921570af302Sopenharmony_ciint main(int argc, char *argv[]) {
922570af302Sopenharmony_ci    remove_all_special_handler(SIGABRT);
923570af302Sopenharmony_ci    test_strcat_0010();
924570af302Sopenharmony_ci    test_strcat_0020();
925570af302Sopenharmony_ci    test_strncat_0010();
926570af302Sopenharmony_ci    test_strncat_0020();
927570af302Sopenharmony_ci    test_strchr_0010();
928570af302Sopenharmony_ci    test_strchr_0020();
929570af302Sopenharmony_ci    test_strncpy_0010();
930570af302Sopenharmony_ci    test_strncpy_0020();
931570af302Sopenharmony_ci    test_stpcpy_0010();
932570af302Sopenharmony_ci    test_stpcpy_0020();
933570af302Sopenharmony_ci    test_stpncpy_0010();
934570af302Sopenharmony_ci    test_stpncpy_0020();
935570af302Sopenharmony_ci    test_memchr_0010();
936570af302Sopenharmony_ci    test_memchr_0020();
937570af302Sopenharmony_ci    test_strrchr_0010();
938570af302Sopenharmony_ci    test_strrchr_0020();
939570af302Sopenharmony_ci    test_strcpy_0010();
940570af302Sopenharmony_ci    test_strcpy_0020();
941570af302Sopenharmony_ci    test_memmove_0010();
942570af302Sopenharmony_ci    test_memmove_0020();
943570af302Sopenharmony_ci    test_memset_0010();
944570af302Sopenharmony_ci    test_memset_0020();
945570af302Sopenharmony_ci    test_memcpy_0010();
946570af302Sopenharmony_ci    test_memcpy_0020();
947570af302Sopenharmony_ci    test_strlen_0010();
948570af302Sopenharmony_ci    test_strlen_0020();
949570af302Sopenharmony_ci
950570af302Sopenharmony_ci    #ifdef _GNU_SOURCE
951570af302Sopenharmony_ci    test_mempcpy_0010();
952570af302Sopenharmony_ci    test_mempcpy_0020();
953570af302Sopenharmony_ci    test_mempcpy_0030();
954570af302Sopenharmony_ci    test_memrchr_0010();
955570af302Sopenharmony_ci    test_memrchr_0020();
956570af302Sopenharmony_ci    #endif
957570af302Sopenharmony_ci
958570af302Sopenharmony_ci    #if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
959570af302Sopenharmony_ci    test_strlcat_0010();
960570af302Sopenharmony_ci    test_strlcat_0020();
961570af302Sopenharmony_ci    test_strlcpy_0010();
962570af302Sopenharmony_ci    test_strlcpy_0020();
963570af302Sopenharmony_ci    #endif
964570af302Sopenharmony_ci    return t_status;
965570af302Sopenharmony_ci}