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
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 EXPECT_PTRNE("nl_langinfo_0100", lo, NULL);
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, and whether the
69 * return value is empty when the abnormal time data is passed to the nl_langinfo function
70 * @tc.level : Level 2
71 */
nl_langinfo_0300()72 void nl_langinfo_0300()
73 {
74 char *lo = setlocale(LC_TIME, "");
75 if (!lo) {
76 EXPECT_PTRNE("nl_langinfo_0300", lo, NULL);
77 return;
78 }
79 lo = setlocale(LC_TIME, "zh_CN.UTF-8");
80 if (!lo) {
81 EXPECT_PTRNE("nl_langinfo_0300", lo, NULL);
82 return;
83 }
84 char *ptr = nl_langinfo(TIME_ERROR_INFO);
85 EXPECT_STREQ("nl_langinfo_0300", ptr, "");
86 }
87
88 /**
89 * @tc.name : nl_langinfo_0400
90 * @tc.desc : Assert whether the data type of LC_MESSAGES is set to zh_CN.UTF-8 through setlocale, and whether
91 * the return value is empty when the abnormal time data is passed to the nl_langinfo function
92 * @tc.level : Level 2
93 */
nl_langinfo_0400()94 void nl_langinfo_0400()
95 {
96 char *lo = setlocale(LC_MESSAGES, "zh_CN.UTF-8");
97 if (!lo) {
98 EXPECT_PTRNE("nl_langinfo_0400", lo, NULL);
99 return;
100 }
101 char *ptr = nl_langinfo(MESSAGES_ERROR_INFO);
102 EXPECT_STREQ("nl_langinfo_0400", ptr, "");
103 }
104
105 /**
106 * @tc.name : nl_langinfo_0500
107 * @tc.desc : Assert whether the return value result is UTF-8 or ASCII
108 * when the function nl_langinfo passes in the CODESET parameter
109 * @tc.level : Level 2
110 */
nl_langinfo_0500()111 void nl_langinfo_0500()
112 {
113 char *lo = setlocale(LC_CTYPE, "");
114 if (!lo) {
115 EXPECT_PTRNE("nl_langinfo_0500", lo, NULL);
116 return;
117 }
118 char *ptr = nl_langinfo(CODESET);
119 EXPECT_STREQ("nl_langinfo_0500", ptr, "UTF-8");
120
121 lo = setlocale(LC_TIME, "");
122 if (!lo) {
123 EXPECT_PTRNE("nl_langinfo_0500", lo, NULL);
124 return;
125 }
126 ptr = nl_langinfo(CODESET);
127 EXPECT_STREQ("nl_langinfo_0500", ptr, "UTF-8");
128 }
129
main(void)130 int main(void)
131 {
132 langinfo_0100();
133 nl_langinfo_0200();
134 nl_langinfo_0300();
135 nl_langinfo_0400();
136 nl_langinfo_0500();
137 return t_status;
138 }