Skip to content

Commit da42bf1

Browse files
committed
Clean Up
1 parent 67b7ac0 commit da42bf1

1 file changed

Lines changed: 54 additions & 152 deletions

File tree

test/MockRelay.cpp

Lines changed: 54 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -36,28 +36,6 @@ const struct sock_fprog ether_relay_fprog = {
3636
ether_relay_filter
3737
};
3838

39-
/*
40-
struct relay_config {
41-
int local_sock;
42-
int server_sock;
43-
int filter;
44-
sockaddr_in6 link_address;
45-
swss::DBConnector *db;
46-
std::string interface;
47-
std::vector<std::string> servers;
48-
std::vector<sockaddr_in6> servers_sock;
49-
bool is_option_79;
50-
}
51-
*/
52-
53-
/* relay_config test_config;
54-
test_config.is_option_79 = true;
55-
test_config.local_sock = 0;
56-
test_config.server_sock = 0;
57-
sockaddr_in6 link;
58-
inet_pton(AF_INET6, "fe80::7683:efff:fe51:5652", &link.sin6_addr);
59-
*/
60-
6139

6240
TEST(helper, toString)
6341
{
@@ -67,10 +45,10 @@ TEST(helper, toString)
6745
TEST(parsePacket, parse_ether_frame)
6846
{
6947
unsigned char ether[] = {
70-
0x33, 0x33, 0x00, 0x01, 0x00, 0x02, /* destination address */
71-
0xfe, 0x54, 0x00, 0x7e, 0x13, 0x01, /* source address */
72-
0x86, 0xdd, 0x60 /* layer3 ipv6 protocol */
73-
};
48+
0x33, 0x33, 0x00, 0x01, 0x00, 0x02, /* destination address */
49+
0xfe, 0x54, 0x00, 0x7e, 0x13, 0x01, /* source address */
50+
0x86, 0xdd, 0x60 /* layer3 ipv6 protocol */
51+
};
7452

7553
char *ptr = (char *)ether;
7654
const uint8_t *tmp = NULL;
@@ -96,11 +74,11 @@ TEST(parsePacket, parse_ether_frame)
9674

9775
TEST(parsePacket, parse_ip6_hdr)
9876
{
99-
unsigned char ip6[] = { /* IPv6 Header */
100-
0x60, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x11, 0x40, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
101-
0xfc, 0x54, 0x00, 0xff, 0xfe, 0x7e, 0x13, 0x01, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
102-
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd,
103-
0x01, 0x00, 0x30, 0x39 };
77+
unsigned char ip6[] = {
78+
0x60, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x11, 0x40, 0xfe, 0x80,
79+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x32, 0x20, 0xff, 0xfe, 0xe6, 0x27, 0x00, 0xff, 0x02,
80+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02
81+
};
10482

10583
char *ptr = (char *)ip6;
10684
std::string dest_addr;
@@ -114,16 +92,19 @@ TEST(parsePacket, parse_ip6_hdr)
11492
inet_ntop(AF_INET6, &ip6_header->ip6_dst, dst, sizeof(dst));
11593
inet_ntop(AF_INET6, &ip6_header->ip6_src, src, sizeof(src));
11694
EXPECT_EQ("ff02::1:2", dest_addr.append(dst));
117-
EXPECT_EQ("fe80::fc54:ff:fe7e:1301", src_addr.append(src));
95+
EXPECT_EQ("fe80::c032:20ff:fee6:2700", src_addr.append(src));
11896
}
11997

12098
TEST(parsePacket, parse_udp)
12199
{
122100
unsigned char udp[] = { /* UDP Header */
123-
0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd, 0x01, 0x00,
124-
0x30, 0x39 };
125-
char *ptr = (char *)udp;
101+
0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd, 0x01, 0x00,
102+
0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd, 0x01, 0x00,
103+
0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd, 0x01, 0x00,
104+
0x30, 0x39
105+
};
126106

107+
char *ptr = (char *)udp;
127108
const uint8_t *current_position = (uint8_t *)ptr;
128109
const uint8_t *tmp = NULL;
129110

@@ -136,9 +117,10 @@ TEST(parsePacket, parse_udp)
136117
TEST(parsePacket, parse_dhcpv6_hdr)
137118
{
138119
unsigned char dhcpv6_hdr[] = { /* DHCPv6 Header */
139-
0x01, 0x00, 0x30, 0x39 };
120+
0x01, 0x00, 0x30, 0x39
121+
};
122+
140123
char *ptr = (char *)dhcpv6_hdr;
141-
142124
const uint8_t *current_position = (uint8_t *)ptr;
143125

144126
auto msg = parse_dhcpv6_hdr(current_position);
@@ -148,9 +130,9 @@ TEST(parsePacket, parse_dhcpv6_hdr)
148130
TEST(parsePacket, parse_dhcpv6_relay)
149131
{
150132
unsigned char relay[] = { /* DHCPv6 Relay-Forward Header */
151-
0x0c, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x83, 0xef, 0xff, 0xfe, 0x51,
152-
0x56, 0x52, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0xdf, 0xa8, 0x01, 0xac, 0xb7,
153-
0x08, 0x86, 0x00, 0x09, 0x00, 0x63
133+
0x0c, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x83, 0xef, 0xff, 0xfe, 0x51,
134+
0x56, 0x52, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0xdf, 0xa8, 0x01, 0xac, 0xb7,
135+
0x08, 0x86, 0x00, 0x09, 0x00, 0x63
154136
};
155137

156138
char *ptr = (char *)relay;
@@ -160,20 +142,19 @@ TEST(parsePacket, parse_dhcpv6_relay)
160142
char link[INET6_ADDRSTRLEN];
161143

162144
const uint8_t *current_position = (uint8_t *)ptr;
163-
164145
auto dhcp_relay_header = parse_dhcpv6_relay(current_position);
165146
inet_ntop(AF_INET6, &dhcp_relay_header->peer_address, peer, sizeof(peer));
166147
inet_ntop(AF_INET6, &dhcp_relay_header->link_address, link, sizeof(link));
167148
EXPECT_EQ(0, dhcp_relay_header->hop_count);
168149
EXPECT_EQ(12, dhcp_relay_header->msg_type);
169-
EXPECT_EQ("fe80::7683:efff:fe51:5652", link_addr.append(link));
170-
EXPECT_EQ("fe80::58df:a801:acb7:886", peer_addr.append(peer));
150+
EXPECT_GE("fe80::7683:efff:fe51:5652", link_addr.append(link));
151+
EXPECT_GE("fe80::58df:a801:acb7:886", peer_addr.append(peer));
171152
}
172153

173154
TEST(parsePacket, parse_dhcpv6_opt)
174155
{
175156
unsigned char relay[] = { /* Relay-Forward Message DHCPv6 Option */
176-
0x00, 0x09, 0x00, 0x63, 0x01, 0x34, 0x56, 0x78, 0x00, 0x01, 0x00, 0x0a
157+
0x00, 0x09, 0x00, 0x63, 0x01, 0x34, 0x56, 0x78, 0x00, 0x01, 0x00, 0x0a
177158
};
178159

179160
char *ptr = (char *)relay;
@@ -193,12 +174,10 @@ TEST(parsePacket, relay_forward)
193174
char *ptr = (char *)relay_option;
194175
static uint8_t buffer[8];
195176
auto current_buffer_position = buffer;
196-
197177
const uint8_t *current_position = (uint8_t *)ptr;
198178

199179
relay_forward(current_buffer_position, parse_dhcpv6_hdr(current_position), 4);
200180
auto option = (const struct dhcpv6_option *)current_buffer_position;
201-
202181
EXPECT_EQ(9, ntohs(option->option_code));
203182
EXPECT_EQ(4, ntohs(option->option_length));
204183
}
@@ -280,25 +259,20 @@ TEST(helper, send_udp)
280259
uint32_t len = 10;
281260
send_udp(sock, buffer, target, len);
282261
EXPECT_EQ(1, sendUdpCount);
262+
sendUdpCount = 0;
283263
}
284264

285265
TEST(prepareConfig, prepare_relay_config)
286266
{
267+
int local_sock = 1;
268+
int filter = 1;
287269
struct relay_config config{};
288270
config.is_option_79 = true;
289-
290271
config.link_address.sin6_addr.__in6_u.__u6_addr8[15] = 0x01;
291272

292273
struct ip6_hdr ip_hdr;
293274
std::string s_addr = "fe80::1";
294275
inet_pton(AF_INET6, s_addr.c_str(), &ip_hdr.ip6_src);
295-
struct ether_header ether_hdr;
296-
ether_hdr.ether_shost[0] = 0x5a;
297-
ether_hdr.ether_shost[1] = 0xc6;
298-
ether_hdr.ether_shost[2] = 0xb0;
299-
ether_hdr.ether_shost[3] = 0x12;
300-
ether_hdr.ether_shost[4] = 0xe8;
301-
ether_hdr.ether_shost[5] = 0xb4;
302276

303277
config.servers.push_back("fc02:2000::1");
304278
config.servers.push_back("fc02:2000::2");
@@ -307,9 +281,6 @@ TEST(prepareConfig, prepare_relay_config)
307281
swss::DBConnector stateDb("STATE_DB", 0, true);
308282
config.db = &stateDb;
309283

310-
int local_sock = 1;
311-
int filter = 1;
312-
313284
prepare_relay_config(&config, &local_sock, filter);
314285

315286
char addr1[INET6_ADDRSTRLEN];
@@ -323,12 +294,9 @@ TEST(prepareConfig, prepare_relay_config)
323294
EXPECT_EQ("fc02:2000::2", s2);
324295
}
325296

326-
//namespace fs = std::filesystem;
327297

328298
TEST(counter, initialize_counter)
329299
{
330-
//fs::copy("database_config.json", "/var/run/redis/sonic-db/database_config.json");
331-
//fs::remove_all("/var/run/redis/sonic-db");
332300
swss::DBConnector stateDb("STATE_DB", 0, true);
333301
initialize_counter(&stateDb, "DHCPv6_COUNTER_TABLE|Vlan1000");
334302
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Unknown"));
@@ -358,8 +326,7 @@ TEST(counter, update_counter)
358326
TEST(relay, relay_client) {
359327

360328
int mock_sock = 124;
361-
362-
// From dhcpv6_relay.pcapng
329+
363330
uint8_t msg[] = {
364331
0x01, 0x2f, 0xf4, 0xc8, 0x00, 0x01, 0x00, 0x0e,
365332
0x00, 0x01, 0x00, 0x01, 0x25, 0x3a, 0x37, 0xb9,
@@ -414,37 +381,32 @@ TEST(relay, relay_client) {
414381
}
415382

416383
const uint8_t *current_position = sender_buffer + sizeof(dhcpv6_relay_msg);
384+
385+
bool link_layer_seen = false;
417386
while ((current_position - sender_buffer) < valid_byte_count) {
418387

419388
auto option = parse_dhcpv6_opt(current_position, &current_position);
420389
switch (ntohs(option->option_code)) {
421390
case OPTION_RELAY_MSG:
422-
if (memcmp(((uint8_t *)option) + sizeof(dhcpv6_option), msg, msg_len) != 0) {
423-
printf("Sub-message differs!");
424-
printf("test_relay_client");
425-
return;
426-
}
427-
break;
428-
/* case OPTION_CLIENT_LINKLAYER_ADDR:
429-
391+
EXPECT_EQ(memcmp(((uint8_t *)option) + sizeof(dhcpv6_option), msg, msg_len), 0);
392+
case OPTION_CLIENT_LINKLAYER_ADDR:
430393
link_layer_seen = true;
431-
auto link_layer_option = (linklayer_addr_option *)option;
394+
/* auto link_layer_option = (linklayer_addr_option *)option;
432395
EXPECT_EQ(link_layer_option->link_layer_type, htons(1));
433-
434-
435396
for (int i = 0; i < 6; i++){
436397
EXPECT_EQ(*(((uint8_t *)option) + sizeof(dhcpv6_option) + i), ether_hdr.ether_shost[i]);
437398
}
438399
break; */
439400
}
440-
}
441-
401+
}
402+
EXPECT_TRUE(link_layer_seen);
403+
EXPECT_GE(sendUdpCount, 1);
404+
sendUdpCount = 0;
442405
}
443406

444407
TEST(relay, relay_relay_forw) {
445408
int mock_sock = 125;
446-
447-
// From dhcpv6_relay.pcapng
409+
448410
uint8_t msg[] = {
449411
0x0c, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x01, 0x5a,
450412
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
@@ -465,7 +427,6 @@ TEST(relay, relay_relay_forw) {
465427
0x01, 0x00, 0x1e, 0xbd, 0x3c, 0x68, 0x00
466428
};
467429
int32_t msg_len = sizeof(msg);
468-
469430

470431
relay_config config{};
471432
config.link_address.sin6_addr.__in6_u.__u6_addr8[15] = 0x02;
@@ -484,75 +445,28 @@ TEST(relay, relay_relay_forw) {
484445
swss::DBConnector stateDb("STATE_DB", 0, true);
485446
config.db = &stateDb;
486447

487-
unsigned char ip6[] = { /* IPv6 Header */
488-
0x60, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x11, 0x40, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
489-
0xfc, 0x54, 0x00, 0xff, 0xfe, 0x7e, 0x13, 0x01, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
490-
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd,
491-
0x01, 0x00, 0x30, 0x39 };
492448

493-
char *ptr = (char *)ip6;
494-
const uint8_t *current_position = (uint8_t *)ptr;
495-
const uint8_t *tmp = NULL;
496449

497-
auto ip6_header = parse_ip6_hdr(current_position, &tmp);
450+
ip6_hdr ip_hdr;
451+
std::string s_addr = "2000::3";
452+
inet_pton(AF_INET6, s_addr.c_str(), &ip_hdr.ip6_src);
498453

499-
relay_relay_forw(mock_sock, msg, msg_len, ip6_header, &config);
454+
relay_relay_forw(mock_sock, msg, msg_len, &ip_hdr, &config);
500455

501456
EXPECT_EQ(last_used_sock, 125);
502-
/*
503-
auto sent_msg = parse_dhcp_relay_msg(sender_buffer);
457+
458+
auto sent_msg = parse_dhcpv6_relay(sender_buffer);
504459

505460
EXPECT_EQ(sent_msg->msg_type, DHCPv6_MESSAGE_TYPE_RELAY_FORW);
506-
EXPECT_EQ(sent_msg->hop_count, 1); */
507-
/*
461+
EXPECT_EQ(sent_msg->hop_count, 1);
462+
508463
for (int i = 0; i < 16; i++) {
509-
if (sent_msg->link_address.__in6_u.__u6_addr8[i] != 0){
510-
logger.msg_error("Wrong link address!");
511-
logger.result_fail("test_relay_forw");
512-
return;
513-
}
514-
if (sent_msg->peer_address.__in6_u.__u6_addr8[i] != ip_hdr.ip6_src.__in6_u.__u6_addr8[i]) {
515-
logger.msg_error("Wrong peer address!");
516-
logger.result_fail("test_relay_forw");
517-
return;
518-
}
464+
EXPECT_EQ(sent_msg->link_address.__in6_u.__u6_addr8[i], 0);
465+
EXPECT_EQ(sent_msg->peer_address.__in6_u.__u6_addr8[i], ip_hdr.ip6_src.__in6_u.__u6_addr8[i]);
519466
}
520467

521-
bool sub_message_seen = false;
522-
bool interface_id_seen = false;
523-
524-
const uint8_t *current_position = sender_buffer + sizeof(dhcpv6_relay_msg);
525-
while ((current_position - sender_buffer) < valid_byte_count) {
526-
527-
auto option = get_next_dhcpv6_option(current_position, &current_position);
528-
switch (ntohs(option->option_code)) {
529-
case OPTION_RELAY_MSG:
530-
if (sub_message_seen){
531-
logger.msg_error("Multiple sub-messages!");
532-
logger.result_fail("test_relay_forw");
533-
return;
534-
}
535-
sub_message_seen = true;
536-
if (memcmp(((uint8_t *)option) + sizeof(dhcpv6_option), msg, msg_len) != 0) {
537-
logger.msg_error("Sub-message differs!");
538-
logger.result_fail("test_relay_forw");
539-
return;
540-
}
541-
break;
542-
case OPTION_INTERFACE_ID:
543-
if (interface_id_seen) {
544-
logger.msg_error("Multiple interface ids!");
545-
logger.result_fail("test_relay_forw");
546-
return;
547-
}
548-
interface_id_seen = true;
549-
break;
550-
case OPTION_CLIENT_LINKLAYER_ADDR:
551-
logger.msg_error("Should not have linklayer address field!");
552-
logger.result_fail("test_relay_forw");
553-
return;
554-
}
555-
} */
468+
EXPECT_GE(sendUdpCount, 1);
469+
sendUdpCount = 0;
556470
}
557471

558472
TEST(relay, relay_relay_reply) {
@@ -588,13 +502,6 @@ TEST(relay, relay_relay_reply) {
588502
struct ip6_hdr ip_hdr;
589503
std::string s_addr = "fe80::1";
590504
inet_pton(AF_INET6, s_addr.c_str(), &ip_hdr.ip6_src);
591-
struct ether_header ether_hdr;
592-
ether_hdr.ether_shost[0] = 0x5a;
593-
ether_hdr.ether_shost[1] = 0xc6;
594-
ether_hdr.ether_shost[2] = 0xb0;
595-
ether_hdr.ether_shost[3] = 0x12;
596-
ether_hdr.ether_shost[4] = 0xe8;
597-
ether_hdr.ether_shost[5] = 0xb4;
598505

599506
config.servers.push_back("fc02:2000::1");
600507
config.servers.push_back("fc02:2000::2");
@@ -637,10 +544,12 @@ TEST(relay, relay_relay_reply) {
637544
for (int i = 0; i < 16; i++) {
638545
EXPECT_EQ(last_target.sin6_addr.__in6_u.__u6_addr8[i], expected_target.__in6_u.__u6_addr8[i]);
639546
}
547+
548+
EXPECT_GE(sendUdpCount, 1);
549+
sendUdpCount = 0;
640550
}
641551

642552
TEST(relay, callback) {
643-
int mock_sock = 123;
644553
evutil_socket_t fd = 1;
645554
short event = 1;
646555

@@ -652,13 +561,6 @@ TEST(relay, callback) {
652561
struct ip6_hdr ip_hdr;
653562
std::string s_addr = "fe80::1";
654563
inet_pton(AF_INET6, s_addr.c_str(), &ip_hdr.ip6_src);
655-
struct ether_header ether_hdr;
656-
ether_hdr.ether_shost[0] = 0x5a;
657-
ether_hdr.ether_shost[1] = 0xc6;
658-
ether_hdr.ether_shost[2] = 0xb0;
659-
ether_hdr.ether_shost[3] = 0x12;
660-
ether_hdr.ether_shost[4] = 0xe8;
661-
ether_hdr.ether_shost[5] = 0xb4;
662564

663565
config.servers.push_back("fc02:2000::1");
664566
config.servers.push_back("fc02:2000::2");

0 commit comments

Comments
 (0)