dns/
async_resolver.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use crate::tcp::DnsTcpStream;
6use crate::udp::DnsUdpSocket;
7use crate::FuchsiaTime;
8use fuchsia_async as fasync;
9use futures::{Future, FutureExt};
10use trust_dns_proto::error::ProtoError;
11use trust_dns_resolver::name_server::{
12    GenericConnection as Connection, GenericConnectionProvider, RuntimeProvider, Spawn,
13};
14use trust_dns_resolver::AsyncResolver;
15
16/// Implement the `trust_dns_resolver::name_server::Spawn` trait in-terms-of fasync::Task.
17#[derive(Clone)]
18pub struct Spawner;
19
20impl Spawn for Spawner {
21    fn spawn_bg<F>(&mut self, future: F)
22    where
23        F: Future<Output = Result<(), ProtoError>> + Send + 'static,
24    {
25        fasync::Task::spawn(future.map(|_| ())).detach()
26    }
27}
28
29/// A Fuchsia Runtime type which uses `Handle`, `DnsTcpStream`, `FuchsiaTime`, and `DnsUdpSocket`
30/// defined in this crate.
31// NOTE: This is an abstracted type used to run trus_dns_resolver::* APIs.
32#[derive(Clone)]
33pub struct FuchsiaRuntime;
34
35impl RuntimeProvider for FuchsiaRuntime {
36    type Handle = Spawner;
37    type Tcp = DnsTcpStream;
38    type Timer = FuchsiaTime;
39    type Udp = DnsUdpSocket;
40}
41
42/// ConnectionProvider that creates new connections to DNS servers on Fuchsia.
43pub type ConnectionProvider = GenericConnectionProvider<FuchsiaRuntime>;
44
45/// Resolver that resolves DNS requests on Fuchsia.
46pub type Resolver = AsyncResolver<Connection, ConnectionProvider>;
47
48#[cfg(test)]
49mod tests {
50    use crate::FuchsiaExec;
51    use trust_dns_resolver::config::ResolverConfig;
52
53    use super::*;
54
55    #[test]
56    fn test_ip_lookup() {
57        use trust_dns_resolver::testing::ip_lookup_test;
58        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
59        ip_lookup_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner)
60    }
61
62    #[test]
63    fn test_ip_lookup_across_threads() {
64        use trust_dns_resolver::testing::ip_lookup_across_threads_test;
65        ip_lookup_across_threads_test::<FuchsiaExec, FuchsiaRuntime>(Spawner)
66    }
67
68    #[test]
69    fn test_localhost_ipv4() {
70        use trust_dns_resolver::testing::localhost_ipv4_test;
71        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
72        localhost_ipv4_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
73    }
74
75    #[test]
76    fn test_localhost_ipv6() {
77        use trust_dns_resolver::testing::localhost_ipv6_test;
78        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
79        localhost_ipv6_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
80    }
81
82    #[test]
83    fn test_search_ipv4_large_ndots() {
84        use trust_dns_resolver::testing::search_ipv4_large_ndots_test;
85        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
86        search_ipv4_large_ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
87    }
88
89    #[test]
90    fn test_search_ipv6_large_ndots() {
91        use trust_dns_resolver::testing::search_ipv6_large_ndots_test;
92        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
93        search_ipv6_large_ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
94    }
95
96    #[test]
97    fn test_search_ipv6_name_parse_fails() {
98        use trust_dns_resolver::testing::search_ipv6_name_parse_fails_test;
99        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
100        search_ipv6_name_parse_fails_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
101    }
102
103    // TODO(chunyingw): Make it as a manual test.
104    // The test requires Internet connection for testing.
105    #[test]
106    #[ignore]
107    fn test_lookup_google() {
108        use trust_dns_resolver::testing::lookup_test;
109        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
110        lookup_test::<FuchsiaExec, FuchsiaRuntime>(ResolverConfig::google(), exec, Spawner)
111    }
112
113    // TODO(chunyingw): Make it as a manual test.
114    // The test requires Internet connection for testing.
115    #[test]
116    #[ignore]
117    fn test_lookup_cloudflare() {
118        use trust_dns_resolver::testing::lookup_test;
119        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
120        lookup_test::<FuchsiaExec, FuchsiaRuntime>(ResolverConfig::cloudflare(), exec, Spawner)
121    }
122
123    // TODO(chunyingw): Make it as a manual test.
124    // The test requires Internet connection for testing.
125    #[test]
126    #[ignore] // TODO(chunyingw): Make it as a manual test.
127              // The test requires Internet connection for testing.
128    fn test_lookup_quad9() {
129        use trust_dns_resolver::testing::lookup_test;
130        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
131        lookup_test::<FuchsiaExec, FuchsiaRuntime>(ResolverConfig::quad9(), exec, Spawner)
132    }
133
134    // TODO(chunyingw): Make it as a manual test.
135    // The test requires Internet connection for testing.
136    #[test]
137    #[ignore]
138    fn test_fqdn() {
139        use trust_dns_resolver::testing::fqdn_test;
140        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
141        fqdn_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
142    }
143
144    // TODO(chunyingw): Make it as a manual test.
145    // The test requires Internet connection for testing.
146    #[test]
147    #[ignore]
148    fn test_ndots() {
149        use trust_dns_resolver::testing::ndots_test;
150        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
151        ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
152    }
153
154    // TODO(chunyingw): Make it as a manual test.
155    // The test requires Internet connection for testing.
156    #[test]
157    #[ignore]
158    fn test_large_ndots() {
159        use trust_dns_resolver::testing::large_ndots_test;
160        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
161        large_ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
162    }
163
164    // TODO(chunyingw): Make it as a manual test.
165    // The test requires Internet connection for testing.
166    #[test]
167    #[ignore]
168    fn test_domain_search() {
169        use trust_dns_resolver::testing::domain_search_test;
170        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
171        domain_search_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
172    }
173
174    // TODO(chunyingw): Make it as a manual test.
175    // The test requires Internet connection for testing.
176    #[test]
177    #[ignore]
178    fn test_search_list() {
179        use trust_dns_resolver::testing::search_list_test;
180        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
181        search_list_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
182    }
183
184    // TODO(chunyingw): Make it as a manual test.
185    // The test requires Internet connection for testing.
186    #[test]
187    #[ignore]
188    fn test_idna() {
189        use trust_dns_resolver::testing::idna_test;
190        let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
191        idna_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
192    }
193}