]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/tests/unit/dns_test.cc
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / seastar / tests / unit / dns_test.cc
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18
19 /*
20 * Copyright (C) 2016 ScyllaDB.
21 */
22 #include <vector>
23 #include <algorithm>
24
25 #include <seastar/core/do_with.hh>
26 #include <seastar/testing/test_case.hh>
27 #include <seastar/core/sstring.hh>
28 #include <seastar/core/reactor.hh>
29 #include <seastar/core/do_with.hh>
30 #include <seastar/core/future-util.hh>
31 #include <seastar/net/dns.hh>
32 #include <seastar/net/inet_address.hh>
33
34 using namespace seastar;
35 using namespace seastar::net;
36
37 static const inet_address google_addr = inet_address("216.58.201.164");
38 static const sstring google_name = "www.google.com";
39
40 static future<> test_resolve(dns_resolver::options opts) {
41 auto d = ::make_lw_shared<dns_resolver>(std::move(opts));
42 return d->get_host_by_name(google_name, inet_address::family::INET).then([d](hostent e) {
43 //BOOST_REQUIRE(std::count(e.addr_list.begin(), e.addr_list.end(), google_addr));
44 return d->get_host_by_addr(e.addr_list.front()).then([d, a = e.addr_list.front()](hostent e) {
45 return d->get_host_by_name(e.names.front(), inet_address::family::INET).then([a](hostent e) {
46 BOOST_REQUIRE(std::count(e.addr_list.begin(), e.addr_list.end(), a));
47 });
48 });
49 }).finally([d]{
50 return d->close();
51 });
52 }
53
54 static future<> test_bad_name(dns_resolver::options opts) {
55 auto d = ::make_lw_shared<dns_resolver>(std::move(opts));
56 return d->get_host_by_name("apa.ninja.gnu", inet_address::family::INET).then_wrapped([d](future<hostent> f) {
57 try {
58 f.get();
59 BOOST_FAIL("should not succeed");
60 } catch (...) {
61 // ok.
62 }
63 }).finally([d]{
64 return d->close();
65 });
66 }
67
68 SEASTAR_TEST_CASE(test_resolve_udp) {
69 return test_resolve(dns_resolver::options());
70 }
71
72 SEASTAR_TEST_CASE(test_bad_name_udp) {
73 return test_bad_name(dns_resolver::options());
74 }
75
76 SEASTAR_TEST_CASE(test_timeout_udp) {
77 dns_resolver::options opts;
78 opts.servers = std::vector<inet_address>({ inet_address("1.2.3.4") }); // not a server
79 opts.udp_port = 29953; // not a dns port
80 opts.timeout = std::chrono::milliseconds(500);
81
82 auto d = ::make_lw_shared<dns_resolver>(engine().net(), opts);
83 return d->get_host_by_name(google_name, inet_address::family::INET).then_wrapped([d](future<hostent> f) {
84 try {
85 f.get();
86 BOOST_FAIL("should not succeed");
87 } catch (...) {
88 // ok.
89 }
90 }).finally([d]{
91 return d->close();
92 });
93 }
94
95 // Currently failing, disable until fixed (#521)
96 #if 0
97 SEASTAR_TEST_CASE(test_resolve_tcp) {
98 dns_resolver::options opts;
99 opts.use_tcp_query = true;
100 return test_resolve(opts);
101 }
102 #endif
103
104 SEASTAR_TEST_CASE(test_bad_name_tcp) {
105 dns_resolver::options opts;
106 opts.use_tcp_query = true;
107 return test_bad_name(opts);
108 }
109
110 static const sstring imaps_service = "imaps";
111 static const sstring gmail_domain = "gmail.com";
112
113 static future<> test_srv() {
114 auto d = ::make_lw_shared<dns_resolver>();
115 return d->get_srv_records(dns_resolver::srv_proto::tcp,
116 imaps_service,
117 gmail_domain).then([d](dns_resolver::srv_records records) {
118 BOOST_REQUIRE(!records.empty());
119 for (auto& record : records) {
120 // record.target should end with "gmail.com"
121 BOOST_REQUIRE_GT(record.target.size(), gmail_domain.size());
122 BOOST_REQUIRE_EQUAL(record.target.compare(record.target.size() - gmail_domain.size(),
123 gmail_domain.size(),
124 gmail_domain),
125 0);
126 }
127 }).finally([d]{
128 return d->close();
129 });
130 }
131
132 SEASTAR_TEST_CASE(test_srv_tcp) {
133 return test_srv();
134 }