• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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      : stpncpy_0100
31  * @tc.desc      : copy a string returning a pointer to its end
32  * @tc.level     : Level 0
33  */
stpncpy_0100(void)34 void stpncpy_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     stpncpy(dest, src, strlen(src));
42 
43     if (strncmp(src, buf, strlen(src))) {
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      : stpncpy_0200
58  * @tc.desc      : copy an empty string returning a pointer to its end
59  * @tc.level     : Level 1
60  */
stpncpy_0200(void)61 void stpncpy_0200(void)
62 {
63     char buf[1];
64     memset(buf, 0, sizeof(buf));
65 
66     char *src = "";
67     char *dest = (char *)buf;
68     stpncpy(dest, src, strlen(src));
69 
70     if (strcmp(src, buf)) {
71         t_error("%s failed: src = %s, buf = %s\n", __func__, src, buf);
72         return;
73     }
74 
75     if (buf[0] != '\0') {
76         t_error("%s failed: buf[0] = %c\n", __func__, buf[0]);
77         return;
78     }
79 }
80 
81 /**
82  * @tc.name      : stpncpy_0300
83  * @tc.desc      : copy a string with the same size
84  * @tc.level     : Level 1
85  */
stpncpy_0300(void)86 void stpncpy_0300(void)
87 {
88     char buf[BUF_SIZE];
89     memset(buf, 'A', sizeof(buf));
90 
91     char *src = "Hello world!";
92     char *dest = (char *)buf;
93     stpncpy(dest, src, strlen(src));
94 
95     if (strncmp(src, buf, strlen(src))) {
96         t_error("%s failed: src = %s, buf = %s\n", __func__, src, buf);
97         return;
98     }
99 }
100 
101 /**
102  * @tc.name      : stpncpy_0400
103  * @tc.desc      : copy a string to a NULL pointer
104  * @tc.level     : Level 2
105  */
stpncpy_0400(void)106 void stpncpy_0400(void)
107 {
108     signal(SIGSEGV, handler);
109 
110     char *src = "Hello world!";
111     stpncpy(NULL, src, strlen(src));
112 }
113 
main(int argc,char * argv[])114 int main(int argc, char *argv[])
115 {
116     stpncpy_0100();
117     stpncpy_0200();
118     stpncpy_0300();
119     stpncpy_0400();
120 
121     return t_status;
122 }
123