TestUnsolService.cpp 5.4 KB

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