Skip to content

Commit

Permalink
Clean Up
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyyeh committed Oct 7, 2022
1 parent 67b7ac0 commit da42bf1
Showing 1 changed file with 54 additions and 152 deletions.
206 changes: 54 additions & 152 deletions test/MockRelay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,6 @@ const struct sock_fprog ether_relay_fprog = {
ether_relay_filter
};

/*
struct relay_config {
int local_sock;
int server_sock;
int filter;
sockaddr_in6 link_address;
swss::DBConnector *db;
std::string interface;
std::vector<std::string> servers;
std::vector<sockaddr_in6> servers_sock;
bool is_option_79;
}
*/

/* relay_config test_config;
test_config.is_option_79 = true;
test_config.local_sock = 0;
test_config.server_sock = 0;
sockaddr_in6 link;
inet_pton(AF_INET6, "fe80::7683:efff:fe51:5652", &link.sin6_addr);
*/


TEST(helper, toString)
{
Expand All @@ -67,10 +45,10 @@ TEST(helper, toString)
TEST(parsePacket, parse_ether_frame)
{
unsigned char ether[] = {
0x33, 0x33, 0x00, 0x01, 0x00, 0x02, /* destination address */
0xfe, 0x54, 0x00, 0x7e, 0x13, 0x01, /* source address */
0x86, 0xdd, 0x60 /* layer3 ipv6 protocol */
};
0x33, 0x33, 0x00, 0x01, 0x00, 0x02, /* destination address */
0xfe, 0x54, 0x00, 0x7e, 0x13, 0x01, /* source address */
0x86, 0xdd, 0x60 /* layer3 ipv6 protocol */
};

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

TEST(parsePacket, parse_ip6_hdr)
{
unsigned char ip6[] = { /* IPv6 Header */
0x60, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x11, 0x40, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xfc, 0x54, 0x00, 0xff, 0xfe, 0x7e, 0x13, 0x01, 0xff, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02, 0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd,
0x01, 0x00, 0x30, 0x39 };
unsigned char ip6[] = {
0x60, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x11, 0x40, 0xfe, 0x80,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x32, 0x20, 0xff, 0xfe, 0xe6, 0x27, 0x00, 0xff, 0x02,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x02
};

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

TEST(parsePacket, parse_udp)
{
unsigned char udp[] = { /* UDP Header */
0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd, 0x01, 0x00,
0x30, 0x39 };
char *ptr = (char *)udp;
0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd, 0x01, 0x00,
0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd, 0x01, 0x00,
0x02, 0x22, 0x02, 0x23, 0x00, 0x0c, 0xbd, 0xfd, 0x01, 0x00,
0x30, 0x39
};

char *ptr = (char *)udp;
const uint8_t *current_position = (uint8_t *)ptr;
const uint8_t *tmp = NULL;

Expand All @@ -136,9 +117,10 @@ TEST(parsePacket, parse_udp)
TEST(parsePacket, parse_dhcpv6_hdr)
{
unsigned char dhcpv6_hdr[] = { /* DHCPv6 Header */
0x01, 0x00, 0x30, 0x39 };
0x01, 0x00, 0x30, 0x39
};

char *ptr = (char *)dhcpv6_hdr;

const uint8_t *current_position = (uint8_t *)ptr;

auto msg = parse_dhcpv6_hdr(current_position);
Expand All @@ -148,9 +130,9 @@ TEST(parsePacket, parse_dhcpv6_hdr)
TEST(parsePacket, parse_dhcpv6_relay)
{
unsigned char relay[] = { /* DHCPv6 Relay-Forward Header */
0x0c, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x83, 0xef, 0xff, 0xfe, 0x51,
0x56, 0x52, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0xdf, 0xa8, 0x01, 0xac, 0xb7,
0x08, 0x86, 0x00, 0x09, 0x00, 0x63
0x0c, 0x00, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x76, 0x83, 0xef, 0xff, 0xfe, 0x51,
0x56, 0x52, 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x58, 0xdf, 0xa8, 0x01, 0xac, 0xb7,
0x08, 0x86, 0x00, 0x09, 0x00, 0x63
};

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

const uint8_t *current_position = (uint8_t *)ptr;

auto dhcp_relay_header = parse_dhcpv6_relay(current_position);
inet_ntop(AF_INET6, &dhcp_relay_header->peer_address, peer, sizeof(peer));
inet_ntop(AF_INET6, &dhcp_relay_header->link_address, link, sizeof(link));
EXPECT_EQ(0, dhcp_relay_header->hop_count);
EXPECT_EQ(12, dhcp_relay_header->msg_type);
EXPECT_EQ("fe80::7683:efff:fe51:5652", link_addr.append(link));
EXPECT_EQ("fe80::58df:a801:acb7:886", peer_addr.append(peer));
EXPECT_GE("fe80::7683:efff:fe51:5652", link_addr.append(link));
EXPECT_GE("fe80::58df:a801:acb7:886", peer_addr.append(peer));
}

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

char *ptr = (char *)relay;
Expand All @@ -193,12 +174,10 @@ TEST(parsePacket, relay_forward)
char *ptr = (char *)relay_option;
static uint8_t buffer[8];
auto current_buffer_position = buffer;

const uint8_t *current_position = (uint8_t *)ptr;

relay_forward(current_buffer_position, parse_dhcpv6_hdr(current_position), 4);
auto option = (const struct dhcpv6_option *)current_buffer_position;

EXPECT_EQ(9, ntohs(option->option_code));
EXPECT_EQ(4, ntohs(option->option_length));
}
Expand Down Expand Up @@ -280,25 +259,20 @@ TEST(helper, send_udp)
uint32_t len = 10;
send_udp(sock, buffer, target, len);
EXPECT_EQ(1, sendUdpCount);
sendUdpCount = 0;
}

TEST(prepareConfig, prepare_relay_config)
{
int local_sock = 1;
int filter = 1;
struct relay_config config{};
config.is_option_79 = true;

config.link_address.sin6_addr.__in6_u.__u6_addr8[15] = 0x01;

struct ip6_hdr ip_hdr;
std::string s_addr = "fe80::1";
inet_pton(AF_INET6, s_addr.c_str(), &ip_hdr.ip6_src);
struct ether_header ether_hdr;
ether_hdr.ether_shost[0] = 0x5a;
ether_hdr.ether_shost[1] = 0xc6;
ether_hdr.ether_shost[2] = 0xb0;
ether_hdr.ether_shost[3] = 0x12;
ether_hdr.ether_shost[4] = 0xe8;
ether_hdr.ether_shost[5] = 0xb4;

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

int local_sock = 1;
int filter = 1;

prepare_relay_config(&config, &local_sock, filter);

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

//namespace fs = std::filesystem;

TEST(counter, initialize_counter)
{
//fs::copy("database_config.json", "/var/run/redis/sonic-db/database_config.json");
//fs::remove_all("/var/run/redis/sonic-db");
swss::DBConnector stateDb("STATE_DB", 0, true);
initialize_counter(&stateDb, "DHCPv6_COUNTER_TABLE|Vlan1000");
EXPECT_TRUE(stateDb.hexists("DHCPv6_COUNTER_TABLE|Vlan1000", "Unknown"));
Expand Down Expand Up @@ -358,8 +326,7 @@ TEST(counter, update_counter)
TEST(relay, relay_client) {

int mock_sock = 124;

// From dhcpv6_relay.pcapng

uint8_t msg[] = {
0x01, 0x2f, 0xf4, 0xc8, 0x00, 0x01, 0x00, 0x0e,
0x00, 0x01, 0x00, 0x01, 0x25, 0x3a, 0x37, 0xb9,
Expand Down Expand Up @@ -414,37 +381,32 @@ TEST(relay, relay_client) {
}

const uint8_t *current_position = sender_buffer + sizeof(dhcpv6_relay_msg);

bool link_layer_seen = false;
while ((current_position - sender_buffer) < valid_byte_count) {

auto option = parse_dhcpv6_opt(current_position, &current_position);
switch (ntohs(option->option_code)) {
case OPTION_RELAY_MSG:
if (memcmp(((uint8_t *)option) + sizeof(dhcpv6_option), msg, msg_len) != 0) {
printf("Sub-message differs!");
printf("test_relay_client");
return;
}
break;
/* case OPTION_CLIENT_LINKLAYER_ADDR:
EXPECT_EQ(memcmp(((uint8_t *)option) + sizeof(dhcpv6_option), msg, msg_len), 0);
case OPTION_CLIENT_LINKLAYER_ADDR:
link_layer_seen = true;
auto link_layer_option = (linklayer_addr_option *)option;
/* auto link_layer_option = (linklayer_addr_option *)option;
EXPECT_EQ(link_layer_option->link_layer_type, htons(1));
for (int i = 0; i < 6; i++){
EXPECT_EQ(*(((uint8_t *)option) + sizeof(dhcpv6_option) + i), ether_hdr.ether_shost[i]);
}
break; */
}
}

}
EXPECT_TRUE(link_layer_seen);
EXPECT_GE(sendUdpCount, 1);
sendUdpCount = 0;
}

TEST(relay, relay_relay_forw) {
int mock_sock = 125;

// From dhcpv6_relay.pcapng

uint8_t msg[] = {
0x0c, 0x00, 0x20, 0x01, 0x0d, 0xb8, 0x01, 0x5a,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Expand All @@ -465,7 +427,6 @@ TEST(relay, relay_relay_forw) {
0x01, 0x00, 0x1e, 0xbd, 0x3c, 0x68, 0x00
};
int32_t msg_len = sizeof(msg);


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

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

char *ptr = (char *)ip6;
const uint8_t *current_position = (uint8_t *)ptr;
const uint8_t *tmp = NULL;

auto ip6_header = parse_ip6_hdr(current_position, &tmp);
ip6_hdr ip_hdr;
std::string s_addr = "2000::3";
inet_pton(AF_INET6, s_addr.c_str(), &ip_hdr.ip6_src);

relay_relay_forw(mock_sock, msg, msg_len, ip6_header, &config);
relay_relay_forw(mock_sock, msg, msg_len, &ip_hdr, &config);

EXPECT_EQ(last_used_sock, 125);
/*
auto sent_msg = parse_dhcp_relay_msg(sender_buffer);

auto sent_msg = parse_dhcpv6_relay(sender_buffer);

EXPECT_EQ(sent_msg->msg_type, DHCPv6_MESSAGE_TYPE_RELAY_FORW);
EXPECT_EQ(sent_msg->hop_count, 1); */
/*
EXPECT_EQ(sent_msg->hop_count, 1);

for (int i = 0; i < 16; i++) {
if (sent_msg->link_address.__in6_u.__u6_addr8[i] != 0){
logger.msg_error("Wrong link address!");
logger.result_fail("test_relay_forw");
return;
}
if (sent_msg->peer_address.__in6_u.__u6_addr8[i] != ip_hdr.ip6_src.__in6_u.__u6_addr8[i]) {
logger.msg_error("Wrong peer address!");
logger.result_fail("test_relay_forw");
return;
}
EXPECT_EQ(sent_msg->link_address.__in6_u.__u6_addr8[i], 0);
EXPECT_EQ(sent_msg->peer_address.__in6_u.__u6_addr8[i], ip_hdr.ip6_src.__in6_u.__u6_addr8[i]);
}

bool sub_message_seen = false;
bool interface_id_seen = false;
const uint8_t *current_position = sender_buffer + sizeof(dhcpv6_relay_msg);
while ((current_position - sender_buffer) < valid_byte_count) {
auto option = get_next_dhcpv6_option(current_position, &current_position);
switch (ntohs(option->option_code)) {
case OPTION_RELAY_MSG:
if (sub_message_seen){
logger.msg_error("Multiple sub-messages!");
logger.result_fail("test_relay_forw");
return;
}
sub_message_seen = true;
if (memcmp(((uint8_t *)option) + sizeof(dhcpv6_option), msg, msg_len) != 0) {
logger.msg_error("Sub-message differs!");
logger.result_fail("test_relay_forw");
return;
}
break;
case OPTION_INTERFACE_ID:
if (interface_id_seen) {
logger.msg_error("Multiple interface ids!");
logger.result_fail("test_relay_forw");
return;
}
interface_id_seen = true;
break;
case OPTION_CLIENT_LINKLAYER_ADDR:
logger.msg_error("Should not have linklayer address field!");
logger.result_fail("test_relay_forw");
return;
}
} */
EXPECT_GE(sendUdpCount, 1);
sendUdpCount = 0;
}

TEST(relay, relay_relay_reply) {
Expand Down Expand Up @@ -588,13 +502,6 @@ TEST(relay, relay_relay_reply) {
struct ip6_hdr ip_hdr;
std::string s_addr = "fe80::1";
inet_pton(AF_INET6, s_addr.c_str(), &ip_hdr.ip6_src);
struct ether_header ether_hdr;
ether_hdr.ether_shost[0] = 0x5a;
ether_hdr.ether_shost[1] = 0xc6;
ether_hdr.ether_shost[2] = 0xb0;
ether_hdr.ether_shost[3] = 0x12;
ether_hdr.ether_shost[4] = 0xe8;
ether_hdr.ether_shost[5] = 0xb4;

config.servers.push_back("fc02:2000::1");
config.servers.push_back("fc02:2000::2");
Expand Down Expand Up @@ -637,10 +544,12 @@ TEST(relay, relay_relay_reply) {
for (int i = 0; i < 16; i++) {
EXPECT_EQ(last_target.sin6_addr.__in6_u.__u6_addr8[i], expected_target.__in6_u.__u6_addr8[i]);
}

EXPECT_GE(sendUdpCount, 1);
sendUdpCount = 0;
}

TEST(relay, callback) {
int mock_sock = 123;
evutil_socket_t fd = 1;
short event = 1;

Expand All @@ -652,13 +561,6 @@ TEST(relay, callback) {
struct ip6_hdr ip_hdr;
std::string s_addr = "fe80::1";
inet_pton(AF_INET6, s_addr.c_str(), &ip_hdr.ip6_src);
struct ether_header ether_hdr;
ether_hdr.ether_shost[0] = 0x5a;
ether_hdr.ether_shost[1] = 0xc6;
ether_hdr.ether_shost[2] = 0xb0;
ether_hdr.ether_shost[3] = 0x12;
ether_hdr.ether_shost[4] = 0xe8;
ether_hdr.ether_shost[5] = 0xb4;

config.servers.push_back("fc02:2000::1");
config.servers.push_back("fc02:2000::2");
Expand Down

0 comments on commit da42bf1

Please sign in to comment.