blob: 9b7aa35fa89dec303d1090b0054773c6606685b1 [file] [log] [blame]
Junxiao Shi77dcadd2014-10-05 14:40:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Spyridon Mastorakis429634f2015-02-19 17:35:33 -08003 * Copyright (c) 2013-2015 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 {
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080034namespace tests {
Junxiao Shi77dcadd2014-10-05 14:40:54 -070035
Spyridon Mastorakis429634f2015-02-19 17:35:33 -080036BOOST_AUTO_TEST_SUITE(UtilEthernet)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070037
Davide Pesavento51dc2ef2014-11-04 20:04:19 +010038BOOST_AUTO_TEST_CASE(BasicChecks)
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
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700113BOOST_AUTO_TEST_SUITE_END()
114
Spyridon Mastorakis429634f2015-02-19 17:35:33 -0800115} // namespace tests
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700116} // namespace util
117} // namespace ndn