• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2022 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <string.h>
33 #include <stdlib.h>
34 #include "ohos_types.h"
35 #include "posix_test.h"
36 #include "los_config.h"
37 #include "kernel_test.h"
38 #include "log.h"
39 #include <time.h>
40 
41 /* *
42  * @tc.desc      : register a test suite, this suite is used to test basic flow and interface dependency
43  * @param        : subsystem name is utils
44  * @param        : module name is utilsFile
45  * @param        : test suit name is PosixStringFuncTestSuite
46  */
47 LITE_TEST_SUIT(Posix, Posixstring, PosixStringFuncTestSuite);
48 
49 
50 /* *
51  * @tc.setup     : setup for all testcases
52  * @return       : setup result, TRUE is success, FALSE is fail
53  */
PosixStringFuncTestSuiteSetUp(void)54 static BOOL PosixStringFuncTestSuiteSetUp(void)
55 {
56     return TRUE;
57 }
58 
59 /* *
60  * @tc.teardown  : teardown for all testcases
61  * @return       : teardown result, TRUE is success, FALSE is fail
62  */
PosixStringFuncTestSuiteTearDown(void)63 static BOOL PosixStringFuncTestSuiteTearDown(void)
64 {
65     printf("+Hello this is a String  function test+\n");
66     return TRUE;
67 }
68 
69 /* *
70  * @tc.number    : SUB_KERNEL_POSIX_STRING_OPERATION_001
71  * @tc.name      : Memony operation for strcmp test
72  * @tc.desc      : [C- SOFTWARE -0200]
73  */
74 LITE_TEST_CASE(PosixStringFuncTestSuite, testStrstrcmp001, Function | MediumTest | Level1)
75 {
76     int retValue = 0;
77     char source[] = {"Compiler exited with error"};
78     char dest[] = {"Compiler exited with error"};
79 
80     retValue = strcmp(source, dest);
81     TEST_ASSERT_EQUAL_INT(retValue, 0);
82 
83     int ret = strcmp("abcdef", "ABCDEF");
84     TEST_ASSERT_LESS_THAN(ret, 0);
85 
86     ret = strcmp("123456", "654321");
87     TEST_ASSERT_GREATER_THAN(ret, 0);
88     TEST_ASSERT_EQUAL_INT(strcmp("~!@#$%^&*()_+", "~!@#$%^&*()_+"), 0);
89     return 0;
90 };
91 
92 
93 /* *
94  * @tc.number    : SUB_KERNEL_POSIX_strcmp_OPERATION_002
95  * @tc.name      : Memony operation for strncmp test
96  * @tc.desc      : [C- SOFTWARE -0200]
97  */
98 LITE_TEST_CASE(PosixStringFuncTestSuite, testStrstrcmp002, Function | MediumTest | Level1)
99 {
100     int retValue = 0;
101     char source[] = {"Compiler exited with error"};
102     char dest[] = {"00000000000"};
103 
104     retValue = strcmp(source, dest);
105     TEST_ASSERT_LESS_THAN(retValue, 0);
106     return 0;
107 };
108 
109 
110 /* *
111  * @tc.number    : SUB_KERNEL_POSIX_strcmp_OPERATION_003
112  * @tc.name      : Memony operation for strncmp test
113  * @tc.desc      : [C- SOFTWARE -0200]
114  */
115 LITE_TEST_CASE(PosixStringFuncTestSuite, testStrstrcmp003, Function | MediumTest | Level1)
116 {
117     int retValue = 0;
118     char source[] = {"0000000"};
119     char dest[] = {"Compiler exited with error"};
120 
121     retValue = strcmp(source, dest);
122     TEST_ASSERT_GREATER_THAN(retValue, 0);
123     return 0;
124 };
125 
126 
127 /* *
128  * @tc.number    : SUB_KERNEL_POSIX_strdup_OPERATION_001
129  * @tc.name      : Memony operation for strdup test
130  * @tc.desc      : [C- SOFTWARE -0200]
131  */
132 LITE_TEST_CASE(PosixStringFuncTestSuite, testStrStrdup001, Function | MediumTest | Level1)
133 {
134     char source[] = {"Compiler exited with error"};
135     char *dest;
136 
137     dest = strdup(source);
138     TEST_ASSERT_NOT_NULL(dest);
139     printf("The Result Display :%s\r\n", dest);
140     TEST_ASSERT_EQUAL_CHAR_ARRAY(dest, source, sizeof(source) / sizeof(source[0]));
141 
142     char src[] = "hello world !";
143     char *ret = strdup(src);
144     TEST_ASSERT_EQUAL_CHAR_ARRAY(ret, src, sizeof(src) / sizeof(src[0]));
145     free(ret);
146 
147     char srcS[] = "This is String1";
148     ret = strdup(srcS);
149     TEST_ASSERT_EQUAL_CHAR_ARRAY(ret, "This is String1", sizeof(srcS) / sizeof(srcS[0]));
150     free(ret);
151     return 0;
152 };
153 
154 
155 /* *
156  * @tc.number    : SUB_KERNEL_POSIX_strdup_OPERATION_002
157  * @tc.name      : Memony operation for strdup test
158  * @tc.desc      : [C- SOFTWARE -0200]
159  */
160 LITE_TEST_CASE(PosixStringFuncTestSuite, testStrStrdup002, Function | MediumTest | Level1)
161 {
162     char source[] = {"export MY_TEST_PATH=/opt/hadoop-2.6.5"};
163     char *dest;
164 
165     dest = strdup(source);
166     TEST_ASSERT_NOT_NULL(dest);
167     printf("The Result Display :%s\r\n", dest);
168     TEST_ASSERT_EQUAL_CHAR_ARRAY(dest, source, sizeof(source) / sizeof(source[0]));
169     return 0;
170 };
171 
172 
173 /* *
174  * @tc.number    : SUB_KERNEL_POSIX_strcspn_OPERATION_001
175  * @tc.name      : Memony operation for strcspn test
176  * @tc.desc      : [C- SOFTWARE -0200]
177  */
178 LITE_TEST_CASE(PosixStringFuncTestSuite, testStrStrcspn001, Function | MediumTest | Level1)
179 {
180     int retValue = 0;
181     char source[] = {"export MY_TEST_PATH=/opt/hadoop-2.6.5"};
182     char dest1[] = {"H"};
183 
184     retValue = strcspn(source, dest1);
185     TEST_ASSERT_EQUAL_INT(18U, retValue);
186 
187     const char dest[] = "hello world !";
188     const char src[] = "!";
189     size_t ret = strcspn(dest, src);
190     TEST_ASSERT_EQUAL_INT(12U, ret);
191 
192     const char srcS[] = "a";
193     ret = strcspn(dest, srcS);
194     TEST_ASSERT_EQUAL_INT(13U, ret);
195     return 0;
196 };
197 
198 /* *
199  * @tc.number    : SUB_KERNEL_POSIX_strcspn_OPERATION_002
200  * @tc.name      : Memony operation for strcspn test
201  * @tc.desc      : [C- SOFTWARE -0200]
202  */
203 LITE_TEST_CASE(PosixStringFuncTestSuite, testStrStrcspn002, Function | MediumTest | Level1)
204 {
205     int retValue = 0;
206     char source[] = {"Compiler exited with error"};
207     char dest[] = {"or"};
208 
209     retValue = strcspn(source, dest);
210     TEST_ASSERT_EQUAL_INT(1, retValue);
211     return 0;
212 };
213 
214 #ifndef LOSCFG_COMPILER_ICCARM
215 /* *
216  * @tc.number    : SUB_KERNEL_POSIX_strptime_OPERATION_001
217  * @tc.name      : Memony operation for strptime test
218  * @tc.desc      : [C- SOFTWARE -0200]
219  */
220 LITE_TEST_CASE(PosixStringFuncTestSuite, testStrStrptime001, Function | MediumTest | Level1)
221 {
222     struct tm tmData;
223     (void)memset_s(&tmData, sizeof(struct tm), 0, sizeof(struct tm));
224     char *ret = strptime("2020-10-29 21:24:00abc", "%Y-%m-%d %H:%M:%S", &tmData);
225     TEST_ASSERT_EQUAL_CHAR_ARRAY(ret, "abc", 3);
226     TEST_ASSERT_EQUAL_INT(tmData.tm_year, 120);
227     TEST_ASSERT_EQUAL_INT(tmData.tm_mon, 9);
228     TEST_ASSERT_EQUAL_INT(tmData.tm_mday, 29);
229     TEST_ASSERT_EQUAL_INT(tmData.tm_hour, 21);
230     TEST_ASSERT_EQUAL_INT(tmData.tm_min, 24);
231     return 0;
232 }
233 #endif
234 
235 
236 RUN_TEST_SUITE(PosixStringFuncTestSuite);
237 
PosixStringFuncTest03()238 void PosixStringFuncTest03()
239 {
240     LOG("begin PosixStringFuncTest03....");
241     RUN_ONE_TESTCASE(testStrstrcmp001);
242     RUN_ONE_TESTCASE(testStrstrcmp002);
243     RUN_ONE_TESTCASE(testStrstrcmp003);
244     RUN_ONE_TESTCASE(testStrStrdup001);
245     RUN_ONE_TESTCASE(testStrStrdup002);
246     RUN_ONE_TESTCASE(testStrStrcspn001);
247     RUN_ONE_TESTCASE(testStrStrcspn002);
248 #ifndef LOSCFG_COMPILER_ICCARM
249     RUN_ONE_TESTCASE(testStrStrptime001);
250 #endif
251 
252     return;
253 }