1 /*
2  * Copyright (C) 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 
17 #include <string>
18 #include <vector>
19 
20 #include <gtest/gtest.h>
21 
22 #include <android-base/strings.h>
23 
24 #include "NetUtilsWrapper.h"
25 
26 #define MAX_ARGS 128
27 #define VALID true
28 #define INVALID false
29 
30 struct Command {
31     bool valid;
32     std::string cmdString;
33 };
34 
35 std::vector<Command> COMMANDS = {
36     {INVALID, "tc qdisc del dev root"},
37     {VALID,   "/system/bin/tc qdisc del dev root"},
38     {VALID,   "/system/bin/ip -6 addr add dev r_rmnet_data6 2001:db8::/64"},
39     {INVALID, "/system/bin/ip -6 addr add dev wlan2 2001:db8::/64"},
40     {VALID,   "/system/bin/ip6tables -w -A INPUT -j qcom_foo"},
41     {INVALID, "/system/bin/ip6tables -w -A INPUT -j routectrl_MANGLE_INPUT"},
42     {VALID,   "/system/bin/ip6tables -w -A INPUT -i rmnet_data9 -j routectrl_MANGLE_INPUT"},
43     {VALID,   "/system/bin/ip6tables -w -F nm_pre_ip4"},
44     {INVALID, "/system/bin/ip6tables -w -F INPUT"},
45     {VALID,   "/system/bin/ndc network interface add oem10"},
46     {VALID,   "/system/bin/ndc network interface add oem10 v_oem9"},
47     {VALID,   "/system/bin/ndc network interface add oem10 oem9"},
48     {INVALID, "/system/bin/ndc network interface add 100 v_oem9"},
49     {VALID,   "/system/bin/ndc network interface add oem10 r_rmnet_data0"},
50     {VALID,   "/system/bin/ip xfrm state"},
51 };
52 
TEST(NetUtilsWrapperTest10,TestCommands)53 TEST(NetUtilsWrapperTest10, TestCommands) {
54     // Overwritten by each test case.
55     char *argv[MAX_ARGS];
56 
57     for (const Command& cmd : COMMANDS) {
58         std::vector<std::string> pieces = android::base::Split(cmd.cmdString, " ");
59         ASSERT_LE(pieces.size(), ARRAY_SIZE(argv));
60         for (size_t i = 0; i < pieces.size(); i++) {
61             argv[i] = const_cast<char*>(pieces[i].c_str());
62         }
63         EXPECT_EQ(cmd.valid, checkExpectedCommand(pieces.size(), argv)) <<
64             "Expected command to be " <<
65             (cmd.valid ? "valid" : "invalid") << ", but was " <<
66             (cmd.valid ? "invalid" : "valid") << ": '" << cmd.cmdString << "'";
67     }
68 }
69