• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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 <gtest/gtest.h>
17 #include <stdlib.h>
18 #include "ActsLibuvTest.h"
19 extern "C"{
20 #include "runner.h"
21 }
22 #include "uv.h"
23 
24 typedef void (*init_plugin_function)();
25 
26 namespace OHOS {
27     using namespace std;
28     using namespace testing::ext;
29     static const char TESTFILE[] = "foo.gz";
30 
31     static uv_once_t once_only = UV_ONCE_INIT;
32 
33     int i = 0;
34 
Increment()35     void Increment()
36     {
37         i++;
38     }
39     // Preset action of the test suite, which is executed before the first test case
SetUpTestCase(void)40     void ActsLibuvTest::SetUpTestCase(void)
41     {
42     }
43     // Test suite cleanup action, which is executed after the last test case
TearDownTestCase(void)44     void ActsLibuvTest::TearDownTestCase(void)
45     {
46     }
47     // Preset action of the test case
SetUp()48     void ActsLibuvTest::SetUp()
49     {
50     }
51     // Cleanup action of the test case
TearDown()52     void ActsLibuvTest::TearDown()
53     {
54     }
55 
56 /**
57  * @tc.number    : ActsZlibTest_0100
58  * @tc.name      : Test uv_version and uv_version_string test
59  * @tc.desc      : [C- SOFTWARE -0200]
60 */
61 HWTEST_F(ActsLibuvTest, TestLibuvTestUVVersion, Function | MediumTest | Level2)
62 {
63     int version = uv_version();
64     ASSERT_EQ(version, UV_VERSION_HEX);
65     fprintf(stderr, "uv_version error: %d\n", version);
66 
67     auto versionString = uv_version_string();
68     fprintf(stderr, "uv_version_string error: %s\n", versionString);
69 
70     int replaceAllocator = uv_replace_allocator(nullptr, nullptr, nullptr, nullptr);
71     ASSERT_EQ(replaceAllocator, UV_EINVAL);
72     fprintf(stderr, "uv_replace_allocator error: %d\n", replaceAllocator);
73 
74     uv_loop_t* loop = uv_loop_new();
75     ASSERT_TRUE(loop != nullptr);
76 }
77 
78 /**
79  * @tc.number    : ActsZlibTest_0200
80  * @tc.name      : Test uv_loop_new
81  * @tc.desc      : [C- SOFTWARE -0200]
82 */
83 HWTEST_F(ActsLibuvTest, TestLibuvTestLoop, Function | MediumTest | Level2)
84 {
85     uv_loop_t* newLoop;
86     newLoop = uv_loop_new();
87     ASSERT_TRUE(newLoop != NULL);
88     uv_loop_delete(newLoop);
89     ASSERT_TRUE(true);
90 }
91 
92 /**
93  * @tc.number    : ActsZlibTest_0300
94  * @tc.name      : Test uv_err_name_r
95  * @tc.desc      : [C- SOFTWARE -0200]
96 */
97 
98 HWTEST_F(ActsLibuvTest, TestLibuvTestErrName, Function | MediumTest | Level2)
99 {
100     int err = 0;
101     char buf[40];
102     size_t buflen = 40;
103     char* newBuf = uv_err_name_r(err, buf, buflen);
104     fprintf(stderr, "uv_err_name_r error: %s\n", newBuf);
105     ASSERT_STREQ(newBuf, "Unknown system error 0");
106 }
107 
108 /**
109  * @tc.number    : ActsZlibTest_0400
110  * @tc.name      : Test uv_req_get_data and  uv_req_set_data and uv_req_get_type
111  * @tc.desc      : [C- SOFTWARE -0200]
112 */
113 HWTEST_F(ActsLibuvTest, TestLibuvTestReqGetData, Function | MediumTest | Level2)
114 {
115     float f = 5.5f;
116     float* pf = &f;
117     uv_req_t* req = (uv_req_t *)malloc(sizeof(uv_req_t));
118     uv_req_t* setReq = (uv_req_t *)malloc(sizeof(uv_req_t));
119     req->data = pf;
120     req->type = UV_UNKNOWN_REQ;
121     fprintf(stderr, "uv_req_get_data: %f\n", *(float*)uv_req_get_data(req));
122     fprintf(stderr, "uv_req_get_type: %d\n", uv_req_get_type(req));
123     ASSERT_EQ(uv_req_get_data(req), pf);
124     ASSERT_EQ(uv_req_get_type(req), UV_UNKNOWN_REQ);
125 
126     uv_req_set_data(setReq, pf);
127     ASSERT_EQ(uv_req_get_data(setReq), pf);
128 }
129 
130 /**
131  * @tc.number    : ActsZlibTest_0500
132  * @tc.name      : Test uv_print_all_handles and uv_print_active_handles
133  * @tc.desc      : [C- SOFTWARE -0200]
134 */
135 HWTEST_F(ActsLibuvTest, TestLibuvTestPrintHandles, Function | MediumTest | Level2)
136 {
137     uv_loop_t* loop = nullptr;
138     FILE *fp = fopen(TESTFILE, "w");
139     uv_print_all_handles(loop, fp);
140     uv_print_active_handles(loop, fp);
141     ASSERT_TRUE(true);
142 }
143 
144 /**
145  * @tc.number    : ActsZlibTest_0600
146  * @tc.name      : Test uv_udp_get_send_queue_size and uv_udp_get_send_queue_count
147  * @tc.desc      : [C- SOFTWARE -0200]
148 */
149 HWTEST_F(ActsLibuvTest, TestLibuvTestUpdSendQueue, Function | MediumTest | Level2)
150 {
151     size_t size, sizeC;
152     uv_udp_t* handle = (uv_udp_t *)malloc(sizeof(uv_udp_t));
153     handle->send_queue_size = 5;
154     handle->send_queue_count = 5;
155     size = uv_udp_get_send_queue_size(handle);
156     ASSERT_TRUE(size == 5);
157 
158     sizeC = uv_udp_get_send_queue_count(handle);
159     ASSERT_TRUE(sizeC == 5);
160 }
161 
162 /**
163  * @tc.number    : ActsZlibTest_0700
164  * @tc.name      : Test uv_fs_lchown
165  * @tc.desc      : [C- SOFTWARE -0200]
166 */
167 HWTEST_F(ActsLibuvTest, TestLibuvTestFsLchown, Function | MediumTest | Level2)
168 {
169     int retC;
170     uv_loop_t loop;
171     uv_fs_t req;
172     const char *path = "/date/";
173     uv_uid_t uid = 10000;
174     uv_gid_t gid = 2020;
175     retC = uv_fs_lchown(&loop, &req, path, uid, gid, nullptr);
176     fprintf(stderr, "uv_err_name_r error: %d\n", retC);
177 }
178 
179 /**
180  * @tc.number    : ActsZlibTest_0800
181  * @tc.name      : Test uv_disable_stdio_inheritance
182  * @tc.desc      : [C- SOFTWARE -0200]
183 */
184 HWTEST_F(ActsLibuvTest, TestLibuvTestDisableStdioInheritance, Function | MediumTest | Level2)
185 {
186     uv_disable_stdio_inheritance();
187     ASSERT_TRUE(true);
188 }
189 
190 /**
191  * @tc.number    : ActsZlibTest_0900
192  * @tc.name      : Test uv_dlsym
193  * @tc.desc      : [C- SOFTWARE -0200]
194  */
195 HWTEST_F(ActsLibuvTest, TestLibuvTestDlsym, Function | MediumTest | Level2) {
196     int argc = 3;
197     const char *tmp[] = {"aaaa", "bbbb", "cccc"};
198     const char **argv = tmp;
199     uv_lib_t *lib = (uv_lib_t *)malloc(sizeof(uv_lib_t));
200     while (--argc) {
201         uv_dlopen(argv[argc], lib);
202         ASSERT_TRUE(true);
203 
204         init_plugin_function init_plugin;
205         uv_dlsym(lib, "initialize", (void **)&init_plugin);
206         ASSERT_TRUE(true);
207     }
208 }
209 
210 /**
211  * @tc.number    : ActsZlibTest_1000
212  * @tc.name      : Test uv_once
213  * @tc.desc      : [C- SOFTWARE -0200]
214 */
215 HWTEST_F(ActsLibuvTest, TestLibuvTestUvOnce, Function | MediumTest | Level2)
216 {
217     uv_once(&once_only, Increment);
218     ASSERT_TRUE(true);
219 }
220 }
221