File: resolver.cpp

package info (click to toggle)
libtorrent-rasterbar 2.0.11-1
  • links: PTS
  • area: main
  • in suites: forky, trixie
  • size: 18,304 kB
  • sloc: cpp: 190,670; python: 7,142; makefile: 1,374; ansic: 574; sh: 317; xml: 104
file content (192 lines) | stat: -rw-r--r-- 5,133 bytes parent folder | download | duplicates (3)
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
/*

Copyright (c) 2015, Arvid Norberg
All rights reserved.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "simulator/simulator.hpp"
#include <functional>

#include "catch.hpp"

#ifdef __GNUC__
// for CATCH's CHECK macro
#pragma GCC diagnostic ignored "-Wparentheses"
#endif

using namespace std::placeholders;
using namespace sim;

using asio::ip::make_address_v4;
using asio::ip::address_v4;
using duration = chrono::high_resolution_clock::duration;
using chrono::duration_cast;
using chrono::milliseconds;

int num_lookups = 0;

void on_name_lookup(boost::system::error_code const& ec
	, asio::ip::tcp::resolver::results_type ips)
{
	++num_lookups;

	std::vector<address_v4> expect = {
		make_address_v4("1.2.3.4")
		, make_address_v4("1.2.3.5")
		, make_address_v4("1.2.3.6")
		, make_address_v4("1.2.3.7") };

	auto expect_it = expect.begin();

	for (auto const& ip : ips)
	{
		assert(ip.endpoint().address() == *expect_it);
		assert(ip.endpoint().port() == 8080);

		++expect_it;
	}

	assert(expect_it == expect.end());
}

void on_failed_name_lookup(boost::system::error_code const& ec
	, asio::ip::tcp::resolver::results_type ips)
{
	++num_lookups;

	assert(ec == boost::system::error_code(asio::error::host_not_found));
}

struct sim_config : sim::default_config
{
	duration hostname_lookup(
		asio::ip::address const& requestor
		, std::string hostname
		, std::vector<asio::ip::address>& result
		, boost::system::error_code& ec)
	{
		if (hostname == "test.com")
		{
			result = {
				make_address_v4("1.2.3.4")
				, make_address_v4("1.2.3.5")
				, make_address_v4("1.2.3.6")
				, make_address_v4("1.2.3.7")
			};
			return duration_cast<duration>(chrono::milliseconds(50));
		}

		return default_config::hostname_lookup(requestor, hostname, result, ec);
	}
};

TEST_CASE("resolve multiple IPv4 addresses", "[resolver]") {
	sim_config cfg;
	simulation sim(cfg);

	chrono::high_resolution_clock::time_point start = chrono::high_resolution_clock::now();
	num_lookups = 0;

	asio::io_context ios(sim, make_address_v4("40.30.20.10"));

	asio::ip::tcp::resolver resolver(ios);
	resolver.async_resolve("test.com", "8080"
		, std::bind(&on_name_lookup, _1, _2));

	sim.run();

	int millis = int(duration_cast<milliseconds>(chrono::high_resolution_clock::now() - start).count());

	CHECK(millis == 50);
	CHECK(num_lookups == 1);
}

TEST_CASE("resolve non-existent hostname", "[resolver]") {
	sim_config cfg;
	simulation sim(cfg);

	chrono::high_resolution_clock::time_point start = chrono::high_resolution_clock::now();
	num_lookups = 0;

	asio::io_context ios(sim, make_address_v4("40.30.20.10"));

	asio::ip::tcp::resolver resolver(ios);
	resolver.async_resolve("non-existent.com", "8080"
		, std::bind(&on_failed_name_lookup, _1, _2));

	sim.run();

	int millis = int(duration_cast<milliseconds>(chrono::high_resolution_clock::now() - start).count());

	CHECK(millis == 100);
	CHECK(num_lookups == 1);
}

TEST_CASE("lookups resolve serially, compounding the latency", "[resolver]") {
	sim_config cfg;
	simulation sim(cfg);

	chrono::high_resolution_clock::time_point start = chrono::high_resolution_clock::now();
	num_lookups = 0;

	asio::io_context ios(sim, make_address_v4("40.30.20.10"));

	asio::ip::tcp::resolver resolver(ios);
	resolver.async_resolve("non-existent.com", "8080", std::bind(&on_failed_name_lookup, _1, _2));
	resolver.async_resolve("non-existent.com", "8080", std::bind(&on_failed_name_lookup, _1, _2));

	sim.run();

	int millis = int(duration_cast<milliseconds>(chrono::high_resolution_clock::now() - start).count());

	CHECK(millis == 200);
	CHECK(num_lookups == 2);
}

TEST_CASE("resolve an IP address", "[resolver]") {
	sim_config cfg;
	simulation sim(cfg);

	chrono::high_resolution_clock::time_point start = chrono::high_resolution_clock::now();
	num_lookups = 0;

	asio::io_context ios(sim, make_address_v4("40.30.20.10"));

	asio::ip::tcp::resolver resolver(ios);
	resolver.async_resolve("10.10.10.10", "8080"
		, [](boost::system::error_code const& ec, asio::ip::tcp::resolver::results_type ips)
	{
		++num_lookups;
		std::vector<address_v4> expect = { make_address_v4("10.10.10.10") };

		auto expect_it = expect.begin();
		for (auto const& ip : ips)
		{
			assert(ip.endpoint().address() == *expect_it);
			assert(ip.endpoint().port() == 8080);
			++expect_it;
		}

		assert(expect_it == expect.end());
	});

	sim.run();

	int millis = int(duration_cast<milliseconds>(chrono::high_resolution_clock::now() - start).count());

	CHECK(millis == 0);
	CHECK(num_lookups == 1);
}