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