TestUnsolService.h 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef _NETD_TEST_UNSOLSERVICE_H_
  17. #define _NETD_TEST_UNSOLSERVICE_H_
  18. #include <string>
  19. #include <vector>
  20. #include <binder/BinderService.h>
  21. #include "android/net/BnNetdUnsolicitedEventListener.h"
  22. enum UnsolEvent : uint32_t {
  23. InterfaceClassActivity = 1 << 0,
  24. QuotaLimitReached = 1 << 1,
  25. InterfaceDnsServersAdded = 1 << 2,
  26. InterfaceAddressUpdated = 1 << 3,
  27. InterfaceAddressRemoved = 1 << 4,
  28. InterfaceAdded = 1 << 5,
  29. InterfaceRemoved = 1 << 6,
  30. InterfaceChanged = 1 << 7,
  31. InterfaceLinkStatusChanged = 1 << 8,
  32. RouteChanged = 1 << 9,
  33. StrictCleartextDetected = 1 << 10,
  34. };
  35. namespace android {
  36. namespace net {
  37. class TestUnsolService : public BinderService<TestUnsolService>,
  38. public BnNetdUnsolicitedEventListener {
  39. public:
  40. TestUnsolService() = default;
  41. ~TestUnsolService() = default;
  42. static TestUnsolService* start();
  43. static char const* getServiceName() { return "testUnsol"; }
  44. const std::vector<std::string>& getEvents() const { return events_; }
  45. void clearEvents() { events_.clear(); }
  46. uint32_t getReceived() { return received_; }
  47. std::condition_variable& getCv() { return cv_; }
  48. std::mutex& getCvMutex() { return cv_mutex_; }
  49. binder::Status onInterfaceClassActivityChanged(bool isActive, int label, int64_t timestamp,
  50. int uid) override;
  51. binder::Status onQuotaLimitReached(const std::string& alertName,
  52. const std::string& ifName) override;
  53. binder::Status onInterfaceDnsServerInfo(const std::string& ifName, int64_t lifetime,
  54. const std::vector<std::string>& servers) override;
  55. binder::Status onInterfaceAddressUpdated(const std::string& addr, const std::string& ifName,
  56. int flags, int scope) override;
  57. binder::Status onInterfaceAddressRemoved(const std::string& addr, const std::string& ifName,
  58. int flags, int scope) override;
  59. binder::Status onInterfaceAdded(const std::string& ifName) override;
  60. binder::Status onInterfaceRemoved(const std::string& ifName) override;
  61. binder::Status onInterfaceChanged(const std::string& ifName, bool status) override;
  62. binder::Status onInterfaceLinkStateChanged(const std::string& ifName, bool status) override;
  63. binder::Status onRouteChanged(bool updated, const std::string& route,
  64. const std::string& gateway, const std::string& ifName) override;
  65. binder::Status onStrictCleartextDetected(int uid, const std::string& hex) override;
  66. std::vector<std::string> tarVec;
  67. private:
  68. void maybeNotify();
  69. void checkTarget(const std::string& ifName, uint32_t flag);
  70. std::vector<std::string> events_;
  71. std::mutex cv_mutex_;
  72. std::condition_variable cv_;
  73. uint32_t received_{};
  74. };
  75. } // namespace net
  76. } // namespace android
  77. #endif // _NETD_TEST_UNSOLSERVICE_H_