blob: e8e59e51bb3804668d62168ee2b12d2406741cb1 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesaventoeee3e822016-11-26 19:19:34 +01003 * Copyright (c) 2013-2016 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
28#include "util/ethernet.hpp"
29
30#include "boost-test.hpp"
31
32namespace ndn {
33namespace util {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080034namespace tests {
Junxiao Shi77dcadd2014-10-05 14:40:54 -070035
Davide Pesaventoeee3e822016-11-26 19:19:34 +010036BOOST_AUTO_TEST_SUITE(Util)
37BOOST_AUTO_TEST_SUITE(TestEthernet)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070038
Davide Pesaventoeee3e822016-11-26 19:19:34 +010039BOOST_AUTO_TEST_CASE(Basic)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070040{
Davide Pesavento51dc2ef2014-11-04 20:04:19 +010041 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 Shi77dcadd2014-10-05 14:40:54 -070057 BOOST_CHECK(ethernet::getBroadcastAddress().isBroadcast());
58 BOOST_CHECK(ethernet::getDefaultMulticastAddress().isMulticast());
59}
60
61BOOST_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
73BOOST_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 Pesaventodfb8a612014-11-25 19:14:11 +0100107BOOST_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 Pesaventoeee3e822016-11-26 19:19:34 +0100114BOOST_AUTO_TEST_SUITE_END() // TestEthernet
115BOOST_AUTO_TEST_SUITE_END() // Util
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700116
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800117} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700118} // namespace util
119} // namespace ndn