• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2024 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 "netstack_log.h"
17 #include "gtest/gtest.h"
18 #include <cstring>
19 #include <iostream>
20 
21 #include "close_context.h"
22 #include "connect_context.h"
23 #include "send_context.h"
24 #include "websocket_async_work.h"
25 #include "websocket_exec.h"
26 #include "websocket_module.h"
27 
28 class WebSocketTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase() {}
31 
TearDownTestCase()32     static void TearDownTestCase() {}
33 
SetUp()34     virtual void SetUp() {}
35 
TearDown()36     virtual void TearDown() {}
37 };
38 
39 namespace {
40 using namespace testing::ext;
41 using namespace OHOS::NetStack;
42 using namespace OHOS::NetStack::Websocket;
43 
44 HWTEST_F(WebSocketTest, WebSocketTest001, TestSize.Level1)
45 {
46     napi_env env = nullptr;
47     auto eventManager = std::make_shared<EventManager>();
48     ConnectContext context(env, eventManager);
49     bool ret = WebSocketExec::ExecConnect(&context);
50     EXPECT_EQ(ret, false);
51 }
52 
53 HWTEST_F(WebSocketTest, WebSocketTest002, TestSize.Level1)
54 {
55     napi_env env = nullptr;
56     auto eventManager = std::make_shared<EventManager>();
57     SendContext context(env, eventManager);
58     bool ret = WebSocketExec::ExecSend(&context);
59     EXPECT_EQ(ret, false);
60 }
61 
62 HWTEST_F(WebSocketTest, WebSocketTest003, TestSize.Level1)
63 {
64     napi_env env = nullptr;
65     auto eventManager = std::make_shared<EventManager>();
66     CloseContext context(env, eventManager);
67     bool ret = WebSocketExec::ExecClose(&context);
68     EXPECT_EQ(ret, false);
69 }
70 
71 HWTEST_F(WebSocketTest, WebSocketTest004, TestSize.Level1)
72 {
73     bool ret = WebSocketExec::ExecConnect(nullptr);
74     EXPECT_EQ(ret, false);
75 }
76 
77 HWTEST_F(WebSocketTest, WebSocketTest005, TestSize.Level1)
78 {
79     napi_env env = nullptr;
80     auto eventManager = std::make_shared<EventManager>();
81     ConnectContext context(env, eventManager);
82     context.caPath_ = "/etc/ssl/certs/test_ca.crt";
83     bool ret = WebSocketExec::ExecConnect(&context);
84     EXPECT_EQ(ret, false);
85 }
86 
87 HWTEST_F(WebSocketTest, WebSocketTest006, TestSize.Level1)
88 {
89     napi_env env = nullptr;
90     auto eventManager = std::make_shared<EventManager>();
91     ConnectContext context(env, eventManager);
92     context.caPath_ = "";
93     bool ret = WebSocketExec::ExecConnect(&context);
94     EXPECT_EQ(ret, false);
95 }
96 
97 HWTEST_F(WebSocketTest, WebSocketTest007, TestSize.Level1)
98 {
99     napi_env env = nullptr;
100     auto eventManager = std::make_shared<EventManager>();
101     ConnectContext context(env, eventManager);
102 
103     context.url = "ws://123.123.123.123:9000";
104     std::string myProtocol = "my-protocol";
105     context.SetProtocol(myProtocol);
106     std::string getMyProtocol = context.GetProtocol();
107     bool ret = WebSocketExec::ExecConnect(&context);
108     EXPECT_EQ(getMyProtocol, "my-protocol");
109     EXPECT_NE(ret, true);
110 }
111 
112 HWTEST_F(WebSocketTest, WebSocketTest008, TestSize.Level1)
113 {
114     napi_env env = nullptr;
115     auto eventManager = std::make_shared<EventManager>();
116     ConnectContext context(env, eventManager);
117 
118     context.url = "ws://123.123.123.123:9000";
119     context.SetWebsocketProxyType(WebsocketProxyType::USE_SPECIFIED);
120     std::string host = "192.168.147.60";
121     int32_t port = 8888;
122     std::string exclusions = "www.httpbin.org";
123     context.SetSpecifiedWebsocketProxy(host, port, exclusions);
124     std::string getHost;
125     uint32_t getPort;
126     std::string getExclusions;
127     context.GetSpecifiedWebsocketProxy(getHost, getPort, getExclusions);
128     bool ret = WebSocketExec::ExecConnect(&context);
129     EXPECT_EQ(getHost, "192.168.147.60");
130     EXPECT_EQ(getPort, 8888);
131     EXPECT_EQ(getExclusions, "www.httpbin.org");
132     EXPECT_NE(ret, true);
133 }
134 } // namespace