Skip to content

Commit

Permalink
Merge pull request #357 from eduazocar/test/net_tools
Browse files Browse the repository at this point in the history
Test/net tools: Integration to net_tools test
  • Loading branch information
CW-75 authored Oct 7, 2022
2 parents b4b6cba + 035954a commit fd9a88c
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 23 deletions.
11 changes: 9 additions & 2 deletions firmware/network/net_tools/include/net_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
extern "C" {
#endif

#define BITS_IN_A_BYTE (8)
#define BITS_IN_A_BYTE (8)
#define PREFIX_LSHIFTTED_BITS (8)

/**
Expand Down Expand Up @@ -76,7 +76,14 @@ int8_t get_ipv6_global(kernel_pid_t iface_pid, ipv6_addr_t *addr);
* @retval -1 Couldn't set the @p ip address in the interface.
*/
int8_t set_ipv6_global(kernel_pid_t iface_index, ipv6_addr_t ip, uint8_t prefix);

/**
* @brief Remove the current global ipv6 in an interface.
*
* @param iface_idx interface index to removes the global ipv6 address.
* @retval 0 address removed successful.
* @retval -1 Doesn't exist a global ipv6 address.
*/
int8_t rm_ipv6_global(kernel_pid_t iface_idx);
/**
* @brief Function to set an ipv6 global address in an iface.
*
Expand Down
15 changes: 15 additions & 0 deletions firmware/network/net_tools/net_tools.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ int8_t set_ipv6_global(kernel_pid_t iface_index, ipv6_addr_t ip, uint8_t prefix)
return 0;
}

int8_t rm_ipv6_global(kernel_pid_t iface_idx) {
ipv6_addr_t ip;
if (get_ipv6_global(iface_idx, &ip) < 0) {
DEBUG("ip address no found \n");
return -1;
}
netif_t *iface = netif_get_by_id(iface_idx);
if (netif_set_opt(iface, NETOPT_IPV6_ADDR_REMOVE, 0, &ip, sizeof(ipv6_addr_t)) < 0) {
DEBUG("error: unable to join IPv6 multicast group\n"
"File: %s, Function: %s, Line: %d\n",
__FILE__, __func__, __LINE__);
return -1;
}
return 0;
}
int8_t set_ipv6_multicast(kernel_pid_t iface_index, ipv6_addr_t ip, uint8_t prefix) {
if (prefix > 128) {
DEBUG("Wrong prefix length\n"
Expand Down
3 changes: 2 additions & 1 deletion firmware/sys/uniqueid/include/uniqueid.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#ifndef UNIQUEID_H
#define UNIQUEID_H

#include <stdint.h>
#include "periph/cpuid.h"
#include "board.h"
#include "net/ipv6/addr.h"
Expand Down Expand Up @@ -78,7 +79,7 @@ union random_buff {
* @param [in] uid_mode defines the uniqueid mode, refers random or static mode.
*
*/
void get_uid_seed(void *val, uint8_t len, uint8_t uid_mode);
int8_t get_uid_seed(void *val, uint8_t len, uint8_t uid_mode);

#ifdef __cplusplus
}
Expand Down
8 changes: 4 additions & 4 deletions firmware/sys/uniqueid/uniqueid.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
#endif
#include "debug.h"

void get_uid_seed(void *val, uint8_t len, uint8_t uid_mode) {
int8_t get_uid_seed(void *val, uint8_t len, uint8_t uid_mode) {
uint32_t rval;
uint8_t cpuid_bytes = CPUID_LEN / 2;
char addr_cpu[CPUID_LEN];
Expand All @@ -57,7 +57,7 @@ void get_uid_seed(void *val, uint8_t len, uint8_t uid_mode) {
return get_uid_seed((uint8_t *)val + cpuid_bytes, len, UNIQUEID_RANDOM_MODE);
} else {
memcpy(val, addr_cpu, len);
break;
return 0;
}
case UNIQUEID_RANDOM_MODE:
DEBUG("rand mode\n");
Expand All @@ -79,9 +79,9 @@ void get_uid_seed(void *val, uint8_t len, uint8_t uid_mode) {
rval = random_uint32();
memcpy(((uint8_t *)val + i), &rval, sizeof(uint8_t));
}
break;
return 0;
default:
break;
}
return;
return -1;
}
7 changes: 7 additions & 0 deletions tests/net_net_tools/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include ../Makefile.tests_common

USEMODULE += embunit
USEMODULE += net_tools
USEMODULE += uniqueid

include $(RIOTBASE)/Makefile.include
106 changes: 106 additions & 0 deletions tests/net_net_tools/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (C) 2022 Mesh4all.org <mesh4all.org>
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/

/**
* @ingroup tests
* @{
*
* @file
* @brief test to net_tools functions
*
* @author eduazocar <[email protected]>
*
* @}
*/

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "embUnit.h"
#include "net_tools.h"
#include "uniqueid.h"

ipv6_addr_t addr;
char saddr[40];

void test_set_ipv6_uid_static(void) {
uint8_t iface = get_wired_iface();
rm_ipv6_global(iface);
ipv6_addr_from_str(&addr, "2001:db8:2:1::");
set_ipv6_by_uid(iface, &addr, 64, UNIQUEID_STATIC_MODE);
ipv6_addr_print(&addr);
printf("\n");
ipv6_addr_to_str(saddr, &addr, sizeof(saddr));
TEST_ASSERT(strcmp(saddr, "2001:db8:2:1::") != 0);
}

void test_set_ipv6_via_uid_rnd(void) {
uint8_t iface = get_wired_iface();
ipv6_addr_from_str(&addr, "2001:db8:2:1::");
set_ipv6_by_uid(iface, &addr, 64, UNIQUEID_RANDOM_MODE);
ipv6_addr_print(&addr);
printf("\n");
ipv6_addr_to_str(saddr, &addr, sizeof(saddr));
TEST_ASSERT(strcmp(saddr, "2001:db8:2:1::") != 0);
}

void test_get_ipv6_addr(void) {
ipv6_addr_t ip;
int err = 0;
uint8_t iface = get_wired_iface();
err = get_ipv6_global(iface, &ip);
TEST_ASSERT_EQUAL_INT(0, err);
}

void test_rm_global_addr(void) {
int err = 0;
uint8_t iface = get_wired_iface();
err = rm_ipv6_global(iface);
TEST_ASSERT_EQUAL_INT(0, err);
}

test_set_ipv6(void) {
ipv6_addr_t ip;
int err = 0;
uint8_t iface = get_wired_iface();
err = set_ipv6_global(iface, ip, 64);
TEST_ASSERT_EQUAL_INT(0, err);
}

Test *tests_net_tools(void) {
EMB_UNIT_TESTFIXTURES(fixtures){
new_TestFixture(test_set_ipv6_uid_static), new_TestFixture(test_set_ipv6_via_uid_rnd),
new_TestFixture(test_get_ipv6_addr), new_TestFixture(test_set_ipv6),
new_TestFixture(test_rm_global_addr),

};

EMB_UNIT_TESTCALLER(tests_net_tools, NULL, NULL, fixtures);

return (Test *)&tests_net_tools;
}

int main(void) {
TESTS_START();
TESTS_RUN(tests_net_tools());
TESTS_END();
return 0;
}
32 changes: 32 additions & 0 deletions tests/net_net_tools/tests/01-run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env python3

# Copyright (C) 2022 Mesh4all.org <mesh4all.org>
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

import sys
from testrunner import run


def testfunc(child):
# put here the pexpect code that checks the output of the test application.
pass


if __name__ == "__main__":
sys.exit(run(testfunc))
16 changes: 0 additions & 16 deletions tests/system_uniqueid/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,6 @@ void test_get_uid_seed_random(void) {
printf("random seed in uint32: %" PRIu32 "\n", val);
}

void test_get_ipv6_static(void) {
uint8_t iface = get_wired_iface();
ipv6_addr_from_str(&addr, "2001:db8:2:1::");
set_ipv6_by_uid(iface, &addr, 64, UNIQUEID_STATIC_MODE);
ipv6_addr_print(&addr);
printf("\n");
}

void test_get_ipv6_random(void) {
uint8_t iface = get_wired_iface();
ipv6_addr_from_str(&addr, "2001:db8:2:1::");
set_ipv6_by_uid(iface, &addr, 64, UNIQUEID_RANDOM_MODE);
ipv6_addr_print(&addr);
printf("\n");
}

void test_randomblocks(void) { random_generator(8); }

void test_uid_random_blocks(void) { random_generator(64); }
Expand Down

0 comments on commit fd9a88c

Please sign in to comment.