1 /*
2  * Copyright (c) 2022-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 #include <sys/socket.h>
18 #include <netinet/in.h>
19 
20 #include <poll.h>
21 #include <fcntl.h>
22 
23 #include "iservice_registry.h"
24 
25 #include "net_conn_manager_test_util.h"
26 #include "netsys_native_service_proxy.h"
27 #include "system_ability_definition.h"
28 #include "securec.h"
29 #include "dns_config_client.h"
30 #include "netnative_log_wrapper.h"
31 
32 #define private public
33 #include "dns_proxy_listen.h"
34 
35 namespace OHOS {
36 namespace NetsysNative {
37 using namespace testing::ext;
38 using namespace OHOS::nmd;
39 using namespace NetManagerStandard::NetConnManagerTestUtil;
40 constexpr uint8_t RESPONSE_FLAG = 0x80;
41 static constexpr const int32_t CLIENT_SOCKET = 99999;
42 static constexpr const uint32_t MAX_REQUESTDATA_LEN = 512;
43 class DnsProxyListenTest : public testing::Test {
44 public:
45     static void SetUpTestCase();
46     static void TearDownTestCase();
47     void SetUp();
48     void TearDown();
49     static inline std::shared_ptr<DnsProxyListen> instance_ = nullptr;
50 };
51 
SetUpTestCase()52 void DnsProxyListenTest::SetUpTestCase() {}
53 
TearDownTestCase()54 void DnsProxyListenTest::TearDownTestCase() {}
55 
SetUp()56 void DnsProxyListenTest::SetUp()
57 {
58     instance_ = std::make_shared<DnsProxyListen>();
59 }
60 
TearDown()61 void DnsProxyListenTest::TearDown() {}
62 
63 HWTEST_F(DnsProxyListenTest, DnsProxyTest001, TestSize.Level1)
64 {
65     NETNATIVE_LOGI("DnsProxyTest001 enter");
66     OHOS::sptr<OHOS::NetsysNative::INetsysService> netsysNativeService = ConnManagerGetProxy();
67     ASSERT_NE(netsysNativeService, nullptr);
68     netsysNativeService->StartDnsProxyListen();
69     const int32_t resSize = 512;
70     unsigned char rsp[resSize] = {0};
71     int proxySockFd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
72     if (proxySockFd < 0) {
73         return;
74     }
75     sockaddr_in proxyAddr;
76     socklen_t len;
77     (void)memset_s(&proxyAddr, sizeof(proxyAddr), 0, sizeof(proxyAddr));
78     proxyAddr.sin_family = AF_INET;
79     proxyAddr.sin_addr.s_addr = htonl(INADDR_ANY);
80     proxyAddr.sin_port = htons(53);
81 
82     unsigned char dnsSendData[] = {
83         "\x58\x40\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00\x03\x77\x77\x77"
84         "\x05\x62\x61\x69\x64\x75\x03\x63\x6f\x6d\x00\x00\x01\x00\x01"};
85 
86     if (sendto(proxySockFd, dnsSendData, sizeof(dnsSendData), 0, reinterpret_cast<sockaddr *>(&proxyAddr),
87                sizeof(proxyAddr)) < 0) {
88         close(proxySockFd);
89         return;
90     }
91     int flags = fcntl(proxySockFd, F_GETFL, 0);
92     uint32_t tempFlags = static_cast<uint32_t>(flags) | O_NONBLOCK;
93     fcntl(proxySockFd, F_SETFL, tempFlags);
94     struct pollfd pfd;
95     pfd.fd = proxySockFd;
96     pfd.events = POLLIN;
97     poll(&pfd, 1, 2000);
98     len = sizeof(proxyAddr);
99     recvfrom(proxySockFd, rsp, resSize, 0, reinterpret_cast<sockaddr *>(&proxyAddr), &len);
100     close(proxySockFd);
101     netsysNativeService->StopDnsProxyListen();
102     NETNATIVE_LOGI("DnsProxyTest001 end");
103 }
104 
105 HWTEST_F(DnsProxyListenTest, StartListenTest, TestSize.Level1)
106 {
107     NETNATIVE_LOGI("StartListenTest enter");
108     DnsProxyListen listener;
109     listener.OnListen();
110     listener.OffListen();
111     listener.SetParseNetId(0);
112     listener.StartListen();
113     EXPECT_EQ(listener.netId_, 0);
114 }
115 
116 HWTEST_F(DnsProxyListenTest, OffListenTest, TestSize.Level1)
117 {
118     DnsProxyListen listener;
119     listener.proxySockFd_ = CLIENT_SOCKET;
120     listener.OffListen();
121     EXPECT_EQ(listener.proxySockFd_, -1);
122 }
123 } // namespace NetsysNative
124 } // namespace OHOS
125