proxygen
MacAddressTest.cpp File Reference

Go to the source code of this file.

Functions

void testMAC (const std::string &str, uint64_t expectedHBO)
 
 TEST (MacAddress, parse)
 
void testFromBinary (const char *str, uint64_t expectedHBO)
 
 TEST (MacAddress, fromBinary)
 
 TEST (MacAddress, toString)
 
 TEST (MacAddress, attributes)
 
 TEST (MacAddress, createMulticast)
 
void testCmp (const char *str1, const char *str2)
 
 TEST (MacAddress, ordering)
 
 TEST (MacAddress, hash)
 

Function Documentation

TEST ( MacAddress  ,
parse   
)

Definition at line 54 of file MacAddressTest.cpp.

References EXPECT_THROW, and testMAC().

54  {
55  testMAC("12:34:56:78:9a:bc", 0x123456789abc);
56  testMAC("00-11-22-33-44-55", 0x1122334455);
57  testMAC("abcdef123456", 0xabcdef123456);
58  testMAC("1:2:3:4:5:6", 0x010203040506);
59  testMAC("0:0:0:0:0:0", 0);
60  testMAC("0:0:5e:0:1:1", 0x00005e000101);
61 
62  EXPECT_THROW(MacAddress(""), std::invalid_argument);
63  EXPECT_THROW(MacAddress("0"), std::invalid_argument);
64  EXPECT_THROW(MacAddress("12:34"), std::invalid_argument);
65  EXPECT_THROW(MacAddress("12:3"), std::invalid_argument);
66  EXPECT_THROW(MacAddress("12:"), std::invalid_argument);
67  EXPECT_THROW(MacAddress("12:x4:56:78:9a:bc"), std::invalid_argument);
68  EXPECT_THROW(MacAddress("12x34:56:78:9a:bc"), std::invalid_argument);
69  EXPECT_THROW(MacAddress("12:34:56:78:9a:bc:de"), std::invalid_argument);
70  EXPECT_THROW(MacAddress("12:34:56:78:9a:bcde"), std::invalid_argument);
71  EXPECT_THROW(MacAddress("12:34:56:78:9a:bc "), std::invalid_argument);
72  EXPECT_THROW(MacAddress(" 12:34:56:78:9a:bc"), std::invalid_argument);
73  EXPECT_THROW(MacAddress("12:34:56:78:-1:bc"), std::invalid_argument);
74 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
void testMAC(const std::string &str, uint64_t expectedHBO)
TEST ( MacAddress  ,
fromBinary   
)

Definition at line 83 of file MacAddressTest.cpp.

References folly::empty(), EXPECT_THROW, and testFromBinary().

83  {
84  testFromBinary("\0\0\0\0\0\0", 0);
85  testFromBinary("\x12\x34\x56\x78\x9a\xbc", 0x123456789abc);
86  testFromBinary("\x11\x22\x33\x44\x55\x66", 0x112233445566);
87 
88  StringPiece empty("");
89  EXPECT_THROW(MacAddress::fromBinary(empty), std::invalid_argument);
90  StringPiece tooShort("\x11", 1);
91  EXPECT_THROW(MacAddress::fromBinary(tooShort), std::invalid_argument);
92  StringPiece tooLong("\x11\x22\x33\x44\x55\x66\x77", 7);
93  EXPECT_THROW(MacAddress::fromBinary(tooLong), std::invalid_argument);
94 }
#define EXPECT_THROW(statement, expected_exception)
Definition: gtest.h:1843
constexpr auto empty(C const &c) -> decltype(c.empty())
Definition: Access.h:55
void testFromBinary(const char *str, uint64_t expectedHBO)
Range< const char * > StringPiece
TEST ( MacAddress  ,
toString   
)

Definition at line 96 of file MacAddressTest.cpp.

References EXPECT_EQ, and fizz::toString().

96  {
97  EXPECT_EQ(
98  "12:34:56:78:9a:bc", MacAddress::fromHBO(0x123456789abc).toString());
99  EXPECT_EQ("12:34:56:78:9a:bc", MacAddress("12:34:56:78:9a:bc").toString());
100  EXPECT_EQ("01:23:45:67:89:ab", MacAddress("01-23-45-67-89-ab").toString());
101  EXPECT_EQ("01:23:45:67:89:ab", MacAddress("0123456789ab").toString());
102 }
folly::StringPiece toString(StateEnum state)
Definition: State.cpp:16
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( MacAddress  ,
attributes   
)

Definition at line 104 of file MacAddressTest.cpp.

References EXPECT_FALSE, and EXPECT_TRUE.

104  {
105  EXPECT_TRUE(MacAddress("ff:ff:ff:ff:ff:ff").isBroadcast());
106  EXPECT_FALSE(MacAddress("7f:ff:ff:ff:ff:ff").isBroadcast());
107  EXPECT_FALSE(MacAddress("7f:ff:ff:ff:ff:fe").isBroadcast());
108  EXPECT_FALSE(MacAddress("00:00:00:00:00:00").isBroadcast());
109  EXPECT_TRUE(MacAddress::fromNBO(0xffffffffffffffffU).isBroadcast());
110 
111  EXPECT_TRUE(MacAddress("ff:ff:ff:ff:ff:ff").isMulticast());
112  EXPECT_TRUE(MacAddress("01:00:00:00:00:00").isMulticast());
113  EXPECT_FALSE(MacAddress("00:00:00:00:00:00").isMulticast());
114  EXPECT_FALSE(MacAddress("fe:ff:ff:ff:ff:ff").isMulticast());
115  EXPECT_FALSE(MacAddress("00:00:5e:00:01:01").isMulticast());
116 
117  EXPECT_FALSE(MacAddress("ff:ff:ff:ff:ff:ff").isUnicast());
118  EXPECT_FALSE(MacAddress("01:00:00:00:00:00").isUnicast());
119  EXPECT_TRUE(MacAddress("00:00:00:00:00:00").isUnicast());
120  EXPECT_TRUE(MacAddress("fe:ff:ff:ff:ff:ff").isUnicast());
121  EXPECT_TRUE(MacAddress("00:00:5e:00:01:01").isUnicast());
122 
123  EXPECT_TRUE(MacAddress("ff:ff:ff:ff:ff:ff").isLocallyAdministered());
124  EXPECT_TRUE(MacAddress("02:00:00:00:00:00").isLocallyAdministered());
125  EXPECT_FALSE(MacAddress("01:00:00:00:00:00").isLocallyAdministered());
126  EXPECT_FALSE(MacAddress("00:00:00:00:00:00").isLocallyAdministered());
127  EXPECT_FALSE(MacAddress("fd:ff:ff:ff:ff:ff").isLocallyAdministered());
128  EXPECT_TRUE(MacAddress("fe:ff:ff:ff:ff:ff").isLocallyAdministered());
129  EXPECT_FALSE(MacAddress("00:00:5e:00:01:01").isLocallyAdministered());
130  EXPECT_TRUE(MacAddress("02:12:34:56:78:9a").isLocallyAdministered());
131 }
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
TEST ( MacAddress  ,
createMulticast   
)

Definition at line 133 of file MacAddressTest.cpp.

References EXPECT_EQ.

133  {
134  EXPECT_EQ(
135  MacAddress("33:33:00:01:00:03"),
136  MacAddress::createMulticast(IPAddressV6("ff02:dead:beef::1:3")));
137  EXPECT_EQ(
138  MacAddress("33:33:12:34:56:78"),
139  MacAddress::createMulticast(IPAddressV6("ff02::abcd:1234:5678")));
140 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
TEST ( MacAddress  ,
ordering   
)

Definition at line 164 of file MacAddressTest.cpp.

References testCmp().

164  {
165  testCmp("00:00:00:00:00:00", "00:00:00:00:00:01");
166  testCmp("00:00:00:00:00:01", "00:00:00:00:00:02");
167  testCmp("01:00:00:00:00:00", "02:00:00:00:00:00");
168  testCmp("00:00:00:00:00:01", "00:00:00:00:01:00");
169 }
void testCmp(const char *str1, const char *str2)
TEST ( MacAddress  ,
hash   
)

Definition at line 171 of file MacAddressTest.cpp.

References EXPECT_EQ, and EXPECT_NE.

171  {
172  EXPECT_EQ(
173  std::hash<MacAddress>()(MacAddress("00:11:22:33:44:55")),
174  std::hash<MacAddress>()(MacAddress("00-11-22-33-44-55")));
175  EXPECT_NE(
176  std::hash<MacAddress>()(MacAddress("00:11:22:33:44:55")),
177  std::hash<MacAddress>()(MacAddress("00:11:22:33:44:56")));
178 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define EXPECT_NE(val1, val2)
Definition: gtest.h:1926
void testCmp ( const char *  str1,
const char *  str2 
)

Definition at line 142 of file MacAddressTest.cpp.

References folly::copy(), EXPECT_EQ, EXPECT_FALSE, EXPECT_TRUE, SCOPED_TRACE, and folly::sformat().

Referenced by TEST().

142  {
143  SCOPED_TRACE(folly::sformat("{} < {}", str1, str2));
144  MacAddress m1(str1);
145  MacAddress m2(str2);
146 
147  // Test the comparison operators
148  EXPECT_TRUE(m1 < m2);
149  EXPECT_FALSE(m1 < m1);
150  EXPECT_TRUE(m1 <= m2);
151  EXPECT_TRUE(m2 > m1);
152  EXPECT_TRUE(m2 >= m1);
153  EXPECT_TRUE(m1 != m2);
154  EXPECT_TRUE(m1 == (m1));
155  EXPECT_FALSE(m1 == m2);
156 
157  // Also test the copy constructor and assignment operator
158  MacAddress copy(m1);
159  EXPECT_EQ(copy, m1);
160  copy = m2;
161  EXPECT_EQ(copy, m2);
162 }
std::string sformat(StringPiece fmt, Args &&...args)
Definition: Format.h:280
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define SCOPED_TRACE(message)
Definition: gtest.h:2115
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
#define EXPECT_FALSE(condition)
Definition: gtest.h:1862
void testFromBinary ( const char *  str,
uint64_t  expectedHBO 
)

Definition at line 76 of file MacAddressTest.cpp.

References EXPECT_EQ, and SCOPED_TRACE.

Referenced by TEST().

76  {
77  StringPiece bin(str, 6);
78  auto mac = MacAddress::fromBinary(bin);
79  SCOPED_TRACE(mac.toString());
80  EXPECT_EQ(expectedHBO, mac.u64HBO());
81 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define SCOPED_TRACE(message)
Definition: gtest.h:2115
Range< const char * > StringPiece
void testMAC ( const std::string str,
uint64_t  expectedHBO 
)

Definition at line 26 of file MacAddressTest.cpp.

References addr, folly::Endian::big(), folly::MacAddress::bytes(), EXPECT_EQ, SCOPED_TRACE, folly::MacAddress::u64HBO(), folly::MacAddress::u64NBO(), uint64_t, and uint8_t.

Referenced by TEST().

26  {
27  SCOPED_TRACE(str);
28  MacAddress addr(str);
29  // Make sure parsing returned the expected value.
30  EXPECT_EQ(expectedHBO, addr.u64HBO());
31 
32  // Perform additional checks on the MacAddress
33 
34  // Check using operator==()
35  EXPECT_EQ(MacAddress::fromHBO(expectedHBO), addr);
36  // Check using operator==() when passing in non-zero padding bytes
37  EXPECT_EQ(MacAddress::fromHBO(expectedHBO | 0xa5a5000000000000), addr);
38 
39  // Similar checks after converting to network byte order
40  uint64_t expectedNBO = folly::Endian::big(expectedHBO);
41  EXPECT_EQ(expectedNBO, addr.u64NBO());
42  EXPECT_EQ(MacAddress::fromNBO(expectedNBO), addr);
43  uint64_t nboWithPad = folly::Endian::big(expectedHBO | 0xa5a5000000000000);
44  EXPECT_EQ(MacAddress::fromNBO(nboWithPad), addr);
45 
46  // Check they value returned by bytes()
47  uint8_t expectedBytes[8];
48  memcpy(expectedBytes, &expectedNBO, 8);
49  for (int n = 0; n < 6; ++n) {
50  EXPECT_EQ(expectedBytes[n + 2], addr.bytes()[n]);
51  }
52 }
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
#define SCOPED_TRACE(message)
Definition: gtest.h:2115
static T big(T x)
Definition: Bits.h:259
ThreadPoolListHook * addr