Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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 | * IdletimerControllerTest.cpp - unit tests for IdletimerController.cpp |
| 17 | */ |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
| 21 | #include <android-base/strings.h> |
| 22 | #include <android-base/stringprintf.h> |
| 23 | |
| 24 | #include "IdletimerController.h" |
| 25 | #include "IptablesBaseTest.h" |
| 26 | |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 27 | using android::base::Join; |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 28 | using android::base::StringPrintf; |
| 29 | |
| 30 | class IdletimerControllerTest : public IptablesBaseTest { |
| 31 | protected: |
| 32 | IdletimerControllerTest() { |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 33 | IdletimerController::execIptablesRestore = fakeExecIptablesRestore; |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 34 | } |
| 35 | IdletimerController mIt; |
| 36 | }; |
| 37 | |
| 38 | TEST_F(IdletimerControllerTest, TestSetupIptablesHooks) { |
| 39 | mIt.setupIptablesHooks(); |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 40 | expectIptablesRestoreCommands(ExpectedIptablesCommands{}); |
| 41 | } |
| 42 | |
| 43 | TEST_F(IdletimerControllerTest, TestEnableDisable) { |
| 44 | std::vector<std::string> expected = { |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 45 | "*raw\n" |
| 46 | ":idletimer_raw_PREROUTING -\n" |
| 47 | "COMMIT\n" |
| 48 | "*mangle\n" |
| 49 | ":idletimer_mangle_POSTROUTING -\n" |
| 50 | "COMMIT\n", |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | mIt.enableIdletimerControl(); |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 54 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 55 | |
| 56 | mIt.enableIdletimerControl(); |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 57 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 58 | |
| 59 | mIt.disableIdletimerControl(); |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 60 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 61 | |
| 62 | mIt.disableIdletimerControl(); |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 63 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | const std::vector<std::string> makeAddRemoveCommands(bool add) { |
| 67 | const char *op = add ? "-A" : "-D"; |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 68 | std::vector<std::string> cmds = { |
| 69 | "*raw", |
| 70 | StringPrintf("%s idletimer_raw_PREROUTING -i wlan0 -j IDLETIMER" |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 71 | " --timeout 12345 --label hello --send_nl_msg 1", op), |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 72 | "COMMIT", |
| 73 | "*mangle", |
| 74 | StringPrintf("%s idletimer_mangle_POSTROUTING -o wlan0 -j IDLETIMER" |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 75 | " --timeout 12345 --label hello --send_nl_msg 1", op), |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 76 | "COMMIT\n", |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 77 | }; |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 78 | return { Join(cmds, '\n') }; |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | TEST_F(IdletimerControllerTest, TestAddRemove) { |
| 82 | auto expected = makeAddRemoveCommands(true); |
| 83 | mIt.addInterfaceIdletimer("wlan0", 12345, "hello"); |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 84 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 85 | |
| 86 | mIt.addInterfaceIdletimer("wlan0", 12345, "hello"); |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 87 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 88 | |
| 89 | expected = makeAddRemoveCommands(false); |
| 90 | mIt.removeInterfaceIdletimer("wlan0", 12345, "hello"); |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 91 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 92 | |
| 93 | mIt.removeInterfaceIdletimer("wlan0", 12345, "hello"); |
Lorenzo Colitti | ad46e76 | 2017-08-10 19:44:34 +0900 | [diff] [blame] | 94 | expectIptablesRestoreCommands(expected); |
Lorenzo Colitti | 85a2160 | 2017-08-10 19:22:45 +0900 | [diff] [blame] | 95 | } |