Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "core/face-uri.hpp" |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 8 | #ifdef HAVE_PCAP |
| 9 | #include "face/ethernet.hpp" |
| 10 | #endif // HAVE_PCAP |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 11 | |
| 12 | #include "tests/test-common.hpp" |
| 13 | |
| 14 | namespace nfd { |
| 15 | namespace tests { |
| 16 | |
| 17 | BOOST_FIXTURE_TEST_SUITE(CoreFaceUri, BaseFixture) |
| 18 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 19 | BOOST_AUTO_TEST_CASE(Internal) |
| 20 | { |
| 21 | FaceUri uri; |
| 22 | |
| 23 | BOOST_CHECK(uri.parse("internal://")); |
| 24 | BOOST_CHECK_EQUAL(uri.getScheme(), "internal"); |
| 25 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 26 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 27 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 28 | |
| 29 | BOOST_CHECK_EQUAL(uri.parse("internal:"), false); |
| 30 | BOOST_CHECK_EQUAL(uri.parse("internal:/"), false); |
| 31 | } |
| 32 | |
| 33 | BOOST_AUTO_TEST_CASE(Udp) |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 34 | { |
| 35 | BOOST_CHECK_NO_THROW(FaceUri("udp://hostname:6363")); |
| 36 | BOOST_CHECK_THROW(FaceUri("udp//hostname:6363"), FaceUri::Error); |
| 37 | BOOST_CHECK_THROW(FaceUri("udp://hostname:port"), FaceUri::Error); |
| 38 | |
| 39 | FaceUri uri; |
| 40 | BOOST_CHECK_EQUAL(uri.parse("udp//hostname:6363"), false); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 41 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 42 | BOOST_CHECK(uri.parse("udp://hostname:80")); |
| 43 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 44 | BOOST_CHECK_EQUAL(uri.getHost(), "hostname"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 45 | BOOST_CHECK_EQUAL(uri.getPort(), "80"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 46 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 47 | |
| 48 | BOOST_CHECK(uri.parse("udp4://192.0.2.1:20")); |
| 49 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp4"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 50 | BOOST_CHECK_EQUAL(uri.getHost(), "192.0.2.1"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 51 | BOOST_CHECK_EQUAL(uri.getPort(), "20"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 52 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 53 | |
| 54 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0::1]:6363")); |
| 55 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 56 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0::1"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 57 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 58 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 59 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 60 | BOOST_CHECK(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86d3]:6363")); |
| 61 | BOOST_CHECK_EQUAL(uri.getScheme(), "udp6"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 62 | BOOST_CHECK_EQUAL(uri.getHost(), "2001:db8:3f9:0:3025:ccc5:eeeb:86d3"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 63 | BOOST_CHECK_EQUAL(uri.getPort(), "6363"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 64 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 65 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 66 | BOOST_CHECK_EQUAL(uri.parse("udp6://[2001:db8:3f9:0:3025:ccc5:eeeb:86dg]:6363"), false); |
| 67 | |
| 68 | using namespace boost::asio; |
| 69 | ip::udp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777); |
| 70 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4)); |
| 71 | BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "udp4://192.0.2.1:7777"); |
| 72 | |
| 73 | ip::udp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777); |
| 74 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6)); |
| 75 | BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "udp6://[2001:db8::1]:7777"); |
| 76 | } |
| 77 | |
| 78 | BOOST_AUTO_TEST_CASE(Tcp) |
| 79 | { |
| 80 | FaceUri uri; |
| 81 | |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 82 | BOOST_CHECK(uri.parse("tcp://random.host.name")); |
| 83 | BOOST_CHECK_EQUAL(uri.getScheme(), "tcp"); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 84 | BOOST_CHECK_EQUAL(uri.getHost(), "random.host.name"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 85 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 86 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 87 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(uri.parse("tcp://192.0.2.1:"), false); |
| 89 | BOOST_CHECK_EQUAL(uri.parse("tcp://[::zzzz]"), false); |
| 90 | |
| 91 | using namespace boost::asio; |
| 92 | ip::tcp::endpoint endpoint4(ip::address_v4::from_string("192.0.2.1"), 7777); |
| 93 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint4)); |
| 94 | BOOST_CHECK_EQUAL(FaceUri(endpoint4).toString(), "tcp4://192.0.2.1:7777"); |
| 95 | |
| 96 | ip::tcp::endpoint endpoint6(ip::address_v6::from_string("2001:DB8::1"), 7777); |
| 97 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint6)); |
| 98 | BOOST_CHECK_EQUAL(FaceUri(endpoint6).toString(), "tcp6://[2001:db8::1]:7777"); |
| 99 | } |
| 100 | |
| 101 | BOOST_AUTO_TEST_CASE(Unix) |
| 102 | { |
| 103 | FaceUri uri; |
| 104 | |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 105 | BOOST_CHECK(uri.parse("unix:///var/run/example.sock")); |
| 106 | BOOST_CHECK_EQUAL(uri.getScheme(), "unix"); |
| 107 | BOOST_CHECK_EQUAL(uri.getHost(), ""); |
| 108 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 109 | BOOST_CHECK_EQUAL(uri.getPath(), "/var/run/example.sock"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 110 | |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 111 | //BOOST_CHECK_EQUAL(uri.parse("unix://var/run/example.sock"), false); |
| 112 | // This is not a valid unix:/ URI, but the parse would treat "var" as host |
| 113 | |
| 114 | #ifdef HAVE_UNIX_SOCKETS |
| 115 | using namespace boost::asio; |
| 116 | local::stream_protocol::endpoint endpoint("/var/run/example.sock"); |
| 117 | BOOST_REQUIRE_NO_THROW(FaceUri(endpoint)); |
| 118 | BOOST_CHECK_EQUAL(FaceUri(endpoint).toString(), "unix:///var/run/example.sock"); |
| 119 | #endif // HAVE_UNIX_SOCKETS |
| 120 | } |
| 121 | |
| 122 | BOOST_AUTO_TEST_CASE(Fd) |
| 123 | { |
| 124 | FaceUri uri; |
| 125 | |
| 126 | BOOST_CHECK(uri.parse("fd://6")); |
| 127 | BOOST_CHECK_EQUAL(uri.getScheme(), "fd"); |
| 128 | BOOST_CHECK_EQUAL(uri.getHost(), "6"); |
| 129 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
Junxiao Shi | 61e3cc5 | 2014-03-03 20:40:28 -0700 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
Junxiao Shi | e38b1c4 | 2014-04-01 23:45:36 -0700 | [diff] [blame] | 131 | |
| 132 | int fd = 21; |
| 133 | BOOST_REQUIRE_NO_THROW(FaceUri::fromFd(fd)); |
| 134 | BOOST_CHECK_EQUAL(FaceUri::fromFd(fd).toString(), "fd://21"); |
| 135 | } |
| 136 | |
| 137 | BOOST_AUTO_TEST_CASE(Ether) |
| 138 | { |
| 139 | FaceUri uri; |
| 140 | |
| 141 | BOOST_CHECK(uri.parse("ether://08:00:27:01:dd:01")); |
| 142 | BOOST_CHECK_EQUAL(uri.getScheme(), "ether"); |
| 143 | BOOST_CHECK_EQUAL(uri.getHost(), "08:00:27:01:dd:01"); |
| 144 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 145 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 146 | |
| 147 | BOOST_CHECK_EQUAL(uri.parse("ether://08:00:27:zz:dd:01"), false); |
| 148 | |
| 149 | #ifdef HAVE_PCAP |
| 150 | ethernet::Address address = ethernet::Address::fromString("33:33:01:01:01:01"); |
| 151 | BOOST_REQUIRE_NO_THROW(FaceUri(address)); |
| 152 | BOOST_CHECK_EQUAL(FaceUri(address).toString(), "ether://33:33:01:01:01:01"); |
| 153 | #endif // HAVE_PCAP |
| 154 | } |
| 155 | |
| 156 | BOOST_AUTO_TEST_CASE(Dev) |
| 157 | { |
| 158 | FaceUri uri; |
| 159 | |
| 160 | BOOST_CHECK(uri.parse("dev://eth0")); |
| 161 | BOOST_CHECK_EQUAL(uri.getScheme(), "dev"); |
| 162 | BOOST_CHECK_EQUAL(uri.getHost(), "eth0"); |
| 163 | BOOST_CHECK_EQUAL(uri.getPort(), ""); |
| 164 | BOOST_CHECK_EQUAL(uri.getPath(), ""); |
| 165 | |
| 166 | std::string ifname = "en1"; |
| 167 | BOOST_REQUIRE_NO_THROW(FaceUri::fromDev(ifname)); |
| 168 | BOOST_CHECK_EQUAL(FaceUri::fromDev(ifname).toString(), "dev://en1"); |
Alexander Afanasyev | 7b9347b | 2014-02-28 08:37:56 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | BOOST_AUTO_TEST_SUITE_END() |
| 172 | |
| 173 | } // namespace tests |
| 174 | } // namespace nfd |