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