blob: 2a4e7e135d442ad0896c1f99b8a31311ff5168cc [file] [log] [blame]
Lorenzo Colitti89faa342016-02-26 11:38:47 +09001/*
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 * FirewallControllerTest.cpp - unit tests for FirewallController.cpp
17 */
18
19#include <string>
20#include <vector>
21#include <stdio.h>
22
23#include <gtest/gtest.h>
24
25#include "FirewallController.h"
26
27
28class FirewallControllerTest : public ::testing::Test {
29protected:
30 FirewallController mFw;
31 std::string makeUidRules(const char *a, bool b, const std::vector<int32_t>& c) {
32 return mFw.makeUidRules(a, b, c);
33 }
34};
35
36
37TEST_F(FirewallControllerTest, TestWhitelist) {
38 std::string expected =
39 "*filter\n"
40 ":FW_whitechain -\n"
41 "-A FW_whitechain -m owner --uid-owner 0-9999 -j RETURN\n"
42 "-A FW_whitechain -m owner --uid-owner 10023 -j RETURN\n"
43 "-A FW_whitechain -m owner --uid-owner 10059 -j RETURN\n"
44 "-A FW_whitechain -m owner --uid-owner 10124 -j RETURN\n"
45 "-A FW_whitechain -m owner --uid-owner 10111 -j RETURN\n"
46 "-A FW_whitechain -m owner --uid-owner 110122 -j RETURN\n"
47 "-A FW_whitechain -m owner --uid-owner 210153 -j RETURN\n"
48 "-A FW_whitechain -m owner --uid-owner 210024 -j RETURN\n"
49 "-A FW_whitechain -j DROP\n"
50 "COMMIT\n\x04";
51
52 std::vector<int32_t> uids = { 10023, 10059, 10124, 10111, 110122, 210153, 210024 };
53 EXPECT_EQ(expected, makeUidRules("FW_whitechain", true, uids));
54}
55
56TEST_F(FirewallControllerTest, TestBlacklist) {
57 std::string expected =
58 "*filter\n"
59 ":FW_blackchain -\n"
60 "-A FW_blackchain -m owner --uid-owner 10023 -j DROP\n"
61 "-A FW_blackchain -m owner --uid-owner 10059 -j DROP\n"
62 "-A FW_blackchain -m owner --uid-owner 10124 -j DROP\n"
63 "-A FW_blackchain -j RETURN\n"
64 "COMMIT\n\x04";
65
66 std::vector<int32_t> uids = { 10023, 10059, 10124 };
67 EXPECT_EQ(expected, makeUidRules("FW_blackchain", false, uids));
68}