Skip to content

Commit

Permalink
Mac neighbor table unit test's.
Browse files Browse the repository at this point in the history
Change-Id: If1de9f64133f361398249d72e519b2c29a3f5fd2
  • Loading branch information
Juha Heiskanen authored and juhhei01 committed Mar 1, 2018
1 parent 0f85841 commit ed45fda
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 0 deletions.
23 changes: 23 additions & 0 deletions test/nanostack/unittest/service_libs/mac_neighbor_table/Makefile
@@ -0,0 +1,23 @@
include ../../makefile_defines.txt

COMPONENT_NAME = mac_neighbor_table_unit

#This must be changed manually
SRC_FILES = \
../../../../../source/Service_Libs/mac_neighbor_table/mac_neighbor_table.c \

TEST_SRC_FILES = \
main.cpp \
mac_neighbor_table_test.cpp \
test_mac_neighbor_table.c \
../../stub/nsdynmemLIB_stub.c \
../../stub/mbed_trace_stub.c \
../../stub/protocol_core_stub.c \
../../stub/ns_list_stub.c \
../../stub/common_functions_stub.c \

include ../../MakefileWorker.mk

CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT


@@ -0,0 +1,41 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CppUTest/TestHarness.h"
#include "test_mac_neighbor_table.h"


TEST_GROUP(mac_neighbor_table)
{
void setup()
{

}

void teardown()
{
}
};

TEST(mac_neighbor_table, test_mac_neighbor_table_create)
{
CHECK(test_mac_neighbor_table_create());
}

TEST(mac_neighbor_table, test_mac_neighbor_table_use)
{
CHECK(test_mac_neighbor_table_use());
}
26 changes: 26 additions & 0 deletions test/nanostack/unittest/service_libs/mac_neighbor_table/main.cpp
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/TestPlugin.h"
#include "CppUTest/TestRegistry.h"
#include "CppUTestExt/MockSupportPlugin.h"
int main(int ac, char** av)
{
return CommandLineTestRunner::RunAllTests(ac, av);
}

IMPORT_TEST_GROUP(mac_neighbor_table);
@@ -0,0 +1,204 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "nsconfig.h"
#include "string.h"
#include "ns_types.h"
#include "ns_trace.h"
#include "common_functions.h"
#include "nsdynmemLIB.h"
#include "Service_Libs/mac_neighbor_table/mac_neighbor_table.h"
#include "nsdynmemLIB_stub.h"
#include "common_functions_stub.h"
#include "test_mac_neighbor_table.h"

/**
* Remove entry notify
*
* \param entry_ptr Pointer to removed entry
* \param user_data pointer for user to detect interface
*/
static void test_neighbor_entry_remove_notify(mac_neighbor_table_entry_t *entry_ptr, void *user_data)
{

}

static bool test_bool = true;
/**
* NUD entry notify
*
* \param entry_ptr Pointer to neighbor entry
* \param user_data pointer for user to detect interface
*
* \return true NUD message generated
* \return false When NUD is not generated
*/
static bool test_neighbor_entry_nud_notify(mac_neighbor_table_entry_t *entry_ptr, void *user_data)
{
return test_bool;
}


bool test_mac_neighbor_table_create()
{
nsdynmemlib_stub.returnCounter = 0;
mac_neighbor_table_t *entry = mac_neighbor_table_create(10, test_neighbor_entry_remove_notify, test_neighbor_entry_nud_notify, NULL, 200);

if (entry) {
return false;
}
nsdynmemlib_stub.returnCounter = 1;
entry = mac_neighbor_table_create(10, test_neighbor_entry_remove_notify, test_neighbor_entry_nud_notify, NULL, 200);

if (!entry) {
return false;
}

mac_neighbor_table_delete(entry);
return true;
}

bool test_mac_neighbor_table_use()
{
nsdynmemlib_stub.returnCounter = 1;
mac_neighbor_table_t *entry = mac_neighbor_table_create(10, test_neighbor_entry_remove_notify, test_neighbor_entry_nud_notify, NULL, 200);

if (!entry) {
return false;
}
uint8_t mac1[8];
uint8_t mac2[8];
uint8_t mac3[8];
uint8_t mac4[8];
memset(mac1, 0, 8);
memset(mac2, 1, 8);
memset(mac3, 2, 8);
memset(mac4, 5, 8);

mac_neighbor_table_entry_t * mac_entry1 = mac_neighbor_table_entry_allocate(entry, mac1);
if (!mac_entry1 || mac_entry1->index != 0) {
return false;
}

mac_neighbor_table_entry_t * mac_entry2 = mac_neighbor_table_entry_allocate(entry, mac2);
if (!mac_entry2 || mac_entry2->index != 1) {
return false;
}

mac_neighbor_table_entry_t *mac_entry3 = mac_neighbor_table_entry_allocate(entry, mac3);

if (!mac_entry3 || mac_entry3->index != 2) {
return false;
}

for (unsigned char i=0; i< 7; i++) {
mac_neighbor_table_entry_t *test = mac_neighbor_table_entry_allocate(entry, mac4);
if (!test || test->index != 3+i) {
return false;
}
mac4[0] += 1;
}

mac_neighbor_table_entry_t *test2 = mac_neighbor_table_entry_allocate(entry, mac4);
if (test2) {
return false;
}


mac_neighbor_table_neighbor_timeout_update(entry, 10);

if (mac_entry3->lifetime != 230 || mac_entry2->lifetime != 230 || mac_entry1->lifetime != 230) {
return false;
}

mac_neighbor_table_neighbor_connected(entry, mac_entry3);

if (!mac_entry3->connected_device) {
return false;
}

mac_neighbor_table_trusted_neighbor(entry, mac_entry3, true);

if (!mac_entry3->trusted_device) {
return false;
}

mac_neighbor_table_neighbor_timeout_update(entry, 31);

if (mac_entry3->lifetime != 199 || mac_entry2->lifetime != 199 || mac_entry1->lifetime != 199) {
return false;
}

mac_neighbor_table_neighbor_timeout_update(entry, 170);

if (mac_entry3->lifetime != 29 || mac_entry2->lifetime != 29 || mac_entry1->lifetime != 29) {
return false;
}

if (!mac_entry3->nud_active || !mac_entry2->nud_active || !mac_entry1->nud_active) {
return false;
}

mac_neighbor_table_neighbor_timeout_update(entry, 1);

mac_neighbor_table_neighbor_refresh(entry, mac_entry2, 79);
mac_neighbor_table_neighbor_refresh(entry, mac_entry1, 600);

mac_neighbor_table_neighbor_timeout_update(entry, 40);

if (!mac_entry2->nud_active) {
return false;
}

if (mac_neighbor_table_attribute_discover(entry, mac_entry3->index)) {
return false;
}

if (!mac_neighbor_table_attribute_discover(entry, mac_entry2->index)) {
return false;
}



if (!mac_neighbor_table_address_discover(entry, mac_entry1, 3) ) {
return false;
}

if (mac_neighbor_table_address_discover(entry, mac_entry1, 2) ) {
return false;
}
mac_entry2->mac16 = 1;
common_functions_stub.uint16_value = 1;

if (!mac_neighbor_table_address_discover(entry, mac_entry1, 2) ) {
return false;
}

if (mac_neighbor_table_address_discover(entry, mac_entry1, 4) ) {
return false;
}

mac_neighbor_table_neighbor_remove(entry, mac_entry2);

mac_neighbor_table_neighbor_remove(entry, mac_entry3);

mac_neighbor_table_neighbor_list_clean(entry);
mac_neighbor_table_delete(entry);
return true;
}


@@ -0,0 +1,32 @@
/*
* Copyright (c) 2018, Arm Limited and affiliates.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef TEST_MAC_NEIGHBOR_TABLE_H_
#define TEST_MAC_NEIGHBOR_TABLE_H_
#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
bool test_mac_neighbor_table_create();
bool test_mac_neighbor_table_use();

#ifdef __cplusplus
}
#endif

#endif /* TEST_MAC_NEIGHBOR_TABLE_H_ */

0 comments on commit ed45fda

Please sign in to comment.