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