Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netdev: Provide test network device with netdev interface #1731

Merged
merged 2 commits into from
Oct 30, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion tests/unittests/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
APPLICATION = unittests
include ../Makefile.tests_common

BOARD_INSUFFICIENT_RAM := chronos msb-430 msb-430h redbee-econotag stm32f0discovery
BOARD_INSUFFICIENT_RAM := chronos msb-430 msb-430h redbee-econotag stm32f0discovery \
telosb wsn430-v1_3b wsn430-v1_4 z1

USEMODULE += embunit

Expand All @@ -18,6 +19,20 @@ else ifeq ($(OUTPUT),COMPILER)
USEMODULE += embunit_textui
endif

# Some randomly generated but still deterministic values for testing
CFLAGS += -DTEST_STRING8="\"o<\\\\rrB/q\""
CFLAGS += -DTEST_STRING12="\"50U'HLKC3_ft\""
CFLAGS += -DTEST_STRING16="\"nvxuO*6o3C=a6g7]\""
CFLAGS += -DTEST_STRING64="\"3key9'=4\\\\wvAM&;.E&U/rOG:>\\\\C!r{K k.E[%;(}_I(D%XQk8+T>_M^G'Mm49,n:))]}\""
CFLAGS += -DTEST_INT8=-69
CFLAGS += -DTEST_INT16=-12229
CFLAGS += -DTEST_INT32=-1894529023L
CFLAGS += -DTEST_INT64=-4562349816927799244LL
CFLAGS += -DTEST_UINT8=95U
CFLAGS += -DTEST_UINT16=10098U
CFLAGS += -DTEST_UINT32=2831907245LU
CFLAGS += -DTEST_UINT64=13500266397057512199LLU

ifeq (, $(filter tests-%, $(MAKECMDGOALS)))
UNIT_TESTS := $(foreach d,$(wildcard tests-*/Makefile),$(shell dirname $(d)))
else
Expand All @@ -29,11 +44,23 @@ DISABLE_MODULE += auto_init
# Pull in `Makefile.include`s from the test suites:
-include $(UNIT_TESTS:%=$(RIOTBASE)/tests/unittests/%/Makefile.include)

ifneq (,$(filter netdev_dummy,$(USEMODULE)))
USEMODULE += netdev_base
DIRS += netdev_dummy
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

netdev_dummy is a module that is only really useful or sensible in the context of unittests. So why not define it's dependencies in the Makefile of unittests?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it has no advantage and deviates from standard practice.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But: I overlooked, that the interface is actually part of the tests/unittests structure, so I'm fine with it staying as it is.

INCLUDES += -I$(RIOTBASE)/tests/unittests/netdev_dummy/include
endif

DIRS += $(UNIT_TESTS)
BASELIBS += $(UNIT_TESTS:%=$(BINDIR)%.a)

include $(RIOTBASE)/Makefile.include

ifeq (,$(filter msp430x16x,$(CPU)))
CFLAGS += -DTEST_INT=TEST_INT32
else
CFLAGS += -DTEST_INT=TEST_INT16
endif

.PHONY: FORCE $(UNIT_TESTS)

FORCE:
Expand Down
1 change: 1 addition & 0 deletions tests/unittests/netdev_dummy/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include $(RIOTBASE)/Makefile.base
160 changes: 160 additions & 0 deletions tests/unittests/netdev_dummy/include/netdev_dummy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright (C) 2014 Martin Lenders <mlenders@inf.fu-berlin.de>
*
* 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 netdev_dummy.h
* @brief Provides a virtual device that understands the netdev_base API
* for testing.
*
* @author Martine Lenders <mlenders@inf.fu-berlin.de>
*/

#ifndef __UNITTESTS_NETDEV_DUMMY_H_
#define __UNITTESTS_NETDEV_DUMMY_H_

#include <stdlib.h>

#include "netdev/base.h"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file does not have a C++ compatible header: 'tests/unittests/netdev_dummy/include/netdev_dummy.h'

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

#ifdef __cplusplus
extern "C" {
#endif

#ifndef UNITTESTS_NETDEV_DUMMY_MAX_DEV
/**
* @brief Maximum number of devices you can initialize with this module
*/
#define UNITTESTS_NETDEV_DUMMY_MAX_DEV (2)
#endif /* UNITTESTS_NETDEV_DUMMY_MAX_DEV */

#ifndef UNITTESTS_NETDEV_DUMMY_MAX_PACKET
/**
* @brief Maximum packet length for the virtual test devices.
*/
#define UNITTESTS_NETDEV_DUMMY_MAX_PACKET (12)
#endif /* UNITTESTS_NETDEV_DUMMY_MAX_PACKET */

#ifndef UNITTESTS_NETDEV_DUMMY_MAX_ADDR_LEN
/**
* @brief The maximum length of the addresses for the test devices
*/
#define UNITTESTS_NETDEV_DUMMY_MAX_ADDR_LEN (2)
#endif /* UNITTESTS_NETDEV_DUMMY_MAX_ADDR_LEN */

#ifndef UNITTESTS_NETDEV_DUMMY_MAX_LONG_ADDR_LEN
/**
* @brief The maximum length of the addresses for the test devices
*/
#define UNITTESTS_NETDEV_DUMMY_MAX_LONG_ADDR_LEN (4)
#endif /* UNITTESTS_NETDEV_DUMMY_MAX_LONG_ADDR_LEN */

#if UNITTESTS_NETDEV_DUMMY_MAX_LONG_ADDR_LEN < UNITTESTS_NETDEV_DUMMY_MAX_ADDR_LEN
#error "UNITTESTS_NETDEV_DUMMY_MAX_ADDR_LEN < UNITTESTS_NETDEV_DUMMY_MAX_ADDR_LEN!\n"
#endif

#if UNITTESTS_NETDEV_DUMMY_MAX_LONG_ADDR_LEN > 64
#warning "UNITTESTS_NETDEV_DUMMY_MAX_LONG_ADDR_LEN > 64 is not recommended"
#endif

#ifndef UNITTESTS_NETDEV_DUMMY_MAX_CB
/**
* @brief Maximum number of callbacks per device
*/
#define UNITTESTS_NETDEV_DUMMY_MAX_CB (4)
#endif /* UNITTESTS_NETDEV_DUMMY_MAX_CB */

/**
* @brief Implementation of the driver for the virtual test device
*/
extern const netdev_driver_t unittest_netdev_dummy_driver;

/**
* @brief Available devices
*/
extern netdev_t unittest_netdev_dummy_devs[UNITTESTS_NETDEV_DUMMY_MAX_DEV];

/**
* @brief Fires a receive event on a virtual test device
*
* @param[in] dev Device you want to fire the receive event on
* @param[in] src Source address of the received packet
* @param[in] src_len Length of *src*
* @param[in] dest Destination address of the received packet
* @param[in] dest_len Length of *dest*
* @param[in] data Content of the received packet
* @param[in] data_len Length of *data*
*
* @return 0 on success
* @return -EAFNOSUPPORT, if src_len or dest_len are neither
* UNITTESTS_NETDEV_DUMMY_MAX_LONG_ADDR_LEN nor
* UNITTESTS_NETDEV_DUMMY_MAX_ADDR_LEN
* @return -ECANCELED, receive event aborted since one of the registered
* callbacks had an error.
* @return -EINVAL, if *src* or *dest* are NULL or
* *data* is NULL and *data_len* is not NULL
* @return -EMSGSIZE, if *data_len* > UNITTESTS_NETDEV_DUMMY_MAX_PACKET
* @return -ENODEV, if device was not initialized by
* unittest_netdev_dummy_driver::init()
*/
int unittest_netdev_dummy_fire_rcv_event(netdev_t *dev, void *src,
size_t src_len, void *dest, size_t dest_len, void *data,
size_t data_len);

/**
* @brief Checks if the data transmitted by
* unittest_netdev_dummy_driver::send_data() is as expected.
*
* @param[in] dev Device you want to check the transmitted data for
* @param[in] expected_dest The expected destination address.
* @param[in] expected_dest_len The expected length of the destination address.
* @param[in] expected_data The expected data
* @param[in] expected_data_len The expected length of the data
*
* @note Please remember that the device prepends the *upper_layer_hdrs*
* passed to unittest_netdev_dummy_driver::send_data()
*
* @return 0 on success and everything is as expected
* @return 1 on success and something is not as expected
* @return -ENODEV, if *dev* was not initialized by
* unittest_netdev_dummy_driver::init()
* @return -EINVAL, if *expected_data* is NULL and *expected_data_len is not 0 or
* if
*/
int unittest_netdev_dummy_check_transmitted(netdev_t *dev,
void *expected_dest,
size_t expected_dest_len,
void *expected_data,
size_t expected_data_len);

/**
* @brief Get last event notified by dev::driver::event
*
* @param[in] dev Device you want to get the last event from.
*
* @return The last event as given as parameter to dev::driver::event
* @return ENODEV, if *dev* was not found
*/
uint32_t unittest_netdev_dummy_get_last_event(netdev_t *dev);

/**
* @brief Resets all dummy devices to their initial state
*/
void unittest_netdev_dummy_init(void);

#ifdef __cplusplus
}
#endif

#endif /* __UNITTESTS_NETDEV_DUMMY_H_ */

/**
* @}
*/
Loading