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