xref: /aosp_15_r20/system/netd/tests/test_utils.cpp (revision 8542734a0dd1db395a4d42aae09c37f3c3c3e7a1)
1*8542734aSAndroid Build Coastguard Worker /*
2*8542734aSAndroid Build Coastguard Worker  * Copyright 2019 The Android Open Source Project
3*8542734aSAndroid Build Coastguard Worker  *
4*8542734aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8542734aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8542734aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8542734aSAndroid Build Coastguard Worker  *
8*8542734aSAndroid Build Coastguard Worker  * http://www.apache.org/licenses/LICENSE-2.0
9*8542734aSAndroid Build Coastguard Worker  *
10*8542734aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8542734aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8542734aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8542734aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8542734aSAndroid Build Coastguard Worker  * limitations under the License.
15*8542734aSAndroid Build Coastguard Worker  *
16*8542734aSAndroid Build Coastguard Worker  * test_utils.cpp - miscellaneous unit test utilities.
17*8542734aSAndroid Build Coastguard Worker  */
18*8542734aSAndroid Build Coastguard Worker 
19*8542734aSAndroid Build Coastguard Worker #include <cstdio>
20*8542734aSAndroid Build Coastguard Worker #include <string>
21*8542734aSAndroid Build Coastguard Worker #include <vector>
22*8542734aSAndroid Build Coastguard Worker 
23*8542734aSAndroid Build Coastguard Worker #include <android-base/stringprintf.h>
24*8542734aSAndroid Build Coastguard Worker #include <android-base/strings.h>
25*8542734aSAndroid Build Coastguard Worker #include <binder/IResultReceiver.h>
26*8542734aSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
27*8542734aSAndroid Build Coastguard Worker #include <binder/IShellCallback.h>
28*8542734aSAndroid Build Coastguard Worker #include <binder/TextOutput.h>
29*8542734aSAndroid Build Coastguard Worker 
30*8542734aSAndroid Build Coastguard Worker #include "test_utils.h"
31*8542734aSAndroid Build Coastguard Worker 
32*8542734aSAndroid Build Coastguard Worker #define IP_PATH "/system/bin/ip"
33*8542734aSAndroid Build Coastguard Worker 
34*8542734aSAndroid Build Coastguard Worker using android::IBinder;
35*8542734aSAndroid Build Coastguard Worker using android::IServiceManager;
36*8542734aSAndroid Build Coastguard Worker using android::sp;
37*8542734aSAndroid Build Coastguard Worker using android::String16;
38*8542734aSAndroid Build Coastguard Worker using android::Vector;
39*8542734aSAndroid Build Coastguard Worker using android::base::Split;
40*8542734aSAndroid Build Coastguard Worker using android::base::StringPrintf;
41*8542734aSAndroid Build Coastguard Worker 
randomUid()42*8542734aSAndroid Build Coastguard Worker int randomUid() {
43*8542734aSAndroid Build Coastguard Worker     // Pick a random UID consisting of:
44*8542734aSAndroid Build Coastguard Worker     // - Random user profile (0 - 6)
45*8542734aSAndroid Build Coastguard Worker     // - Random app ID starting from 12000 (FIRST_APPLICATION_UID + 2000). This ensures no conflicts
46*8542734aSAndroid Build Coastguard Worker     //   with existing app UIDs unless the user has installed more than 2000 apps, and is still less
47*8542734aSAndroid Build Coastguard Worker     //   than LAST_APPLICATION_UID (19999).
48*8542734aSAndroid Build Coastguard Worker     return 100000 * arc4random_uniform(7) + 12000 + arc4random_uniform(3000);
49*8542734aSAndroid Build Coastguard Worker }
50*8542734aSAndroid Build Coastguard Worker 
runCommand(const std::string & command)51*8542734aSAndroid Build Coastguard Worker std::vector<std::string> runCommand(const std::string& command) {
52*8542734aSAndroid Build Coastguard Worker     std::vector<std::string> lines;
53*8542734aSAndroid Build Coastguard Worker     FILE* f = popen(command.c_str(), "r");  // NOLINT(cert-env33-c)
54*8542734aSAndroid Build Coastguard Worker     if (f == nullptr) {
55*8542734aSAndroid Build Coastguard Worker         perror("popen");
56*8542734aSAndroid Build Coastguard Worker         return lines;
57*8542734aSAndroid Build Coastguard Worker     }
58*8542734aSAndroid Build Coastguard Worker 
59*8542734aSAndroid Build Coastguard Worker     char* line = nullptr;
60*8542734aSAndroid Build Coastguard Worker     size_t bufsize = 0;
61*8542734aSAndroid Build Coastguard Worker     ssize_t linelen = 0;
62*8542734aSAndroid Build Coastguard Worker     while ((linelen = getline(&line, &bufsize, f)) >= 0) {
63*8542734aSAndroid Build Coastguard Worker         std::string str = std::string(line, linelen);
64*8542734aSAndroid Build Coastguard Worker         const size_t lastNotWhitespace = str.find_last_not_of(" \t\n\r");
65*8542734aSAndroid Build Coastguard Worker         if (lastNotWhitespace != std::string::npos) {
66*8542734aSAndroid Build Coastguard Worker             str = str.substr(0, lastNotWhitespace + 1);
67*8542734aSAndroid Build Coastguard Worker         }
68*8542734aSAndroid Build Coastguard Worker         lines.push_back(str);
69*8542734aSAndroid Build Coastguard Worker         free(line);
70*8542734aSAndroid Build Coastguard Worker         line = nullptr;
71*8542734aSAndroid Build Coastguard Worker     }
72*8542734aSAndroid Build Coastguard Worker 
73*8542734aSAndroid Build Coastguard Worker     pclose(f);
74*8542734aSAndroid Build Coastguard Worker     return lines;
75*8542734aSAndroid Build Coastguard Worker }
76*8542734aSAndroid Build Coastguard Worker 
runBinderCommand(const std::string serviceName,const std::string & command)77*8542734aSAndroid Build Coastguard Worker android::status_t runBinderCommand(const std::string serviceName, const std::string& command) {
78*8542734aSAndroid Build Coastguard Worker     // For services implementing the shell command binder method, we want to avoid forking a shell
79*8542734aSAndroid Build Coastguard Worker     // and directly transact on the binder instead.
80*8542734aSAndroid Build Coastguard Worker     sp<IServiceManager> sm = android::defaultServiceManager();
81*8542734aSAndroid Build Coastguard Worker     sp<IBinder> service = sm->checkService(String16(serviceName.c_str()));
82*8542734aSAndroid Build Coastguard Worker 
83*8542734aSAndroid Build Coastguard Worker     if (!service) return android::NAME_NOT_FOUND;
84*8542734aSAndroid Build Coastguard Worker 
85*8542734aSAndroid Build Coastguard Worker     const std::vector<std::string> args = Split(command, " ");
86*8542734aSAndroid Build Coastguard Worker     android::Vector<String16> argVec;
87*8542734aSAndroid Build Coastguard Worker     for (const auto arg : args) {
88*8542734aSAndroid Build Coastguard Worker         argVec.add(String16(arg.data(), arg.size()));
89*8542734aSAndroid Build Coastguard Worker     }
90*8542734aSAndroid Build Coastguard Worker     return IBinder::shellCommand(service, STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO, argVec,
91*8542734aSAndroid Build Coastguard Worker                                  nullptr /* cb */, nullptr /* result */);
92*8542734aSAndroid Build Coastguard Worker }
93*8542734aSAndroid Build Coastguard Worker 
listIpRules(const char * ipVersion)94*8542734aSAndroid Build Coastguard Worker std::vector<std::string> listIpRules(const char* ipVersion) {
95*8542734aSAndroid Build Coastguard Worker     std::string command = StringPrintf("%s %s rule list", IP_PATH, ipVersion);
96*8542734aSAndroid Build Coastguard Worker     return runCommand(command);
97*8542734aSAndroid Build Coastguard Worker }
98*8542734aSAndroid Build Coastguard Worker 
listIptablesRule(const char * binary,const char * chainName)99*8542734aSAndroid Build Coastguard Worker std::vector<std::string> listIptablesRule(const char* binary, const char* chainName) {
100*8542734aSAndroid Build Coastguard Worker     std::string command = StringPrintf("%s -w -n -L %s", binary, chainName);
101*8542734aSAndroid Build Coastguard Worker     return runCommand(command);
102*8542734aSAndroid Build Coastguard Worker }
103*8542734aSAndroid Build Coastguard Worker 
iptablesRuleLineLength(const char * binary,const char * chainName)104*8542734aSAndroid Build Coastguard Worker int iptablesRuleLineLength(const char* binary, const char* chainName) {
105*8542734aSAndroid Build Coastguard Worker     return listIptablesRule(binary, chainName).size();
106*8542734aSAndroid Build Coastguard Worker }
107*8542734aSAndroid Build Coastguard Worker 
iptablesRuleExists(const char * binary,const char * chainName,const std::string & expectedRule)108*8542734aSAndroid Build Coastguard Worker bool iptablesRuleExists(const char* binary, const char* chainName,
109*8542734aSAndroid Build Coastguard Worker                         const std::string& expectedRule) {
110*8542734aSAndroid Build Coastguard Worker     std::vector<std::string> rules = listIptablesRule(binary, chainName);
111*8542734aSAndroid Build Coastguard Worker     for (const auto& rule : rules) {
112*8542734aSAndroid Build Coastguard Worker         if (rule.find(expectedRule) != std::string::npos) {
113*8542734aSAndroid Build Coastguard Worker             return true;
114*8542734aSAndroid Build Coastguard Worker         }
115*8542734aSAndroid Build Coastguard Worker     }
116*8542734aSAndroid Build Coastguard Worker     return false;
117*8542734aSAndroid Build Coastguard Worker }
118*8542734aSAndroid Build Coastguard Worker 
listIpRoutes(const char * ipVersion,const char * table)119*8542734aSAndroid Build Coastguard Worker std::vector<std::string> listIpRoutes(const char* ipVersion, const char* table) {
120*8542734aSAndroid Build Coastguard Worker     std::string command = StringPrintf("%s %s route ls table %s", IP_PATH, ipVersion, table);
121*8542734aSAndroid Build Coastguard Worker     return runCommand(command);
122*8542734aSAndroid Build Coastguard Worker }
123*8542734aSAndroid Build Coastguard Worker 
ipRouteExists(const char * ipVersion,const char * table,const std::vector<std::string> & ipRouteSubstrings)124*8542734aSAndroid Build Coastguard Worker bool ipRouteExists(const char* ipVersion, const char* table,
125*8542734aSAndroid Build Coastguard Worker                    const std::vector<std::string>& ipRouteSubstrings) {
126*8542734aSAndroid Build Coastguard Worker     std::vector<std::string> routes = listIpRoutes(ipVersion, table);
127*8542734aSAndroid Build Coastguard Worker     for (const auto& route : routes) {
128*8542734aSAndroid Build Coastguard Worker         bool matched = true;
129*8542734aSAndroid Build Coastguard Worker         for (const auto& substring : ipRouteSubstrings) {
130*8542734aSAndroid Build Coastguard Worker             if (route.find(substring) == std::string::npos) {
131*8542734aSAndroid Build Coastguard Worker                 matched = false;
132*8542734aSAndroid Build Coastguard Worker                 break;
133*8542734aSAndroid Build Coastguard Worker             }
134*8542734aSAndroid Build Coastguard Worker         }
135*8542734aSAndroid Build Coastguard Worker 
136*8542734aSAndroid Build Coastguard Worker         if (matched) {
137*8542734aSAndroid Build Coastguard Worker             return true;
138*8542734aSAndroid Build Coastguard Worker         }
139*8542734aSAndroid Build Coastguard Worker     }
140*8542734aSAndroid Build Coastguard Worker     return false;
141*8542734aSAndroid Build Coastguard Worker }
142