Skip to content

Commit

Permalink
gnrc_mac: add unitest for mac internal.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhuoshuguo committed Nov 19, 2016
1 parent 45d2d28 commit dabe766
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 12 deletions.
6 changes: 3 additions & 3 deletions sys/include/net/gnrc/mac/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#define GNRC_MAC_INTERNAL_H_

#include <stdint.h>
#include <types.h>
#include <net/gnrc/mac/types.h>
#include <net/ieee802154.h>

#ifdef __cplusplus
Expand Down Expand Up @@ -59,8 +59,8 @@ void* _gnrc_pktbuf_find(gnrc_pktsnip_t* pkt, gnrc_nettype_t type);
static inline bool _packet_is_broadcast(gnrc_pktsnip_t* pkt)
{
gnrc_netif_hdr_t* netif_hdr = _gnrc_pktbuf_find(pkt, GNRC_NETTYPE_NETIF);
return ( (netif_hdr == NULL) ? false :
(netif_hdr->flags & (GNRC_NETIF_HDR_FLAGS_BROADCAST | GNRC_NETIF_HDR_FLAGS_MULTICAST)) );
return ((netif_hdr == NULL) ? false :
(netif_hdr->flags & (GNRC_NETIF_HDR_FLAGS_BROADCAST | GNRC_NETIF_HDR_FLAGS_MULTICAST)));
}

/* @brief Check whether the two given IEEE802.15.4 addresses match each other (are the same).
Expand Down
17 changes: 8 additions & 9 deletions sys/net/gnrc/link_layer/gnrc_mac/internal.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,18 @@ int _get_dest_address(gnrc_pktsnip_t* pkt, uint8_t* pointer_to_addr[])
if(!pkt)
return -ENODEV;

gnrc_pktsnip_t* netif_snip = _gnrc_pktbuf_find(pkt, GNRC_NETTYPE_NETIF);
netif_hdr = _gnrc_pktbuf_find(pkt, GNRC_NETTYPE_NETIF);

if(netif_hdr) {
if((res = netif_hdr->dst_l2addr_len) <= 0)
return -ENOENT;

*pointer_to_addr = gnrc_netif_hdr_get_dst_addr(netif_hdr);
return res;

if(netif_snip) {
netif_hdr = (gnrc_netif_hdr_t*) netif_snip->data;
} else {
return -ENOENT;
}

if((res = netif_hdr->dst_l2addr_len) <= 0)
return -ENOENT;

*pointer_to_addr = gnrc_netif_hdr_get_dst_addr(netif_hdr);
return res;
}

void* _gnrc_pktbuf_find(gnrc_pktsnip_t* pkt, gnrc_nettype_t type)
Expand Down
1 change: 1 addition & 0 deletions tests/unittests/tests-gnrc_mac_internal/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
1 change: 1 addition & 0 deletions tests/unittests/tests-gnrc_mac_internal/Makefile.include
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
USEMODULE += gnrc_mac
97 changes: 97 additions & 0 deletions tests/unittests/tests-gnrc_mac_internal/tests-gnrc_mac_internal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright (C) 2016, 2016 Shuguo Zhuo <shuguo.zhuo@inria.fr>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @{
*
* @file
*/
#include <string.h>

#include "embUnit.h"

#include "net/gnrc/pkt.h"
#include "net/gnrc/mac/internal.h"

#include "unittests-constants.h"
#include "tests-gnrc_mac_internal.h"

#define PKT_INIT_ELEM(len, data, next, type) \
{ 1, (next), (data), (len), type }
#define PKT_INIT_ELEM_STATIC_DATA(data, next, type) PKT_INIT_ELEM(sizeof(data), data, next, type)
#define PKTQUEUE_INIT_ELEM(pkt) { NULL, pkt }

static void set_up(void)
{
gnrc_pktbuf_init();
}

static void test_gnrc_pktbuf_find(void)
{
gnrc_pktsnip_t *pkt = gnrc_pktbuf_add(NULL, TEST_STRING4, sizeof(TEST_STRING4),
GNRC_NETTYPE_UNDEF);
pkt = gnrc_pktbuf_add(pkt, TEST_STRING8, sizeof(TEST_STRING8),
GNRC_NETTYPE_NETIF);
pkt = gnrc_pktbuf_add(pkt, TEST_STRING16, sizeof(TEST_STRING16),
GNRC_NETTYPE_IOVEC);

void* data;
data = _gnrc_pktbuf_find(pkt, GNRC_NETTYPE_IOVEC);
TEST_ASSERT_EQUAL_STRING(TEST_STRING16, data);

data = _gnrc_pktbuf_find(pkt, GNRC_NETTYPE_NETIF);
TEST_ASSERT_EQUAL_STRING(TEST_STRING8, data);

data = _gnrc_pktbuf_find(pkt, GNRC_NETTYPE_UNDEF);
TEST_ASSERT_EQUAL_STRING(TEST_STRING4, data);
}

static void test_get_dest_address(void)
{
gnrc_netif_hdr_t* netif_hdr;
uint8_t dst_addr[2];
uint8_t* add;
int addr_len2 = 0;
gnrc_pktsnip_t *pkt = gnrc_pktbuf_add(NULL, TEST_STRING4, sizeof(TEST_STRING4),
GNRC_NETTYPE_UNDEF);
pkt = gnrc_pktbuf_add(pkt, NULL, sizeof(gnrc_netif_hdr_t) + 2,
GNRC_NETTYPE_NETIF);
dst_addr[0] = 0x76;
dst_addr[1] = 0xb6;

netif_hdr = (gnrc_netif_hdr_t*) _gnrc_pktbuf_find(pkt, GNRC_NETTYPE_NETIF);
gnrc_netif_hdr_init(netif_hdr, 0, 2);
gnrc_netif_hdr_set_dst_addr(netif_hdr, dst_addr, 2);

addr_len2 = _get_dest_address(pkt,&add);

TEST_ASSERT(netif_hdr == pkt->data);
TEST_ASSERT(addr_len2 == 2);
TEST_ASSERT_EQUAL_STRING(TEST_STRING4, pkt->next->data);
TEST_ASSERT(add[0] == 0x76);
TEST_ASSERT(add[1] == 0xb6);
}


Test *tests_gnrc_mac_internal_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
new_TestFixture(test_gnrc_pktbuf_find),
new_TestFixture(test_get_dest_address),
};

EMB_UNIT_TESTCALLER(gnrc_mac_internal_tests, set_up, NULL, fixtures);

return (Test *)&gnrc_mac_internal_tests;
}

void tests_gnrc_mac_internal(void)
{
TESTS_RUN(tests_gnrc_mac_internal_tests());
}
/** @} */
37 changes: 37 additions & 0 deletions tests/unittests/tests-gnrc_mac_internal/tests-gnrc_mac_internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2016 Shuguo Zhuo <shuguo.zhuo@inria.fr>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/

/**
* @addtogroup unittests
* @{
*
* @file
* @brief Unittests for the ``gnrc_priority_pktqueue`` module
*
* @author Shuguo Zhuo <shuguo.zhuo@inria.fr>
*/
#ifndef TESTS_PRIORITY_PKTQUEUE_H_
#define TESTS_PRIORITY_PKTQUEUE_H_

#include "embUnit.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief The entry point of this test suite.
*/
void tests_gnrc_mac_internal(void);

#ifdef __cplusplus
}
#endif

#endif /* TESTS_PRIORITY_PKTQUEUE_H_ */
/** @} */

0 comments on commit dabe766

Please sign in to comment.