Skip to content

Commit

Permalink
Make TestAddressResolve_DefaultImpl.cpp work on thread devices (#30249)
Browse files Browse the repository at this point in the history
* Update AddressResolve test to not need IPv4.

EFR32 unit tests were failing occassionally and I believe
it is due to an IPv4 usage in a thread device.

Changes:

- only use IPV6 in the test
- Add logs in case IP address parse fails
- Add validation assertions that low/medium/high addresses are ordered
  as the test expects.

* Restyle
  • Loading branch information
andy31415 authored Nov 7, 2023
1 parent 5c4a9c8 commit 3cb6fcd
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions src/lib/address_resolve/tests/TestAddressResolve_DefaultImpl.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -25,33 +24,45 @@ using namespace chip::AddressResolve;

namespace {

using chip::Dnssd::IPAddressSorter::IpScore;
using chip::Dnssd::IPAddressSorter::ScoreIpAddress;

constexpr uint8_t kNumberOfAvailableSlots = CHIP_CONFIG_MDNS_RESOLVE_LOOKUP_RESULTS;

Transport::PeerAddress GetAddressWithLowScore(uint16_t port = CHIP_PORT, Inet::InterfaceId interfaceId = Inet::InterfaceId::Null())
{
// Embedded IPv4
// Unique Local - expect score "3"
Inet::IPAddress ipAddress;
Inet::IPAddress::FromString("0:0:0:0:0:0:111.22.33.44", ipAddress);

if (!Inet::IPAddress::FromString("fdff:aabb:ccdd:1::4", ipAddress))
{
ChipLogError(NotSpecified, "!!!!!!!! IP Parse failure");
}
return Transport::PeerAddress::UDP(ipAddress, port, interfaceId);
}

Transport::PeerAddress GetAddressWithMediumScore(uint16_t port = CHIP_PORT,
Inet::InterfaceId interfaceId = Inet::InterfaceId::Null())
{
// Unique Local
Inet::IPAddress ipAddress;
Inet::IPAddress::FromString("fdff:aabb:ccdd:1::4", ipAddress);

// Global Unicast - expect score '4'
Inet::IPAddress ipAddress;
if (!Inet::IPAddress::FromString("2001::aabb:ccdd:2233:4455", ipAddress))
{
ChipLogError(NotSpecified, "!!!!!!!! IP Parse failure");
}
return Transport::PeerAddress::UDP(ipAddress, port, interfaceId);
}

Transport::PeerAddress GetAddressWithHighScore(uint16_t port = CHIP_PORT, Inet::InterfaceId interfaceId = Inet::InterfaceId::Null())
{
// Global Unicast
// LinkLocal - expect score '7'
// Likely that the interfaceId is wrong (link local needs it),
// however we do not expect sorter to care
Inet::IPAddress ipAddress;
Inet::IPAddress::FromString("2001::aabb:ccdd:2233:4455", ipAddress);

if (!Inet::IPAddress::FromString("fe80::aabb:ccdd:2233:4455", ipAddress))
{
ChipLogError(NotSpecified, "!!!!!!!! IP Parse failure");
}
return Transport::PeerAddress::UDP(ipAddress, port, interfaceId);
}

Expand All @@ -66,6 +77,15 @@ void TestLookupResult(nlTestSuite * inSuite, void * inContext)
ResolveResult highResult;
highResult.address = GetAddressWithHighScore();

// Ensure test expectations regarding ordering is matched

IpScore lowScore = ScoreIpAddress(lowResult.address.GetIPAddress(), Inet::InterfaceId::Null());
IpScore mediumScore = ScoreIpAddress(mediumResult.address.GetIPAddress(), Inet::InterfaceId::Null());
IpScore highScore = ScoreIpAddress(highResult.address.GetIPAddress(), Inet::InterfaceId::Null());

NL_TEST_ASSERT(inSuite, to_underlying(lowScore) < to_underlying(mediumScore));
NL_TEST_ASSERT(inSuite, to_underlying(mediumScore) < to_underlying(highScore));

ResolveResult outResult;

AddressResolve::NodeLookupHandle handle;
Expand Down

0 comments on commit 3cb6fcd

Please sign in to comment.