1 /** 2 * Copyright (c) 2019, 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 #pragma once 18 19 #include "base_metrics_listener.h" 20 21 enum EventFlag : uint32_t { 22 onDnsEvent = 1 << 0, 23 onPrivateDnsValidationEvent = 1 << 1, 24 onConnectEvent = 1 << 2, 25 onWakeupEvent = 1 << 3, 26 onTcpSocketStatsEvent = 1 << 4, 27 onNat64PrefixEvent = 1 << 5, 28 }; 29 30 namespace android { 31 namespace net { 32 namespace metrics { 33 34 // Base class for metrics event unit test. Used for notifications about DNS event changes. Should 35 // be extended by unit tests wanting notifications. 36 class BaseTestMetricsEvent : public BaseMetricsListener { 37 public: 38 // Returns TRUE if the verification was successful. Otherwise, returns FALSE. 39 virtual bool isVerified() = 0; 40 getCv()41 std::condition_variable& getCv() { return mCv; } getCvMutex()42 std::mutex& getCvMutex() { return mCvMutex; } 43 44 private: 45 // The verified event(s) as a bitwise-OR combination of enum EventFlag flags. 46 uint32_t mVerified{}; 47 48 // This lock prevents racing condition between signaling thread(s) and waiting thread(s). 49 std::mutex mCvMutex; 50 51 // Condition variable signaled when notify() is called. 52 std::condition_variable mCv; 53 54 protected: 55 // Notify who is waiting for test results. See also mCvMutex and mCv. 56 void notify(); 57 58 // Get current verified event(s). getVerified()59 uint32_t getVerified() const { return mVerified; } 60 61 // Set the specific event as verified if its verification was successful. 62 void setVerified(EventFlag event); 63 }; 64 65 // Derived class for testing onDnsEvent(). 66 class TestOnDnsEvent : public BaseTestMetricsEvent { 67 public: 68 // Both latencyMs and uid are not verified. No special reason. 69 // TODO: Considering to verify uid. 70 struct TestResult { 71 int netId; 72 int eventType; 73 int returnCode; 74 int ipAddressesCount; 75 std::string hostname; 76 std::string ipAddress; // Check first address only. 77 }; 78 79 TestOnDnsEvent() = delete; TestOnDnsEvent(const std::vector<TestResult> & results)80 TestOnDnsEvent(const std::vector<TestResult>& results) : mResults(results){}; 81 82 // BaseTestMetricsEvent::isVerified() override. isVerified()83 bool isVerified() override { return (getVerified() & EventFlag::onDnsEvent) != 0; } 84 85 // Override for testing verification. 86 ::ndk::ScopedAStatus onDnsEvent(int32_t netId, int32_t eventType, int32_t returnCode, 87 int32_t /*latencyMs*/, const std::string& hostname, 88 const std::vector<std::string>& ipAddresses, 89 int32_t ipAddressesCount, int32_t /*uid*/) override; 90 91 private: 92 const std::vector<TestResult>& mResults; // Expected results for test verification. 93 }; 94 95 } // namespace metrics 96 } // namespace net 97 } // namespace android