Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | * binder_test.cpp - unit tests for netd binder RPCs. |
| 17 | */ |
| 18 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 19 | #include <cerrno> |
| 20 | #include <cinttypes> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 21 | #include <cstdint> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 22 | #include <cstdio> |
| 23 | #include <cstdlib> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 24 | #include <set> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 25 | #include <vector> |
| 26 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 27 | #include <fcntl.h> |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 28 | #include <ifaddrs.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 29 | #include <netdb.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 30 | #include <sys/socket.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 31 | #include <sys/types.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 32 | #include <netinet/in.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 33 | #include <linux/if.h> |
| 34 | #include <linux/if_tun.h> |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 35 | #include <openssl/base64.h> |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 36 | |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 37 | #include <android-base/macros.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 38 | #include <android-base/stringprintf.h> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 39 | #include <android-base/strings.h> |
Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame] | 40 | #include <bpf/BpfUtils.h> |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 41 | #include <cutils/multiuser.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 42 | #include <gtest/gtest.h> |
| 43 | #include <logwrap/logwrap.h> |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 44 | #include <netutils/ifc.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 45 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 46 | #include "InterfaceController.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 47 | #include "NetdConstants.h" |
Robin Lee | 7e05cc9 | 2016-09-21 16:31:33 +0900 | [diff] [blame] | 48 | #include "Stopwatch.h" |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 49 | #include "XfrmController.h" |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 50 | #include "tun_interface.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 51 | #include "android/net/INetd.h" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 52 | #include "android/net/UidRange.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 53 | #include "binder/IServiceManager.h" |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 54 | #include "netdutils/Syscalls.h" |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 55 | |
Lorenzo Colitti | 5c68b9c | 2017-08-10 18:50:10 +0900 | [diff] [blame] | 56 | #define IP_PATH "/system/bin/ip" |
| 57 | #define IP6TABLES_PATH "/system/bin/ip6tables" |
| 58 | #define IPTABLES_PATH "/system/bin/iptables" |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 59 | #define TUN_DEV "/dev/tun" |
| 60 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 61 | using namespace android; |
| 62 | using namespace android::base; |
| 63 | using namespace android::binder; |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 64 | using android::base::StartsWith; |
Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame] | 65 | using android::bpf::hasBpfSupport; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 66 | using android::net::INetd; |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 67 | using android::net::TunInterface; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 68 | using android::net::UidRange; |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 69 | using android::net::XfrmController; |
| 70 | using android::netdutils::sSyscalls; |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 71 | using android::os::PersistableBundle; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 72 | |
Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame] | 73 | #define SKIP_IF_BPF_SUPPORTED \ |
| 74 | do { \ |
| 75 | if (hasBpfSupport()) return; \ |
| 76 | } while (0); |
| 77 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 78 | static const char* IP_RULE_V4 = "-4"; |
| 79 | static const char* IP_RULE_V6 = "-6"; |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 80 | static const int TEST_NETID1 = 65501; |
| 81 | static const int TEST_NETID2 = 65502; |
| 82 | constexpr int BASE_UID = AID_USER_OFFSET * 5; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 83 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 84 | static const std::string NO_SOCKET_ALLOW_RULE("! owner UID match 0-4294967294"); |
| 85 | static const std::string ESP_ALLOW_RULE("esp"); |
| 86 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 87 | class BinderTest : public ::testing::Test { |
| 88 | |
| 89 | public: |
| 90 | BinderTest() { |
| 91 | sp<IServiceManager> sm = defaultServiceManager(); |
| 92 | sp<IBinder> binder = sm->getService(String16("netd")); |
| 93 | if (binder != nullptr) { |
| 94 | mNetd = interface_cast<INetd>(binder); |
| 95 | } |
| 96 | } |
| 97 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 98 | void SetUp() override { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 99 | ASSERT_NE(nullptr, mNetd.get()); |
| 100 | } |
| 101 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 102 | void TearDown() override { |
| 103 | mNetd->networkDestroy(TEST_NETID1); |
| 104 | mNetd->networkDestroy(TEST_NETID2); |
| 105 | } |
| 106 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 107 | bool allocateIpSecResources(bool expectOk, int32_t *spi); |
| 108 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 109 | // Static because setting up the tun interface takes about 40ms. |
| 110 | static void SetUpTestCase() { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 111 | ASSERT_EQ(0, sTun.init()); |
| 112 | ASSERT_LE(sTun.name().size(), static_cast<size_t>(IFNAMSIZ)); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void TearDownTestCase() { |
| 116 | // Closing the socket removes the interface and IP addresses. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 117 | sTun.destroy(); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void fakeRemoteSocketPair(int *clientSocket, int *serverSocket, int *acceptedSocket); |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 121 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 122 | protected: |
| 123 | sp<INetd> mNetd; |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 124 | static TunInterface sTun; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 125 | }; |
| 126 | |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 127 | TunInterface BinderTest::sTun; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 128 | |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame] | 129 | class TimedOperation : public Stopwatch { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 130 | public: |
Chih-Hung Hsieh | 1805105 | 2016-05-06 10:36:13 -0700 | [diff] [blame] | 131 | explicit TimedOperation(const std::string &name): mName(name) {} |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 132 | virtual ~TimedOperation() { |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame] | 133 | fprintf(stderr, " %s: %6.1f ms\n", mName.c_str(), timeTaken()); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | private: |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 137 | std::string mName; |
| 138 | }; |
| 139 | |
| 140 | TEST_F(BinderTest, TestIsAlive) { |
| 141 | TimedOperation t("isAlive RPC"); |
| 142 | bool isAlive = false; |
| 143 | mNetd->isAlive(&isAlive); |
| 144 | ASSERT_TRUE(isAlive); |
| 145 | } |
| 146 | |
| 147 | static int randomUid() { |
| 148 | return 100000 * arc4random_uniform(7) + 10000 + arc4random_uniform(5000); |
| 149 | } |
| 150 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 151 | static std::vector<std::string> runCommand(const std::string& command) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 152 | std::vector<std::string> lines; |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 153 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 154 | FILE *f = popen(command.c_str(), "r"); // NOLINT(cert-env33-c) |
| 155 | if (f == nullptr) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 156 | perror("popen"); |
| 157 | return lines; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 158 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 159 | |
| 160 | char *line = nullptr; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 161 | size_t bufsize = 0; |
| 162 | ssize_t linelen = 0; |
| 163 | while ((linelen = getline(&line, &bufsize, f)) >= 0) { |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 164 | lines.push_back(std::string(line, linelen)); |
| 165 | free(line); |
| 166 | line = nullptr; |
| 167 | } |
| 168 | |
| 169 | pclose(f); |
| 170 | return lines; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 171 | } |
| 172 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 173 | static std::vector<std::string> listIpRules(const char *ipVersion) { |
| 174 | std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion); |
| 175 | return runCommand(command); |
| 176 | } |
| 177 | |
| 178 | static std::vector<std::string> listIptablesRule(const char *binary, const char *chainName) { |
Lorenzo Colitti | 8054577 | 2016-06-09 14:20:08 +0900 | [diff] [blame] | 179 | std::string command = StringPrintf("%s -w -n -L %s", binary, chainName); |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 180 | return runCommand(command); |
| 181 | } |
| 182 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 183 | static int iptablesRuleLineLength(const char *binary, const char *chainName) { |
| 184 | return listIptablesRule(binary, chainName).size(); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 185 | } |
| 186 | |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 187 | static bool iptablesRuleExists(const char *binary, |
| 188 | const char *chainName, |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 189 | const std::string& expectedRule) { |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 190 | std::vector<std::string> rules = listIptablesRule(binary, chainName); |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 191 | for (const auto& rule : rules) { |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 192 | if(rule.find(expectedRule) != std::string::npos) { |
| 193 | return true; |
| 194 | } |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
| 199 | static bool iptablesNoSocketAllowRuleExists(const char *chainName){ |
| 200 | return iptablesRuleExists(IPTABLES_PATH, chainName, NO_SOCKET_ALLOW_RULE) && |
| 201 | iptablesRuleExists(IP6TABLES_PATH, chainName, NO_SOCKET_ALLOW_RULE); |
| 202 | } |
| 203 | |
| 204 | static bool iptablesEspAllowRuleExists(const char *chainName){ |
| 205 | return iptablesRuleExists(IPTABLES_PATH, chainName, ESP_ALLOW_RULE) && |
| 206 | iptablesRuleExists(IP6TABLES_PATH, chainName, ESP_ALLOW_RULE); |
| 207 | } |
| 208 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 209 | TEST_F(BinderTest, TestFirewallReplaceUidChain) { |
Chenbo Feng | 837ddfc | 2018-05-08 13:45:08 -0700 | [diff] [blame] | 210 | SKIP_IF_BPF_SUPPORTED; |
| 211 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 212 | std::string chainName = StringPrintf("netd_binder_test_%u", arc4random_uniform(10000)); |
| 213 | const int kNumUids = 500; |
| 214 | std::vector<int32_t> noUids(0); |
| 215 | std::vector<int32_t> uids(kNumUids); |
| 216 | for (int i = 0; i < kNumUids; i++) { |
| 217 | uids[i] = randomUid(); |
| 218 | } |
| 219 | |
| 220 | bool ret; |
| 221 | { |
| 222 | TimedOperation op(StringPrintf("Programming %d-UID whitelist chain", kNumUids)); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 223 | mNetd->firewallReplaceUidChain(chainName, true, uids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 224 | } |
| 225 | EXPECT_EQ(true, ret); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 226 | EXPECT_EQ((int) uids.size() + 9, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 227 | EXPECT_EQ((int) uids.size() + 15, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
| 228 | EXPECT_EQ(true, iptablesNoSocketAllowRuleExists(chainName.c_str())); |
| 229 | EXPECT_EQ(true, iptablesEspAllowRuleExists(chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 230 | { |
| 231 | TimedOperation op("Clearing whitelist chain"); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 232 | mNetd->firewallReplaceUidChain(chainName, false, noUids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 233 | } |
| 234 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 235 | EXPECT_EQ(5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 236 | EXPECT_EQ(5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 237 | |
| 238 | { |
| 239 | TimedOperation op(StringPrintf("Programming %d-UID blacklist chain", kNumUids)); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 240 | mNetd->firewallReplaceUidChain(chainName, false, uids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 241 | } |
| 242 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 243 | EXPECT_EQ((int) uids.size() + 5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 244 | EXPECT_EQ((int) uids.size() + 5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Benedict Wong | b2daefb | 2017-12-06 22:05:46 -0800 | [diff] [blame] | 245 | EXPECT_EQ(false, iptablesNoSocketAllowRuleExists(chainName.c_str())); |
| 246 | EXPECT_EQ(false, iptablesEspAllowRuleExists(chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 247 | |
| 248 | { |
| 249 | TimedOperation op("Clearing blacklist chain"); |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 250 | mNetd->firewallReplaceUidChain(chainName, false, noUids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 251 | } |
| 252 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | 50b198a | 2017-03-30 02:50:09 +0900 | [diff] [blame] | 253 | EXPECT_EQ(5, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 254 | EXPECT_EQ(5, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 255 | |
| 256 | // Check that the call fails if iptables returns an error. |
| 257 | std::string veryLongStringName = "netd_binder_test_UnacceptablyLongIptablesChainName"; |
Erik Kline | f52d452 | 2018-03-14 15:01:46 +0900 | [diff] [blame] | 258 | mNetd->firewallReplaceUidChain(veryLongStringName, true, noUids, &ret); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 259 | EXPECT_EQ(false, ret); |
| 260 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 261 | |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 262 | TEST_F(BinderTest, TestVirtualTunnelInterface) { |
George Burgess IV | c4a6d27 | 2018-05-21 14:41:57 -0700 | [diff] [blame] | 263 | const struct TestData { |
| 264 | const std::string family; |
| 265 | const std::string deviceName; |
| 266 | const std::string localAddress; |
| 267 | const std::string remoteAddress; |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 268 | int32_t iKey; |
| 269 | int32_t oKey; |
| 270 | } kTestData[] = { |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 271 | {"IPV4", "test_vti", "127.0.0.1", "8.8.8.8", 0x1234 + 53, 0x1234 + 53}, |
| 272 | {"IPV6", "test_vti6", "::1", "2001:4860:4860::8888", 0x1234 + 50, 0x1234 + 50}, |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 273 | }; |
| 274 | |
| 275 | for (unsigned int i = 0; i < arraysize(kTestData); i++) { |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 276 | const auto& td = kTestData[i]; |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 277 | |
| 278 | binder::Status status; |
| 279 | |
| 280 | // Create Virtual Tunnel Interface. |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 281 | status = mNetd->addVirtualTunnelInterface(td.deviceName, td.localAddress, td.remoteAddress, |
| 282 | td.iKey, td.oKey); |
manojboopathi | 8707f23 | 2018-01-02 14:45:47 -0800 | [diff] [blame] | 283 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
| 284 | |
| 285 | // Update Virtual Tunnel Interface. |
| 286 | status = mNetd->updateVirtualTunnelInterface(td.deviceName, td.localAddress, |
| 287 | td.remoteAddress, td.iKey, td.oKey); |
| 288 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
| 289 | |
| 290 | // Remove Virtual Tunnel Interface. |
| 291 | status = mNetd->removeVirtualTunnelInterface(td.deviceName); |
| 292 | EXPECT_TRUE(status.isOk()) << td.family << status.exceptionMessage(); |
| 293 | } |
| 294 | } |
| 295 | |
Nathan Harold | 2deff32 | 2018-05-10 14:03:48 -0700 | [diff] [blame] | 296 | // IPsec tests are not run in 32 bit mode; both 32-bit kernels and |
| 297 | // mismatched ABIs (64-bit kernel with 32-bit userspace) are unsupported. |
| 298 | #if INTPTR_MAX != INT32_MAX |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 299 | #define RETURN_FALSE_IF_NEQ(_expect_, _ret_) \ |
| 300 | do { if ((_expect_) != (_ret_)) return false; } while(false) |
| 301 | bool BinderTest::allocateIpSecResources(bool expectOk, int32_t *spi) { |
| 302 | netdutils::Status status = XfrmController::ipSecAllocateSpi(0, "::", "::1", 123, spi); |
| 303 | SCOPED_TRACE(status); |
| 304 | RETURN_FALSE_IF_NEQ(status.ok(), expectOk); |
| 305 | |
| 306 | // Add a policy |
| 307 | status = XfrmController::ipSecAddSecurityPolicy(0, 0, "::", "::1", 123, 0, 0); |
| 308 | SCOPED_TRACE(status); |
| 309 | RETURN_FALSE_IF_NEQ(status.ok(), expectOk); |
| 310 | |
| 311 | // Add an ipsec interface |
| 312 | status = netdutils::statusFromErrno( |
| 313 | XfrmController::addVirtualTunnelInterface( |
| 314 | "ipsec_test", "::", "::1", 0xF00D, 0xD00D, false), |
| 315 | "addVirtualTunnelInterface"); |
| 316 | return (status.ok() == expectOk); |
| 317 | } |
| 318 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 319 | TEST_F(BinderTest, TestXfrmControllerInit) { |
| 320 | netdutils::Status status; |
| 321 | status = XfrmController::Init(); |
| 322 | SCOPED_TRACE(status); |
Nathan Harold | 2deff32 | 2018-05-10 14:03:48 -0700 | [diff] [blame] | 323 | |
| 324 | // Older devices or devices with mismatched Kernel/User ABI cannot support the IPsec |
| 325 | // feature. |
| 326 | if (status.code() == EOPNOTSUPP) return; |
| 327 | |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 328 | ASSERT_TRUE(status.ok()); |
| 329 | |
| 330 | int32_t spi = 0; |
| 331 | |
| 332 | ASSERT_TRUE(allocateIpSecResources(true, &spi)); |
| 333 | ASSERT_TRUE(allocateIpSecResources(false, &spi)); |
| 334 | |
| 335 | status = XfrmController::Init(); |
Nathan Harold | 39ad662 | 2018-04-25 12:56:56 -0700 | [diff] [blame] | 336 | ASSERT_TRUE(status.ok()); |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 337 | ASSERT_TRUE(allocateIpSecResources(true, &spi)); |
| 338 | |
| 339 | // Clean up |
| 340 | status = XfrmController::ipSecDeleteSecurityAssociation(0, "::", "::1", 123, spi, 0); |
| 341 | SCOPED_TRACE(status); |
| 342 | ASSERT_TRUE(status.ok()); |
| 343 | |
| 344 | status = XfrmController::ipSecDeleteSecurityPolicy(0, 0, "::", "::1", 0, 0); |
| 345 | SCOPED_TRACE(status); |
| 346 | ASSERT_TRUE(status.ok()); |
| 347 | |
| 348 | // Remove Virtual Tunnel Interface. |
| 349 | status = netdutils::statusFromErrno( |
| 350 | XfrmController::removeVirtualTunnelInterface("ipsec_test"), |
| 351 | "removeVirtualTunnelInterface"); |
| 352 | |
| 353 | ASSERT_TRUE(status.ok()); |
| 354 | } |
Nathan Harold | 2deff32 | 2018-05-10 14:03:48 -0700 | [diff] [blame] | 355 | #endif |
Nathan Harold | 21299f7 | 2018-03-16 20:13:03 -0700 | [diff] [blame] | 356 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 357 | static int bandwidthDataSaverEnabled(const char *binary) { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 358 | std::vector<std::string> lines = listIptablesRule(binary, "bw_data_saver"); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 359 | |
| 360 | // Output looks like this: |
| 361 | // |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 362 | // Chain bw_data_saver (1 references) |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 363 | // target prot opt source destination |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 364 | // RETURN all -- 0.0.0.0/0 0.0.0.0/0 |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 365 | // |
| 366 | // or: |
| 367 | // |
| 368 | // Chain bw_data_saver (1 references) |
| 369 | // target prot opt source destination |
| 370 | // ... possibly connectivity critical packet rules here ... |
| 371 | // REJECT all -- ::/0 ::/0 |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 372 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 373 | EXPECT_GE(lines.size(), 3U); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 374 | |
Lorenzo Colitti | aff2879 | 2017-09-26 17:46:18 +0900 | [diff] [blame] | 375 | if (lines.size() == 3 && StartsWith(lines[2], "RETURN ")) { |
| 376 | // Data saver disabled. |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | size_t minSize = (std::string(binary) == IPTABLES_PATH) ? 3 : 9; |
| 381 | |
| 382 | if (lines.size() >= minSize && StartsWith(lines[lines.size() -1], "REJECT ")) { |
| 383 | // Data saver enabled. |
| 384 | return 1; |
| 385 | } |
| 386 | |
| 387 | return -1; |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | bool enableDataSaver(sp<INetd>& netd, bool enable) { |
| 391 | TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver"); |
| 392 | bool ret; |
| 393 | netd->bandwidthEnableDataSaver(enable, &ret); |
| 394 | return ret; |
| 395 | } |
| 396 | |
| 397 | int getDataSaverState() { |
| 398 | const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH); |
| 399 | const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH); |
| 400 | EXPECT_EQ(enabled4, enabled6); |
| 401 | EXPECT_NE(-1, enabled4); |
| 402 | EXPECT_NE(-1, enabled6); |
| 403 | if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) { |
| 404 | return -1; |
| 405 | } |
| 406 | return enabled6; |
| 407 | } |
| 408 | |
| 409 | TEST_F(BinderTest, TestBandwidthEnableDataSaver) { |
| 410 | const int wasEnabled = getDataSaverState(); |
| 411 | ASSERT_NE(-1, wasEnabled); |
| 412 | |
| 413 | if (wasEnabled) { |
| 414 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 415 | EXPECT_EQ(0, getDataSaverState()); |
| 416 | } |
| 417 | |
| 418 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 419 | EXPECT_EQ(0, getDataSaverState()); |
| 420 | |
| 421 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 422 | EXPECT_EQ(1, getDataSaverState()); |
| 423 | |
| 424 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 425 | EXPECT_EQ(1, getDataSaverState()); |
| 426 | |
| 427 | if (!wasEnabled) { |
| 428 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 429 | EXPECT_EQ(0, getDataSaverState()); |
| 430 | } |
| 431 | } |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 432 | |
| 433 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRange& range, |
| 434 | const std::string& action, const char* ipVersion) { |
| 435 | // Output looks like this: |
Robin Lee | 6c84ef6 | 2016-05-03 13:17:58 +0100 | [diff] [blame] | 436 | // "12500:\tfrom all fwmark 0x0/0x20000 iif lo uidrange 1000-2000 prohibit" |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 437 | std::vector<std::string> rules = listIpRules(ipVersion); |
| 438 | |
| 439 | std::string prefix = StringPrintf("%" PRIu32 ":", priority); |
| 440 | std::string suffix = StringPrintf(" iif lo uidrange %d-%d %s\n", |
| 441 | range.getStart(), range.getStop(), action.c_str()); |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 442 | for (const auto& line : rules) { |
Elliott Hughes | 2f44508 | 2017-12-20 12:39:35 -0800 | [diff] [blame] | 443 | if (android::base::StartsWith(line, prefix) && android::base::EndsWith(line, suffix)) { |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 444 | return true; |
| 445 | } |
| 446 | } |
| 447 | return false; |
| 448 | } |
| 449 | |
| 450 | static bool ipRuleExistsForRange(const uint32_t priority, const UidRange& range, |
| 451 | const std::string& action) { |
| 452 | bool existsIp4 = ipRuleExistsForRange(priority, range, action, IP_RULE_V4); |
| 453 | bool existsIp6 = ipRuleExistsForRange(priority, range, action, IP_RULE_V6); |
| 454 | EXPECT_EQ(existsIp4, existsIp6); |
| 455 | return existsIp4; |
| 456 | } |
| 457 | |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 458 | TEST_F(BinderTest, TestNetworkInterfaces) { |
| 459 | EXPECT_TRUE(mNetd->networkCreatePhysical(TEST_NETID1, "").isOk()); |
| 460 | EXPECT_EQ(EEXIST, mNetd->networkCreatePhysical(TEST_NETID1, "").serviceSpecificErrorCode()); |
| 461 | EXPECT_EQ(EEXIST, mNetd->networkCreateVpn(TEST_NETID1, false, true).serviceSpecificErrorCode()); |
| 462 | EXPECT_TRUE(mNetd->networkCreateVpn(TEST_NETID2, false, true).isOk()); |
| 463 | |
| 464 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 465 | EXPECT_EQ(EBUSY, |
| 466 | mNetd->networkAddInterface(TEST_NETID2, sTun.name()).serviceSpecificErrorCode()); |
| 467 | |
| 468 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 469 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID2, sTun.name()).isOk()); |
| 470 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID2).isOk()); |
| 471 | } |
| 472 | |
| 473 | TEST_F(BinderTest, TestNetworkUidRules) { |
| 474 | const uint32_t RULE_PRIORITY_SECURE_VPN = 12000; |
| 475 | |
| 476 | EXPECT_TRUE(mNetd->networkCreateVpn(TEST_NETID1, false, true).isOk()); |
| 477 | EXPECT_EQ(EEXIST, mNetd->networkCreateVpn(TEST_NETID1, false, true).serviceSpecificErrorCode()); |
| 478 | EXPECT_TRUE(mNetd->networkAddInterface(TEST_NETID1, sTun.name()).isOk()); |
| 479 | |
| 480 | std::vector<UidRange> uidRanges = { |
| 481 | {BASE_UID + 8005, BASE_UID + 8012}, |
| 482 | {BASE_UID + 8090, BASE_UID + 8099} |
| 483 | }; |
| 484 | UidRange otherRange(BASE_UID + 8190, BASE_UID + 8299); |
| 485 | std::string suffix = StringPrintf("lookup %s ", sTun.name().c_str()); |
| 486 | |
| 487 | EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk()); |
| 488 | |
| 489 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[0], suffix)); |
| 490 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, otherRange, suffix)); |
| 491 | EXPECT_TRUE(mNetd->networkRemoveUidRanges(TEST_NETID1, uidRanges).isOk()); |
| 492 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[0], suffix)); |
| 493 | |
| 494 | EXPECT_TRUE(mNetd->networkAddUidRanges(TEST_NETID1, uidRanges).isOk()); |
| 495 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[1], suffix)); |
| 496 | EXPECT_TRUE(mNetd->networkDestroy(TEST_NETID1).isOk()); |
| 497 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY_SECURE_VPN, uidRanges[1], suffix)); |
| 498 | |
| 499 | EXPECT_EQ(ENONET, mNetd->networkDestroy(TEST_NETID1).serviceSpecificErrorCode()); |
| 500 | } |
| 501 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 502 | TEST_F(BinderTest, TestNetworkRejectNonSecureVpn) { |
Robin Lee | 6c84ef6 | 2016-05-03 13:17:58 +0100 | [diff] [blame] | 503 | constexpr uint32_t RULE_PRIORITY = 12500; |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 504 | |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 505 | std::vector<UidRange> uidRanges = { |
Lorenzo Colitti | d33e96d | 2016-12-15 23:59:01 +0900 | [diff] [blame] | 506 | {BASE_UID + 150, BASE_UID + 224}, |
| 507 | {BASE_UID + 226, BASE_UID + 300} |
Robin Lee | b808736 | 2016-03-30 18:43:08 +0100 | [diff] [blame] | 508 | }; |
| 509 | |
| 510 | const std::vector<std::string> initialRulesV4 = listIpRules(IP_RULE_V4); |
| 511 | const std::vector<std::string> initialRulesV6 = listIpRules(IP_RULE_V6); |
| 512 | |
| 513 | // Create two valid rules. |
| 514 | ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(true, uidRanges).isOk()); |
| 515 | EXPECT_EQ(initialRulesV4.size() + 2, listIpRules(IP_RULE_V4).size()); |
| 516 | EXPECT_EQ(initialRulesV6.size() + 2, listIpRules(IP_RULE_V6).size()); |
| 517 | for (auto const& range : uidRanges) { |
| 518 | EXPECT_TRUE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit")); |
| 519 | } |
| 520 | |
| 521 | // Remove the rules. |
| 522 | ASSERT_TRUE(mNetd->networkRejectNonSecureVpn(false, uidRanges).isOk()); |
| 523 | EXPECT_EQ(initialRulesV4.size(), listIpRules(IP_RULE_V4).size()); |
| 524 | EXPECT_EQ(initialRulesV6.size(), listIpRules(IP_RULE_V6).size()); |
| 525 | for (auto const& range : uidRanges) { |
| 526 | EXPECT_FALSE(ipRuleExistsForRange(RULE_PRIORITY, range, "prohibit")); |
| 527 | } |
| 528 | |
| 529 | // Fail to remove the rules a second time after they are already deleted. |
| 530 | binder::Status status = mNetd->networkRejectNonSecureVpn(false, uidRanges); |
| 531 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 532 | EXPECT_EQ(ENOENT, status.serviceSpecificErrorCode()); |
| 533 | |
| 534 | // All rules should be the same as before. |
| 535 | EXPECT_EQ(initialRulesV4, listIpRules(IP_RULE_V4)); |
| 536 | EXPECT_EQ(initialRulesV6, listIpRules(IP_RULE_V6)); |
| 537 | } |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 538 | |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 539 | // Create a socket pair that isLoopbackSocket won't think is local. |
| 540 | void BinderTest::fakeRemoteSocketPair(int *clientSocket, int *serverSocket, int *acceptedSocket) { |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 541 | *serverSocket = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 542 | struct sockaddr_in6 server6 = { .sin6_family = AF_INET6, .sin6_addr = sTun.dstAddr() }; |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 543 | ASSERT_EQ(0, bind(*serverSocket, (struct sockaddr *) &server6, sizeof(server6))); |
| 544 | |
| 545 | socklen_t addrlen = sizeof(server6); |
| 546 | ASSERT_EQ(0, getsockname(*serverSocket, (struct sockaddr *) &server6, &addrlen)); |
| 547 | ASSERT_EQ(0, listen(*serverSocket, 10)); |
| 548 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 549 | *clientSocket = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0); |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 550 | struct sockaddr_in6 client6 = { .sin6_family = AF_INET6, .sin6_addr = sTun.srcAddr() }; |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 551 | ASSERT_EQ(0, bind(*clientSocket, (struct sockaddr *) &client6, sizeof(client6))); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 552 | ASSERT_EQ(0, connect(*clientSocket, (struct sockaddr *) &server6, sizeof(server6))); |
| 553 | ASSERT_EQ(0, getsockname(*clientSocket, (struct sockaddr *) &client6, &addrlen)); |
| 554 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 555 | *acceptedSocket = accept4(*serverSocket, (struct sockaddr *) &server6, &addrlen, SOCK_CLOEXEC); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 556 | ASSERT_NE(-1, *acceptedSocket); |
| 557 | |
| 558 | ASSERT_EQ(0, memcmp(&client6, &server6, sizeof(client6))); |
| 559 | } |
| 560 | |
| 561 | void checkSocketpairOpen(int clientSocket, int acceptedSocket) { |
| 562 | char buf[4096]; |
| 563 | EXPECT_EQ(4, write(clientSocket, "foo", sizeof("foo"))); |
| 564 | EXPECT_EQ(4, read(acceptedSocket, buf, sizeof(buf))); |
| 565 | EXPECT_EQ(0, memcmp(buf, "foo", sizeof("foo"))); |
| 566 | } |
| 567 | |
| 568 | void checkSocketpairClosed(int clientSocket, int acceptedSocket) { |
| 569 | // Check that the client socket was closed with ECONNABORTED. |
| 570 | int ret = write(clientSocket, "foo", sizeof("foo")); |
| 571 | int err = errno; |
| 572 | EXPECT_EQ(-1, ret); |
| 573 | EXPECT_EQ(ECONNABORTED, err); |
| 574 | |
| 575 | // Check that it sent a RST to the server. |
| 576 | ret = write(acceptedSocket, "foo", sizeof("foo")); |
| 577 | err = errno; |
| 578 | EXPECT_EQ(-1, ret); |
| 579 | EXPECT_EQ(ECONNRESET, err); |
| 580 | } |
| 581 | |
| 582 | TEST_F(BinderTest, TestSocketDestroy) { |
| 583 | int clientSocket, serverSocket, acceptedSocket; |
Lorenzo Colitti | 755faa9 | 2016-07-27 22:10:49 +0900 | [diff] [blame] | 584 | ASSERT_NO_FATAL_FAILURE(fakeRemoteSocketPair(&clientSocket, &serverSocket, &acceptedSocket)); |
Lorenzo Colitti | 563d98b | 2016-04-24 13:13:14 +0900 | [diff] [blame] | 585 | |
| 586 | // Pick a random UID in the system UID range. |
| 587 | constexpr int baseUid = AID_APP - 2000; |
| 588 | static_assert(baseUid > 0, "Not enough UIDs? Please fix this test."); |
| 589 | int uid = baseUid + 500 + arc4random_uniform(1000); |
| 590 | EXPECT_EQ(0, fchown(clientSocket, uid, -1)); |
| 591 | |
| 592 | // UID ranges that don't contain uid. |
| 593 | std::vector<UidRange> uidRanges = { |
| 594 | {baseUid + 42, baseUid + 449}, |
| 595 | {baseUid + 1536, AID_APP - 4}, |
| 596 | {baseUid + 498, uid - 1}, |
| 597 | {uid + 1, baseUid + 1520}, |
| 598 | }; |
| 599 | // A skip list that doesn't contain UID. |
| 600 | std::vector<int32_t> skipUids { baseUid + 123, baseUid + 1600 }; |
| 601 | |
| 602 | // Close sockets. Our test socket should be intact. |
| 603 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 604 | checkSocketpairOpen(clientSocket, acceptedSocket); |
| 605 | |
| 606 | // UID ranges that do contain uid. |
| 607 | uidRanges = { |
| 608 | {baseUid + 42, baseUid + 449}, |
| 609 | {baseUid + 1536, AID_APP - 4}, |
| 610 | {baseUid + 498, baseUid + 1520}, |
| 611 | }; |
| 612 | // Add uid to the skip list. |
| 613 | skipUids.push_back(uid); |
| 614 | |
| 615 | // Close sockets. Our test socket should still be intact because it's in the skip list. |
| 616 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 617 | checkSocketpairOpen(clientSocket, acceptedSocket); |
| 618 | |
| 619 | // Now remove uid from skipUids, and close sockets. Our test socket should have been closed. |
| 620 | skipUids.resize(skipUids.size() - 1); |
| 621 | EXPECT_TRUE(mNetd->socketDestroy(uidRanges, skipUids).isOk()); |
| 622 | checkSocketpairClosed(clientSocket, acceptedSocket); |
| 623 | |
| 624 | close(clientSocket); |
| 625 | close(serverSocket); |
| 626 | close(acceptedSocket); |
| 627 | } |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 628 | |
| 629 | namespace { |
| 630 | |
| 631 | int netmaskToPrefixLength(const uint8_t *buf, size_t buflen) { |
| 632 | if (buf == nullptr) return -1; |
| 633 | |
| 634 | int prefixLength = 0; |
| 635 | bool endOfContiguousBits = false; |
| 636 | for (unsigned int i = 0; i < buflen; i++) { |
| 637 | const uint8_t value = buf[i]; |
| 638 | |
| 639 | // Bad bit sequence: check for a contiguous set of bits from the high |
| 640 | // end by verifying that the inverted value + 1 is a power of 2 |
| 641 | // (power of 2 iff. (v & (v - 1)) == 0). |
| 642 | const uint8_t inverse = ~value + 1; |
| 643 | if ((inverse & (inverse - 1)) != 0) return -1; |
| 644 | |
| 645 | prefixLength += (value == 0) ? 0 : CHAR_BIT - ffs(value) + 1; |
| 646 | |
| 647 | // Bogus netmask. |
| 648 | if (endOfContiguousBits && value != 0) return -1; |
| 649 | |
| 650 | if (value != 0xff) endOfContiguousBits = true; |
| 651 | } |
| 652 | |
| 653 | return prefixLength; |
| 654 | } |
| 655 | |
| 656 | template<typename T> |
| 657 | int netmaskToPrefixLength(const T *p) { |
| 658 | return netmaskToPrefixLength(reinterpret_cast<const uint8_t*>(p), sizeof(T)); |
| 659 | } |
| 660 | |
| 661 | |
| 662 | static bool interfaceHasAddress( |
| 663 | const std::string &ifname, const char *addrString, int prefixLength) { |
| 664 | struct addrinfo *addrinfoList = nullptr; |
| 665 | ScopedAddrinfo addrinfoCleanup(addrinfoList); |
| 666 | |
| 667 | const struct addrinfo hints = { |
| 668 | .ai_flags = AI_NUMERICHOST, |
| 669 | .ai_family = AF_UNSPEC, |
| 670 | .ai_socktype = SOCK_DGRAM, |
| 671 | }; |
| 672 | if (getaddrinfo(addrString, nullptr, &hints, &addrinfoList) != 0 || |
| 673 | addrinfoList == nullptr || addrinfoList->ai_addr == nullptr) { |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | struct ifaddrs *ifaddrsList = nullptr; |
| 678 | ScopedIfaddrs ifaddrsCleanup(ifaddrsList); |
| 679 | |
| 680 | if (getifaddrs(&ifaddrsList) != 0) { |
| 681 | return false; |
| 682 | } |
| 683 | |
| 684 | for (struct ifaddrs *addr = ifaddrsList; addr != nullptr; addr = addr->ifa_next) { |
| 685 | if (std::string(addr->ifa_name) != ifname || |
| 686 | addr->ifa_addr == nullptr || |
| 687 | addr->ifa_addr->sa_family != addrinfoList->ai_addr->sa_family) { |
| 688 | continue; |
| 689 | } |
| 690 | |
| 691 | switch (addr->ifa_addr->sa_family) { |
| 692 | case AF_INET: { |
| 693 | auto *addr4 = reinterpret_cast<const struct sockaddr_in*>(addr->ifa_addr); |
| 694 | auto *want = reinterpret_cast<const struct sockaddr_in*>(addrinfoList->ai_addr); |
| 695 | if (memcmp(&addr4->sin_addr, &want->sin_addr, sizeof(want->sin_addr)) != 0) { |
| 696 | continue; |
| 697 | } |
| 698 | |
| 699 | if (prefixLength < 0) return true; // not checking prefix lengths |
| 700 | |
| 701 | if (addr->ifa_netmask == nullptr) return false; |
| 702 | auto *nm = reinterpret_cast<const struct sockaddr_in*>(addr->ifa_netmask); |
| 703 | EXPECT_EQ(prefixLength, netmaskToPrefixLength(&nm->sin_addr)); |
| 704 | return (prefixLength == netmaskToPrefixLength(&nm->sin_addr)); |
| 705 | } |
| 706 | case AF_INET6: { |
| 707 | auto *addr6 = reinterpret_cast<const struct sockaddr_in6*>(addr->ifa_addr); |
| 708 | auto *want = reinterpret_cast<const struct sockaddr_in6*>(addrinfoList->ai_addr); |
| 709 | if (memcmp(&addr6->sin6_addr, &want->sin6_addr, sizeof(want->sin6_addr)) != 0) { |
| 710 | continue; |
| 711 | } |
| 712 | |
| 713 | if (prefixLength < 0) return true; // not checking prefix lengths |
| 714 | |
| 715 | if (addr->ifa_netmask == nullptr) return false; |
| 716 | auto *nm = reinterpret_cast<const struct sockaddr_in6*>(addr->ifa_netmask); |
| 717 | EXPECT_EQ(prefixLength, netmaskToPrefixLength(&nm->sin6_addr)); |
| 718 | return (prefixLength == netmaskToPrefixLength(&nm->sin6_addr)); |
| 719 | } |
| 720 | default: |
| 721 | // Cannot happen because we have already screened for matching |
| 722 | // address families at the top of each iteration. |
| 723 | continue; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | return false; |
| 728 | } |
| 729 | |
| 730 | } // namespace |
| 731 | |
| 732 | TEST_F(BinderTest, TestInterfaceAddRemoveAddress) { |
| 733 | static const struct TestData { |
| 734 | const char *addrString; |
| 735 | const int prefixLength; |
| 736 | const bool expectSuccess; |
| 737 | } kTestData[] = { |
| 738 | { "192.0.2.1", 24, true }, |
| 739 | { "192.0.2.2", 25, true }, |
| 740 | { "192.0.2.3", 32, true }, |
| 741 | { "192.0.2.4", 33, false }, |
| 742 | { "192.not.an.ip", 24, false }, |
| 743 | { "2001:db8::1", 64, true }, |
| 744 | { "2001:db8::2", 65, true }, |
| 745 | { "2001:db8::3", 128, true }, |
| 746 | { "2001:db8::4", 129, false }, |
| 747 | { "foo:bar::bad", 64, false }, |
| 748 | }; |
| 749 | |
| 750 | for (unsigned int i = 0; i < arraysize(kTestData); i++) { |
| 751 | const auto &td = kTestData[i]; |
| 752 | |
| 753 | // [1.a] Add the address. |
| 754 | binder::Status status = mNetd->interfaceAddAddress( |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 755 | sTun.name(), td.addrString, td.prefixLength); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 756 | if (td.expectSuccess) { |
| 757 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 758 | } else { |
| 759 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 760 | ASSERT_NE(0, status.serviceSpecificErrorCode()); |
| 761 | } |
| 762 | |
| 763 | // [1.b] Verify the addition meets the expectation. |
| 764 | if (td.expectSuccess) { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 765 | EXPECT_TRUE(interfaceHasAddress(sTun.name(), td.addrString, td.prefixLength)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 766 | } else { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 767 | EXPECT_FALSE(interfaceHasAddress(sTun.name(), td.addrString, -1)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | // [2.a] Try to remove the address. If it was not previously added, removing it fails. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 771 | status = mNetd->interfaceDelAddress(sTun.name(), td.addrString, td.prefixLength); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 772 | if (td.expectSuccess) { |
| 773 | EXPECT_TRUE(status.isOk()) << status.exceptionMessage(); |
| 774 | } else { |
| 775 | ASSERT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 776 | ASSERT_NE(0, status.serviceSpecificErrorCode()); |
| 777 | } |
| 778 | |
| 779 | // [2.b] No matter what, the address should not be present. |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 780 | EXPECT_FALSE(interfaceHasAddress(sTun.name(), td.addrString, -1)); |
Erik Kline | cc4f273 | 2016-08-03 11:24:27 +0900 | [diff] [blame] | 781 | } |
| 782 | } |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 783 | |
| 784 | TEST_F(BinderTest, TestSetProcSysNet) { |
| 785 | static const struct TestData { |
| 786 | const int family; |
| 787 | const int which; |
| 788 | const char *ifname; |
| 789 | const char *parameter; |
| 790 | const char *value; |
| 791 | const int expectedReturnCode; |
| 792 | } kTestData[] = { |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 793 | { INetd::IPV4, INetd::CONF, sTun.name().c_str(), "arp_ignore", "1", 0 }, |
| 794 | { -1, INetd::CONF, sTun.name().c_str(), "arp_ignore", "1", EAFNOSUPPORT }, |
| 795 | { INetd::IPV4, -1, sTun.name().c_str(), "arp_ignore", "1", EINVAL }, |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 796 | { INetd::IPV4, INetd::CONF, "..", "conf/lo/arp_ignore", "1", EINVAL }, |
| 797 | { INetd::IPV4, INetd::CONF, ".", "lo/arp_ignore", "1", EINVAL }, |
Lorenzo Colitti | 1e299c6 | 2017-02-27 17:16:10 +0900 | [diff] [blame] | 798 | { INetd::IPV4, INetd::CONF, sTun.name().c_str(), "../all/arp_ignore", "1", EINVAL }, |
| 799 | { INetd::IPV6, INetd::NEIGH, sTun.name().c_str(), "ucast_solicit", "7", 0 }, |
Erik Kline | 55b06f8 | 2016-07-04 09:57:18 +0900 | [diff] [blame] | 800 | }; |
| 801 | |
| 802 | for (unsigned int i = 0; i < arraysize(kTestData); i++) { |
| 803 | const auto &td = kTestData[i]; |
| 804 | |
| 805 | const binder::Status status = mNetd->setProcSysNet( |
| 806 | td.family, td.which, td.ifname, td.parameter, |
| 807 | td.value); |
| 808 | |
| 809 | if (td.expectedReturnCode == 0) { |
| 810 | SCOPED_TRACE(String8::format("test case %d should have passed", i)); |
| 811 | EXPECT_EQ(0, status.exceptionCode()); |
| 812 | EXPECT_EQ(0, status.serviceSpecificErrorCode()); |
| 813 | } else { |
| 814 | SCOPED_TRACE(String8::format("test case %d should have failed", i)); |
| 815 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
| 816 | EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode()); |
| 817 | } |
| 818 | } |
| 819 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 820 | |
| 821 | static std::string base64Encode(const std::vector<uint8_t>& input) { |
| 822 | size_t out_len; |
| 823 | EXPECT_EQ(1, EVP_EncodedLength(&out_len, input.size())); |
| 824 | // out_len includes the trailing NULL. |
| 825 | uint8_t output_bytes[out_len]; |
| 826 | EXPECT_EQ(out_len - 1, EVP_EncodeBlock(output_bytes, input.data(), input.size())); |
| 827 | return std::string(reinterpret_cast<char*>(output_bytes)); |
| 828 | } |
| 829 | |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 830 | TEST_F(BinderTest, TestSetResolverConfiguration_Tls) { |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 831 | const std::vector<std::string> LOCALLY_ASSIGNED_DNS{"8.8.8.8", "2001:4860:4860::8888"}; |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 832 | std::vector<uint8_t> fp(SHA256_SIZE); |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 833 | std::vector<uint8_t> short_fp(1); |
| 834 | std::vector<uint8_t> long_fp(SHA256_SIZE + 1); |
| 835 | std::vector<std::string> test_domains; |
| 836 | std::vector<int> test_params = { 300, 25, 8, 8 }; |
| 837 | unsigned test_netid = 0; |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 838 | static const struct TestData { |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 839 | const std::vector<std::string> servers; |
| 840 | const std::string tlsName; |
| 841 | const std::vector<std::vector<uint8_t>> tlsFingerprints; |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 842 | const int expectedReturnCode; |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 843 | } kTlsTestData[] = { |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 844 | { {"192.0.2.1"}, "", {}, 0 }, |
| 845 | { {"2001:db8::2"}, "host.name", {}, 0 }, |
| 846 | { {"192.0.2.3"}, "@@@@", { fp }, 0 }, |
| 847 | { {"2001:db8::4"}, "", { fp }, 0 }, |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 848 | { {}, "", {}, 0 }, |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 849 | { {""}, "", {}, EINVAL }, |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 850 | { {"192.0.*.5"}, "", {}, EINVAL }, |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 851 | { {"2001:dg8::6"}, "", {}, EINVAL }, |
| 852 | { {"2001:db8::c"}, "", { short_fp }, EINVAL }, |
| 853 | { {"192.0.2.12"}, "", { long_fp }, EINVAL }, |
| 854 | { {"2001:db8::e"}, "", { fp, fp, fp }, 0 }, |
| 855 | { {"192.0.2.14"}, "", { fp, short_fp }, EINVAL }, |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 856 | }; |
| 857 | |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 858 | for (unsigned int i = 0; i < arraysize(kTlsTestData); i++) { |
| 859 | const auto &td = kTlsTestData[i]; |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 860 | |
| 861 | std::vector<std::string> fingerprints; |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 862 | for (const auto& fingerprint : td.tlsFingerprints) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 863 | fingerprints.push_back(base64Encode(fingerprint)); |
| 864 | } |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 865 | binder::Status status = mNetd->setResolverConfiguration( |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 866 | test_netid, LOCALLY_ASSIGNED_DNS, test_domains, test_params, |
| 867 | td.tlsName, td.servers, fingerprints); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 868 | |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 869 | if (td.expectedReturnCode == 0) { |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 870 | SCOPED_TRACE(String8::format("test case %d should have passed", i)); |
| 871 | SCOPED_TRACE(status.toString8()); |
| 872 | EXPECT_EQ(0, status.exceptionCode()); |
| 873 | } else { |
| 874 | SCOPED_TRACE(String8::format("test case %d should have failed", i)); |
| 875 | EXPECT_EQ(binder::Status::EX_SERVICE_SPECIFIC, status.exceptionCode()); |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 876 | EXPECT_EQ(td.expectedReturnCode, status.serviceSpecificErrorCode()); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 877 | } |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 878 | } |
Ben Schwartz | 4204ecf | 2017-10-02 12:35:48 -0400 | [diff] [blame] | 879 | // Ensure TLS is disabled before the start of the next test. |
| 880 | mNetd->setResolverConfiguration( |
Erik Kline | a1476fb | 2018-03-04 21:01:56 +0900 | [diff] [blame] | 881 | test_netid, kTlsTestData[0].servers, test_domains, test_params, |
| 882 | "", {}, {}); |
Ben Schwartz | e760181 | 2017-04-28 16:38:29 -0400 | [diff] [blame] | 883 | } |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 884 | |
| 885 | void expectNoTestCounterRules() { |
| 886 | for (const auto& binary : { IPTABLES_PATH, IP6TABLES_PATH }) { |
| 887 | std::string command = StringPrintf("%s -w -nvL tetherctrl_counters", binary); |
| 888 | std::string allRules = Join(runCommand(command), "\n"); |
| 889 | EXPECT_EQ(std::string::npos, allRules.find("netdtest_")); |
| 890 | } |
| 891 | } |
| 892 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 893 | void addTetherCounterValues(const char* path, const std::string& if1, const std::string& if2, |
| 894 | int byte, int pkt) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 895 | runCommand(StringPrintf("%s -w -A tetherctrl_counters -i %s -o %s -j RETURN -c %d %d", |
| 896 | path, if1.c_str(), if2.c_str(), pkt, byte)); |
| 897 | } |
| 898 | |
Bernie Innocenti | f691826 | 2018-06-11 17:37:35 +0900 | [diff] [blame] | 899 | void delTetherCounterValues(const char* path, const std::string& if1, const std::string& if2) { |
Lorenzo Colitti | 9a8a9ff | 2017-01-31 19:06:59 +0900 | [diff] [blame] | 900 | runCommand(StringPrintf("%s -w -D tetherctrl_counters -i %s -o %s -j RETURN", |
| 901 | path, if1.c_str(), if2.c_str())); |
| 902 | runCommand(StringPrintf("%s -w -D tetherctrl_counters -i %s -o %s -j RETURN", |
| 903 | path, if2.c_str(), if1.c_str())); |
| 904 | } |
| 905 | |
| 906 | TEST_F(BinderTest, TestTetherGetStats) { |
| 907 | expectNoTestCounterRules(); |
| 908 | |
| 909 | // TODO: fold this into more comprehensive tests once we have binder RPCs for enabling and |
| 910 | // disabling tethering. We don't check the return value because these commands will fail if |
| 911 | // tethering is already enabled. |
| 912 | runCommand(StringPrintf("%s -w -N tetherctrl_counters", IPTABLES_PATH)); |
| 913 | runCommand(StringPrintf("%s -w -N tetherctrl_counters", IP6TABLES_PATH)); |
| 914 | |
| 915 | std::string intIface1 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 916 | std::string intIface2 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 917 | std::string intIface3 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 918 | std::string extIface1 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 919 | std::string extIface2 = StringPrintf("netdtest_%u", arc4random_uniform(10000)); |
| 920 | |
| 921 | addTetherCounterValues(IPTABLES_PATH, intIface1, extIface1, 123, 111); |
| 922 | addTetherCounterValues(IP6TABLES_PATH, intIface1, extIface1, 456, 10); |
| 923 | addTetherCounterValues(IPTABLES_PATH, extIface1, intIface1, 321, 222); |
| 924 | addTetherCounterValues(IP6TABLES_PATH, extIface1, intIface1, 654, 20); |
| 925 | // RX is from external to internal, and TX is from internal to external. |
| 926 | // So rxBytes is 321 + 654 = 975, txBytes is 123 + 456 = 579, etc. |
| 927 | std::vector<int64_t> expected1 = { 975, 242, 579, 121 }; |
| 928 | |
| 929 | addTetherCounterValues(IPTABLES_PATH, intIface2, extIface2, 1000, 333); |
| 930 | addTetherCounterValues(IP6TABLES_PATH, intIface2, extIface2, 3000, 30); |
| 931 | |
| 932 | addTetherCounterValues(IPTABLES_PATH, extIface2, intIface2, 2000, 444); |
| 933 | addTetherCounterValues(IP6TABLES_PATH, extIface2, intIface2, 4000, 40); |
| 934 | |
| 935 | addTetherCounterValues(IP6TABLES_PATH, intIface3, extIface2, 1000, 25); |
| 936 | addTetherCounterValues(IP6TABLES_PATH, extIface2, intIface3, 2000, 35); |
| 937 | std::vector<int64_t> expected2 = { 8000, 519, 5000, 388 }; |
| 938 | |
| 939 | PersistableBundle stats; |
| 940 | binder::Status status = mNetd->tetherGetStats(&stats); |
| 941 | EXPECT_TRUE(status.isOk()) << "Getting tethering stats failed: " << status; |
| 942 | |
| 943 | std::vector<int64_t> actual1; |
| 944 | EXPECT_TRUE(stats.getLongVector(String16(extIface1.c_str()), &actual1)); |
| 945 | EXPECT_EQ(expected1, actual1); |
| 946 | |
| 947 | std::vector<int64_t> actual2; |
| 948 | EXPECT_TRUE(stats.getLongVector(String16(extIface2.c_str()), &actual2)); |
| 949 | EXPECT_EQ(expected2, actual2); |
| 950 | |
| 951 | for (const auto& path : { IPTABLES_PATH, IP6TABLES_PATH }) { |
| 952 | delTetherCounterValues(path, intIface1, extIface1); |
| 953 | delTetherCounterValues(path, intIface2, extIface2); |
| 954 | if (path == IP6TABLES_PATH) { |
| 955 | delTetherCounterValues(path, intIface3, extIface2); |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | expectNoTestCounterRules(); |
| 960 | } |