Lorenzo Colitti | 7ef8c0f | 2019-01-11 22:34:58 +0900 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * ClatdControllerTest.cpp - unit tests for ClatdController.cpp |
| 17 | */ |
| 18 | |
| 19 | #include <arpa/inet.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include <string> |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #include <android-base/stringprintf.h> |
| 26 | #include <android-base/strings.h> |
| 27 | #include <netutils/ifc.h> |
| 28 | |
| 29 | extern "C" { |
| 30 | #include <netutils/checksum.h> |
| 31 | } |
| 32 | |
| 33 | #include "ClatdController.h" |
| 34 | #include "tun_interface.h" |
| 35 | |
| 36 | static const char kIPv4LocalAddr[] = "192.0.0.4"; |
| 37 | |
| 38 | namespace android { |
| 39 | namespace net { |
| 40 | |
| 41 | using android::base::StringPrintf; |
| 42 | |
| 43 | // Mock functions for isIpv4AddressFree. |
| 44 | bool neverFree(in_addr_t /* addr */) { |
| 45 | return 0; |
| 46 | } |
| 47 | bool alwaysFree(in_addr_t /* addr */) { |
| 48 | return 1; |
| 49 | } |
| 50 | bool only2Free(in_addr_t addr) { |
| 51 | return (ntohl(addr) & 0xff) == 2; |
| 52 | } |
| 53 | bool over6Free(in_addr_t addr) { |
| 54 | return (ntohl(addr) & 0xff) >= 6; |
| 55 | } |
| 56 | bool only10Free(in_addr_t addr) { |
| 57 | return (ntohl(addr) & 0xff) == 10; |
| 58 | } |
| 59 | |
| 60 | class ClatdControllerTest : public ::testing::Test { |
| 61 | public: |
| 62 | void SetUp() { resetIpv4AddressFreeFunc(); } |
| 63 | |
| 64 | protected: |
| 65 | void setIpv4AddressFreeFunc(bool (*func)(in_addr_t)) { |
| 66 | ClatdController::isIpv4AddressFreeFunc = func; |
| 67 | } |
| 68 | void resetIpv4AddressFreeFunc() { |
| 69 | ClatdController::isIpv4AddressFreeFunc = ClatdController::isIpv4AddressFree; |
| 70 | } |
| 71 | in_addr_t selectIpv4Address(const in_addr a, int16_t b) { |
| 72 | return ClatdController::selectIpv4Address(a, b); |
| 73 | } |
| 74 | void makeChecksumNeutral(in6_addr* a, const in_addr b, const in6_addr& c) { |
| 75 | ClatdController::makeChecksumNeutral(a, b, c); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | TEST_F(ClatdControllerTest, SelectIpv4Address) { |
| 80 | struct in_addr addr; |
| 81 | |
| 82 | inet_pton(AF_INET, kIPv4LocalAddr, &addr); |
| 83 | |
| 84 | // If no addresses are free, return INADDR_NONE. |
| 85 | setIpv4AddressFreeFunc(neverFree); |
| 86 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 29)); |
| 87 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 16)); |
| 88 | |
| 89 | // If the configured address is free, pick that. But a prefix that's too big is invalid. |
| 90 | setIpv4AddressFreeFunc(alwaysFree); |
| 91 | EXPECT_EQ(inet_addr(kIPv4LocalAddr), selectIpv4Address(addr, 29)); |
| 92 | EXPECT_EQ(inet_addr(kIPv4LocalAddr), selectIpv4Address(addr, 20)); |
| 93 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 15)); |
| 94 | |
| 95 | // A prefix length of 32 works, but anything above it is invalid. |
| 96 | EXPECT_EQ(inet_addr(kIPv4LocalAddr), selectIpv4Address(addr, 32)); |
| 97 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 33)); |
| 98 | |
| 99 | // If another address is free, pick it. |
| 100 | setIpv4AddressFreeFunc(over6Free); |
| 101 | EXPECT_EQ(inet_addr("192.0.0.6"), selectIpv4Address(addr, 29)); |
| 102 | |
| 103 | // Check that we wrap around to addresses that are lower than the first address. |
| 104 | setIpv4AddressFreeFunc(only2Free); |
| 105 | EXPECT_EQ(inet_addr("192.0.0.2"), selectIpv4Address(addr, 29)); |
| 106 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 30)); |
| 107 | |
| 108 | // If a free address exists outside the prefix, we don't pick it. |
| 109 | setIpv4AddressFreeFunc(only10Free); |
| 110 | EXPECT_EQ(INADDR_NONE, selectIpv4Address(addr, 29)); |
| 111 | EXPECT_EQ(inet_addr("192.0.0.10"), selectIpv4Address(addr, 24)); |
| 112 | |
| 113 | // Now try using the real function which sees if IP addresses are free using bind(). |
| 114 | // Assume that the machine running the test has the address 127.0.0.1, but not 8.8.8.8. |
| 115 | resetIpv4AddressFreeFunc(); |
| 116 | addr.s_addr = inet_addr("8.8.8.8"); |
| 117 | EXPECT_EQ(inet_addr("8.8.8.8"), selectIpv4Address(addr, 29)); |
| 118 | |
| 119 | addr.s_addr = inet_addr("127.0.0.1"); |
| 120 | EXPECT_EQ(inet_addr("127.0.0.2"), selectIpv4Address(addr, 29)); |
| 121 | } |
| 122 | |
| 123 | TEST_F(ClatdControllerTest, MakeChecksumNeutral) { |
| 124 | // We can't test generateIPv6Address here since it requires manipulating routing, which we can't |
| 125 | // do without talking to the real netd on the system. |
| 126 | uint32_t rand = arc4random_uniform(0xffffffff); |
| 127 | uint16_t rand1 = rand & 0xffff; |
| 128 | uint16_t rand2 = (rand >> 16) & 0xffff; |
| 129 | std::string v6PrefixStr = StringPrintf("2001:db8:%x:%x", rand1, rand2); |
| 130 | std::string v6InterfaceAddrStr = StringPrintf("%s::%x:%x", v6PrefixStr.c_str(), rand2, rand1); |
| 131 | std::string nat64PrefixStr = StringPrintf("2001:db8:%x:%x::", rand2, rand1); |
| 132 | |
| 133 | in_addr v4 = {inet_addr(kIPv4LocalAddr)}; |
| 134 | in6_addr v6InterfaceAddr; |
| 135 | ASSERT_TRUE(inet_pton(AF_INET6, v6InterfaceAddrStr.c_str(), &v6InterfaceAddr)); |
| 136 | in6_addr nat64Prefix; |
| 137 | ASSERT_TRUE(inet_pton(AF_INET6, nat64PrefixStr.c_str(), &nat64Prefix)); |
| 138 | |
| 139 | // Generate a boatload of random IIDs. |
| 140 | int onebits = 0; |
| 141 | uint64_t prev_iid = 0; |
| 142 | for (int i = 0; i < 100000; i++) { |
| 143 | in6_addr v6 = v6InterfaceAddr; |
| 144 | makeChecksumNeutral(&v6, v4, nat64Prefix); |
| 145 | |
| 146 | // Check the generated IP address is in the same prefix as the interface IPv6 address. |
| 147 | EXPECT_EQ(0, memcmp(&v6, &v6InterfaceAddr, 8)); |
| 148 | |
| 149 | // Check that consecutive IIDs are not the same. |
| 150 | uint64_t iid = *(uint64_t*)(&v6.s6_addr[8]); |
| 151 | ASSERT_TRUE(iid != prev_iid) |
| 152 | << "Two consecutive random IIDs are the same: " << std::showbase << std::hex << iid |
| 153 | << "\n"; |
| 154 | prev_iid = iid; |
| 155 | |
| 156 | // Check that the IID is checksum-neutral with the NAT64 prefix and the |
| 157 | // local prefix. |
| 158 | uint16_t c1 = ip_checksum_finish(ip_checksum_add(0, &v4, sizeof(v4))); |
| 159 | uint16_t c2 = ip_checksum_finish(ip_checksum_add(0, &nat64Prefix, sizeof(nat64Prefix)) + |
| 160 | ip_checksum_add(0, &v6, sizeof(v6))); |
| 161 | |
| 162 | if (c1 != c2) { |
| 163 | char v6Str[INET6_ADDRSTRLEN]; |
| 164 | inet_ntop(AF_INET6, &v6, v6Str, sizeof(v6Str)); |
| 165 | FAIL() << "Bad IID: " << v6Str << " not checksum-neutral with " << kIPv4LocalAddr |
| 166 | << " and " << nat64PrefixStr.c_str() << std::showbase << std::hex |
| 167 | << "\n IPv4 checksum: " << c1 << "\n IPv6 checksum: " << c2 << "\n"; |
| 168 | } |
| 169 | |
| 170 | // Check that IIDs are roughly random and use all the bits by counting the |
| 171 | // total number of bits set to 1 in a random sample of 100000 generated IIDs. |
| 172 | onebits += __builtin_popcountll(*(uint64_t*)&iid); |
| 173 | } |
| 174 | EXPECT_LE(3190000, onebits); |
| 175 | EXPECT_GE(3210000, onebits); |
| 176 | } |
| 177 | |
| 178 | } // namespace net |
| 179 | } // namespace android |