blob: 35c0deec9b52f68bbe639a4595dde29d1923f7a7 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Junxiao Shie3ef6ee2014-10-05 14:40:54 -07003 * 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 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 {
34
35BOOST_AUTO_TEST_SUITE(UtilTestEthernet)
36
Davide Pesavento51dc2ef2014-11-04 20:04:19 +010037BOOST_AUTO_TEST_CASE(BasicChecks)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070038{
Davide Pesavento51dc2ef2014-11-04 20:04:19 +010039 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 Shi77dcadd2014-10-05 14:40:54 -070055 BOOST_CHECK(ethernet::getBroadcastAddress().isBroadcast());
56 BOOST_CHECK(ethernet::getDefaultMulticastAddress().isMulticast());
57}
58
59BOOST_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
71BOOST_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 Pesaventodfb8a612014-11-25 19:14:11 +0100105BOOST_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 Shi77dcadd2014-10-05 14:40:54 -0700112BOOST_AUTO_TEST_SUITE_END()
113
114} // namespace util
115} // namespace ndn