forked from sonic-net/sonic-swss-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmacaddress_ut.cpp
65 lines (51 loc) · 2.61 KB
/
macaddress_ut.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <net/ethernet.h>
#include <gtest/gtest.h>
#include <stdexcept>
#include "common/macaddress.h"
using namespace swss;
using namespace std;
TEST(MacAddress, to_string)
{
const uint8_t a_bin[] = {0x52, 0x54, 0x00, 0xac, 0x3a, 0x99};
auto a = MacAddress(a_bin);
EXPECT_EQ(a.to_string(), "52:54:00:ac:3a:99");
EXPECT_EQ(MacAddress::to_string(a_bin), "52:54:00:ac:3a:99");
const uint8_t b_bin[] = {0xa0, 0xf9, 0x0a, 0x9f, 0x43, 0x21};
auto b = MacAddress(b_bin);
EXPECT_EQ(b.to_string(), "a0:f9:0a:9f:43:21");
EXPECT_EQ(MacAddress::to_string(b_bin), "a0:f9:0a:9f:43:21");
}
TEST(MacAddress, comparison)
{
auto a = MacAddress("52:54:00:ac:3a:99");
auto b = MacAddress("02:09:87:65:43:21");
EXPECT_EQ(a, a);
EXPECT_NE(a, b);
EXPECT_LT(b, a);
EXPECT_FALSE(!a);
EXPECT_FALSE(!b);
auto c = MacAddress("00:00:00:00:00:00");
EXPECT_TRUE(!c);
}
TEST(MacAddress, parse)
{
uint8_t mac[6];
uint8_t ex_mac[] = {0xa0, 0xf9, 0xaa, 0xff, 0x0a, 0x9f};
EXPECT_TRUE(MacAddress::parseMacString("A0:F9:aA:fF:0a:9f", mac)); // correct mac address which uses all edge values
EXPECT_EQ(memcmp(mac, ex_mac, sizeof(mac)), 0); // check that we got correct result
EXPECT_TRUE(MacAddress::parseMacString("A0-F9-aA-fF-0a-9f", mac)); // correct mac address with '-' as delimiter
EXPECT_FALSE(MacAddress::parseMacString("52:34:51:42:42:42", NULL)); // wrong destination parameter
EXPECT_FALSE(MacAddress::parseMacString("52:54:00:25::E9", mac)); // wrong length 1
EXPECT_FALSE(MacAddress::parseMacString("52:54:00:25:12:E9 ", mac)); // wrong length 2
EXPECT_FALSE(MacAddress::parseMacString(" 52:54:00:25:12:E", mac)); // wrong address
EXPECT_FALSE(MacAddress::parseMacString("52:54:00:25:Z9:E9", mac)); // wrong hex number 1
EXPECT_FALSE(MacAddress::parseMacString("52:54:00:25:aZ:E9", mac)); // wrong hex number 2
EXPECT_FALSE(MacAddress::parseMacString("52:54:00:25:z9:E9", mac)); // wrong hex number 3
EXPECT_FALSE(MacAddress::parseMacString("52:54:00:25:az:E9", mac)); // wrong hex number 4
EXPECT_FALSE(MacAddress::parseMacString("52_34:51:42:42:42", mac)); // wrong delimiter 1
EXPECT_FALSE(MacAddress::parseMacString("52:34_51:42:42:42", mac)); // wrong delimiter 2
EXPECT_FALSE(MacAddress::parseMacString("52:34:51_42:42:42", mac)); // wrong delimiter 3
EXPECT_FALSE(MacAddress::parseMacString("52:34:51:42_42:42", mac)); // wrong delimiter 4
EXPECT_FALSE(MacAddress::parseMacString("52:34:51:42:42_42", mac)); // wrong delimiter 5
EXPECT_THROW(MacAddress("52:54:00:25:E9"), invalid_argument);
}