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