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 <signal.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "test.h"
21
22 #define BUF_SIZE (13)
23
handler(int sig)24 void handler(int sig)
25 {
26 exit(t_status);
27 }
28
29 /**
30 * @tc.name : stpcpy_0100
31 * @tc.desc : copy a string returning a pointer to its end
32 * @tc.level : Level 0
33 */
stpcpy_0100(void)34 void stpcpy_0100(void)
35 {
36 char buf[BUF_SIZE];
37 memset(buf, 'A', sizeof(buf));
38
39 char *src = "Hello";
40 char *dest = (char *)buf;
41 strcpy(dest, src);
42
43 if (strcmp(src, buf)) {
44 t_error("%s failed: src = %s, buf = %s\n", __func__, src, buf);
45 return;
46 }
47
48 for (int i = strlen(src) + 1; i < BUF_SIZE; i++) {
49 if (buf[i] != 'A') {
50 t_error("%s failed: buf[%d] = %c\n", __func__, i, buf[i]);
51 return;
52 }
53 }
54 }
55
56 /**
57 * @tc.name : stpcpy_0200
58 * @tc.desc : copy an empty string returning a pointer to its end
59 * @tc.level : Level 1
60 */
stpcpy_0200(void)61 void stpcpy_0200(void)
62 {
63 char buf[1];
64
65 char *src = "";
66 char *dest = (char *)buf;
67 strcpy(dest, src);
68
69 if (strcmp(src, buf)) {
70 t_error("%s failed: src = %s, buf = %s\n", __func__, src, buf);
71 return;
72 }
73
74 if (buf[0] != '\0') {
75 t_error("%s failed: buf[0] = %c\n", __func__, buf[0]);
76 return;
77 }
78 }
79
80 /**
81 * @tc.name : stpcpy_0300
82 * @tc.desc : copy a string with the same size
83 * @tc.level : Level 1
84 */
stpcpy_0300(void)85 void stpcpy_0300(void)
86 {
87 char buf[BUF_SIZE];
88 memset(buf, 'A', sizeof(buf));
89
90 char *src = "Hello world!";
91 char *dest = (char *)buf;
92 strcpy(dest, src);
93
94 if (strcmp(src, buf)) {
95 t_error("%s failed: src = %s, buf = %s\n", __func__, src, buf);
96 return;
97 }
98 }
99
100 /**
101 * @tc.name : stpcpy_0400
102 * @tc.desc : copy a string to a NULL pointer
103 * @tc.level : Level 2
104 */
stpcpy_0400(void)105 void stpcpy_0400(void)
106 {
107 signal(SIGSEGV, handler);
108
109 char *src = "Hello world!";
110 strcpy(NULL, src);
111 }
112
main(int argc,char * argv[])113 int main(int argc, char *argv[])
114 {
115 stpcpy_0100();
116 stpcpy_0200();
117 stpcpy_0300();
118 stpcpy_0400();
119
120 return t_status;
121 }
122