Exemplo n.º 1
0
        public async Task TestQuery()
        {
            using var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));

            var endpoint = GenerateEndPoint();

            // Create a real upstream DNS client to serve the request (todo: mock this)
            using var upstream = new DnsUdpClient(IPAddress.Parse("1.1.1.1"));

            // Create a loopback server
            using var server = new DnsUdpServer(endpoint, upstream);

            // Start recieving
            using var receiveTask = server.Listen(tokenSource.Token);

            // Create a loopback DNS client to talk to the server
            using var client = new DnsUdpClient(endpoint);

            try
            {
                var query = DnsQueryFactory.CreateQuery("google.com");

                // Send a DNS request to the server, verify the results
                var response = await client.Query(query, tokenSource.Token);

                Assert.Equal(query.Id, response.Header.Id);
                Assert.Equal(query.Host, response.Header.Host);
                Assert.True(response.Answers.Count > 0);
            }
            finally
            {
                tokenSource.Cancel();
                await receiveTask;
            }
        }
Exemplo n.º 2
0
        public async Task TestStartupShutdown()
        {
            using var tokenSource = new CancellationTokenSource(TimeSpan.FromMilliseconds(500));

            using var server = new DnsUdpServer(GenerateEndPoint(), null);

            await server.Listen(tokenSource.Token);
        }