• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 <gtest/gtest.h>
17 #include <unistd.h>
18 #include <sys/socket.h>
19 
20 #define private public
21 
22 #include "dns_proxy_request_socket.h"
23 #include "netnative_log_wrapper.h"
24 
25 namespace OHOS::nmd {
26 
27 using namespace testing;
28 using namespace testing::ext;
29 
30 class DnsProxyRequestSocketTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp();
35     void TearDown();
36 };
37 
SetUpTestCase()38 void DnsProxyRequestSocketTest::SetUpTestCase() {}
TearDownTestCase()39 void DnsProxyRequestSocketTest::TearDownTestCase() {}
SetUp()40 void DnsProxyRequestSocketTest::SetUp() {}
TearDown()41 void DnsProxyRequestSocketTest::TearDown() {}
42 
43 HWTEST_F(DnsProxyRequestSocketTest, Create_01, TestSize.Level0)
44 {
45     int32_t sock = socket(10000, SOCK_CLOEXEC, 0);
46     std::unique_ptr<AlignedSockAddr> clientSock = std::make_unique<AlignedSockAddr>();
47     std::unique_ptr<RecvBuff> recvBuff = std::make_unique<RecvBuff>();
48     DnsProxyRequestSocket dnsProxyRequestSocket(sock, std::move(clientSock), std::move(recvBuff));
49     EXPECT_EQ(dnsProxyRequestSocket.sock, sock);
50     EXPECT_EQ(dnsProxyRequestSocket.event.data.fd, sock);
51     EXPECT_EQ(dnsProxyRequestSocket.event.events, EPOLLIN);
52     EXPECT_NE(dnsProxyRequestSocket.clientSock, nullptr);
53     EXPECT_NE(dnsProxyRequestSocket.recvBuff, nullptr);
54 }
55 
56 HWTEST_F(DnsProxyRequestSocketTest, Release_01, TestSize.Level0)
57 {
58     int32_t sock = -1;
59     std::unique_ptr<AlignedSockAddr> clientSock = nullptr;
60     std::unique_ptr<RecvBuff> recvBuff = nullptr;
61     DnsProxyRequestSocket dnsProxyRequestSocket(sock, std::move(clientSock), std::move(recvBuff));
62     dnsProxyRequestSocket.~DnsProxyRequestSocket();
63     EXPECT_EQ(dnsProxyRequestSocket.sock, sock);
64 }
65 
66 HWTEST_F(DnsProxyRequestSocketTest, Release_02, TestSize.Level0)
67 {
68     int32_t sock = socket(10000, SOCK_CLOEXEC, 0);
69     std::unique_ptr<AlignedSockAddr> clientSock = std::make_unique<AlignedSockAddr>();
70     std::unique_ptr<RecvBuff> recvBuff = std::make_unique<RecvBuff>();
71     DnsProxyRequestSocket dnsProxyRequestSocket(sock, std::move(clientSock), std::move(recvBuff));
72     dnsProxyRequestSocket.~DnsProxyRequestSocket();
73     EXPECT_EQ(dnsProxyRequestSocket.sock, sock);
74 }
75 
76 }  // namespace OHOS::nmd
77