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 18 #pragma once 19 20 #include <iostream> 21 #include <string> 22 #include <vector> 23 24 #include <gmock/gmock.h> 25 #include <stats.pb.h> 26 27 namespace android::net { 28 29 android::net::NetworkDnsEventReported fromNetworkDnsEventReportedStr(const std::string& str); 30 31 // Used for gmock printing our desired debug messages. (Another approach is defining operator<<()) 32 void PrintTo(const DnsQueryEvents& event, std::ostream* os); 33 void PrintTo(const DnsQueryEvent& event, std::ostream* os); 34 void PrintTo(const android::net::NetworkDnsEventReported& event, std::ostream* os); 35 void PrintTo(const Servers& event, std::ostream* os); 36 void PrintTo(const Server& event, std::ostream* os); 37 void PrintTo(const NetworkDnsServerSupportReported& event, std::ostream* os); 38 39 MATCHER_P(DnsQueryEventEq, other, "") { 40 return ::testing::ExplainMatchResult( 41 ::testing::AllOf( 42 ::testing::Property("rcode", &DnsQueryEvent::rcode, 43 ::testing::Eq(other.rcode())), 44 ::testing::Property("ns_type", &DnsQueryEvent::type, 45 ::testing::Eq(other.type())), 46 ::testing::Property("cache_hit", &DnsQueryEvent::cache_hit, 47 ::testing::Eq(other.cache_hit())), 48 ::testing::Property("ip_version", &DnsQueryEvent::ip_version, 49 ::testing::Eq(other.ip_version())), 50 ::testing::Property("protocol", &DnsQueryEvent::protocol, 51 ::testing::Eq(other.protocol())), 52 ::testing::Property("retry_times", &DnsQueryEvent::retry_times, 53 ::testing::Eq(other.retry_times())), 54 ::testing::Property("dns_server_index", &DnsQueryEvent::dns_server_index, 55 ::testing::Eq(other.dns_server_index())), 56 // Removing the latency check, because we can't predict the time. 57 /* ::testing::Property("latency_micros", &DnsQueryEvent::latency_micros, 58 ::testing::Eq(other.latency_micros())),*/ 59 ::testing::Property("linux_errno", &DnsQueryEvent::linux_errno, 60 ::testing::Eq(other.linux_errno())), 61 ::testing::Property("connected", &DnsQueryEvent::connected, 62 ::testing::Eq(other.connected()))), 63 arg, result_listener); 64 } 65 66 MATCHER_P(DnsQueryEventsEq, other, "") { 67 const int eventSize = arg.dns_query_event_size(); 68 if (eventSize != other.dns_query_event_size()) { 69 *result_listener << "Expected dns query event size: " << other.dns_query_event_size() 70 << " \n"; 71 for (int i = 0; i < other.dns_query_event_size(); ++i) 72 PrintTo(other.dns_query_event(i), result_listener->stream()); 73 *result_listener << "Actual dns query event size: " << eventSize << "\n"; 74 for (int i = 0; i < eventSize; ++i) 75 PrintTo(arg.dns_query_event(i), result_listener->stream()); 76 return false; 77 } 78 79 for (int i = 0; i < eventSize; ++i) { 80 bool result = 81 ::testing::Value(arg.dns_query_event(i), DnsQueryEventEq(other.dns_query_event(i))); 82 if (!result) { 83 *result_listener << "Expected event num: " << i << " \n"; 84 PrintTo(arg.dns_query_event(i), result_listener->stream()); 85 *result_listener << "Actual event num: " << i << " "; 86 PrintTo(other.dns_query_event(i), result_listener->stream()); 87 return false; 88 } 89 } 90 return true; 91 } 92 93 MATCHER_P(NetworkDnsEventEq, other, "") { 94 return ::testing::ExplainMatchResult( 95 ::testing::AllOf( 96 // Removing following fields check, because we can't verify those fields in unit 97 // test. 98 /* 99 ::testing::Property("event_type", 100 &android::net::NetworkDnsEventReported::event_type, 101 ::testing::Eq(other.event_type())), 102 ::testing::Property("return_code", 103 &android::net::NetworkDnsEventReported::return_code, 104 ::testing::Eq(other.return_code())), 105 ::testing::Property("latency_micros", 106 &android::net::NetworkDnsEventReported::latency_micros, 107 ::testing::Eq(other.latency_micros())), 108 ::testing::Property("hints_ai_flags", 109 &android::net::NetworkDnsEventReported::hints_ai_flags, 110 ::testing::Eq(other.hints_ai_flags())), 111 ::testing::Property("res_nsend_flags", 112 &android::net::NetworkDnsEventReported::res_nsend_flags, 113 ::testing::Eq(other.res_nsend_flags())), 114 ::testing::Property("network_type", 115 &android::net::NetworkDnsEventReported::network_type, 116 ::testing::Eq(other.network_type())), 117 ::testing::Property("private_dns_modes", 118 &android::net::NetworkDnsEventReported::private_dns_modes, 119 ::testing::Eq(other.private_dns_modes())), 120 */ 121 ::testing::Property("dns_query_events", 122 &android::net::NetworkDnsEventReported::dns_query_events, 123 DnsQueryEventsEq(other.dns_query_events())), 124 ::testing::Property("uid", &android::net::NetworkDnsEventReported::uid, 125 ::testing::Eq(other.uid()))), 126 arg, result_listener); 127 } 128 129 MATCHER_P(ServerEq, other, "") { 130 return ::testing::ExplainMatchResult( 131 ::testing::AllOf( 132 ::testing::Property("protocol", &Server::protocol, 133 ::testing::Eq(other.protocol())), 134 ::testing::Property("index", &Server::index, ::testing::Eq(other.index())), 135 ::testing::Property("validated", &Server::validated, 136 ::testing::Eq(other.validated()))), 137 arg, result_listener); 138 } 139 140 MATCHER_P(ServersEq, other, "") { 141 const int server_size = arg.server_size(); 142 if (server_size != other.server_size()) { 143 return false; 144 } 145 146 for (int i = 0; i < server_size; ++i) { 147 if (!::testing::Value(arg.server(i), ServerEq(other.server(i)))) { 148 *result_listener << "Expected server num: " << i << " \n"; 149 PrintTo(arg.server(i), result_listener->stream()); 150 *result_listener << "Actual server num: " << i << " "; 151 PrintTo(other.server(i), result_listener->stream()); 152 return false; 153 } 154 } 155 return true; 156 } 157 158 MATCHER_P(NetworkDnsServerSupportEq, other, "") { 159 return ::testing::ExplainMatchResult( 160 ::testing::AllOf( 161 ::testing::Property("network_type", 162 &NetworkDnsServerSupportReported::network_type, 163 ::testing::Eq(other.network_type())), 164 ::testing::Property("private_dns_modes", 165 &NetworkDnsServerSupportReported::private_dns_modes, 166 ::testing::Eq(other.private_dns_modes())), 167 ::testing::Property("servers", &NetworkDnsServerSupportReported::servers, 168 ServersEq(other.servers()))), 169 arg, result_listener); 170 } 171 172 } // namespace android::net 173