• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #include <gtest/gtest.h>
16 #include <gmock/gmock.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <cstddef>
20 #include <cstdint>
21 #include <sys/stat.h>
22 #include <sys/socket.h>
23 #include <linux/rtnetlink.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include "securec.h"
27 #include "dhcp_logger.h"
28 #include "dhcp_function.h"
29 #include "dhcp_ipv6_client.h"
30 #include "dhcp_ipv6_dns_repository.h"
31 
32 using ::testing::_;
33 using ::testing::AtLeast;
34 using ::testing::DoAll;
35 using ::testing::Eq;
36 using ::testing::Ref;
37 using ::testing::Return;
38 using ::testing::SetArgReferee;
39 using ::testing::StrEq;
40 using ::testing::TypedEq;
41 using ::testing::ext::TestSize;
42 DEFINE_DHCPLOG_DHCP_LABEL("DnsServerRepositoryTest");
43 using namespace OHOS::DHCP;
44 class DnsServerRepositoryTest : public testing::Test {
45 public:
SetUpTestCase()46     static void SetUpTestCase()
47     {}
TearDownTestCase()48     static void TearDownTestCase()
49     {}
SetUp()50     virtual void SetUp()
51     {
52         mRepository = new DnsServerRepository(minLifetime_);
53     }
TearDown()54     virtual void TearDown()
55     {
56         delete mRepository;
57         mRepository = nullptr;
58     }
59     DnsServerRepository* mRepository;
60     const int minLifetime_ = 1000;
61 };
62 
63 
64 HWTEST_F(DnsServerRepositoryTest, TestAddServersWithValidLifetime, TestSize.Level1) {
65     DHCP_LOGI("TestAddServersWithValidLifetime enter!");
66     std::vector<std::string> addresses = {"2001:db8::1", "2001:db8::2"};
67     bool result = mRepository->AddServers(2000, addresses);
68     EXPECT_TRUE(result);
69 }
70 
71 HWTEST_F(DnsServerRepositoryTest, TestAddServersWithZeroLifetime, TestSize.Level1) {
72     DHCP_LOGI("TestAddServersWithZeroLifetime enter!");
73     std::vector<std::string> addresses = {"2001:db8::1"};
74     mRepository->AddServers(2000, addresses);
75     bool result = mRepository->AddServers(0, addresses);
76     EXPECT_TRUE(result);
77 }
78 
79 HWTEST_F(DnsServerRepositoryTest, TestAddServersWithInvalidLifetime, TestSize.Level1) {
80     DHCP_LOGI("TestAddServersWithInvalidLifetime enter!");
81     std::vector<std::string> addresses = {"2001:db8::1"};
82     bool result = mRepository->AddServers(500, addresses);
83     EXPECT_FALSE(result);
84 }
85 
86 HWTEST_F(DnsServerRepositoryTest, TestSetCurrentServers, TestSize.Level1) {
87     DHCP_LOGI("TestSetCurrentServers enter!");
88     DhcpIpv6Info ipv6Info;
89     std::vector<std::string> addresses = {"2001:db8::1", "2001:db8::2"};
90     mRepository->currentServers_ = std::unordered_set<std::string>(addresses.begin(), addresses.end());
91     bool result = mRepository->SetCurrentServers(ipv6Info);
92     EXPECT_TRUE(result);
93 }
94 
95 HWTEST_F(DnsServerRepositoryTest, TestUpdateExistingEntry, TestSize.Level1) {
96     DHCP_LOGI("TestUpdateExistingEntry enter!");
97     std::string address = "2001:db8::1";
98     uint64_t newExpiry = 1000000;
99     mRepository->allServers_.push_back({address, 500000});
100     bool result = mRepository->UpdateExistingEntry(address, newExpiry);
101     EXPECT_TRUE(result);
102 }
103 
104 HWTEST_F(DnsServerRepositoryTest, TestUpdateCurrentServers, TestSize.Level1) {
105     DHCP_LOGI("TestUpdateCurrentServers!");
106     std::string address = "2001:db8::11";
107     uint64_t now = std::chrono::duration_cast<std::chrono::milliseconds>(
108         std::chrono::system_clock::now().time_since_epoch()
109     ).count();
110 
111     mRepository->allServers_.push_back({address, now + 500000});
112     mRepository->currentServers_.clear();
113     bool result = mRepository->UpdateCurrentServers();
114     EXPECT_TRUE(result);
115 }
116