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.
45use 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;
1516/// Implement the `trust_dns_resolver::name_server::Spawn` trait in-terms-of fasync::Task.
17#[derive(Clone)]
18pub struct Spawner;
1920impl Spawn for Spawner {
21fn spawn_bg<F>(&mut self, future: F)
22where
23F: Future<Output = Result<(), ProtoError>> + Send + 'static,
24 {
25 fasync::Task::spawn(future.map(|_| ())).detach()
26 }
27}
2829/// 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;
3435impl RuntimeProvider for FuchsiaRuntime {
36type Handle = Spawner;
37type Tcp = DnsTcpStream;
38type Timer = FuchsiaTime;
39type Udp = DnsUdpSocket;
40}
4142/// ConnectionProvider that creates new connections to DNS servers on Fuchsia.
43pub type ConnectionProvider = GenericConnectionProvider<FuchsiaRuntime>;
4445/// Resolver that resolves DNS requests on Fuchsia.
46pub type Resolver = AsyncResolver<Connection, ConnectionProvider>;
4748#[cfg(test)]
49mod tests {
50use crate::FuchsiaExec;
51use trust_dns_resolver::config::ResolverConfig;
5253use super::*;
5455#[test]
56fn test_ip_lookup() {
57use trust_dns_resolver::testing::ip_lookup_test;
58let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
59 ip_lookup_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner)
60 }
6162#[test]
63fn test_ip_lookup_across_threads() {
64use trust_dns_resolver::testing::ip_lookup_across_threads_test;
65 ip_lookup_across_threads_test::<FuchsiaExec, FuchsiaRuntime>(Spawner)
66 }
6768#[test]
69fn test_localhost_ipv4() {
70use trust_dns_resolver::testing::localhost_ipv4_test;
71let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
72 localhost_ipv4_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
73 }
7475#[test]
76fn test_localhost_ipv6() {
77use trust_dns_resolver::testing::localhost_ipv6_test;
78let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
79 localhost_ipv6_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
80 }
8182#[test]
83fn test_search_ipv4_large_ndots() {
84use trust_dns_resolver::testing::search_ipv4_large_ndots_test;
85let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
86 search_ipv4_large_ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
87 }
8889#[test]
90fn test_search_ipv6_large_ndots() {
91use trust_dns_resolver::testing::search_ipv6_large_ndots_test;
92let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
93 search_ipv6_large_ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
94 }
9596#[test]
97fn test_search_ipv6_name_parse_fails() {
98use trust_dns_resolver::testing::search_ipv6_name_parse_fails_test;
99let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
100 search_ipv6_name_parse_fails_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
101 }
102103// TODO(chunyingw): Make it as a manual test.
104 // The test requires Internet connection for testing.
105#[test]
106 #[ignore]
107fn test_lookup_google() {
108use trust_dns_resolver::testing::lookup_test;
109let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
110 lookup_test::<FuchsiaExec, FuchsiaRuntime>(ResolverConfig::google(), exec, Spawner)
111 }
112113// TODO(chunyingw): Make it as a manual test.
114 // The test requires Internet connection for testing.
115#[test]
116 #[ignore]
117fn test_lookup_cloudflare() {
118use trust_dns_resolver::testing::lookup_test;
119let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
120 lookup_test::<FuchsiaExec, FuchsiaRuntime>(ResolverConfig::cloudflare(), exec, Spawner)
121 }
122123// 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.
128fn test_lookup_quad9() {
129use trust_dns_resolver::testing::lookup_test;
130let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
131 lookup_test::<FuchsiaExec, FuchsiaRuntime>(ResolverConfig::quad9(), exec, Spawner)
132 }
133134// TODO(chunyingw): Make it as a manual test.
135 // The test requires Internet connection for testing.
136#[test]
137 #[ignore]
138fn test_fqdn() {
139use trust_dns_resolver::testing::fqdn_test;
140let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
141 fqdn_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
142 }
143144// TODO(chunyingw): Make it as a manual test.
145 // The test requires Internet connection for testing.
146#[test]
147 #[ignore]
148fn test_ndots() {
149use trust_dns_resolver::testing::ndots_test;
150let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
151 ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
152 }
153154// TODO(chunyingw): Make it as a manual test.
155 // The test requires Internet connection for testing.
156#[test]
157 #[ignore]
158fn test_large_ndots() {
159use trust_dns_resolver::testing::large_ndots_test;
160let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
161 large_ndots_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
162 }
163164// TODO(chunyingw): Make it as a manual test.
165 // The test requires Internet connection for testing.
166#[test]
167 #[ignore]
168fn test_domain_search() {
169use trust_dns_resolver::testing::domain_search_test;
170let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
171 domain_search_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
172 }
173174// TODO(chunyingw): Make it as a manual test.
175 // The test requires Internet connection for testing.
176#[test]
177 #[ignore]
178fn test_search_list() {
179use trust_dns_resolver::testing::search_list_test;
180let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
181 search_list_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
182 }
183184// TODO(chunyingw): Make it as a manual test.
185 // The test requires Internet connection for testing.
186#[test]
187 #[ignore]
188fn test_idna() {
189use trust_dns_resolver::testing::idna_test;
190let exec = FuchsiaExec::new().expect("failed to create fuchsia executor");
191 idna_test::<FuchsiaExec, FuchsiaRuntime>(exec, Spawner);
192 }
193}