| 1 | // Copyright (C) 2021-2025 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-do run { target c++14 } } |
| 19 | // { dg-require-effective-target net_ts_ip } |
| 20 | // { dg-add-options net_ts } |
| 21 | |
| 22 | #include <experimental/internet> |
| 23 | #include <sstream> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | using std::experimental::net::ip::address_v6; |
| 27 | |
| 28 | constexpr bool |
| 29 | test01() |
| 30 | { |
| 31 | address_v6 a; |
| 32 | VERIFY( a.is_unspecified() ); |
| 33 | VERIFY( !a.is_loopback() ); |
| 34 | VERIFY( !a.is_multicast() ); |
| 35 | VERIFY( !a.is_link_local() ); |
| 36 | VERIFY( !a.is_site_local() ); |
| 37 | |
| 38 | a = address_v6::any(); |
| 39 | VERIFY( a.is_unspecified() ); |
| 40 | VERIFY( !a.is_loopback() ); |
| 41 | VERIFY( !a.is_multicast() ); |
| 42 | VERIFY( !a.is_link_local() ); |
| 43 | VERIFY( !a.is_site_local() ); |
| 44 | |
| 45 | a = address_v6::loopback(); |
| 46 | VERIFY( !a.is_unspecified() ); |
| 47 | VERIFY( a.is_loopback() ); |
| 48 | VERIFY( !a.is_multicast() ); |
| 49 | VERIFY( !a.is_link_local() ); |
| 50 | VERIFY( !a.is_site_local() ); |
| 51 | |
| 52 | a = address_v6{address_v6::loopback().to_bytes(), 1}; |
| 53 | VERIFY( !a.is_unspecified() ); |
| 54 | VERIFY( !a.is_loopback() ); |
| 55 | VERIFY( !a.is_multicast() ); |
| 56 | VERIFY( !a.is_link_local() ); |
| 57 | VERIFY( !a.is_site_local() ); |
| 58 | |
| 59 | return true; |
| 60 | } |
| 61 | |
| 62 | static_assert(test01(), ""); |
| 63 | |
| 64 | constexpr bool |
| 65 | test02() |
| 66 | { |
| 67 | auto a = address_v6{address_v6::bytes_type{0xFF}}; |
| 68 | VERIFY( a.is_multicast() ); |
| 69 | |
| 70 | a = address_v6{address_v6::bytes_type{0xFF, 0x01}}; |
| 71 | VERIFY( a.is_multicast() ); |
| 72 | |
| 73 | a = address_v6{address_v6::bytes_type{0xFF, 0x00, 0x01}}; |
| 74 | VERIFY( a.is_multicast() ); |
| 75 | |
| 76 | a = address_v6{address_v6::bytes_type{0xFE, 0x80}}; |
| 77 | VERIFY( !a.is_multicast() ); |
| 78 | |
| 79 | a = address_v6{address_v6::bytes_type{0xFE, 0xC0}}; |
| 80 | VERIFY( !a.is_multicast() ); |
| 81 | |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | static_assert(test02(), ""); |
| 86 | |
| 87 | void |
| 88 | test03() |
| 89 | { |
| 90 | // Assume these addresses use the preferred forms: |
| 91 | VERIFY( address_v6::any().to_string() == "::" ); |
| 92 | VERIFY( address_v6::loopback().to_string() == "::1" ); |
| 93 | |
| 94 | // Choose values with no leading zeros, so output is portable: |
| 95 | address_v6 a{address_v6::bytes_type{21,22,23,24,25,26,27,28,29}, 42}; |
| 96 | const std::string s = a.to_string(); |
| 97 | if (s.find("::") != s.npos) |
| 98 | VERIFY( s == "1516:1718:191a:1b1c:1d00::%42" ); |
| 99 | else |
| 100 | { |
| 101 | // Contiguous zeros were not shortened to "::" |
| 102 | VERIFY( s.substr(0, 25) == "1516:1718:191a:1b1c:1d00:" ); |
| 103 | VERIFY( s.substr(s.size() - 3) == "%42" ); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | test04() |
| 109 | { |
| 110 | address_v6 a{address_v6::bytes_type{1,2,3,4,5,6,7,8,9}, 42}; |
| 111 | std::ostringstream ss; |
| 112 | ss << address_v6::any() << ' ' << address_v6::loopback() << ' ' << a; |
| 113 | VERIFY( ss.str() == (":: ::1 " + a.to_string()) ); |
| 114 | } |
| 115 | |
| 116 | int |
| 117 | main() |
| 118 | { |
| 119 | test01(); |
| 120 | test02(); |
| 121 | test03(); |
| 122 | test04(); |
| 123 | } |