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 <android-base/strings.h>
18 #include <gtest/gtest.h>
19 #include <netdutils/NetNativeTestBase.h>
20 #include <stats.pb.h>
21 
22 #include "resolv_stats_test_utils.h"
23 
24 namespace android::net {
25 
26 class ResolvStatsUtilsTest : public NetNativeTestBase {};
27 
TEST_F(ResolvStatsUtilsTest,NetworkDnsEventEq)28 TEST_F(ResolvStatsUtilsTest, NetworkDnsEventEq) {
29     NetworkDnsEventReported event1;
30     // Following fields will not be verified during the test in proto NetworkDnsEventReported.
31     // So don't need to config those values: event_type, return_code, latency_micros,
32     // hints_ai_flags, res_nsend_flags, network_type, private_dns_modes.
33     constexpr char event2[] = R"Event(
34              NetworkDnsEventReported {
35              dns_query_events:
36              {
37                dns_query_event:[
38                 {
39                  rcode: 3,
40                  type: 28,
41                  cache_hit: 1,
42                  ip_version: 1,
43                  protocol: 3,
44                  retry_times: 28,
45                  dns_server_index: 0,
46                  connected: 1,
47                  latency_micros: 5,
48                 },
49                 {
50                  rcode: 0,
51                  type: 1,
52                  cache_hit: 1,
53                  ip_version: 1,
54                  protocol: 1,
55                  retry_times: 56,
56                  dns_server_index: 1,
57                  connected: 0,
58                  latency_micros: 0,
59                 }
60                ]
61              },
62              uid: 1000,
63         })Event";
64 
65     // TODO: Add integration test to verify Level 1 fields of NetworkDnsEventReported.
66     // Level 1 fields, including event_type, return_code, hints_ai_flags, network_type, etc.
67     DnsQueryEvent* dnsQueryEvent1 = event1.mutable_dns_query_events()->add_dns_query_event();
68     dnsQueryEvent1->set_rcode(NS_R_NXDOMAIN);
69     dnsQueryEvent1->set_type(NS_T_AAAA);
70     dnsQueryEvent1->set_cache_hit(CS_NOTFOUND);
71     dnsQueryEvent1->set_ip_version(IV_IPV4);
72     dnsQueryEvent1->set_protocol(PROTO_DOT);
73     dnsQueryEvent1->set_retry_times(28);
74     dnsQueryEvent1->set_dns_server_index(0);
75     dnsQueryEvent1->set_connected(1);
76     dnsQueryEvent1->set_latency_micros(5);
77     DnsQueryEvent* dnsQueryEvent2 = event1.mutable_dns_query_events()->add_dns_query_event();
78     dnsQueryEvent2->set_rcode(NS_R_NO_ERROR);
79     dnsQueryEvent2->set_type(NS_T_A);
80     dnsQueryEvent2->set_cache_hit(CS_NOTFOUND);
81     dnsQueryEvent2->set_ip_version(IV_IPV4);
82     dnsQueryEvent2->set_protocol(PROTO_UDP);
83     dnsQueryEvent2->set_retry_times(56);
84     dnsQueryEvent2->set_dns_server_index(1);
85     dnsQueryEvent2->set_connected(0);
86     dnsQueryEvent2->set_latency_micros(5);
87     event1.set_uid(1000);
88     EXPECT_THAT(event1, NetworkDnsEventEq(fromNetworkDnsEventReportedStr(event2)));
89 }
90 
91 }  // namespace android::net
92