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