]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/experimental/net/internet/resolver/ops/lookup.cc
Initial commit of Networking TS implementation
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / experimental / net / internet / resolver / ops / lookup.cc
1 // Copyright (C) 2015-2018 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-options "-std=gnu++14" }
19
20 #include <experimental/internet>
21 #include <testsuite_hooks.h>
22
23 using namespace std::experimental::net;
24
25 void
26 test01()
27 {
28 bool test __attribute__((unused)) = false;
29
30 std::error_code ec;
31 io_context ctx;
32 ip::tcp::resolver resolv(ctx);
33 auto addrs = resolv.resolve("localhost", "http", ec);
34 VERIFY( !ec );
35 VERIFY( addrs.size() > 0 );
36 VERIFY( addrs.begin() != addrs.end() );
37 VERIFY( ! addrs.empty() );
38
39 auto addrs2 = resolv.resolve("localhost", "http");
40 VERIFY( addrs == addrs2 );
41 }
42
43 void
44 test02()
45 {
46 bool test __attribute__((unused)) = false;
47
48 std::error_code ec;
49 io_context ctx;
50 ip::tcp::resolver resolv(ctx);
51 auto flags = ip::resolver_base::numeric_host | ip::tcp::resolver::numeric_service;
52 auto addrs = resolv.resolve("127.0.0.1", "42", flags, ec);
53 VERIFY( !ec );
54 VERIFY( addrs.size() > 0 );
55 VERIFY( addrs.begin() != addrs.end() );
56
57 auto addrs2 = resolv.resolve("127.0.0.1", "42", flags);
58 VERIFY( addrs == addrs2 );
59
60 addrs = resolv.resolve("localhost", "42", flags, ec);
61 VERIFY( ec );
62 VERIFY( addrs.empty() );
63 addrs = resolv.resolve("127.0.0.1", "nameserver", flags, ec);
64 VERIFY( ec );
65 VERIFY( addrs.empty() );
66
67 #if __cpp_exceptions
68 bool caught = false;
69 try {
70 resolv.resolve("localhost", "http", flags);
71 } catch (const std::system_error& e) {
72 caught = true;
73 VERIFY( e.code() == ec );
74 }
75 VERIFY( caught );
76 #endif
77 }
78
79 void
80 test03()
81 {
82 bool test __attribute__((unused)) = false;
83
84 std::error_code ec;
85 io_context ctx;
86 ip::tcp::resolver resolv(ctx);
87 auto addrs = resolv.resolve("test.invalid", "http", ec);
88 VERIFY( ec );
89 VERIFY( addrs.size() == 0 );
90 VERIFY( addrs.begin() == addrs.end() );
91 VERIFY( addrs.empty() );
92 #if __cpp_exceptions
93 bool caught = false;
94 try {
95 resolv.resolve("test.invalid", "http");
96 } catch (const std::system_error& e) {
97 caught = true;
98 VERIFY( e.code() == ec );
99 }
100 VERIFY( caught );
101 #endif
102 }
103
104 int
105 main()
106 {
107 test01();
108 test02();
109 test03();
110 }