Skip to content

Commit

Permalink
net: tests: Migrate DHCPv4 tests
Browse files Browse the repository at this point in the history
Migrate DHCPv4 tests to new net_pkt API.

Signed-off-by: Ravi kumar Veeramally <[email protected]>
  • Loading branch information
rveerama1 authored and nashif committed Feb 26, 2019
1 parent 985c5d7 commit c4f2ab1
Showing 1 changed file with 79 additions and 196 deletions.
275 changes: 79 additions & 196 deletions tests/net/dhcpv4/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ LOG_MODULE_REGISTER(net_test, CONFIG_NET_DHCPV4_LOG_LEVEL);
#include <net/dhcpv4.h>
#include <net/ethernet.h>
#include <net/net_mgmt.h>
#include <net/udp.h>
#include <net/dummy.h>

#include "ipv4.h"
#include "udp_internal.h"

#include <tc_util.h>
#include <ztest.h>

Expand Down Expand Up @@ -138,10 +140,6 @@ static const unsigned char ack[382] = {
0x56, 0x8f, 0xb6, 0xfa, 0x69, 0xff
};

static const struct net_eth_addr src_addr = {
{ 0x00, 0x00, 0x5E, 0x00, 0x53, 0x01 } };
static const struct net_eth_addr dst_addr = {
{ 0x00, 0x00, 0x5E, 0x00, 0x53, 0x02 } };
static const struct in_addr server_addr = { { { 192, 0, 2, 1 } } };
static const struct in_addr client_addr = { { { 255, 255, 255, 255 } } };

Expand All @@ -156,12 +154,14 @@ struct dhcp_msg {
u8_t type;
};

static struct k_sem test_lock;

static void test_result(bool pass)
{
if (pass) {
TC_END_REPORT(TC_PASS);
} else {
TC_END_REPORT(TC_PASS);
TC_END_REPORT(TC_FAIL);
}
}

Expand Down Expand Up @@ -203,128 +203,40 @@ static void net_dhcpv4_iface_init(struct net_if *iface)
net_if_set_link_addr(iface, mac, 6, NET_LINK_ETHERNET);
}

static struct net_buf *pkt_get_data(struct net_pkt *pkt, struct net_if *iface)
{
struct net_buf *frag;
struct net_eth_hdr *hdr;

frag = net_pkt_get_frag(pkt, K_FOREVER);
if (!frag) {
return NULL;
}

net_buf_add(frag, sizeof(struct net_eth_hdr));
net_pkt_frag_add(pkt, frag);

hdr = (struct net_eth_hdr *)(net_pkt_data(pkt));
hdr->type = htons(NET_ETH_PTYPE_IP);

net_ipaddr_copy(&hdr->dst, &src_addr);
net_ipaddr_copy(&hdr->src, &dst_addr);

return frag;
}

static void set_ipv4_header(struct net_pkt *pkt)
{
struct net_ipv4_hdr *ipv4;
u16_t length;

ipv4 = NET_IPV4_HDR(pkt);

ipv4->vhl = 0x45; /* IP version and header length */
ipv4->tos = 0x00;

length = sizeof(offer) + sizeof(struct net_ipv4_hdr) +
sizeof(struct net_udp_hdr);

ipv4->len = htons(length);

(void)memset(ipv4->id, 0, 4); /* id and offset */

ipv4->ttl = 0xFF;
ipv4->proto = IPPROTO_UDP;

net_ipaddr_copy(&ipv4->src, &server_addr);
net_ipaddr_copy(&ipv4->dst, &client_addr);
}

static void set_udp_header(struct net_pkt *pkt)
{
struct net_udp_hdr *udp;
u16_t length;

udp = (struct net_udp_hdr *)((u8_t *)(NET_IPV4_HDR(pkt)) +
sizeof(struct net_ipv4_hdr));

udp->src_port = htons(SERVER_PORT);
udp->dst_port = htons(CLIENT_PORT);

length = sizeof(offer) + sizeof(struct net_udp_hdr);
udp->len = htons(length);
udp->chksum = 0;
}

struct net_pkt *prepare_dhcp_offer(struct net_if *iface, u32_t xid)
{
struct net_pkt *pkt;
struct net_buf *frag;
int bytes, remaining = sizeof(offer), pos = 0;
u16_t offset;

pkt = net_pkt_get_reserve_rx(K_FOREVER);
if (!pkt) {
return NULL;
}

frag = pkt_get_data(pkt, iface);
if (!frag) {
net_pkt_unref(pkt);
return NULL;
}

net_pkt_set_iface(pkt, iface);
net_pkt_set_family(pkt, AF_INET);
net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr));

/* Place the IPv4 header */
set_ipv4_header(pkt);

/* Place the UDP header */
set_udp_header(pkt);

net_buf_add(frag, NET_IPV4UDPH_LEN);
offset = NET_IPV4UDPH_LEN;
pkt = net_pkt_alloc_with_buffer(iface,
sizeof(struct net_ipv4_hdr) +
sizeof(struct net_udp_hdr) +
sizeof(offer), AF_INET,
IPPROTO_UDP, K_FOREVER);

while (remaining > 0) {
int copy;
net_pkt_set_ipv4_ttl(pkt, 0xFF);

bytes = net_buf_tailroom(frag);
copy = remaining > bytes ? bytes : remaining;
if (net_ipv4_create_new(pkt, &server_addr, &client_addr) ||
net_udp_create(pkt, htons(SERVER_PORT), htons(CLIENT_PORT))) {
goto fail;
}

memcpy(frag->data + offset, &offer[pos], copy);
if (net_pkt_write_new(pkt, offer, 4)) {
goto fail;
}

net_buf_add(frag, copy);
/* Update xid from the client request */
if (net_pkt_write_be32_new(pkt, xid)) {
goto fail;
}

pos += bytes;
remaining -= bytes;
if (net_pkt_write_new(pkt, offer + 8, sizeof(offer) - 8)) {
goto fail;
}

if (remaining > 0) {
frag = pkt_get_data(pkt, iface);
if (!frag) {
goto fail;
}
net_pkt_cursor_init(pkt);

offset = 0U;
net_pkt_frag_add(pkt, frag);
}
}
net_ipv4_finalize(pkt, IPPROTO_UDP);

/* Now fixup the expect XID */
frag = net_pkt_write_be32(pkt, pkt->frags,
(sizeof(struct net_ipv4_hdr) +
sizeof(struct net_udp_hdr)) + 4,
&offset, xid);
return pkt;

fail:
Expand All @@ -335,63 +247,37 @@ struct net_pkt *prepare_dhcp_offer(struct net_if *iface, u32_t xid)
struct net_pkt *prepare_dhcp_ack(struct net_if *iface, u32_t xid)
{
struct net_pkt *pkt;
struct net_buf *frag;
int bytes, remaining = sizeof(ack), pos = 0;
u16_t offset;

pkt = net_pkt_get_reserve_rx(K_FOREVER);
if (!pkt) {
return NULL;
}
pkt = net_pkt_alloc_with_buffer(iface,
sizeof(struct net_ipv4_hdr) +
sizeof(struct net_udp_hdr) +
sizeof(offer), AF_INET,
IPPROTO_UDP, K_FOREVER);

frag = pkt_get_data(pkt, iface);
if (!frag) {
net_pkt_unref(pkt);
return NULL;
}
net_pkt_set_ipv4_ttl(pkt, 0xFF);

net_pkt_set_iface(pkt, iface);
net_pkt_set_family(pkt, AF_INET);
net_pkt_set_ip_hdr_len(pkt, sizeof(struct net_ipv4_hdr));

/* Place the IPv4 header */
set_ipv4_header(pkt);

/* Place the UDP header */
set_udp_header(pkt);

net_buf_add(frag, NET_IPV4UDPH_LEN);
offset = NET_IPV4UDPH_LEN;

while (remaining > 0) {
int copy;

bytes = net_buf_tailroom(frag);
copy = remaining > bytes ? bytes : remaining;
if (net_ipv4_create_new(pkt, &server_addr, &client_addr) ||
net_udp_create(pkt, htons(SERVER_PORT), htons(CLIENT_PORT))) {
goto fail;
}

memcpy(frag->data + offset, &ack[pos], copy);
if (net_pkt_write_new(pkt, ack, 4)) {
goto fail;
}

net_buf_add(frag, copy);
/* Update xid from the client request */
if (net_pkt_write_be32_new(pkt, xid)) {
goto fail;
}

pos += bytes;
remaining -= bytes;
if (net_pkt_write_new(pkt, ack + 8, sizeof(ack) - 8)) {
goto fail;
}

if (remaining > 0) {
frag = pkt_get_data(pkt, iface);
if (!frag) {
goto fail;
}
net_pkt_cursor_init(pkt);

offset = 0U;
net_pkt_frag_add(pkt, frag);
}
}
net_ipv4_finalize(pkt, IPPROTO_UDP);

/* Now fixup the expect XID */
frag = net_pkt_write_be32(pkt, pkt->frags,
(sizeof(struct net_ipv4_hdr) +
sizeof(struct net_udp_hdr)) + 4,
&offset, xid);
return pkt;

fail:
Expand All @@ -401,59 +287,53 @@ struct net_pkt *prepare_dhcp_ack(struct net_if *iface, u32_t xid)

static int parse_dhcp_message(struct net_pkt *pkt, struct dhcp_msg *msg)
{
struct net_buf *frag = pkt->frags;
u8_t type;
u16_t offset;
net_pkt_cursor_init(pkt);

frag = net_frag_skip(frag, 0, &offset,
/* size of op, htype, hlen, hops */
(sizeof(struct net_ipv4_hdr) +
sizeof(struct net_udp_hdr)) + 4);
if (!frag) {
/* Skip IPv4 and UDP headers */
if (net_pkt_skip(pkt, NET_IPV4UDPH_LEN)) {
return 0;
}

frag = net_frag_read_be32(frag, offset, &offset, &msg->xid);
if (!frag) {
/* Skip DHCPv4 headers (size of op, htype, hlen, hops) */
if (net_pkt_skip(pkt, 4)) {
return 0;
}

frag = net_frag_skip(frag, offset, &offset,
/* size of op, htype ... cookie */
(36 + 64 + 128 + 4));
if (!frag) {
if (net_pkt_read_be32_new(pkt, &msg->xid)) {
return 0;
}

while (frag) {
u8_t length;
/* Skip DHCPv4 Options (size of op, htype, ... cookie) */
if (net_pkt_skip(pkt, 36 + 64 + 128 + 4)) {
return 0;
}

frag = net_frag_read_u8(frag, offset, &offset, &type);
if (!frag) {
while (1) {
u8_t length = 0U;
u8_t type;

if (net_pkt_read_u8_new(pkt, &type)) {
return 0;
}

if (type == MSG_TYPE) {
frag = net_frag_skip(frag, offset, &offset, 1);
if (!frag) {
if (net_pkt_skip(pkt, 1)) {
return 0;
}

frag = net_frag_read_u8(frag, offset, &offset,
&msg->type);
if (!frag) {
if (net_pkt_read_u8_new(pkt, &msg->type)) {
return 0;
}

return 1;
}

frag = net_frag_read_u8(frag, offset, &offset, &length);
if (frag) {
frag = net_frag_skip(frag, offset, &offset, length);
if (!frag) {
return 0;
}
if (net_pkt_read_u8_new(pkt, &length)) {
return 0;
}

if (length && net_pkt_skip(pkt, length)) {
return 0;
}
}

Expand Down Expand Up @@ -487,7 +367,6 @@ static int tester_send(struct device *dev, struct net_pkt *pkt)
if (!rpkt) {
return -EINVAL;
}

} else {
/* Invalid message type received */
return -EINVAL;
Expand Down Expand Up @@ -520,8 +399,11 @@ static struct net_mgmt_event_callback rx_cb;
static void receiver_cb(struct net_mgmt_event_callback *cb,
u32_t nm_event, struct net_if *iface)
{
k_sem_give(&test_lock);

if (nm_event != NET_EVENT_IPV4_ADDR_ADD) {
/* Spurious callback. */
test_result(false);
return;
}

Expand All @@ -532,7 +414,8 @@ void test_dhcp(void)
{
struct net_if *iface;

k_thread_priority_set(k_current_get(), K_PRIO_COOP(7));
k_sem_init(&test_lock, 1, UINT_MAX);

net_mgmt_init_event_callback(&rx_cb, receiver_cb,
NET_EVENT_IPV4_ADDR_ADD);

Expand All @@ -546,7 +429,7 @@ void test_dhcp(void)

net_dhcpv4_start(iface);

k_yield();
k_sem_take(&test_lock, K_FOREVER);
}

/**test case main entry */
Expand Down

0 comments on commit c4f2ab1

Please sign in to comment.