blob: 162170b43c3b4d3b8dc261e4f77f57cc960c62db [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
3 * Copyright (c) 2014-2018 Regents of the University of California,
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08004 * 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 Shi77dcadd2014-10-05 14:40:54 -070010 *
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
Davide Pesavento7e780642018-11-24 15:51:34 -050028#include "ndn-cxx/net/ethernet.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070029
Davide Pesavento7e780642018-11-24 15:51:34 -050030#include "tests/boost-test.hpp"
Junxiao Shi77dcadd2014-10-05 14:40:54 -070031
32namespace ndn {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080033namespace tests {
Junxiao Shi77dcadd2014-10-05 14:40:54 -070034
Junxiao Shi25467942017-06-30 02:53:14 +000035BOOST_AUTO_TEST_SUITE(Net)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036BOOST_AUTO_TEST_SUITE(TestEthernet)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070037
Davide Pesaventoeee3e822016-11-26 19:19:34 +010038BOOST_AUTO_TEST_CASE(Basic)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070039{
Davide Pesavento51dc2ef2014-11-04 20:04:19 +010040 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 Shi77dcadd2014-10-05 14:40:54 -070056 BOOST_CHECK(ethernet::getBroadcastAddress().isBroadcast());
57 BOOST_CHECK(ethernet::getDefaultMulticastAddress().isMulticast());
58}
59
60BOOST_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
72BOOST_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 Pesaventodfb8a612014-11-25 19:14:11 +0100106BOOST_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 Pesaventoeee3e822016-11-26 19:19:34 +0100113BOOST_AUTO_TEST_SUITE_END() // TestEthernet
Junxiao Shi25467942017-06-30 02:53:14 +0000114BOOST_AUTO_TEST_SUITE_END() // Net
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700115
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800116} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700117} // namespace ndn