1 /*
2 * Copyright (c) 2021-2023 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 <gtest/gtest.h>
17
18 #include "net_conn_constants.h"
19 #include "net_conn_types.h"
20 #include "net_handle.h"
21 #include "net_mgr_log_wrapper.h"
22
23 namespace OHOS {
24 namespace NetManagerStandard {
25 using namespace testing::ext;
26 class NetHandleTest : public testing::Test {
27 public:
28 static void SetUpTestCase();
29 static void TearDownTestCase();
30 void SetUp();
31 void TearDown();
32 };
33
SetUpTestCase()34 void NetHandleTest::SetUpTestCase() {}
35
TearDownTestCase()36 void NetHandleTest::TearDownTestCase() {}
37
SetUp()38 void NetHandleTest::SetUp() {}
39
TearDown()40 void NetHandleTest::TearDown() {}
41
42 HWTEST_F(NetHandleTest, BindSocket001, TestSize.Level1)
43 {
44 int32_t socketFd = 1;
45 int32_t netId = 101;
46 auto handler = DelayedSingleton<NetHandle>::GetInstance();
47 handler->SetNetId(netId);
48 int32_t result = handler->BindSocket(socketFd);
49 ASSERT_TRUE(result == NETMANAGER_SUCCESS);
50 }
51
52 HWTEST_F(NetHandleTest, BindSocket002, TestSize.Level1)
53 {
54 int32_t socketFd = -1;
55 int32_t netId = 101;
56 auto handler = DelayedSingleton<NetHandle>::GetInstance();
57 handler->SetNetId(netId);
58 int32_t result = handler->BindSocket(socketFd);
59 ASSERT_TRUE(result == NETMANAGER_ERR_PARAMETER_ERROR);
60 }
61
62 HWTEST_F(NetHandleTest, GetAddressesByName001, TestSize.Level1)
63 {
64 std::string host = "www.baidu.com";
65 std::vector<INetAddr> addrList;
66 int32_t netId = 5;
67 auto handler = DelayedSingleton<NetHandle>::GetInstance();
68 handler->SetNetId(netId);
69 int32_t ret = handler->GetAddressesByName(host, addrList);
70 EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
71 }
72
73 HWTEST_F(NetHandleTest, GetAddressesByName002, TestSize.Level1)
74 {
75 std::string host;
76 std::vector<INetAddr> addrList;
77 int32_t netId = 5;
78 auto handler = DelayedSingleton<NetHandle>::GetInstance();
79 handler->SetNetId(netId);
80 int32_t ret = handler->GetAddressesByName(host, addrList);
81 EXPECT_EQ(ret, NETMANAGER_ERR_PARAMETER_ERROR);
82 }
83
84 HWTEST_F(NetHandleTest, GetAddressByName001, TestSize.Level1)
85 {
86 std::string host = "www.baidu.com";
87 INetAddr addr;
88 int32_t netId = 5;
89 auto handler = DelayedSingleton<NetHandle>::GetInstance();
90 handler->SetNetId(netId);
91 int32_t ret = handler->GetAddressByName(host, addr);
92 EXPECT_EQ(ret, NETMANAGER_ERR_PERMISSION_DENIED);
93 }
94
95 HWTEST_F(NetHandleTest, GetAddressByName002, TestSize.Level1)
96 {
97 std::string host;
98 INetAddr addr;
99 int32_t netId = 5;
100 auto handler = DelayedSingleton<NetHandle>::GetInstance();
101 handler->SetNetId(netId);
102 int32_t ret = handler->GetAddressByName(host, addr);
103 EXPECT_EQ(ret, NETMANAGER_ERR_PARAMETER_ERROR);
104 }
105 } // namespace NetManagerStandard
106 } // namespace OHOS
107