Fix various issues in the virtionet tests (#40963)

* Fix various issues in the virtionet tests * Guard empty addresses

Blue committed Jul 2, 2026 at 10:08 UTC 012065b1aff77387f9a1cb24556caf03613ce0b7
4 files changed +20 -52
test/windows/Common.cpp
+6 -43
@@ -17,6 +17,7 @@ Abstract:
17 #include "precomp.h"
18 #include "Common.h"
19 #include "LxssDynamicFunction.h"
20 +#include "WslCoreNetworkEndpointSettings.h"
21 #include <tlhelp32.h>
22 #include <werapi.h>
23 #include <Dbghelp.h>
@@ -3041,53 +3042,15 @@ void ExpectHttpResponse(LPCWSTR Url, std::optional<int> expectedCode, bool retry
3042 }
3043 }
3044
3044 -std::optional<std::string> GetHostAdapterIpv4()
3045 +std::optional<std::wstring> GetHostAdapterIpv4()
3046 {
3046 - ULONG bufferSize = 0;
3047 - constexpr ULONG flags = GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER;
3048 - auto result = GetAdaptersAddresses(AF_INET, flags, nullptr, nullptr, &bufferSize);
3049 - if (result != ERROR_BUFFER_OVERFLOW)
3047 + auto endpoint = wsl::core::networking::GetHostEndpointSettings();
3048 + if (!endpoint || endpoint->PreferredIpAddress.AddressString.empty())
3049 {
3051 - return std::nullopt;
3052 - }
3053 -
3054 - std::vector<BYTE> buffer(bufferSize);
3055 - auto* adapters = reinterpret_cast<PIP_ADAPTER_ADDRESSES>(buffer.data());
3056 - result = GetAdaptersAddresses(AF_INET, flags, nullptr, adapters, &bufferSize);
3057 - if (result != ERROR_SUCCESS)
3058 - {
3059 - return std::nullopt;
3060 - }
3061 -
3062 - for (auto* adapter = adapters; adapter != nullptr; adapter = adapter->Next)
3063 - {
3064 - if (adapter->OperStatus != IfOperStatusUp || adapter->IfType == IF_TYPE_SOFTWARE_LOOPBACK || adapter->IfType == IF_TYPE_TUNNEL)
3065 - {
3066 - continue;
3067 - }
3068 -
3069 - for (auto* addr = adapter->FirstUnicastAddress; addr != nullptr; addr = addr->Next)
3070 - {
3071 - if (addr->Address.lpSockaddr->sa_family != AF_INET)
3072 - {
3073 - continue;
3074 - }
3075 -
3076 - auto& ipv4 = reinterpret_cast<sockaddr_in*>(addr->Address.lpSockaddr)->sin_addr;
3077 -
3078 - // Skip APIPA (169.254.x.x) addresses.
3079 - if ((ntohl(ipv4.s_addr) & 0xFFFF0000) == 0xA9FE0000)
3080 - {
3081 - continue;
3082 - }
3083 -
3084 - char buf[INET_ADDRSTRLEN];
3085 - VERIFY_IS_NOT_NULL(inet_ntop(AF_INET, &ipv4, buf, sizeof(buf)));
3086 - return std::string(buf);
3087 - }
3050 + return {};
3051 }
3052
3090 - return std::nullopt;
3053 + return endpoint->PreferredIpAddress.AddressString;
3054 }
3055
3056 void SetPathAccess(const std::filesystem::path& path, DWORD Permissions, ACCESS_MODE Mode)
test/windows/Common.h
+1 -1
@@ -655,7 +655,7 @@ void LoadTestImage(IWSLCSession& session, std::string_view imageName);
655
656 void ExpectHttpResponse(LPCWSTR Url, std::optional<int> expectedCode, bool retry = false);
657
658 -std::optional<std::string> GetHostAdapterIpv4();
658 +std::optional<std::wstring> GetHostAdapterIpv4();
659
660 template <typename T>
661 void VerifyAreEqualUnordered(const std::vector<T>& expected, const std::vector<T>& actual, const std::source_location& source = std::source_location::current())
test/windows/WSLCTests.cpp
+12 -7
@@ -8131,6 +8131,11 @@ class WSLCTests
8131 auto [restore, session] = SetupPortMappingsTest(WSLCNetworkingModeConsomme);
8132
8133 auto hostIp = GetHostAdapterIpv4();
8134 + std::optional<std::string> hostIpNarrow;
8135 + if (hostIp.has_value())
8136 + {
8137 + hostIpNarrow = wsl::shared::string::WideToMultiByte(hostIp.value());
8138 + }
8139
8140 struct PortMapping
8141 {
@@ -8142,7 +8147,7 @@ class WSLCTests
8147 };
8148
8149 auto runCustomBindingTests = [&](const std::string& containerNetworkType) {
8145 - LogInfo("Container network type: %s", containerNetworkType.c_str());
8150 + LogInfo("Container network type: %hs", containerNetworkType.c_str());
8151
8152 auto createTcpContainer = [&](const std::vector<PortMapping>& ports) {
8153 static int containerIndex = 0;
@@ -8207,7 +8212,7 @@ class WSLCTests
8212 // Verify reachable via host adapter IP to confirm wildcard semantics.
8213 if (hostIp.has_value())
8214 {
8210 - auto url = std::format(L"http://{}:1261", wsl::shared::string::MultiByteToWide(hostIp.value()));
8215 + auto url = std::format(L"http://{}:1261", hostIp.value());
8216 ExpectHttpResponse(url.c_str(), 200);
8217 }
8218 else
@@ -8220,10 +8225,10 @@ class WSLCTests
8225 {
8226 if (hostIp.has_value())
8227 {
8223 - auto container = createTcpContainer({{1262, 8000, AF_INET, IPPROTO_TCP, hostIp.value()}});
8224 - validateInspectPortBinding(container, 8000, IPPROTO_TCP, hostIp.value(), 1262);
8228 + auto container = createTcpContainer({{1262, 8000, AF_INET, IPPROTO_TCP, hostIpNarrow.value()}});
8229 + validateInspectPortBinding(container, 8000, IPPROTO_TCP, hostIpNarrow.value(), 1262);
8230
8226 - auto url = std::format(L"http://{}:1262", wsl::shared::string::MultiByteToWide(hostIp.value()));
8231 + auto url = std::format(L"http://{}:1262", hostIp.value());
8232 ExpectHttpResponse(url.c_str(), 200);
8233 }
8234 else
@@ -8244,8 +8249,8 @@ class WSLCTests
8249 {
8250 if (hostIp.has_value())
8251 {
8247 - auto container = createTcpContainer({{WSLC_EPHEMERAL_PORT, 8000, AF_INET, IPPROTO_TCP, hostIp.value()}});
8248 - auto hostPort = validateInspectPortBinding(container, 8000, IPPROTO_TCP, hostIp.value(), std::nullopt);
8252 + auto container = createTcpContainer({{WSLC_EPHEMERAL_PORT, 8000, AF_INET, IPPROTO_TCP, hostIpNarrow.value()}});
8253 + auto hostPort = validateInspectPortBinding(container, 8000, IPPROTO_TCP, hostIpNarrow.value(), std::nullopt);
8254
8255 ExpectHttpResponse(std::format(L"http://{}:{}", hostIp.value(), hostPort).c_str(), 200);
8256 }
test/windows/wslc/e2e/WSLCE2EContainerRunTests.cpp
+1 -1
@@ -633,7 +633,7 @@ class WSLCE2EContainerRunTests
633 auto portBindings = inspectContainer.Ports[portKey];
634 VERIFY_ARE_EQUAL(1u, portBindings.size());
635 VERIFY_ARE_EQUAL(std::to_string(HostTestPort1), portBindings[0].HostPort);
636 - VERIFY_ARE_EQUAL(*hostIp, portBindings[0].HostIp);
636 + VERIFY_ARE_EQUAL(wsl::shared::string::WideToMultiByte(*hostIp), portBindings[0].HostIp);
637 }
638
639 WSLC_TEST_METHOD(WSLCE2E_Container_Run_Port_TCP)