• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2018, 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 #define LOG_TAG "TestUnsolService"
18 
19 #include <algorithm>
20 #include <cinttypes>
21 #include <vector>
22 
23 #include <android-base/stringprintf.h>
24 #include <android-base/strings.h>
25 #include <log/log.h>
26 #include <utils/Errors.h>
27 #include <utils/String16.h>
28 
29 #include <binder/IPCThreadState.h>
30 #include <binder/IServiceManager.h>
31 #include "android/net/BnNetdUnsolicitedEventListener.h"
32 
33 #include "TestUnsolService.h"
34 
35 using android::base::StringPrintf;
36 
37 namespace android {
38 namespace net {
39 
start()40 TestUnsolService* TestUnsolService::start() {
41     IPCThreadState::self()->disableBackgroundScheduling(true);
42     sp<IServiceManager> sm(defaultServiceManager());
43 
44     auto service = new TestUnsolService();
45     const status_t ret = sm->addService(String16(getServiceName()), service, false,
46                                         IServiceManager::DUMP_FLAG_PRIORITY_DEFAULT);
47     if (ret != android::OK) {
48         return nullptr;
49     }
50     sp<ProcessState> ps(ProcessState::self());
51     ps->startThreadPool();
52     ps->giveThreadPoolName();
53 
54     return service;
55 }
56 
57 namespace {
58 
containsSubstring(const std::vector<std::string> & vec,const std::string & subStr)59 bool containsSubstring(const std::vector<std::string>& vec, const std::string& subStr) {
60     return std::any_of(vec.begin(), vec.end(), [&subStr](const std::string& str) {
61         return (str.find(subStr) != std::string::npos);
62     });
63 }
64 
65 }  // namespace
66 
checkTarget(const std::string & ifName,uint32_t flag)67 void TestUnsolService::checkTarget(const std::string& ifName, uint32_t flag) {
68     if (containsSubstring(tarVec, ifName)) {
69         received_ |= flag;
70         maybeNotify();
71     };
72 }
73 
maybeNotify()74 void TestUnsolService::maybeNotify() {
75     // We only have test case for below event currently.
76     if (received_ == (InterfaceAddressUpdated | InterfaceAdded | InterfaceRemoved |
77                       InterfaceLinkStatusChanged | RouteChanged)) {
78         std::lock_guard lock(cv_mutex_);
79         cv_.notify_one();
80     }
81 }
82 
onInterfaceClassActivityChanged(bool isActive,int label,int64_t timestamp,int uid)83 binder::Status TestUnsolService::onInterfaceClassActivityChanged(bool isActive, int label,
84                                                                  int64_t timestamp, int uid) {
85     events_.push_back(StringPrintf("onInterfaceClassActivityChanged %d %d %" PRId64 "%d", isActive,
86                                    label, timestamp, uid));
87     return binder::Status::ok();
88 }
89 
onQuotaLimitReached(const std::string & alertName,const std::string & ifName)90 binder::Status TestUnsolService::onQuotaLimitReached(const std::string& alertName,
91                                                      const std::string& ifName) {
92     events_.push_back(StringPrintf("onQuotaLimitReached %s %s", alertName.c_str(), ifName.c_str()));
93     return binder::Status::ok();
94 }
95 
onInterfaceDnsServerInfo(const std::string & ifName,int64_t lifetime,const std::vector<std::string> & servers)96 binder::Status TestUnsolService::onInterfaceDnsServerInfo(const std::string& ifName,
97                                                           int64_t lifetime,
98                                                           const std::vector<std::string>& servers) {
99     events_.push_back(StringPrintf("onInterfaceDnsServerInfo %s %" PRId64 "%s", ifName.c_str(),
100                                    lifetime, base::Join(servers, ", ").c_str()));
101     return binder::Status::ok();
102 }
103 
onInterfaceAddressUpdated(const std::string &,const std::string & ifName,int,int)104 binder::Status TestUnsolService::onInterfaceAddressUpdated(const std::string&,
105                                                            const std::string& ifName, int, int) {
106     checkTarget(ifName, InterfaceAddressUpdated);
107     return binder::Status::ok();
108 }
109 
onInterfaceAddressRemoved(const std::string & addr,const std::string & ifName,int flags,int scope)110 binder::Status TestUnsolService::onInterfaceAddressRemoved(const std::string& addr,
111                                                            const std::string& ifName, int flags,
112                                                            int scope) {
113     events_.push_back(StringPrintf("onInterfaceAddressRemoved %s %s %d %d", addr.c_str(),
114                                    ifName.c_str(), flags, scope));
115     return binder::Status::ok();
116 }
117 
onInterfaceAdded(const std::string & ifName)118 binder::Status TestUnsolService::onInterfaceAdded(const std::string& ifName) {
119     checkTarget(ifName, InterfaceAdded);
120     return binder::Status::ok();
121 }
122 
onInterfaceRemoved(const std::string & ifName)123 binder::Status TestUnsolService::onInterfaceRemoved(const std::string& ifName) {
124     checkTarget(ifName, InterfaceRemoved);
125     return binder::Status::ok();
126 }
127 
onInterfaceChanged(const std::string & ifName,bool status)128 binder::Status TestUnsolService::onInterfaceChanged(const std::string& ifName, bool status) {
129     events_.push_back(StringPrintf("onInterfaceChanged %s %d", ifName.c_str(), status));
130     return binder::Status::ok();
131 }
132 
onInterfaceLinkStateChanged(const std::string & ifName,bool)133 binder::Status TestUnsolService::onInterfaceLinkStateChanged(const std::string& ifName, bool) {
134     checkTarget(ifName, InterfaceLinkStatusChanged);
135     return binder::Status::ok();
136 }
137 
onRouteChanged(bool,const std::string &,const std::string &,const std::string & ifName)138 binder::Status TestUnsolService::onRouteChanged(bool, const std::string&, const std::string&,
139                                                 const std::string& ifName) {
140     checkTarget(ifName, RouteChanged);
141     return binder::Status::ok();
142 }
143 
onStrictCleartextDetected(int uid,const std::string & hex)144 binder::Status TestUnsolService::onStrictCleartextDetected(int uid, const std::string& hex) {
145     events_.push_back(StringPrintf("onStrictCleartextDetected %d %s", uid, hex.c_str()));
146     return binder::Status::ok();
147 }
148 
149 }  // namespace net
150 }  // namespace android