1 /*
2  * Copyright 2019 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  * test_utils.cpp - miscellaneous unit test utilities.
17  */
18 
19 #include <cstdio>
20 #include <string>
21 #include <vector>
22 
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 #include <binder/IResultReceiver.h>
26 #include <binder/IServiceManager.h>
27 #include <binder/IShellCallback.h>
28 #include <binder/TextOutput.h>
29 
30 #include "test_utils.h"
31 
32 #define IP_PATH "/system/bin/ip"
33 
34 using android::IBinder;
35 using android::IServiceManager;
36 using android::sp;
37 using android::String16;
38 using android::Vector;
39 using android::base::Split;
40 using android::base::StringPrintf;
41 
randomUid()42 int randomUid() {
43     // Pick a random UID consisting of:
44     // - Random user profile (0 - 6)
45     // - Random app ID starting from 12000 (FIRST_APPLICATION_UID + 2000). This ensures no conflicts
46     //   with existing app UIDs unless the user has installed more than 2000 apps, and is still less
47     //   than LAST_APPLICATION_UID (19999).
48     return 100000 * arc4random_uniform(7) + 12000 + arc4random_uniform(3000);
49 }
50 
runCommand(const std::string & command)51 std::vector<std::string> runCommand(const std::string& command) {
52     std::vector<std::string> lines;
53     FILE* f = popen(command.c_str(), "r");  // NOLINT(cert-env33-c)
54     if (f == nullptr) {
55         perror("popen");
56         return lines;
57     }
58 
59     char* line = nullptr;
60     size_t bufsize = 0;
61     ssize_t linelen = 0;
62     while ((linelen = getline(&line, &bufsize, f)) >= 0) {
63         std::string str = std::string(line, linelen);
64         const size_t lastNotWhitespace = str.find_last_not_of(" \t\n\r");
65         if (lastNotWhitespace != std::string::npos) {
66             str = str.substr(0, lastNotWhitespace + 1);
67         }
68         lines.push_back(str);
69         free(line);
70         line = nullptr;
71     }
72 
73     pclose(f);
74     return lines;
75 }
76 
runBinderCommand(const std::string serviceName,const std::string & command)77 android::status_t runBinderCommand(const std::string serviceName, const std::string& command) {
78     // For services implementing the shell command binder method, we want to avoid forking a shell
79     // and directly transact on the binder instead.
80     sp<IServiceManager> sm = android::defaultServiceManager();
81     sp<IBinder> service = sm->checkService(String16(serviceName.c_str()));
82 
83     if (!service) return android::NAME_NOT_FOUND;
84 
85     const std::vector<std::string> args = Split(command, " ");
86     android::Vector<String16> argVec;
87     for (const auto arg : args) {
88         argVec.add(String16(arg.data(), arg.size()));
89     }
90     return IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, argVec,
91                                  nullptr /* cb */, nullptr /* result */);
92 }
93 
listIpRules(const char * ipVersion)94 std::vector<std::string> listIpRules(const char* ipVersion) {
95     std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion);
96     return runCommand(command);
97 }
98 
listIptablesRule(const char * binary,const char * chainName)99 std::vector<std::string> listIptablesRule(const char* binary, const char* chainName) {
100     std::string command = StringPrintf("%s -w -n -L %s", binary, chainName);
101     return runCommand(command);
102 }
103 
iptablesRuleLineLength(const char * binary,const char * chainName)104 int iptablesRuleLineLength(const char* binary, const char* chainName) {
105     return listIptablesRule(binary, chainName).size();
106 }
107 
iptablesRuleExists(const char * binary,const char * chainName,const std::string & expectedRule)108 bool iptablesRuleExists(const char* binary, const char* chainName,
109                         const std::string& expectedRule) {
110     std::vector<std::string> rules = listIptablesRule(binary, chainName);
111     for (const auto& rule : rules) {
112         if (rule.find(expectedRule) != std::string::npos) {
113             return true;
114         }
115     }
116     return false;
117 }
118 
listIpRoutes(const char * ipVersion,const char * table)119 std::vector<std::string> listIpRoutes(const char* ipVersion, const char* table) {
120     std::string command = StringPrintf("%s %s route ls table %s", IP_PATH, ipVersion, table);
121     return runCommand(command);
122 }
123 
ipRouteExists(const char * ipVersion,const char * table,const std::vector<std::string> & ipRouteSubstrings)124 bool ipRouteExists(const char* ipVersion, const char* table,
125                    const std::vector<std::string>& ipRouteSubstrings) {
126     std::vector<std::string> routes = listIpRoutes(ipVersion, table);
127     for (const auto& route : routes) {
128         bool matched = true;
129         for (const auto& substring : ipRouteSubstrings) {
130             if (route.find(substring) == std::string::npos) {
131                 matched = false;
132                 break;
133             }
134         }
135 
136         if (matched) {
137             return true;
138         }
139     }
140     return false;
141 }
142