1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 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 "ohos_types.h"
33 #include "posix_test.h"
34 #include "los_config.h"
35 #include "kernel_test.h"
36 #include <regex.h>
37 #include "log.h"
38
39 #define EQUAL 0
40
41 #ifndef REG_OK
42 #define REG_OK 0
43 #endif
44 /* *
45 * @tc.desc : register a test suite, this suite is used to test basic flow and interface dependency
46 * @param : subsystem name is utils
47 * @param : module name is utilsFile
48 * @param : test suit name is PosixStrCaseCmpFuncTestSuite
49 */
50 LITE_TEST_SUIT(Posix, PosixRegexTest, PosixRegexFuncTestSuite);
51
52 /* *
53 * @tc.setup : setup for all testcases
54 * @return : setup result, TRUE is success, FALSE is fail
55 */
PosixRegexFuncTestSuiteSetUp(void)56 static BOOL PosixRegexFuncTestSuiteSetUp(void)
57 {
58 LOG("+-------------------------------------------+\n");
59 LOG("+--------PosixRegexFuncTestSuiteSetUp-------+\n");
60 LOG("+-------------------------------------------+\n");
61 return TRUE;
62 }
63
64 /* *
65 * @tc.teardown : teardown for all testcases
66 * @return : teardown result, TRUE is success, FALSE is fail
67 */
PosixRegexFuncTestSuiteTearDown(void)68 static BOOL PosixRegexFuncTestSuiteTearDown(void)
69 {
70 LOG("+-------------------------------------------+\n");
71 LOG("+-------PosixRegexFuncTestSuiteTearDown-----+\n");
72 LOG("+-------------------------------------------+\n");
73 return TRUE;
74 }
75
TestRegex(int flag,const char * pattern,const char * buf,const int expectedStatus,const char * expectedRes)76 int TestRegex(int flag, const char *pattern, const char *buf, const int expectedStatus, const char *expectedRes)
77 {
78 regmatch_t pmatch[1];
79 const size_t nmatch = 1;
80 regex_t reg;
81 char res[64];
82 int j = 0;
83 regcomp(®, pattern, flag);
84 int status = regexec(®, buf, nmatch, pmatch, 0);
85 TEST_ASSERT_EQUAL_INT(status, expectedStatus);
86 if (status == REG_NOMATCH) {
87 LOG("no match");
88 } else if (status == 0) {
89 LOG("Match:");
90 for (int i = pmatch[0].rm_so; i < pmatch[0].rm_eo; i++) {
91 putchar(buf[i]);
92 res[j] = buf[i];
93 j++;
94 }
95 res[j] = 0;
96 LOG("\n");
97 TEST_ASSERT_EQUAL_STRING(res, expectedRes);
98 }
99 regfree(®);
100 return 0;
101 }
102
TestRegcomp(int flag,const char * pattern,const int expectedStatus)103 int TestRegcomp(int flag, const char *pattern, const int expectedStatus)
104 {
105 regex_t reg;
106 int status = regcomp(®, pattern, flag);
107 LOG("pattern : %s ,real status : %d \n", pattern, status);
108 TEST_ASSERT_EQUAL_INT(status, expectedStatus);
109 regfree(®);
110 return 0;
111 }
112
113 /* *
114 * @tc.number SUB_KERNEL_SYS_REGCOMP_0100
115 * @tc.name test regcomp/regexec/regfree cflags = Extended
116 * @tc.desc [C- SOFTWARE -0200]
117 */
118 LITE_TEST_CASE(PosixRegexFuncTestSuite, testRegexExtended001, Function | MediumTest | Level1)
119 {
120 TestRegex(REG_EXTENDED, "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*.\\w+([-.]\\w+)*$", "harmony20000925@abcdef.com", REG_OK,
121 "harmony20000925@abcdef.com");
122
123 TestRegex(REG_EXTENDED, "^addr=([^&]*)", "fawei123&sex=girl&age=18\r\naddr=bantian&hobby=movie", REG_NOMATCH,
124 NULL);
125 return 0;
126 }
127
128 /* *
129 * @tc.number SUB_KERNEL_SYS_REGCOMP_0200
130 * @tc.name test regcomp/regexec/regfree cflags = ICASE
131 * @tc.desc [C- SOFTWARE -0200]
132 */
133 LITE_TEST_CASE(PosixRegexFuncTestSuite, testRegexIcase001, Function | MediumTest | Level1)
134 {
135 TestRegex(REG_ICASE, "HARMONY[1-9]", "harmony20000925@abcdef.com", REG_OK, "harmony2");
136
137 TestRegex(REG_ICASE, "HARMONY([1-9])", "harmony20000925@abcdef.com", REG_NOMATCH, NULL);
138 return 0;
139 }
140
141 /* *
142 * @tc.number SUB_KERNEL_SYS_REGCOMP_0300
143 * @tc.name test regcomp/regexec/regfree cflags = NEWLINE
144 * @tc.desc [C- SOFTWARE -0200]
145 */
146 LITE_TEST_CASE(PosixRegexFuncTestSuite, testRegexNewline001, Function | MediumTest | Level1)
147 {
148 TestRegex(REG_EXTENDED | REG_NEWLINE, "^addr=([^&]*)", "fawei123&sex=girl&age=18\r\naddr=bantian&hobby=movie",
149 REG_OK, "addr=bantian");
150
151 TestRegex(REG_EXTENDED | REG_NEWLINE, "^addr=([^&]*)", "fawei123&sex=girl&age=18&addr=bantian&hobby=movie",
152 REG_NOMATCH, NULL);
153 return 0;
154 }
155
156 /* *
157 * @tc.number SUB_KERNEL_SYS_REGCOMP_0400
158 * @tc.name test regcomp/regexec/regfree cflags = NOSUB
159 * @tc.desc [C- SOFTWARE -0200]
160 */
161 LITE_TEST_CASE(PosixRegexFuncTestSuite, testRegexNosub001, Function | MediumTest | Level1)
162 {
163 int cflags = REG_ICASE | REG_NOSUB;
164 regex_t reg;
165 const char *buf = "harmony20000925@abcdef.com";
166 const char *pattern2 = "HARMONY[1-9]";
167 regcomp(®, pattern2, cflags);
168 int status = regexec(®, buf, (size_t)0, NULL, 0);
169 TEST_ASSERT_EQUAL_INT(status, 0);
170 regfree(®);
171 return 0;
172 }
173
174 /* *
175 * @tc.number SUB_KERNEL_SYS_REGCOMP_0500
176 * @tc.name test regcomp cflags = NOSUB
177 * @tc.desc [C- SOFTWARE -0200]
178 */
179 LITE_TEST_CASE(PosixRegexFuncTestSuite, testRegcomp001, Function | MediumTest | Level1)
180 {
181 TestRegcomp(REG_EXTENDED, "[[.]", REG_ECOLLATE);
182 TestRegcomp(REG_EXTENDED, "[[:class:", REG_ECTYPE);
183 TestRegcomp(REG_EXTENDED, "[abcdefg", REG_EBRACK);
184 TestRegcomp(REG_EXTENDED, "\\x{4e00-\\x{9fa5}", REG_EBRACE);
185 TestRegcomp(REG_EXTENDED, "*abcdefg", REG_BADRPT);
186 return 0;
187 }
188
189 RUN_TEST_SUITE(PosixRegexFuncTestSuite);
190
PosixRegexFuncTest()191 void PosixRegexFuncTest()
192 {
193 LOG("begin PosixRegexFuncTest....");
194 RUN_ONE_TESTCASE(testRegexExtended001);
195 RUN_ONE_TESTCASE(testRegexIcase001);
196 RUN_ONE_TESTCASE(testRegexNewline001);
197 RUN_ONE_TESTCASE(testRegexNosub001);
198 RUN_ONE_TESTCASE(testRegcomp001);
199
200 return;
201 }
202