• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
27 using android::base::Join;
28 using android::base::StringPrintf;
29 
30 class IdletimerControllerTest : public IptablesBaseTest {
31 protected:
IdletimerControllerTest()32     IdletimerControllerTest() {
33         IdletimerController::execIptablesRestore = fakeExecIptablesRestore;
34     }
35     IdletimerController mIt;
36 };
37 
TEST_F(IdletimerControllerTest,TestSetupIptablesHooks)38 TEST_F(IdletimerControllerTest, TestSetupIptablesHooks) {
39     mIt.setupIptablesHooks();
40     expectIptablesRestoreCommands(ExpectedIptablesCommands{});
41 }
42 
makeAddRemoveCommands(bool add)43 const std::vector<std::string> makeAddRemoveCommands(bool add) {
44     const char *op = add ? "-A" : "-D";
45     std::vector<std::string> cmds = {
46         "*raw",
47         StringPrintf("%s idletimer_raw_PREROUTING -i wlan0 -j IDLETIMER"
48                      " --timeout 12345 --label hello --send_nl_msg 1", op),
49         "COMMIT",
50         "*mangle",
51         StringPrintf("%s idletimer_mangle_POSTROUTING -o wlan0 -j IDLETIMER"
52                      " --timeout 12345 --label hello --send_nl_msg 1", op),
53         "COMMIT\n",
54     };
55     return { Join(cmds, '\n') };
56 }
57 
TEST_F(IdletimerControllerTest,TestAddRemove)58 TEST_F(IdletimerControllerTest, TestAddRemove) {
59     auto expected = makeAddRemoveCommands(true);
60     mIt.addInterfaceIdletimer("wlan0", 12345, "hello");
61     expectIptablesRestoreCommands(expected);
62 
63     mIt.addInterfaceIdletimer("wlan0", 12345, "hello");
64     expectIptablesRestoreCommands(expected);
65 
66     expected = makeAddRemoveCommands(false);
67     mIt.removeInterfaceIdletimer("wlan0", 12345, "hello");
68     expectIptablesRestoreCommands(expected);
69 
70     mIt.removeInterfaceIdletimer("wlan0", 12345, "hello");
71     expectIptablesRestoreCommands(expected);
72 }
73