Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +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 | * BandwidthControllerTest.cpp - unit tests for BandwidthController.cpp |
| 17 | */ |
| 18 | |
| 19 | #include <string> |
| 20 | #include <vector> |
| 21 | #include <stdio.h> |
| 22 | |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | #include "BandwidthController.h" |
| 26 | |
| 27 | std::vector<std::string> gCmds = {}; |
| 28 | |
| 29 | extern "C" int fake_android_fork_exec(int argc, char* argv[], int *status, bool, bool) { |
| 30 | std::string cmd = argv[0]; |
| 31 | for (int i = 1; i < argc; i++) { |
| 32 | cmd += " "; |
| 33 | cmd += argv[i]; |
| 34 | } |
| 35 | gCmds.push_back(cmd); |
| 36 | *status = 0; |
| 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | FILE *fake_popen(const char *, const char *) { |
| 41 | return NULL; |
| 42 | }; |
| 43 | |
| 44 | void expectIptablesCommands(std::vector<std::string> expectedCmds) { |
| 45 | EXPECT_EQ(expectedCmds.size() * 2, gCmds.size()); |
| 46 | if (expectedCmds.size() * 2 != gCmds.size()) return; |
| 47 | |
| 48 | for (size_t i = 0; i < expectedCmds.size(); i ++) { |
| 49 | EXPECT_EQ("/system/bin/iptables -w " + expectedCmds[i], gCmds[2 * i]); |
| 50 | EXPECT_EQ("/system/bin/ip6tables -w " + expectedCmds[i], gCmds[2 * i + 1]); |
| 51 | } |
| 52 | |
| 53 | gCmds.clear(); |
| 54 | } |
| 55 | |
| 56 | class BandwidthControllerTest : public ::testing::Test { |
| 57 | public: |
| 58 | BandwidthControllerTest() { |
| 59 | BandwidthController::execFunction = fake_android_fork_exec; |
| 60 | BandwidthController::popenFunction = fake_popen; |
| 61 | gCmds.clear(); |
| 62 | } |
| 63 | BandwidthController mBw; |
| 64 | }; |
| 65 | |
| 66 | |
| 67 | TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) { |
| 68 | mBw.enableBandwidthControl(false); |
| 69 | std::vector<std::string> expected = { |
| 70 | "-F bw_INPUT", |
| 71 | "-F bw_OUTPUT", |
| 72 | "-F bw_FORWARD", |
| 73 | "-F bw_happy_box", |
| 74 | "-F bw_penalty_box", |
| 75 | "-F bw_costly_shared", |
| 76 | "-t raw -F bw_raw_PREROUTING", |
| 77 | "-t mangle -F bw_mangle_POSTROUTING", |
| 78 | "-A bw_INPUT -m owner --socket-exists", |
| 79 | "-A bw_OUTPUT -m owner --socket-exists", |
| 80 | "-t raw -A bw_raw_PREROUTING -m owner --socket-exists", |
| 81 | "-t mangle -A bw_mangle_POSTROUTING -m owner --socket-exists", |
| 82 | "-A bw_costly_shared --jump bw_penalty_box", |
| 83 | "-A bw_costly_shared --jump bw_happy_box", |
| 84 | "-A bw_costly_shared --jump RETURN", |
| 85 | "-A bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN", |
| 86 | }; |
| 87 | expectIptablesCommands(expected); |
| 88 | } |
| 89 | |
| 90 | TEST_F(BandwidthControllerTest, TestEnableDataSaver) { |
| 91 | mBw.enableDataSaver(true); |
| 92 | std::vector<std::string> expected = { |
| 93 | "-R bw_costly_shared 3 --jump REJECT", |
| 94 | }; |
| 95 | expectIptablesCommands(expected); |
| 96 | |
| 97 | mBw.enableDataSaver(false); |
| 98 | expected = { |
| 99 | "-R bw_costly_shared 3 --jump RETURN", |
| 100 | }; |
| 101 | expectIptablesCommands(expected); |
| 102 | } |