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 <signal.h>
17 #include <stdlib.h>
18 #include <wchar.h>
19
20 #include "test.h"
21
22 #define SIZEOF_WCHAR(x) (sizeof(x) / sizeof(wchar_t))
23
handler(int sig)24 void handler(int sig)
25 {
26 exit(t_status);
27 }
28
29 /**
30 * @tc.name : wcsncpy_0100
31 * @tc.desc : copy a fixed-size string of wide characters
32 * @tc.level : Level 0
33 */
wcsncpy_0100(void)34 void wcsncpy_0100(void)
35 {
36 const wchar_t src[] = L"Hello";
37 wchar_t buf[SIZEOF_WCHAR(src) + 1];
38 wmemset(buf, L'A', SIZEOF_WCHAR(buf));
39 wchar_t *dest = (wchar_t *)buf;
40
41 if (dest != wcsncpy(dest, src, SIZEOF_WCHAR(src))) {
42 t_error("%s failed: wcsncpy. src = %ls, dest = %ls\n", __func__, src, dest);
43 }
44 }
45
46 /**
47 * @tc.name : wcsncpy_0200
48 * @tc.desc : copy a 0-size string of wide characters
49 * @tc.level : Level 1
50 */
wcsncpy_0200(void)51 void wcsncpy_0200(void)
52 {
53 const wchar_t src[] = L"Hello";
54 wchar_t buf[SIZEOF_WCHAR(src)];
55 wmemset(buf, L'A', SIZEOF_WCHAR(buf));
56 wchar_t *dest = (wchar_t *)buf;
57
58 if (dest != wcsncpy(dest, src, 0)) {
59 t_error("%s failed: wcsncpy. src = %ls, dest = %ls\n", __func__, src, dest);
60 }
61 }
62
63 /**
64 * @tc.name : wcsncpy_0300
65 * @tc.desc : copy a fixed-size string of wide characters with a larger size
66 * @tc.level : Level 1
67 */
wcsncpy_0300(void)68 void wcsncpy_0300(void)
69 {
70 const wchar_t src[] = L"Hello";
71 wchar_t buf[SIZEOF_WCHAR(src) + SIZEOF_WCHAR(src)];
72 wmemset(buf, L'A', SIZEOF_WCHAR(buf));
73 wchar_t *dest = (wchar_t *)buf;
74
75 if (dest != wcsncpy(dest, src, SIZEOF_WCHAR(src) + SIZEOF_WCHAR(src) - 1)) {
76 t_error("%s failed: wcsncpy. src = %ls, dest = %ls\n", __func__, src, dest);
77 }
78
79 int i = 0;
80 for (i = SIZEOF_WCHAR(src) + 1; i < SIZEOF_WCHAR(buf) - 1; i++) {
81 if (buf[i] != L'\0') {
82 t_error("%s failed: buf[%d] = %lc\n", __func__, i, buf[i]);
83 }
84 }
85
86 if (buf[i] != L'A') {
87 t_error("%s failed: buf[%d] = %lc\n", __func__, i, buf[i]);
88 }
89 }
90
91 /**
92 * @tc.name : wcsncpy_0400
93 * @tc.desc : copy a fixed-size string of wide characters to a NULL pointer
94 * @tc.level : Level 2
95 */
wcsncpy_0400(void)96 void wcsncpy_0400(void)
97 {
98 signal(SIGSEGV, handler);
99
100 const wchar_t src[] = L"Hello";
101 wcsncpy(NULL, src, SIZEOF_WCHAR(src));
102 }
103
main(int argc,char * argv[])104 int main(int argc, char *argv[])
105 {
106 wcsncpy_0100();
107 wcsncpy_0200();
108 wcsncpy_0300();
109 wcsncpy_0400();
110
111 return t_status;
112 }
113