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 "test_common.h"
17
18 #include "constant.h"
19 #include "securec.h"
20 #include "websocket_module.h"
21
22 namespace OHOS::NetStack {
23 napi_value WEBSOCKET_HANDLE = nullptr;
24
25 int MESSAGE_NUM = 0;
26
ConnectCallback(napi_env env,napi_callback_info info)27 napi_value ConnectCallback(napi_env env, napi_callback_info info)
28 {
29 napi_value thisVal = nullptr;
30 size_t paramsCount = 1;
31 napi_value params[1] = {nullptr};
32 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
33
34 bool connected = false;
35 NAPI_CALL(env, napi_get_value_bool(env, params[0], &connected));
36 NETSTACK_LOGI("connected: ? %{public}d", connected);
37 return NapiUtils::GetUndefined(env);
38 }
39
SendCallback(napi_env env,napi_callback_info info)40 napi_value SendCallback(napi_env env, napi_callback_info info)
41 {
42 NETSTACK_LOGI("Send OK");
43 return NapiUtils::GetUndefined(env);
44 }
45
OnOpen(napi_env env,napi_callback_info info)46 napi_value OnOpen(napi_env env, napi_callback_info info)
47 {
48 napi_value thisVal = nullptr;
49 size_t paramsCount = 1;
50 napi_value params[1] = {nullptr};
51 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
52
53 NETSTACK_LOGI("status: %{public}u,", NapiUtils::GetUint32Property(env, params[0], "status"));
54 NETSTACK_LOGI("message: %{public}s", NapiUtils::GetStringPropertyUtf8(env, params[0], "message").c_str());
55
56 napi_value send = NapiUtils::GetNamedProperty(env, WEBSOCKET_HANDLE, WebSocketModule::WebSocket::FUNCTION_SEND);
57 napi_value param[FUNCTION_PARAM_TWO] = {
58 NapiUtils::CreateStringUtf8(env, "Hello World"),
59 NapiUtils::CreateFunction(env, "SendCallback", SendCallback, nullptr),
60 };
61 NapiUtils::CallFunction(env, WEBSOCKET_HANDLE, send, FUNCTION_PARAM_TWO, param);
62 return NapiUtils::GetUndefined(env);
63 }
64
65 static constexpr const int MAX_MESSAGE_NUM = 10;
66
OnMessage(napi_env env,napi_callback_info info)67 napi_value OnMessage(napi_env env, napi_callback_info info)
68 {
69 napi_value thisVal = nullptr;
70 size_t paramsCount = 1;
71 napi_value params[1] = {nullptr};
72 NAPI_CALL(env, napi_get_cb_info(env, info, ¶msCount, params, &thisVal, nullptr));
73
74 ++MESSAGE_NUM;
75 NETSTACK_LOGI("message: %{public}s", NapiUtils::GetStringFromValueUtf8(env, params[0]).c_str());
76
77 if (MESSAGE_NUM <= MAX_MESSAGE_NUM) {
78 napi_value send = NapiUtils::GetNamedProperty(env, WEBSOCKET_HANDLE, WebSocketModule::WebSocket::FUNCTION_SEND);
79 napi_value param[FUNCTION_PARAM_TWO] = {
80 NapiUtils::CreateStringUtf8(env, "Hello World"),
81 NapiUtils::CreateFunction(env, "SendCallback", SendCallback, nullptr),
82 };
83 NapiUtils::CallFunction(env, WEBSOCKET_HANDLE, send, 1, param);
84 return NapiUtils::GetUndefined(env);
85 }
86
87 napi_value close = NapiUtils::GetNamedProperty(env, WEBSOCKET_HANDLE, WebSocketModule::WebSocket::FUNCTION_CLOSE);
88 napi_value options = NapiUtils::CreateObject(env);
89 NapiUtils::SetUint32Property(env, options, "code", CLOSE_REASON_NORMAL_CLOSE);
90 NapiUtils::SetStringPropertyUtf8(env, options, "reason", "CLOSE_NORMAL");
91 napi_value param[1] = {options};
92 NapiUtils::CallFunction(env, WEBSOCKET_HANDLE, close, 1, param);
93 return NapiUtils::GetUndefined(env);
94 }
95
96 [[maybe_unused]] HWTEST_F(NativeEngineTest, name, testing::ext::TestSize.Level0)
97 {
98 auto env = (napi_env)engine_;
99 napi_value exports = NapiUtils::CreateObject(env);
100
101 WebSocketModule::InitWebSocketModule(env, exports);
102 napi_value createWebsocket = NapiUtils::GetNamedProperty(env, exports, WebSocketModule::FUNCTION_CREATE_WEB_SOCKET);
103
104 if (WEBSOCKET_HANDLE == nullptr) {
105 WEBSOCKET_HANDLE = NapiUtils::CallFunction(env, exports, createWebsocket, 0, nullptr);
106 }
107
108 ASSERT_TRUE(NapiUtils::GetValueType(env, WEBSOCKET_HANDLE) == napi_object);
109
110 napi_value connect =
111 NapiUtils::GetNamedProperty(env, WEBSOCKET_HANDLE, WebSocketModule::WebSocket::FUNCTION_CONNECT);
112 napi_value header = NapiUtils::CreateObject(env);
113 NapiUtils::SetStringPropertyUtf8(env, header, "Name1", "Value1");
114 NapiUtils::SetStringPropertyUtf8(env, header, "Name2", "Value2");
115 napi_value obj = NapiUtils::CreateObject(env);
116 NapiUtils::SetNamedProperty(env, obj, "header", header);
117 napi_value param[FUNCTION_PARAM_THREE] = {
118 NapiUtils::CreateStringUtf8(env, "wss://x.x.x.x:443/socket"),
119 obj,
120 NapiUtils::CreateFunction(env, "ConnectCallback", ConnectCallback, nullptr),
121 };
122 NapiUtils::CallFunction(env, WEBSOCKET_HANDLE, connect, FUNCTION_PARAM_TWO, param);
123
124 napi_value on = NapiUtils::GetNamedProperty(env, WEBSOCKET_HANDLE, WebSocketModule::WebSocket::FUNCTION_ON);
125 napi_value onOpenParam[FUNCTION_PARAM_TWO] = {
126 NapiUtils::CreateStringUtf8(env, "open"),
127 NapiUtils::CreateFunction(env, "OnOpen", OnOpen, nullptr),
128 };
129 NapiUtils::CallFunction(env, WEBSOCKET_HANDLE, on, FUNCTION_PARAM_TWO, onOpenParam);
130
131 napi_value onMessageParam[FUNCTION_PARAM_TWO] = {
132 NapiUtils::CreateStringUtf8(env, "message"),
133 NapiUtils::CreateFunction(env, "OnMessage", OnMessage, nullptr),
134 };
135
136 // call on("open") more times
137 NapiUtils::CallFunction(env, WEBSOCKET_HANDLE, on, FUNCTION_PARAM_TWO, onOpenParam);
138 NapiUtils::CallFunction(env, WEBSOCKET_HANDLE, on, FUNCTION_PARAM_TWO, onMessageParam);
139 }
140 } // namespace OHOS::NetStack
141
main(int argc,char ** argv)142 int main(int argc, char **argv)
143 {
144 testing::GTEST_FLAG(output) = "xml:./";
145 testing::InitGoogleTest(&argc, argv);
146
147 JSRuntime *rt = JS_NewRuntime();
148
149 if (rt == nullptr) {
150 return 0;
151 }
152
153 JSContext *ctx = JS_NewContext(rt);
154 if (ctx == nullptr) {
155 return 0;
156 }
157
158 js_std_add_helpers(ctx, 0, nullptr);
159
160 g_nativeEngine =
161 reinterpret_cast<NativeEngine *>(new QuickJSNativeEngine(rt, ctx, nullptr)); // default instance id 0
162
163 int ret = RUN_ALL_TESTS();
164 (void)ret;
165
166 g_nativeEngine->Loop(LOOP_DEFAULT);
167
168 return 0;
169 }