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 <langinfo.h>
17 #include <locale.h>
18 #include <stdlib.h>
19 #include "functionalext.h"
20
21 #define TIME_ERROR_INFO 0x20034
22 #define MESSAGES_ERROR_INFO 0x50004
23
24 /**
25 * @tc.name : langinfo_0100
26 * @tc.desc : Asserts whether the nl_langinfo function succeeds in reading data
27 * from the MUSL_LOCPATH environment variable set to zh_CN. Utf-8(There is no file in "/etc/locale" now)
28 * @tc.level : Level 0
29 */
langinfo_0100(void)30 void langinfo_0100(void)
31 {
32 setenv("MUSL_LOCPATH", "/etc/locale", 1);
33 char *lo = setlocale(LC_TIME, "");
34 if (!lo) {
35 EXPECT_PTRNE("nl_langinfo_0100", lo, NULL);
36 return;
37 }
38 lo = setlocale(LC_TIME, "zh_CN.UTF-8");
39 char *ptr = nl_langinfo(DAY_2);
40 EXPECT_STREQ("nl_langinfo_0100", ptr, "Monday");
41 }
42
43 /**
44 * @tc.name : nl_langinfo_0200
45 * @tc.desc : Assert that the nl_langinfo function does not read data from the default zh_CN.UTF-8 file
46 * when the locale is reset to the default environment
47 * @tc.level : Level 0
48 */
nl_langinfo_0200()49 void nl_langinfo_0200()
50 {
51 char *lo = setlocale(LC_TIME, "");
52 if (!lo) {
53 EXPECT_PTRNE("nl_langinfo_0200", lo, NULL);
54 return;
55 }
56 char *ptr = nl_langinfo(DAY_2);
57 if (ptr) {
58 EXPECT_STRNE("nl_langinfo_0200", ptr, "lm星期一");
59 }
60 }
61
62 /**
63 * @tc.name : nl_langinfo_0300
64 * @tc.desc : Assert whether the LC_TIME data type is set to zh_CN.UTF-8 through setlocale,
65 * and whether the return value is empty when the abnormal time data is passed to the nl_langinfo function.
66 * MUSL_LOCPATH is `invalid` in system environment so it should be changed to `invalid` check
67 * @tc.level : Level 2
68 */
nl_langinfo_0300()69 void nl_langinfo_0300()
70 {
71 char *lo = setlocale(LC_TIME, "");
72 if (!lo) {
73 EXPECT_PTRNE("nl_langinfo_0300", lo, NULL);
74 return;
75 }
76 lo = setlocale(LC_TIME, "zh_CN.UTF-8");
77 char *ptr = nl_langinfo(TIME_ERROR_INFO);
78 EXPECT_STREQ("nl_langinfo_0300", ptr, "");
79 }
80
81 /**
82 * @tc.name : nl_langinfo_0400
83 * @tc.desc : Assert whether the data type of LC_MESSAGES is set to zh_CN.UTF-8 through setlocale,
84 * and whether the return value is empty when the abnormal time data is passed to the nl_langinfo function
85 * MUSL_LOCPATH is `invalid` in system environment so it should be changed to `invalid` check
86 * @tc.level : Level 2
87 */
nl_langinfo_0400()88 void nl_langinfo_0400()
89 {
90 setlocale(LC_MESSAGES, "zh_CN.UTF-8");
91 char *ptr = nl_langinfo(MESSAGES_ERROR_INFO);
92 EXPECT_STREQ("nl_langinfo_0400", ptr, "");
93 }
94
95 /**
96 * @tc.name : nl_langinfo_0500
97 * @tc.desc : Assert whether the return value result is UTF-8 or ASCII
98 * when the function nl_langinfo passes in the CODESET parameter
99 * @tc.level : Level 2
100 */
nl_langinfo_0500()101 void nl_langinfo_0500()
102 {
103 char *lo = setlocale(LC_CTYPE, "");
104 if (!lo) {
105 EXPECT_PTRNE("nl_langinfo_0500", lo, NULL);
106 return;
107 }
108 char *ptr = nl_langinfo(CODESET);
109 EXPECT_STREQ("nl_langinfo_0500", ptr, "UTF-8");
110
111 lo = setlocale(LC_TIME, "");
112 if (!lo) {
113 EXPECT_PTRNE("nl_langinfo_0500", lo, NULL);
114 return;
115 }
116 ptr = nl_langinfo(CODESET);
117 EXPECT_STREQ("nl_langinfo_0500", ptr, "UTF-8");
118 }
119
120 /**
121 * @tc.name : nl_langinfo_0600
122 * @tc.desc : Assert whether the return value result is not "C.UTF-8"
123 * when the function nl_langinfo passes in the "65535" parameter
124 * @tc.level : Level 2
125 */
nl_langinfo_0600()126 void nl_langinfo_0600()
127 {
128 char *lo = setlocale(LC_ALL, "");
129 if (!lo) {
130 EXPECT_PTRNE("nl_langinfo_0600", lo, NULL);
131 return;
132 }
133 char *ptr = nl_langinfo(RADIXCHAR - 1);
134 EXPECT_STREQ("nl_langinfo_0600", ptr, "C.UTF-8");
135 }
136
137 /**
138 * @tc.name : nl_langinfo_0700
139 * @tc.desc : Assert whether the return value result is not ""
140 * when the function nl_langinfo passes in the THOUSEP parameter
141 * @tc.level : Level 2
142 */
nl_langinfo_0700()143 void nl_langinfo_0700()
144 {
145 char *lo = setlocale(LC_ALL, "");
146 if (!lo) {
147 EXPECT_PTRNE("nl_langinfo_0700", lo, NULL);
148 return;
149 }
150 char *ptr = nl_langinfo(THOUSEP);
151 EXPECT_STREQ("nl_langinfo_0700", ptr, "");
152 }
153
154 /**
155 * @tc.name : nl_langinfo_0800
156 * @tc.desc : Assert whether the return value result is not ""
157 * when the function nl_langinfo passes in the THOUSEP * LC_MONETARY parameter
158 * @tc.level : Level 2
159 */
nl_langinfo_0800()160 void nl_langinfo_0800()
161 {
162 char *lo = setlocale(LC_ALL, "");
163 if (!lo) {
164 EXPECT_PTRNE("nl_langinfo_0800", lo, NULL);
165 return;
166 }
167 char *ptr = nl_langinfo(THOUSEP * LC_MONETARY);
168 EXPECT_STREQ("nl_langinfo_0800", ptr, "");
169 }
170
171 /**
172 * @tc.name : nl_langinfo_0900
173 * @tc.desc : Assert whether the return value result is not ""
174 * when the function nl_langinfo passes in the RADIXCHAR * LC_MONETARY parameter
175 * @tc.level : Level 2
176 */
nl_langinfo_0900()177 void nl_langinfo_0900()
178 {
179 char *lo = setlocale(LC_ALL, "");
180 if (!lo) {
181 EXPECT_PTRNE("nl_langinfo_0900", lo, NULL);
182 return;
183 }
184 char *ptr = nl_langinfo(RADIXCHAR * LC_MONETARY);
185 EXPECT_STREQ("nl_langinfo_0900", ptr, "");
186 }
187
main(void)188 int main(void)
189 {
190 if (unsetenv("LANG") != 0) {
191 perror("unsetenv failed");
192 return t_status;
193 }
194 langinfo_0100();
195 nl_langinfo_0200();
196 nl_langinfo_0300();
197 nl_langinfo_0400();
198 nl_langinfo_0500();
199 nl_langinfo_0600();
200 nl_langinfo_0700();
201 nl_langinfo_0800();
202 nl_langinfo_0900();
203 return t_status;
204 }