From d38b7cb30fef988f778988fc180871276cbadca0 Mon Sep 17 00:00:00 2001 From: Handa Wang Date: Thu, 1 Aug 2024 07:04:45 +0000 Subject: [PATCH] [Thread] add an integration test case for NAT64 This CL adds an integration test case for NAT64. Because the feature is not fully ready, we need some extra setup steps to make the feature work. That is, specifying a CIDR for NAT64. Also, this test case is based on `ot-ctl` but eventually the test should be based on API. This CL requires https://github.com/openthread/openthread/pull/10566. Bug: 356770473 Test: atest ThreadNetworkIntegrationTests Change-Id: Idfb5ad848f7b2369591e47558ea67017c0b31e19 --- .../android/net/thread/BorderRoutingTest.java | 29 +++++++++++++++---- .../net/thread/utils/OtDaemonController.java | 23 +++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/thread/tests/integration/src/android/net/thread/BorderRoutingTest.java b/thread/tests/integration/src/android/net/thread/BorderRoutingTest.java index 48f966fd6e..9e8dc3a994 100644 --- a/thread/tests/integration/src/android/net/thread/BorderRoutingTest.java +++ b/thread/tests/integration/src/android/net/thread/BorderRoutingTest.java @@ -619,17 +619,36 @@ public class BorderRoutingTest { ICMPV6_ECHO_REQUEST_TYPE, ftdOmr, GROUP_ADDR_SCOPE_4)); } + @Test + public void nat64_threadDevicePingIpv4InfraDevice_outboundPacketIsForwarded() throws Exception { + FullThreadDevice ftd = mFtds.get(0); + joinNetworkAndWaitForOmr(ftd, DEFAULT_DATASET); + // TODO: enable NAT64 via ThreadNetworkController API instead of ot-ctl + mOtCtl.setNat64Cidr(NAT64_CIDR); + mOtCtl.setNat64Enabled(true); + waitFor(() -> mOtCtl.hasNat64PrefixInNetdata(), Duration.ofSeconds(10)); + + ftd.ping(IPV4_SERVER_ADDR); + + assertNotNull(pollForIcmpPacketOnInfraNetwork(ICMP_ECHO, null, IPV4_SERVER_ADDR)); } private void setUpInfraNetwork() throws Exception { + LinkProperties lp = new LinkProperties(); + // NAT64 feature requires the infra network to have an IPv4 default route. + lp.addRoute( + new RouteInfo( + new IpPrefix("0.0.0.0/0") /* destination */, + null /* gateway */, + null, + RouteInfo.RTN_UNICAST, + 1500 /* mtu */)); mInfraNetworkTracker = runAsShell( MANAGE_TEST_NETWORKS, - () -> - initTestNetwork( - mContext, new LinkProperties(), 5000 /* timeoutMs */)); - mController.setTestNetworkAsUpstreamAndWait( - mInfraNetworkTracker.getTestIface().getInterfaceName()); + () -> initTestNetwork(mContext, lp, 5000 /* timeoutMs */)); + String infraNetworkName = mInfraNetworkTracker.getTestIface().getInterfaceName(); + mController.setTestNetworkAsUpstreamAndWait(infraNetworkName); } private void tearDownInfraNetwork() { diff --git a/thread/tests/integration/src/android/net/thread/utils/OtDaemonController.java b/thread/tests/integration/src/android/net/thread/utils/OtDaemonController.java index b3175fd1c9..15a3f5c074 100644 --- a/thread/tests/integration/src/android/net/thread/utils/OtDaemonController.java +++ b/thread/tests/integration/src/android/net/thread/utils/OtDaemonController.java @@ -105,6 +105,29 @@ public final class OtDaemonController { return prefixes.isEmpty() ? null : prefixes.get(0); } + /** Enables/Disables NAT64 feature. */ + public void setNat64Enabled(boolean enabled) { + executeCommand("nat64 " + (enabled ? "enable" : "disable")); + } + + /** Sets the NAT64 CIDR. */ + public void setNat64Cidr(String cidr) { + executeCommand("nat64 cidr " + cidr); + } + + /** Returns whether there's a NAT64 prefix in network data */ + public boolean hasNat64PrefixInNetdata() { + // Example (in the 'Routes' section): + // fdb2:bae3:5b59:2:0:0::/96 sn low c000 + List outputLines = executeCommandAndParse("netdata show"); + for (String line : outputLines) { + if (line.contains(" sn")) { + return true; + } + } + return false; + } + public String executeCommand(String cmd) { return SystemUtil.runShellCommand(OT_CTL + " " + cmd); }