diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp index ca3f3283dc1be369401761e86bde73fb04e7d9ae..29b9387655ad383ffaadd5737db993d6219f96bf 100755 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.cpp @@ -3306,13 +3306,15 @@ void StaStateMachine::DhcpResultNotify::TryToSaveIpV4Result(IpInfo &ipInfo, IpV6 /* If not phone hotspot, set .isDataRestricted = 0. */ std::string strVendor = result->strOptVendor; std::string ipAddress = result->strOptClientId; + int maskLength = IpTools::GetMaskLength(result->strOptSubnet); pStaStateMachine->linkedInfo.isDataRestricted = (strVendor.find("ANDROID_METERED") == std::string::npos && strVendor.find("OPEN_HARMONY") == std::string::npos) ? 0 : 1; if (!pStaStateMachine->linkedInfo.isDataRestricted) { pStaStateMachine->linkedInfo.isDataRestricted = (strVendor.find("hostname:") != std::string::npos && - ipAddress.find("172.20.10.") != std::string::npos); + ipAddress.find("172.20.10.") != std::string::npos && + maskLength >= HOTSPOT_SUBNETMASK_MIN_LENGTH); } pStaStateMachine->linkedInfo.platformType = strVendor; WIFI_LOGI("WifiLinkedInfo.isDataRestricted = %{public}d, WifiLinkedInfo.platformType = %{public}s", diff --git a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.h b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.h index 7166ec7b00071f8482ad3efc442bdcd58fd5365b..192d9c1af97c2d6e255b4134fb1b0514702ca4b3 100644 --- a/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.h +++ b/wifi/services/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine.h @@ -79,6 +79,8 @@ constexpr int PIN_CODE_LEN = 8; /* DHCP timeout interval */ constexpr int DHCP_TIME = 15; +/* Subnet mask length threshold for mobile hotspot detection */ +constexpr int HOTSPOT_SUBNETMASK_MIN_LENGTH = 24; /* rssi thresholds */ constexpr int INVALID_RSSI_VALUE = -127; constexpr int MAX_RSSI_VALUE = 200; diff --git a/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine_test.cpp b/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine_test.cpp index 91726555dbee8fbb9fb73753a4993e968fb797fc..e1baf8280721e5d1299a9e909034fb94614f2d9b 100644 --- a/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine_test.cpp +++ b/wifi/test/wifi_standard/wifi_framework/wifi_manage/wifi_sta/sta_state_machine_test.cpp @@ -435,6 +435,146 @@ public: pStaStateMachine->pSeparatedState->GoOutState(); } + void TryToSaveIpV4ResultHostnameMatchTest() + { + IpInfo ipInfo; + IpV6Info ipv6Info; + DhcpResult result; + + // 测试所有条件满足的情况:包含hostname、IP匹配172.20.10.、掩码>=24 + memset_s(&result, sizeof(result), 0, sizeof(result)); + result.iptype = 0; // IPv4 + + if (snprintf_s(result.strOptVendor, sizeof(result.strOptVendor), sizeof(result.strOptVendor) - 1, + "%s", "hostname:TestHost") < 0) { + return; + } + + if (snprintf_s(result.strOptClientId, sizeof(result.strOptClientId), sizeof(result.strOptClientId) - 1, + "%s", "172.20.10.100") < 0) { + return; + } + + if (snprintf_s(result.strOptSubnet, sizeof(result.strOptSubnet), sizeof(result.strOptSubnet) - 1, + "%s", "255.255.255.0") < 0) { + return; + } + + if (snprintf_s(result.strOptRouter1, sizeof(result.strOptRouter1), sizeof(result.strOptRouter1) - 1, + "%s", "172.20.10.1") < 0) { + return; + } + + pStaStateMachine->linkedInfo.isDataRestricted = 0; + pStaStateMachine->pDhcpResultNotify->TryToSaveIpV4Result(ipInfo, ipv6Info, &result); + EXPECT_EQ(pStaStateMachine->linkedInfo.isDataRestricted, 1); + } + + void TryToSaveIpV4ResultHostnameNoMatchTest() + { + IpInfo ipInfo; + IpV6Info ipv6Info; + DhcpResult result; + + // 测试不包含hostname的情况 + memset_s(&result, sizeof(result), 0, sizeof(result)); + result.iptype = 0; // IPv4 + + if (snprintf_s(result.strOptVendor, sizeof(result.strOptVendor), sizeof(result.strOptVendor) - 1, + "%s", "test vendor string") < 0) { + return; + } + + if (snprintf_s(result.strOptClientId, sizeof(result.strOptClientId), sizeof(result.strOptClientId) - 1, + "%s", "172.20.10.100") < 0) { + return; + } + + if (snprintf_s(result.strOptSubnet, sizeof(result.strOptSubnet), sizeof(result.strOptSubnet) - 1, + "%s", "255.255.255.0") < 0) { + return; + } + + if (snprintf_s(result.strOptRouter1, sizeof(result.strOptRouter1), sizeof(result.strOptRouter1) - 1, + "%s", "172.20.10.1") < 0) { + return; + } + + pStaStateMachine->linkedInfo.isDataRestricted = 0; + pStaStateMachine->pDhcpResultNotify->TryToSaveIpV4Result(ipInfo, ipv6Info, &result); + EXPECT_EQ(pStaStateMachine->linkedInfo.isDataRestricted, 0); + } + + void TryToSaveIpV4ResultIpNoMatchTest() + { + IpInfo ipInfo; + IpV6Info ipv6Info; + DhcpResult result; + + // 测试IP不匹配172.20.10.的情况 + memset_s(&result, sizeof(result), 0, sizeof(result)); + result.iptype = 0; // IPv4 + + if (snprintf_s(result.strOptVendor, sizeof(result.strOptVendor), sizeof(result.strOptVendor) - 1, + "%s", "hostname:TestHost") < 0) { + return; + } + + if (snprintf_s(result.strOptClientId, sizeof(result.strOptClientId), sizeof(result.strOptClientId) - 1, + "%s", "192.168.1.100") < 0) { + return; + } + + if (snprintf_s(result.strOptSubnet, sizeof(result.strOptSubnet), sizeof(result.strOptSubnet) - 1, + "%s", "255.255.255.0") < 0) { + return; + } + + if (snprintf_s(result.strOptRouter1, sizeof(result.strOptRouter1), sizeof(result.strOptRouter1) - 1, + "%s", "192.168.1.1") < 0) { + return; + } + + pStaStateMachine->linkedInfo.isDataRestricted = 0; + pStaStateMachine->pDhcpResultNotify->TryToSaveIpV4Result(ipInfo, ipv6Info, &result); + EXPECT_EQ(pStaStateMachine->linkedInfo.isDataRestricted, 0); + } + + void TryToSaveIpV4ResultMaskNoMatchTest() + { + IpInfo ipInfo; + IpV6Info ipv6Info; + DhcpResult result; + + // 测试掩码长度<24的情况 + memset_s(&result, sizeof(result), 0, sizeof(result)); + result.iptype = 0; // IPv4 + + if (snprintf_s(result.strOptVendor, sizeof(result.strOptVendor), sizeof(result.strOptVendor) - 1, + "%s", "hostname:TestHost") < 0) { + return; + } + + if (snprintf_s(result.strOptClientId, sizeof(result.strOptClientId), sizeof(result.strOptClientId) - 1, + "%s", "172.20.10.100") < 0) { + return; + } + + if (snprintf_s(result.strOptSubnet, sizeof(result.strOptSubnet), sizeof(result.strOptSubnet) - 1, + "%s", "255.255.254.0") < 0) { + return; + } + + if (snprintf_s(result.strOptRouter1, sizeof(result.strOptRouter1), sizeof(result.strOptRouter1) - 1, + "%s", "172.20.10.1") < 0) { + return; + } + + pStaStateMachine->linkedInfo.isDataRestricted = 0; + pStaStateMachine->pDhcpResultNotify->TryToSaveIpV4Result(ipInfo, ipv6Info, &result); + EXPECT_EQ(pStaStateMachine->linkedInfo.isDataRestricted, 0); + } + void SeparatedStateExeMsgSuccess1() { InternalMessagePtr msg = std::make_shared(); @@ -2779,5 +2919,29 @@ HWTEST_F(StaStateMachineTest, DhcpResultNotifyClear2Test, TestSize.Level1) pStaStateMachine->pDhcpResultNotify->ClearDhcpResult(&pStaStateMachine->pDhcpResultNotify->DhcpIpv4Result); EXPECT_TRUE(pStaStateMachine->pDhcpResultNotify->DhcpIpv4Result.isOptSuc == 0); } + +HWTEST_F(StaStateMachineTest, TryToSaveIpV4ResultHostnameMatchTest, TestSize.Level1) +{ + TryToSaveIpV4ResultHostnameMatchTest(); + EXPECT_FALSE(g_errLog.find("service is null") != std::string::npos); +} + +HWTEST_F(StaStateMachineTest, TryToSaveIpV4ResultHostnameNoMatchTest, TestSize.Level1) +{ + TryToSaveIpV4ResultHostnameNoMatchTest(); + EXPECT_FALSE(g_errLog.find("service is null") != std::string::npos); +} + +HWTEST_F(StaStateMachineTest, TryToSaveIpV4ResultIpNoMatchTest, TestSize.Level1) +{ + TryToSaveIpV4ResultIpNoMatchTest(); + EXPECT_FALSE(g_errLog.find("service is null") != std::string::npos); +} + +HWTEST_F(StaStateMachineTest, TryToSaveIpV4ResultMaskNoMatchTest, TestSize.Level1) +{ + TryToSaveIpV4ResultMaskNoMatchTest(); + EXPECT_FALSE(g_errLog.find("service is null") != std::string::npos); +} } // namespace Wifi } // namespace OHOS \ No newline at end of file