Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | e3ef6ee | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014, Regents of the University of California, |
| 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 { |
| 34 | |
| 35 | BOOST_AUTO_TEST_SUITE(UtilTestEthernet) |
| 36 | |
Davide Pesavento | 51dc2ef | 2014-11-04 20:04:19 +0100 | [diff] [blame] | 37 | BOOST_AUTO_TEST_CASE(BasicChecks) |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 38 | { |
Davide Pesavento | 51dc2ef | 2014-11-04 20:04:19 +0100 | [diff] [blame] | 39 | ethernet::Address a; |
| 40 | BOOST_CHECK(a.isNull()); |
| 41 | |
| 42 | a = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB}; |
| 43 | ethernet::Address b(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB); |
| 44 | const uint8_t bytes[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB}; |
| 45 | ethernet::Address c(bytes); |
| 46 | ethernet::Address d(a); |
| 47 | ethernet::Address e; |
| 48 | e = a; |
| 49 | |
| 50 | BOOST_CHECK_EQUAL(a, b); |
| 51 | BOOST_CHECK_EQUAL(a, c); |
| 52 | BOOST_CHECK_EQUAL(a, d); |
| 53 | BOOST_CHECK_EQUAL(a, e); |
| 54 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 55 | BOOST_CHECK(ethernet::getBroadcastAddress().isBroadcast()); |
| 56 | BOOST_CHECK(ethernet::getDefaultMulticastAddress().isMulticast()); |
| 57 | } |
| 58 | |
| 59 | BOOST_AUTO_TEST_CASE(ToString) |
| 60 | { |
| 61 | BOOST_CHECK_EQUAL(ethernet::Address().toString('-'), |
| 62 | "00-00-00-00-00-00"); |
| 63 | BOOST_CHECK_EQUAL(ethernet::getBroadcastAddress().toString(), |
| 64 | "ff:ff:ff:ff:ff:ff"); |
| 65 | BOOST_CHECK_EQUAL(ethernet::Address(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB).toString('-'), |
| 66 | "01-23-45-67-89-ab"); |
| 67 | BOOST_CHECK_EQUAL(ethernet::Address(0x01, 0x23, 0x45, 0x67, 0x89, 0xAB).toString(), |
| 68 | "01:23:45:67:89:ab"); |
| 69 | } |
| 70 | |
| 71 | BOOST_AUTO_TEST_CASE(FromString) |
| 72 | { |
| 73 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("0:0:0:0:0:0"), |
| 74 | ethernet::Address()); |
| 75 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("ff-ff-ff-ff-ff-ff"), |
| 76 | ethernet::getBroadcastAddress()); |
| 77 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("de:ad:be:ef:1:2"), |
| 78 | ethernet::Address(0xde, 0xad, 0xbe, 0xef, 0x01, 0x02)); |
| 79 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("DE:AD:BE:EF:1:2"), |
| 80 | ethernet::Address(0xde, 0xad, 0xbe, 0xef, 0x01, 0x02)); |
| 81 | |
| 82 | // malformed inputs |
| 83 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("01.23.45.67.89.ab"), |
| 84 | ethernet::Address()); |
| 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::1"), |
| 88 | ethernet::Address()); |
| 89 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("01-23-45-67-89"), |
| 90 | ethernet::Address()); |
| 91 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("01:23:45:67:89:ab:cd"), |
| 92 | ethernet::Address()); |
| 93 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("01:23:45:67-89-ab"), |
| 94 | ethernet::Address()); |
| 95 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("qw-er-ty-12-34-56"), |
| 96 | ethernet::Address()); |
| 97 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("this-is-not-an-ethernet-address"), |
| 98 | ethernet::Address()); |
| 99 | BOOST_CHECK_EQUAL(ethernet::Address::fromString("foobar"), |
| 100 | ethernet::Address()); |
| 101 | BOOST_CHECK_EQUAL(ethernet::Address::fromString(""), |
| 102 | ethernet::Address()); |
| 103 | } |
| 104 | |
Davide Pesavento | dfb8a61 | 2014-11-25 19:14:11 +0100 | [diff] [blame] | 105 | BOOST_AUTO_TEST_CASE(StdHash) |
| 106 | { |
| 107 | // make sure we can use ethernet::Address as key type in std::unordered_map |
| 108 | std::hash<ethernet::Address> h; |
| 109 | BOOST_CHECK_NO_THROW(h(ethernet::getDefaultMulticastAddress())); |
| 110 | } |
| 111 | |
Junxiao Shi | 77dcadd | 2014-10-05 14:40:54 -0700 | [diff] [blame] | 112 | BOOST_AUTO_TEST_SUITE_END() |
| 113 | |
| 114 | } // namespace util |
| 115 | } // namespace ndn |