Hungming Chen | 5e81d4d | 2019-01-25 10:47:40 +0800 | [diff] [blame] | 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 | #include <netdb.h> |
| 18 | |
| 19 | #include "TestMetrics.h" |
| 20 | |
| 21 | namespace android { |
| 22 | namespace net { |
| 23 | namespace metrics { |
| 24 | |
| 25 | android::binder::Status TestOnDnsEvent::onDnsEvent(int32_t netId, int32_t eventType, |
| 26 | int32_t returnCode, int32_t /*latencyMs*/, |
| 27 | const std::string& hostname, |
| 28 | const ::std::vector<std::string>& ipAddresses, |
| 29 | int32_t ipAddressesCount, int32_t /*uid*/) { |
| 30 | // A bitwise-OR combination of all expected test cases. |
| 31 | // Currently, the maximum number of test case is 32 because a 32-bits bitwise-OR combination |
| 32 | // is used for checking and recording verified test cases. |
| 33 | static const uint32_t kExpectedTests = (1 << mResults.size()) - 1; |
| 34 | |
| 35 | // A bitwise-OR combination for recording verified test cases. |
| 36 | static uint32_t verifiedTests = 0; |
| 37 | |
| 38 | for (size_t i = 0; i < mResults.size(); ++i) { |
| 39 | // Generate bitwise flag to trace which testcase is running. |
| 40 | const uint32_t currentTestFlag = 1 << i; |
| 41 | |
| 42 | // Ignore verified testcase. |
| 43 | if (verifiedTests & currentTestFlag) continue; |
| 44 | |
| 45 | // Verify expected event content. |
| 46 | auto& result = mResults[i]; |
| 47 | if (netId != result.netId) continue; |
| 48 | if (eventType != result.eventType) continue; |
| 49 | if (hostname != result.hostname) continue; |
| 50 | if (returnCode != result.returnCode) continue; |
| 51 | if (ipAddressesCount != result.ipAddressesCount) continue; |
| 52 | if (result.returnCode == 0 /*success*/) { |
| 53 | // Only verify first address. |
| 54 | // TODO: Check all addresses. |
| 55 | if (ipAddresses.empty() || ipAddresses.front() != result.ipAddress) continue; |
| 56 | } |
| 57 | |
| 58 | // Record current testcase as verified. |
| 59 | verifiedTests |= currentTestFlag; |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | // All testcases of onDnsEvent are verified. Notify who was waiting for test results. |
| 64 | if (verifiedTests == kExpectedTests) { |
| 65 | setVerified(EventFlag::onDnsEvent); |
| 66 | notify(); |
| 67 | } |
| 68 | |
| 69 | return android::binder::Status::ok(); |
| 70 | }; |
| 71 | |
| 72 | } // namespace metrics |
| 73 | } // namespace net |
| 74 | } // namespace android |