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> |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 24 | #include <android-base/strings.h> |
| 25 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 26 | #include "BandwidthController.h" |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 27 | #include "IptablesBaseTest.h" |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 28 | |
| 29 | FILE *fake_popen(const char *, const char *) { |
| 30 | return NULL; |
| 31 | }; |
| 32 | |
Lorenzo Colitti | 0f15055 | 2016-03-28 02:30:27 +0900 | [diff] [blame] | 33 | class BandwidthControllerTest : public IptablesBaseTest { |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 34 | public: |
| 35 | BandwidthControllerTest() { |
| 36 | BandwidthController::execFunction = fake_android_fork_exec; |
| 37 | BandwidthController::popenFunction = fake_popen; |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 38 | BandwidthController::iptablesRestoreFunction = fakeExecIptablesRestore; |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 39 | } |
| 40 | BandwidthController mBw; |
| 41 | }; |
| 42 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 43 | TEST_F(BandwidthControllerTest, TestSetupIptablesHooks) { |
| 44 | mBw.setupIptablesHooks(); |
| 45 | std::vector<std::string> expected = { |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 46 | "*filter\n" |
| 47 | ":bw_INPUT -\n" |
| 48 | ":bw_OUTPUT -\n" |
| 49 | ":bw_FORWARD -\n" |
| 50 | ":bw_happy_box -\n" |
| 51 | ":bw_penalty_box -\n" |
| 52 | ":bw_data_saver -\n" |
| 53 | ":bw_costly_shared -\n" |
| 54 | "COMMIT\n" |
| 55 | "*raw\n" |
| 56 | ":bw_raw_PREROUTING -\n" |
| 57 | "COMMIT\n" |
| 58 | "*mangle\n" |
| 59 | ":bw_mangle_POSTROUTING -\n" |
| 60 | "COMMIT\n\x04" |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 61 | }; |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 62 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 63 | } |
| 64 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 65 | TEST_F(BandwidthControllerTest, TestEnableBandwidthControl) { |
| 66 | mBw.enableBandwidthControl(false); |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 67 | std::string expectedFlush = |
| 68 | "*filter\n" |
| 69 | ":bw_INPUT -\n" |
| 70 | ":bw_OUTPUT -\n" |
| 71 | ":bw_FORWARD -\n" |
| 72 | ":bw_happy_box -\n" |
| 73 | ":bw_penalty_box -\n" |
| 74 | ":bw_data_saver -\n" |
| 75 | ":bw_costly_shared -\n" |
| 76 | "COMMIT\n" |
| 77 | "*raw\n" |
| 78 | ":bw_raw_PREROUTING -\n" |
| 79 | "COMMIT\n" |
| 80 | "*mangle\n" |
| 81 | ":bw_mangle_POSTROUTING -\n" |
| 82 | "COMMIT\n\x04"; |
| 83 | std::string expectedAccounting = |
| 84 | "*filter\n" |
| 85 | "-A bw_INPUT -m owner --socket-exists\n" |
| 86 | "-A bw_OUTPUT -m owner --socket-exists\n" |
| 87 | "-A bw_costly_shared --jump bw_penalty_box\n" |
| 88 | "-A bw_penalty_box --jump bw_happy_box\n" |
| 89 | "-A bw_happy_box --jump bw_data_saver\n" |
| 90 | "-A bw_data_saver -j RETURN\n" |
| 91 | "-I bw_happy_box -m owner --uid-owner 0-9999 --jump RETURN\n" |
| 92 | "COMMIT\n" |
| 93 | "*raw\n" |
| 94 | "-A bw_raw_PREROUTING -m owner --socket-exists\n" |
| 95 | "COMMIT\n" |
| 96 | "*mangle\n" |
| 97 | "-A bw_mangle_POSTROUTING -m owner --socket-exists\n" |
| 98 | "COMMIT\n\x04"; |
| 99 | |
| 100 | expectIptablesRestoreCommands({ expectedFlush, expectedAccounting }); |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 101 | } |
| 102 | |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 103 | TEST_F(BandwidthControllerTest, TestDisableBandwidthControl) { |
| 104 | mBw.disableBandwidthControl(); |
Lorenzo Colitti | 13debb8 | 2016-03-27 17:46:30 +0900 | [diff] [blame] | 105 | const std::string expected = |
| 106 | "*filter\n" |
| 107 | ":bw_INPUT -\n" |
| 108 | ":bw_OUTPUT -\n" |
| 109 | ":bw_FORWARD -\n" |
| 110 | ":bw_happy_box -\n" |
| 111 | ":bw_penalty_box -\n" |
| 112 | ":bw_data_saver -\n" |
| 113 | ":bw_costly_shared -\n" |
| 114 | "COMMIT\n" |
| 115 | "*raw\n" |
| 116 | ":bw_raw_PREROUTING -\n" |
| 117 | "COMMIT\n" |
| 118 | "*mangle\n" |
| 119 | ":bw_mangle_POSTROUTING -\n" |
| 120 | "COMMIT\n\x04"; |
| 121 | expectIptablesRestoreCommands({ expected }); |
Lorenzo Colitti | a0dc8a5 | 2016-03-26 22:42:07 +0900 | [diff] [blame] | 122 | } |
| 123 | |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 124 | TEST_F(BandwidthControllerTest, TestEnableDataSaver) { |
| 125 | mBw.enableDataSaver(true); |
| 126 | std::vector<std::string> expected = { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 127 | "-R bw_data_saver 1 --jump REJECT", |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 128 | }; |
| 129 | expectIptablesCommands(expected); |
| 130 | |
| 131 | mBw.enableDataSaver(false); |
| 132 | expected = { |
Lorenzo Colitti | 464eabe | 2016-03-25 13:38:19 +0900 | [diff] [blame] | 133 | "-R bw_data_saver 1 --jump RETURN", |
Lorenzo Colitti | 86a4798 | 2016-03-18 17:52:25 +0900 | [diff] [blame] | 134 | }; |
| 135 | expectIptablesCommands(expected); |
| 136 | } |