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 #include <time.h>
32 #include <locale.h>
33 #include <langinfo.h>
34 #include "It_test_IO.h"
35
36 /* Zh_cn.utf-8 File content */
37 int fileWords[] = {
38 0x950412de, 0x0, 0x3, 0x1c, 0x34, 0x5,
39 0x4c, 0x0, 0x60, 0x3, 0x61, 0x3,
40 0x65, 0x14f, 0x69, 0x9, 0x1b9, 0x6,
41 0x1c3, 0x1, 0x3, 0x0, 0x0, 0x2,
42 0x69724600, 0x6e614a00, 0x6f725000, 0x7463656a, 0x2d64492d, 0x73726556,
43 0x3a6e6f69, 0x6d695620, 0x6d695328, 0x66696c70, 0x20646569, 0x6e696843,
44 0x29657365, 0x7065520a, 0x2d74726f, 0x6967734d, 0x75422d64, 0x542d7367,
45 0xa203a6f, 0x522d4f50, 0x73697665, 0x2d6e6f69, 0x65746144, 0x3032203a,
46 0x302d3630, 0x31322d34, 0x3a343120, 0x302b3030, 0xa303038, 0x7473614c,
47 0x6172542d, 0x616c736e, 0x3a726f74, 0x68755920, 0x20676e65, 0xa656958,
48 0x676e614c, 0x65676175, 0x6165542d, 0x53203a6d, 0x6c706d69, 0x65696669,
49 0x68432064, 0x73656e69, 0x494d0a65, 0x562d454d, 0x69737265, 0x203a6e6f,
50 0xa302e31, 0x746e6f43, 0x2d746e65, 0x65707954, 0x6574203a, 0x702f7478,
51 0x6e69616c, 0x6863203b, 0x65737261, 0x54553d74, 0xa382d46, 0x746e6f43,
52 0x2d746e65, 0x6e617254, 0x72656673, 0x636e452d, 0x6e69646f, 0x38203a67,
53 0xa746962, 0x72756c50, 0x462d6c61, 0x736d726f, 0x706e203a, 0x6172756c,
54 0x313d736c, 0x6c70203b, 0x6c617275, 0xa3b303d, 0x676e614c, 0x65676175,
55 0x687a203a, 0xa4e435f, 0x65472d58, 0x6172656e, 0x3a726f74, 0x656f5020,
56 0x20746964, 0xa302e33, 0x9f98e600, 0xe49f9ce6, 0xe40094ba, 0x9ce680b8,
57 0x88
58 };
59
SonFunc(VOID)60 UINT32 SonFunc(VOID)
61 {
62 int ret;
63 struct tm timer_;
64 struct tm *timer = &timer_;
65 char buffer[80]; /* 80, The number of characters returned by strftime */
66 char *retptr = nullptr;
67
68 /* set timer as 'Thu Jan 1 23:48:56 1970'" */
69 timer->tm_sec = 32; /* 32, example */
70 timer->tm_min = 3; /* 3, example */
71 timer->tm_hour = 1;
72 timer->tm_mday = 2;
73 timer->tm_mon = 0;
74 timer->tm_year = 70; /* 70, example */
75 timer->tm_wday = 5; /* 5, example */
76 timer->tm_yday = 1;
77 timer->tm_isdst = 0;
78 timer->__tm_gmtoff = 0;
79 timer->__tm_zone = nullptr;
80
81 ret = setenv("MUSL_LOCPATH", "/storage", 1);
82 ICUNIT_ASSERT_EQUAL(ret, 0, -1);
83 retptr = getenv("MUSL_LOCPATH");
84 ICUNIT_ASSERT_NOT_EQUAL(retptr, NULL, -1);
85 ICUNIT_ASSERT_STRING_EQUAL(retptr, "/storage", -1);
86
87 retptr = setlocale(LC_TIME, "en_US.UTF-8");
88 ICUNIT_ASSERT_NOT_EQUAL(retptr, NULL, -1);
89 ICUNIT_ASSERT_STRING_EQUAL(retptr, "en_US.UTF-8", -1);
90
91 ret = strftime(buffer, 80, "%c", timer); /* 80, The maximum number of characters in the string str */
92 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, -1);
93 ICUNIT_ASSERT_STRING_EQUAL(buffer, "Fri Jan 2 01:03:32 1970", -1);
94
95 retptr = setlocale(LC_TIME, "zh_CN.UTF-8");
96 ICUNIT_ASSERT_NOT_EQUAL(retptr, NULL, -1);
97 ICUNIT_ASSERT_STRING_EQUAL(retptr, "zh_CN.UTF-8", -1);
98
99 ret = strftime(buffer, 80, "%c", timer); /* 80, The maximum number of characters in the string str */
100 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, -1);
101 ICUNIT_ASSERT_STRING_EQUAL(buffer, "星期五 一月 2 01:03:32 1970", -1);
102
103 retptr = setlocale(LC_TIME, "");
104 ICUNIT_ASSERT_NOT_EQUAL(retptr, NULL, -1);
105 ICUNIT_ASSERT_STRING_EQUAL(retptr, "C", -1);
106
107 ret = strftime(buffer, 80, "%c", timer); /* 80, The maximum number of characters in the string str */
108 ICUNIT_ASSERT_NOT_EQUAL(ret, 0, -1);
109 ICUNIT_ASSERT_STRING_EQUAL(buffer, "Fri Jan 2 01:03:32 1970", -1);
110
111 return 0;
112 }
113
testcase(VOID)114 static UINT32 testcase(VOID)
115 {
116 int ret, status;
117 locale_t oldloc = LC_GLOBAL_LOCALE;
118 locale_t newloc = nullptr;
119
120 char *pathList[] = {"/storage/zh_CN.UTF-8"};
121 char *streamList[] = {reinterpret_cast<char *>(fileWords)};
122 int streamLen[] = {sizeof(fileWords) - 2};
123
124 newloc = duplocale(oldloc);
125 ICUNIT_ASSERT_NOT_EQUAL(newloc, nullptr, -1);
126 free(newloc);
127
128 ret = PrepareFileEnv(pathList, streamList, streamLen, 1);
129 if (ret != 0) {
130 printf("error: need some env file, but prepare is not ok");
131 (VOID)RecoveryFileEnv(pathList, 1);
132 return -1;
133 }
134
135 pid_t pid = fork();
136 if (pid == 0) {
137 ret = SonFunc();
138 exit(ret);
139 }
140
141 ret = waitpid(pid, &status, 0);
142 ICUNIT_ASSERT_EQUAL(ret, pid, ret);
143 (VOID)RecoveryFileEnv(pathList, 1);
144 status = WEXITSTATUS(status);
145 ICUNIT_ASSERT_EQUAL(status, 0, status);
146 setlocale(LC_ALL, "C");
147
148 return LOS_OK;
149 }
150
IO_TEST_DUPLOCALE_001(void)151 VOID IO_TEST_DUPLOCALE_001(void)
152 {
153 TEST_ADD_CASE(__FUNCTION__, testcase, TEST_LIB, TEST_LIBC, TEST_LEVEL1, TEST_FUNCTION);
154 }
155