1 /* 2 * Copyright (C) 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 "dhcp_result.h" 19 #include "dhcp_logger.h" 20 21 DEFINE_DHCPLOG_DHCP_LABEL("DhcpResultTest"); 22 23 using namespace testing::ext; 24 using namespace OHOS::Wifi; 25 namespace OHOS { 26 namespace Wifi { 27 constexpr int ARRAY_SIZE = 1030; 28 class DhcpResultTest : 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 40 HWTEST_F(DhcpResultTest, SplitStrTest, TestSize.Level1) 41 { 42 DHCP_LOGE("enter SplitStrTest"); 43 std::string src; 44 std::string delim; 45 int count = 0; 46 std::vector<std::string> splits {}; 47 EXPECT_EQ(false, SplitStr(src, delim, count, splits)); 48 src = ""; 49 delim = "test"; 50 EXPECT_EQ(false, SplitStr(src, delim, count, splits)); 51 src = "testcode"; 52 delim = ""; 53 EXPECT_EQ(false, SplitStr(src, delim, count, splits)); 54 src = "testcode"; 55 delim = "test"; 56 count = 1; 57 EXPECT_EQ(true, SplitStr(src, delim, count, splits)); 58 } 59 60 HWTEST_F(DhcpResultTest, GetDhcpEventIpv4ResultTest, TestSize.Level1) 61 { 62 DHCP_LOGE("enter GetDhcpEventIpv4ResultTest"); 63 std::vector<std::string> splits; 64 splits.push_back("wlan0"); 65 splits.push_back("12"); 66 splits.push_back("*"); 67 EXPECT_EQ(DHCP_OPT_FAILED, GetDhcpEventIpv4Result(-1, splits)); 68 splits.push_back("wlan4"); 69 splits.push_back("wlan5"); 70 splits.push_back("wlan6"); 71 splits.push_back("wlan7"); 72 splits.push_back("wlan8"); 73 splits.push_back("wlan9"); 74 splits.push_back("wlan10"); 75 splits.push_back("wlan11"); 76 EXPECT_EQ(DHCP_OPT_FAILED, GetDhcpEventIpv4Result(-1, splits)); 77 splits[2] = "wlan3"; 78 EXPECT_EQ(DHCP_OPT_SUCCESS, GetDhcpEventIpv4Result(0, splits)); 79 splits[0] = ""; 80 EXPECT_EQ(DHCP_OPT_FAILED, GetDhcpEventIpv4Result(0, splits)); 81 splits[0] = "wlan0"; 82 splits[1] = ""; 83 EXPECT_EQ(DHCP_OPT_FAILED, GetDhcpEventIpv4Result(0, splits)); 84 } 85 86 HWTEST_F(DhcpResultTest, DhcpEventResultHandleTest, TestSize.Level1) 87 { 88 DHCP_LOGE("enter DhcpEventResultHandleTest"); 89 std::string data; 90 int code = 0; 91 EXPECT_EQ(DHCP_OPT_FAILED, DhcpEventResultHandle(code, data)); 92 data = "ipv4:ipv4,1640995200,*,*,*,*,*,*,*,*,*"; 93 EXPECT_EQ(DHCP_OPT_FAILED, DhcpEventResultHandle(code, data)); 94 data = "ipv6:ipv6,1640995200,*,*,*,*,*,*,*,*,*"; 95 EXPECT_EQ(DHCP_OPT_SUCCESS, DhcpEventResultHandle(code, data)); 96 data = "wlan0:wlan0,1640995200,*,*,*,*,*,*,*,*,*"; 97 EXPECT_EQ(DHCP_OPT_FAILED, DhcpEventResultHandle(code, data)); 98 } 99 100 HWTEST_F(DhcpResultTest, PublishDhcpIpv4ResultEventTest1, TestSize.Level1) 101 { 102 DHCP_LOGI("PublishDhcpIpv4ResultEventTest1 enter!"); 103 char data[] = "ipv6:ipv6,1640995200,*,*,*,*,*,*,*,*,*"; 104 char ifname[] = "testcode"; 105 bool result = PublishDhcpIpv4ResultEvent(1, data, ifname); 106 DHCP_LOGE("PublishDhcpIpv4ResultEventTest1 result(%{public}d)", result); 107 EXPECT_EQ(true, result); 108 } 109 110 HWTEST_F(DhcpResultTest, PublishDhcpIpv4ResultEventTest2, TestSize.Level1) 111 { 112 DHCP_LOGI("PublishDhcpIpv4ResultEventTest2 enter!"); 113 char data[] = "wlan0:wlan0,1640995200,*,*,*,*,*,*,*,*,*"; 114 char ifname[ARRAY_SIZE] = {1}; 115 bool result = PublishDhcpIpv4ResultEvent(1, data, ifname); 116 DHCP_LOGE("PublishDhcpIpv4ResultEventTest2 result(%{public}d)", result); 117 EXPECT_EQ(false, result); 118 } 119 } 120 } 121