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 | |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 19 | #include <cstdint> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 20 | #include <cstdio> |
| 21 | #include <cstdlib> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 22 | #include <vector> |
| 23 | |
| 24 | #include <android-base/stringprintf.h> |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 25 | #include <android-base/strings.h> |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 26 | #include <gtest/gtest.h> |
| 27 | #include <logwrap/logwrap.h> |
| 28 | |
| 29 | #include "NetdConstants.h" |
| 30 | #include "android/net/INetd.h" |
| 31 | #include "binder/IServiceManager.h" |
| 32 | |
| 33 | using namespace android; |
| 34 | using namespace android::base; |
| 35 | using namespace android::binder; |
| 36 | using android::net::INetd; |
| 37 | |
| 38 | class BinderTest : public ::testing::Test { |
| 39 | |
| 40 | public: |
| 41 | BinderTest() { |
| 42 | sp<IServiceManager> sm = defaultServiceManager(); |
| 43 | sp<IBinder> binder = sm->getService(String16("netd")); |
| 44 | if (binder != nullptr) { |
| 45 | mNetd = interface_cast<INetd>(binder); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | void SetUp() { |
| 50 | ASSERT_NE(nullptr, mNetd.get()); |
| 51 | } |
| 52 | |
| 53 | protected: |
| 54 | sp<INetd> mNetd; |
| 55 | }; |
| 56 | |
| 57 | |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame^] | 58 | class TimedOperation : public Stopwatch { |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 59 | public: |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame^] | 60 | TimedOperation(std::string name): mName(name) {} |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 61 | virtual ~TimedOperation() { |
Lorenzo Colitti | 699aa99 | 2016-04-15 10:22:37 +0900 | [diff] [blame^] | 62 | fprintf(stderr, " %s: %6.1f ms\n", mName.c_str(), timeTaken()); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | private: |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 66 | std::string mName; |
| 67 | }; |
| 68 | |
| 69 | TEST_F(BinderTest, TestIsAlive) { |
| 70 | TimedOperation t("isAlive RPC"); |
| 71 | bool isAlive = false; |
| 72 | mNetd->isAlive(&isAlive); |
| 73 | ASSERT_TRUE(isAlive); |
| 74 | } |
| 75 | |
| 76 | static int randomUid() { |
| 77 | return 100000 * arc4random_uniform(7) + 10000 + arc4random_uniform(5000); |
| 78 | } |
| 79 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 80 | static std::vector<std::string> listIptablesRule(const char *binary, const char *chainName) { |
| 81 | std::vector<std::string> lines; |
| 82 | FILE *f; |
| 83 | |
| 84 | std::string command = StringPrintf("%s -n -L %s", binary, chainName); |
| 85 | if ((f = popen(command.c_str(), "r")) == nullptr) { |
| 86 | perror("popen"); |
| 87 | return lines; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 88 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 89 | |
| 90 | char *line = nullptr; |
| 91 | size_t linelen = 0; |
| 92 | while (getline(&line, &linelen, f) >= 0) { |
| 93 | lines.push_back(std::string(line, linelen)); |
| 94 | free(line); |
| 95 | line = nullptr; |
| 96 | } |
| 97 | |
| 98 | pclose(f); |
| 99 | return lines; |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 100 | } |
| 101 | |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 102 | static int iptablesRuleLineLength(const char *binary, const char *chainName) { |
| 103 | return listIptablesRule(binary, chainName).size(); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
| 107 | TEST_F(BinderTest, TestFirewallReplaceUidChain) { |
| 108 | std::string chainName = StringPrintf("netd_binder_test_%u", arc4random_uniform(10000)); |
| 109 | const int kNumUids = 500; |
| 110 | std::vector<int32_t> noUids(0); |
| 111 | std::vector<int32_t> uids(kNumUids); |
| 112 | for (int i = 0; i < kNumUids; i++) { |
| 113 | uids[i] = randomUid(); |
| 114 | } |
| 115 | |
| 116 | bool ret; |
| 117 | { |
| 118 | TimedOperation op(StringPrintf("Programming %d-UID whitelist chain", kNumUids)); |
| 119 | mNetd->firewallReplaceUidChain(String16(chainName.c_str()), true, uids, &ret); |
| 120 | } |
| 121 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 122 | EXPECT_EQ((int) uids.size() + 4, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 123 | EXPECT_EQ((int) uids.size() + 4, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 124 | { |
| 125 | TimedOperation op("Clearing whitelist chain"); |
| 126 | mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, noUids, &ret); |
| 127 | } |
| 128 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 129 | EXPECT_EQ(2, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 130 | EXPECT_EQ(2, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 131 | |
| 132 | { |
| 133 | TimedOperation op(StringPrintf("Programming %d-UID blacklist chain", kNumUids)); |
| 134 | mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, uids, &ret); |
| 135 | } |
| 136 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 137 | EXPECT_EQ((int) uids.size() + 3, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 138 | EXPECT_EQ((int) uids.size() + 3, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 139 | |
| 140 | { |
| 141 | TimedOperation op("Clearing blacklist chain"); |
| 142 | mNetd->firewallReplaceUidChain(String16(chainName.c_str()), false, noUids, &ret); |
| 143 | } |
| 144 | EXPECT_EQ(true, ret); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 145 | EXPECT_EQ(2, iptablesRuleLineLength(IPTABLES_PATH, chainName.c_str())); |
| 146 | EXPECT_EQ(2, iptablesRuleLineLength(IP6TABLES_PATH, chainName.c_str())); |
Lorenzo Colitti | 89faa34 | 2016-02-26 11:38:47 +0900 | [diff] [blame] | 147 | |
| 148 | // Check that the call fails if iptables returns an error. |
| 149 | std::string veryLongStringName = "netd_binder_test_UnacceptablyLongIptablesChainName"; |
| 150 | mNetd->firewallReplaceUidChain(String16(veryLongStringName.c_str()), true, noUids, &ret); |
| 151 | EXPECT_EQ(false, ret); |
| 152 | } |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 153 | |
| 154 | static int bandwidthDataSaverEnabled(const char *binary) { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 155 | std::vector<std::string> lines = listIptablesRule(binary, "bw_data_saver"); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 156 | |
| 157 | // Output looks like this: |
| 158 | // |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 159 | // Chain bw_data_saver (1 references) |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 160 | // target prot opt source destination |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 161 | // RETURN all -- 0.0.0.0/0 0.0.0.0/0 |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 162 | EXPECT_EQ(3U, lines.size()); |
| 163 | if (lines.size() != 3) return -1; |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 164 | |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 165 | EXPECT_TRUE(android::base::StartsWith(lines[2], "RETURN ") || |
| 166 | android::base::StartsWith(lines[2], "REJECT ")); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 167 | |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 168 | return android::base::StartsWith(lines[2], "REJECT"); |
Lorenzo Colitti | dedd271 | 2016-03-22 12:36:29 +0900 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | bool enableDataSaver(sp<INetd>& netd, bool enable) { |
| 172 | TimedOperation op(enable ? " Enabling data saver" : "Disabling data saver"); |
| 173 | bool ret; |
| 174 | netd->bandwidthEnableDataSaver(enable, &ret); |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | int getDataSaverState() { |
| 179 | const int enabled4 = bandwidthDataSaverEnabled(IPTABLES_PATH); |
| 180 | const int enabled6 = bandwidthDataSaverEnabled(IP6TABLES_PATH); |
| 181 | EXPECT_EQ(enabled4, enabled6); |
| 182 | EXPECT_NE(-1, enabled4); |
| 183 | EXPECT_NE(-1, enabled6); |
| 184 | if (enabled4 != enabled6 || (enabled6 != 0 && enabled6 != 1)) { |
| 185 | return -1; |
| 186 | } |
| 187 | return enabled6; |
| 188 | } |
| 189 | |
| 190 | TEST_F(BinderTest, TestBandwidthEnableDataSaver) { |
| 191 | const int wasEnabled = getDataSaverState(); |
| 192 | ASSERT_NE(-1, wasEnabled); |
| 193 | |
| 194 | if (wasEnabled) { |
| 195 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 196 | EXPECT_EQ(0, getDataSaverState()); |
| 197 | } |
| 198 | |
| 199 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 200 | EXPECT_EQ(0, getDataSaverState()); |
| 201 | |
| 202 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 203 | EXPECT_EQ(1, getDataSaverState()); |
| 204 | |
| 205 | ASSERT_TRUE(enableDataSaver(mNetd, true)); |
| 206 | EXPECT_EQ(1, getDataSaverState()); |
| 207 | |
| 208 | if (!wasEnabled) { |
| 209 | ASSERT_TRUE(enableDataSaver(mNetd, false)); |
| 210 | EXPECT_EQ(0, getDataSaverState()); |
| 211 | } |
| 212 | } |