• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
36 MATCHER_P(DnsQueryEventEq, other, "") {
37     return ::testing::ExplainMatchResult(
38             ::testing::AllOf(
39                     ::testing::Property("rcode", &DnsQueryEvent::rcode,
40                                         ::testing::Eq(other.rcode())),
41                     ::testing::Property("ns_type", &DnsQueryEvent::type,
42                                         ::testing::Eq(other.type())),
43                     ::testing::Property("cache_hit", &DnsQueryEvent::cache_hit,
44                                         ::testing::Eq(other.cache_hit())),
45                     ::testing::Property("ip_version", &DnsQueryEvent::ip_version,
46                                         ::testing::Eq(other.ip_version())),
47                     ::testing::Property("protocol", &DnsQueryEvent::protocol,
48                                         ::testing::Eq(other.protocol())),
49                     ::testing::Property("retry_times", &DnsQueryEvent::retry_times,
50                                         ::testing::Eq(other.retry_times())),
51                     ::testing::Property("dns_server_index", &DnsQueryEvent::dns_server_index,
52                                         ::testing::Eq(other.dns_server_index())),
53                     // Removing the latency check, because we can't predict the time.
54                     /*  ::testing::Property("latency_micros", &DnsQueryEvent::latency_micros,
55                              ::testing::Eq(other.latency_micros())),*/
56                     ::testing::Property("linux_errno", &DnsQueryEvent::linux_errno,
57                                         ::testing::Eq(other.linux_errno())),
58                     ::testing::Property("connected", &DnsQueryEvent::connected,
59                                         ::testing::Eq(other.connected()))),
60             arg, result_listener);
61 }
62 
63 MATCHER_P(DnsQueryEventsEq, other, "") {
64     const int eventSize = arg.dns_query_event_size();
65     if (eventSize != other.dns_query_event_size()) {
66         *result_listener << "Expected dns query event size: " << other.dns_query_event_size()
67                          << " \n";
68         for (int i = 0; i < other.dns_query_event_size(); ++i)
69             PrintTo(other.dns_query_event(i), result_listener->stream());
70         *result_listener << "Actual dns query event size: " << eventSize << "\n";
71         for (int i = 0; i < eventSize; ++i)
72             PrintTo(arg.dns_query_event(i), result_listener->stream());
73         return false;
74     }
75 
76     for (int i = 0; i < eventSize; ++i) {
77         bool result =
78                 ::testing::Value(arg.dns_query_event(i), DnsQueryEventEq(other.dns_query_event(i)));
79         if (!result) {
80             *result_listener << "Expected event num: " << i << " \n";
81             PrintTo(arg.dns_query_event(i), result_listener->stream());
82             *result_listener << "Actual event num: " << i << " ";
83             PrintTo(other.dns_query_event(i), result_listener->stream());
84             return false;
85         }
86     }
87     return true;
88 }
89 
90 MATCHER_P(NetworkDnsEventEq, other, "") {
91     return ::testing::ExplainMatchResult(
92             ::testing::AllOf(
93                     // Removing following fields check, because we can't verify those fields in unit
94                     // test.
95                     /*
96                     ::testing::Property("event_type",
97                                         &android::net::NetworkDnsEventReported::event_type,
98                                         ::testing::Eq(other.event_type())),
99                     ::testing::Property("return_code",
100                                         &android::net::NetworkDnsEventReported::return_code,
101                                         ::testing::Eq(other.return_code())),
102                     ::testing::Property("latency_micros",
103                        &android::net::NetworkDnsEventReported::latency_micros,
104                               ::testing::Eq(other.latency_micros())),
105                     ::testing::Property("hints_ai_flags",
106                                         &android::net::NetworkDnsEventReported::hints_ai_flags,
107                                         ::testing::Eq(other.hints_ai_flags())),
108                     ::testing::Property("res_nsend_flags",
109                                         &android::net::NetworkDnsEventReported::res_nsend_flags,
110                                         ::testing::Eq(other.res_nsend_flags())),
111                     ::testing::Property("network_type",
112                                         &android::net::NetworkDnsEventReported::network_type,
113                                         ::testing::Eq(other.network_type())),
114                     ::testing::Property("private_dns_modes",
115                                         &android::net::NetworkDnsEventReported::private_dns_modes,
116                                         ::testing::Eq(other.private_dns_modes())),
117                     */
118                     ::testing::Property("dns_query_events",
119                                         &android::net::NetworkDnsEventReported::dns_query_events,
120                                         DnsQueryEventsEq(other.dns_query_events()))),
121             arg, result_listener);
122 }
123 
124 }  // namespace android::net
125