Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | b5f8bcc | 2017-02-05 17:58:05 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2016 Regents of the University of California, |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology, |
| 9 | * The University of Memphis. |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 10 | * |
| 11 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 12 | * |
| 13 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 14 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 15 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 18 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 19 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 20 | * |
| 21 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 22 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 23 | * <http://www.gnu.org/licenses/>. |
| 24 | * |
| 25 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 26 | */ |
| 27 | |
| 28 | #include "util/ethernet.hpp" |
| 29 | |
| 30 | #include "boost-test.hpp" |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace util { |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 34 | namespace tests { |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 35 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 36 | BOOST_AUTO_TEST_SUITE(Util) |
| 37 | BOOST_AUTO_TEST_SUITE(TestEthernet) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 38 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 39 | BOOST_AUTO_TEST_CASE(Basic) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 40 | { |
Davide Pesavento | 51dc2ef | 2014-11-04 20:04:19 +0100 | [diff] [blame] | 41 | ethernet::Address a; |
| 42 | BOOST_CHECK(a.isNull()); |
| 43 | |
| 44 | a = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB}; |
| 45 | ethernet::Address b(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB); |
| 46 | const uint8_t bytes[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB}; |
| 47 | ethernet::Address c(bytes); |
| 48 | ethernet::Address d(a); |
| 49 | ethernet::Address e; |
| 50 | e = a; |
| 51 | |
| 52 | BOOST_CHECK_EQUAL(a, b); |
| 53 | BOOST_CHECK_EQUAL(a, c); |
| 54 | BOOST_CHECK_EQUAL(a, d); |
| 55 | BOOST_CHECK_EQUAL(a, e); |
| 56 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 57 | BOOST_CHECK(ethernet::getBroadcastAddress().isBroadcast()); |
| 58 | BOOST_CHECK(ethernet::getDefaultMulticastAddress().isMulticast()); |
| 59 | } |
| 60 | |
| 61 | BOOST_AUTO_TEST_CASE(ToString) |
| 62 | { |
| 63 | BOOST_CHECK_EQUAL(ethernet::Address().toString('-'), |
| 64 | "00-00-00-00-00-00"); |
| 65 | BOOST_CHECK_EQUAL(ethernet::getBroadcastAddress().toString(), |
| 66 | "ff:ff:ff:ff:ff:ff"); |
| 67 | BOOST_CHECK_EQUAL(ethernet::Address(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB).toString('-'), |
| 68 | "01-23-45-67-89-ab"); |
| 69 | BOOST_CHECK_EQUAL(ethernet::Address(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB).toString(), |
| 70 | "01:23:45:67:89:ab"); |
| 71 | } |
| 72 | |
| 73 | BOOST_AUTO_TEST_CASE(FromString) |
| 74 | { |
| 75 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("0:0:0:0:0:0"), |
| 76 | ethernet::Address()); |
| 77 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("ff-ff-ff-ff-ff-ff"), |
| 78 | ethernet::getBroadcastAddress()); |
| 79 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("de:ad:be:ef:1:2"), |
| 80 | ethernet::Address(0xde, 0xad, 0xbe, 0xef, 0x01, 0x02)); |
| 81 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("DE:AD:BE:EF:1:2"), |
| 82 | ethernet::Address(0xde, 0xad, 0xbe, 0xef, 0x01, 0x02)); |
| 83 | |
| 84 | // malformed inputs |
| 85 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("01.23.45.67.89.ab"), |
| 86 | ethernet::Address()); |
| 87 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("01:23:45 :67:89:ab"), |
| 88 | ethernet::Address()); |
| 89 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("01:23:45:67:89::1"), |
| 90 | ethernet::Address()); |
| 91 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("01-23-45-67-89"), |
| 92 | ethernet::Address()); |
| 93 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("01:23:45:67:89:ab:cd"), |
| 94 | ethernet::Address()); |
| 95 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("01:23:45:67-89-ab"), |
| 96 | ethernet::Address()); |
| 97 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("qw-er-ty-12-34-56"), |
| 98 | ethernet::Address()); |
| 99 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("this-is-not-an-ethernet-address"), |
| 100 | ethernet::Address()); |
| 101 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("foobar"), |
| 102 | ethernet::Address()); |
| 103 | BOOST_CHECK_EQUAL(ethernet::Address::fromString(""), |
| 104 | ethernet::Address()); |
| 105 | } |
| 106 | |
Davide Pesavento | dfb8a61 | 2014-11-25 19:14:11 +0100 | [diff] [blame] | 107 | BOOST_AUTO_TEST_CASE(StdHash) |
| 108 | { |
| 109 | // make sure we can use ethernet::Address as key type in std::unordered_map |
| 110 | std::hash<ethernet::Address> h; |
| 111 | BOOST_CHECK_NO_THROW(h(ethernet::getDefaultMulticastAddress())); |
| 112 | } |
| 113 | |
Davide Pesavento | eee3e82 | 2016-11-26 19:19:34 +0100 | [diff] [blame] | 114 | BOOST_AUTO_TEST_SUITE_END() // TestEthernet |
| 115 | BOOST_AUTO_TEST_SUITE_END() // Util |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 116 | |
Spyridon Mastorakis | 429634f | 2015-02-19 17:35:33 -0800 | [diff] [blame] | 117 | } // namespace tests |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 118 | } // namespace util |
| 119 | } // namespace ndn |