1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2020 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use {
    crate::{tcp::DnsTcpStream, udp::DnsUdpSocket, FuchsiaTime},
    fuchsia_async as fasync,
    futures::{Future, FutureExt},
    trust_dns_proto::error::ProtoError,
    trust_dns_resolver::{
        name_server::{
            GenericConnection as Connection, GenericConnectionProvider, RuntimeProvider, Spawn,
        },
        AsyncResolver,
    },
};

/// Implement the `trust_dns_resolver::name_server::Spawn` trait in-terms-of fasync::Task.
#[derive(Clone)]
pub struct Spawner;

impl Spawn for Spawner {
    fn spawn_bg<F>(&mut self, future: F)
    where
        F: Future<Output = Result<(), ProtoError>> + Send + 'static,
    {
        fasync::Task::spawn(future.map(|_| ())).detach()
    }
}

/// A Fuchsia Runtime type which uses `Handle`, `DnsTcpStream`, `FuchsiaTime`, and `DnsUdpSocket`
/// defined in this crate.
// NOTE: This is an abstracted type used to run trus_dns_resolver::* APIs.
#[derive(Clone)]
pub struct FuchsiaRuntime;

impl RuntimeProvider for FuchsiaRuntime {
    type Handle = Spawner;
    type Tcp = DnsTcpStream;
    type Timer = FuchsiaTime;
    type Udp = DnsUdpSocket;
}

/// ConnectionProvider that creates new connections to DNS servers on Fuchsia.
pub type ConnectionProvider = GenericConnectionProvider<FuchsiaRuntime>;

/// Resolver that resolves DNS requests on Fuchsia.
pub type Resolver = AsyncResolver<Connection, ConnectionProvider>;

#[cfg(test)]
mod tests {
    use crate::FuchsiaExec;
    use trust_dns_resolver::config::ResolverConfig;

    use super::*;

    #[test]
    fn test_ip_lookup() {
        use trust_dns_resolver::testing::ip_lookup_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        ip_lookup_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner)
    }

    #[test]
    fn test_ip_lookup_across_threads() {
        use trust_dns_resolver::testing::ip_lookup_across_threads_test;
        ip_lookup_across_threads_test::<FuchsiaExec, FuchsiaRuntime>(Spawner)
    }

    #[test]
    fn test_localhost_ipv4() {
        use trust_dns_resolver::testing::localhost_ipv4_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        localhost_ipv4_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }

    #[test]
    fn test_localhost_ipv6() {
        use trust_dns_resolver::testing::localhost_ipv6_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        localhost_ipv6_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }

    #[test]
    fn test_search_ipv4_large_ndots() {
        use trust_dns_resolver::testing::search_ipv4_large_ndots_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        search_ipv4_large_ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }

    #[test]
    fn test_search_ipv6_large_ndots() {
        use trust_dns_resolver::testing::search_ipv6_large_ndots_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        search_ipv6_large_ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }

    #[test]
    fn test_search_ipv6_name_parse_fails() {
        use trust_dns_resolver::testing::search_ipv6_name_parse_fails_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        search_ipv6_name_parse_fails_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }

    // TODO(chunyingw): Make it as a manual test.
    // The test requires Internet connection for testing.
    #[test]
    #[ignore]
    fn test_lookup_google() {
        use trust_dns_resolver::testing::lookup_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        lookup_test::<FuchsiaExec, FuchsiaRuntime>(ResolverConfig::google(), exec, Spawner)
    }

    // TODO(chunyingw): Make it as a manual test.
    // The test requires Internet connection for testing.
    #[test]
    #[ignore]
    fn test_lookup_cloudflare() {
        use trust_dns_resolver::testing::lookup_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        lookup_test::<FuchsiaExec, FuchsiaRuntime>(ResolverConfig::cloudflare(), exec, Spawner)
    }

    // TODO(chunyingw): Make it as a manual test.
    // The test requires Internet connection for testing.
    #[test]
    #[ignore] // TODO(chunyingw): Make it as a manual test.
              // The test requires Internet connection for testing.
    fn test_lookup_quad9() {
        use trust_dns_resolver::testing::lookup_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        lookup_test::<FuchsiaExec, FuchsiaRuntime>(ResolverConfig::quad9(), exec, Spawner)
    }

    // TODO(chunyingw): Make it as a manual test.
    // The test requires Internet connection for testing.
    #[test]
    #[ignore]
    fn test_fqdn() {
        use trust_dns_resolver::testing::fqdn_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        fqdn_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }

    // TODO(chunyingw): Make it as a manual test.
    // The test requires Internet connection for testing.
    #[test]
    #[ignore]
    fn test_ndots() {
        use trust_dns_resolver::testing::ndots_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }

    // TODO(chunyingw): Make it as a manual test.
    // The test requires Internet connection for testing.
    #[test]
    #[ignore]
    fn test_large_ndots() {
        use trust_dns_resolver::testing::large_ndots_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        large_ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }

    // TODO(chunyingw): Make it as a manual test.
    // The test requires Internet connection for testing.
    #[test]
    #[ignore]
    fn test_domain_search() {
        use trust_dns_resolver::testing::domain_search_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        domain_search_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }

    // TODO(chunyingw): Make it as a manual test.
    // The test requires Internet connection for testing.
    #[test]
    #[ignore]
    fn test_search_list() {
        use trust_dns_resolver::testing::search_list_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        search_list_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }

    // TODO(chunyingw): Make it as a manual test.
    // The test requires Internet connection for testing.
    #[test]
    #[ignore]
    fn test_idna() {
        use trust_dns_resolver::testing::idna_test;
        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
        idna_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
    }
}