blob: 0fc5357ba9d142d71e21573dabea37e6210eca0e [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/*
Davide Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2014-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040032namespace ndn::tests {
Junxiao Shi77dcadd2014-10-05 14:40:54 -070033
Junxiao Shi25467942017-06-30 02:53:14 +000034BOOST_AUTO_TEST_SUITE(Net)
Davide Pesaventoeee3e822016-11-26 19:19:34 +010035BOOST_AUTO_TEST_SUITE(TestEthernet)
Junxiao Shi77dcadd2014-10-05 14:40:54 -070036
Davide Pesaventoeee3e822016-11-26 19:19:34 +010037BOOST_AUTO_TEST_CASE(Basic)
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
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100112BOOST_AUTO_TEST_SUITE_END() // TestEthernet
Junxiao Shi25467942017-06-30 02:53:14 +0000113BOOST_AUTO_TEST_SUITE_END() // Net
Junxiao Shi77dcadd2014-10-05 14:40:54 -0700114
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400115} // namespace ndn::tests