blob: bb9678d665ed74196dd7c2218822c4b2b515ccdd [file] [log] [blame]
Hungming Chened7b4602021-12-17 15:03:47 +08001// Copyright (C) 2022 The Android Open Source Project
2//
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#include "libclat/clatutils.h"
16
17#include <android-base/stringprintf.h>
18#include <arpa/inet.h>
19#include <gtest/gtest.h>
20
21extern "C" {
22#include "checksum.h"
23}
24
25// Default translation parameters.
26static const char kIPv4LocalAddr[] = "192.0.0.4";
27
28namespace android {
29namespace net {
30namespace clat {
31
32using base::StringPrintf;
33
34class ClatUtils : public ::testing::Test {};
35
Hungming Chen2f623f32021-12-17 17:24:58 +080036// Mock functions for isIpv4AddressFree.
37bool neverFree(in_addr_t /* addr */) {
38 return 0;
39}
40bool alwaysFree(in_addr_t /* addr */) {
41 return 1;
42}
43bool only2Free(in_addr_t addr) {
44 return (ntohl(addr) & 0xff) == 2;
45}
46bool over6Free(in_addr_t addr) {
47 return (ntohl(addr) & 0xff) >= 6;
48}
49bool only10Free(in_addr_t addr) {
50 return (ntohl(addr) & 0xff) == 10;
51}
52
53// Apply mocked isIpv4AddressFree function for selectIpv4Address test.
54in_addr_t selectIpv4Address(const in_addr ip, int16_t prefixlen,
55 isIpv4AddrFreeFn fn /* mocked function */) {
56 // Call internal function to replace isIpv4AddressFreeFn for testing.
57 return selectIpv4AddressInternal(ip, prefixlen, fn);
58}
59
60TEST_F(ClatUtils, SelectIpv4Address) {
61 struct in_addr addr;
62
63 inet_pton(AF_INET, kIPv4LocalAddr, &addr);
64
65 // If no addresses are free, return INADDR_NONE.
66 EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 29, neverFree));
67 EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 16, neverFree));
68
69 // If the configured address is free, pick that. But a prefix that's too big is invalid.
70 EXPECT_EQ(inet_addr(kIPv4LocalAddr), selectIpv4Address(addr, 29, alwaysFree));
71 EXPECT_EQ(inet_addr(kIPv4LocalAddr), selectIpv4Address(addr, 20, alwaysFree));
72 EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 15, alwaysFree));
73
74 // A prefix length of 32 works, but anything above it is invalid.
75 EXPECT_EQ(inet_addr(kIPv4LocalAddr), selectIpv4Address(addr, 32, alwaysFree));
76 EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 33, alwaysFree));
77
78 // If another address is free, pick it.
79 EXPECT_EQ(inet_addr("192.0.0.6"), selectIpv4Address(addr, 29, over6Free));
80
81 // Check that we wrap around to addresses that are lower than the first address.
82 EXPECT_EQ(inet_addr("192.0.0.2"), selectIpv4Address(addr, 29, only2Free));
83 EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 30, only2Free));
84
85 // If a free address exists outside the prefix, we don't pick it.
86 EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 29, only10Free));
87 EXPECT_EQ(inet_addr("192.0.0.10"), selectIpv4Address(addr, 24, only10Free));
88
89 // Now try using the real function which sees if IP addresses are free using bind().
90 // Assume that the machine running the test has the address 127.0.0.1, but not 8.8.8.8.
91 addr.s_addr = inet_addr("8.8.8.8");
92 EXPECT_EQ(inet_addr("8.8.8.8"), selectIpv4Address(addr, 29));
93
94 addr.s_addr = inet_addr("127.0.0.1");
95 EXPECT_EQ(inet_addr("127.0.0.2"), selectIpv4Address(addr, 29));
96}
97
Hungming Chened7b4602021-12-17 15:03:47 +080098TEST_F(ClatUtils, MakeChecksumNeutral) {
99 // We can't test generateIPv6Address here since it requires manipulating routing, which we can't
100 // do without talking to the real netd on the system.
101 uint32_t rand = arc4random_uniform(0xffffffff);
102 uint16_t rand1 = rand & 0xffff;
103 uint16_t rand2 = (rand >> 16) & 0xffff;
104 std::string v6PrefixStr = StringPrintf("2001:db8:%x:%x", rand1, rand2);
105 std::string v6InterfaceAddrStr = StringPrintf("%s::%x:%x", v6PrefixStr.c_str(), rand2, rand1);
106 std::string nat64PrefixStr = StringPrintf("2001:db8:%x:%x::", rand2, rand1);
107
108 in_addr v4 = {inet_addr(kIPv4LocalAddr)};
109 in6_addr v6InterfaceAddr;
110 ASSERT_TRUE(inet_pton(AF_INET6, v6InterfaceAddrStr.c_str(), &v6InterfaceAddr));
111 in6_addr nat64Prefix;
112 ASSERT_TRUE(inet_pton(AF_INET6, nat64PrefixStr.c_str(), &nat64Prefix));
113
114 // Generate a boatload of random IIDs.
115 int onebits = 0;
116 uint64_t prev_iid = 0;
117 for (int i = 0; i < 100000; i++) {
118 in6_addr v6 = v6InterfaceAddr;
119 makeChecksumNeutral(&v6, v4, nat64Prefix);
120
121 // Check the generated IP address is in the same prefix as the interface IPv6 address.
122 EXPECT_EQ(0, memcmp(&v6, &v6InterfaceAddr, 8));
123
124 // Check that consecutive IIDs are not the same.
125 uint64_t iid = *(uint64_t*)(&v6.s6_addr[8]);
126 ASSERT_TRUE(iid != prev_iid)
127 << "Two consecutive random IIDs are the same: " << std::showbase << std::hex << iid
128 << "\n";
129 prev_iid = iid;
130
131 // Check that the IID is checksum-neutral with the NAT64 prefix and the
132 // local prefix.
133 uint16_t c1 = ip_checksum_finish(ip_checksum_add(0, &v4, sizeof(v4)));
134 uint16_t c2 = ip_checksum_finish(ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) +
135 ip_checksum_add(0, &v6, sizeof(v6)));
136
137 if (c1 != c2) {
138 char v6Str[INET6_ADDRSTRLEN];
139 inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str));
140 FAIL() << "Bad IID: " << v6Str << " not checksum-neutral with " << kIPv4LocalAddr
141 << " and " << nat64PrefixStr.c_str() << std::showbase << std::hex
142 << "\n IPv4 checksum: " << c1 << "\n IPv6 checksum: " << c2 << "\n";
143 }
144
145 // Check that IIDs are roughly random and use all the bits by counting the
146 // total number of bits set to 1 in a random sample of 100000 generated IIDs.
147 onebits += __builtin_popcountll(*(uint64_t*)&iid);
148 }
149 EXPECT_LE(3190000, onebits);
150 EXPECT_GE(3210000, onebits);
151}
152
Hungming Chen86a56de2021-12-17 19:40:59 +0800153TEST_F(ClatUtils, DetectMtu) {
154 // ::1 with bottom 32 bits set to 1 is still ::1 which routes via lo with mtu of 64KiB
155 ASSERT_EQ(detect_mtu(&in6addr_loopback, htonl(1), 0 /*MARK_UNSET*/), 65536);
156}
157
Hungming Chened7b4602021-12-17 15:03:47 +0800158} // namespace clat
159} // namespace net
160} // namespace android