Skip to content

Commit

Permalink
Ipv6 helpers update (#44)
Browse files Browse the repository at this point in the history
* Update ipv6 trace helper functions.

In a recent update to libService the ip6tos function was slightly updated and ip6_prefix_tos was added.

Update mbed-trace to make use of the updated functions, simplifying code and getting rid of some temporary variables.

* Updated unittests.

Added libservice as a dependency for bitcopy and removed its stub. It should be as reliable as std library functions so no need for stubbing.
Added a stub for ip6_prefix_tos function.
Made the ip6tos stub control structures clearer.
Added tests for trace_ipv6_prefix error cases. The previous formatting tests now make little sense
as no actual formatting is done by trace_ipv6_prefix.
  • Loading branch information
Tommi Käsmä authored and jupe committed Jul 13, 2016
1 parent 7030a75 commit 5f114b3
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 70 deletions.
8 changes: 5 additions & 3 deletions module.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
],
"testTargetDependencies": {
"x86-linux-native": {
"cpputest": "ARMmbed/cpputest"
"cpputest": "ARMmbed/cpputest",
"nanostack-libservice": "^3.6.0"
},
"x86-windows-native": {
"cpputest": "ARMmbed/cpputest"
"cpputest": "ARMmbed/cpputest",
"nanostack-libservice": "^3.6.0"
}
},
"dependencies": {
"nanostack-libservice": "^3.5.2"
"nanostack-libservice": "^3.6.0"
}
}
24 changes: 5 additions & 19 deletions source/mbed_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,7 @@ char *mbed_trace_ipv6(const void *addr_ptr)
return "<null>";
}
str[0] = 0;
ip6tos(addr_ptr, str);
m_trace.tmp_data_ptr += strlen(str) + 1;
m_trace.tmp_data_ptr += ip6tos(addr_ptr, str) + 1;
return str;
}
char *mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len)
Expand All @@ -503,31 +502,18 @@ char *mbed_trace_ipv6_prefix(const uint8_t *prefix, uint8_t prefix_len)
m_trace.mutex_lock_count++;
}
char *str = m_trace.tmp_data_ptr;
int retval, bLeft = tmp_data_left();
char tmp[40];
uint8_t addr[16] = {0};

if (str == NULL) {
return "";
}
if (bLeft < 45) {
if (tmp_data_left() < 45) {
return "";
}

if (prefix_len != 0) {
if (prefix == NULL || prefix_len > 128) {
return "<err>";
}
bitcopy(addr, prefix, prefix_len);
}

ip6tos(addr, tmp);
retval = snprintf(str, bLeft, "%s/%u", tmp, prefix_len);
if (retval <= 0 || retval > bLeft) {
return "";
if ((prefix_len != 0 && prefix == NULL) || prefix_len > 128) {
return "<err>";
}

m_trace.tmp_data_ptr += retval + 1;
m_trace.tmp_data_ptr += ip6_prefix_tos(prefix, prefix_len, str) + 1;
return str;
}
#endif //YOTTA_CFG_MBED_TRACE_FEA_IPV6
Expand Down
2 changes: 1 addition & 1 deletion test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
if(DEFINED TARGET_LIKE_X86_WINDOWS_NATIVE OR DEFINED TARGET_LIKE_X86_LINUX_NATIVE)

# describe the test executable
add_executable(mbed_trace_test EXCLUDE_FROM_ALL Test.cpp stubs/common_functions_stub.c stubs/ip6tos_stub.c)
add_executable(mbed_trace_test EXCLUDE_FROM_ALL Test.cpp stubs/ip6tos_stub.c)

include_directories("../yotta_modules/cpputest" "./stubs")

Expand Down
40 changes: 33 additions & 7 deletions test/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#define YOTTA_CFG_MBED_TRACE_FEA_IPV6 1

#include "mbed-trace/mbed_trace.h"
#include "ip6tos_stub.h"

int main(int ac, char **av)
{
Expand Down Expand Up @@ -128,18 +129,43 @@ TEST(trace, BufferResize)
}

#if YOTTA_CFG_MBED_TRACE_FEA_IPV6 == 1
const char *ip6tos_output_string;
uint8_t ip6tos_input_array[16];
ip6tos_stub_def_t ip6tos_stub; // extern variable

TEST(trace, ipv6)
{
uint8_t prefix[] = { 0x14, 0x6e, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00 };
int prefix_len = 64;
ip6tos_output_string = "146e:a00::";

char expected_str1[] = "146e:a00::/64";
ip6tos_stub.output_string = "146e:a00::/64";
char *str = mbed_trace_ipv6_prefix(prefix, prefix_len);
CHECK(memcmp(ip6tos_input_array, prefix, 8) == 0);
STRCMP_EQUAL("146e:a00::/64", str);
CHECK(memcmp(ip6tos_stub.input_array, prefix, 8) == 0);
STRCMP_EQUAL(expected_str1, str);
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "flush buffers and locks");

char expected_str2[] = "::/0";
ip6tos_stub.output_string = "::/0";
str = mbed_trace_ipv6_prefix(NULL, 0);
STRCMP_EQUAL(expected_str2, str);
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "flush buffers and locks");

char expected_str3[] = "<err>";
str = mbed_trace_ipv6_prefix(NULL, 1);
STRCMP_EQUAL(expected_str3, str);
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "flush buffers and locks");

char expected_str4[] = "<err>";
str = mbed_trace_ipv6_prefix(prefix, 200);
STRCMP_EQUAL(expected_str4, str);
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "flush buffers and locks");

char expected_str5[] = "";
ip6tos_stub.output_string = "0123456789012345678901234567890123456789";
str = mbed_trace_ipv6_prefix(prefix, 64); // Fill the tmp_data buffer
str = mbed_trace_ipv6_prefix(prefix, 64);
str = mbed_trace_ipv6_prefix(prefix, 64);
str = mbed_trace_ipv6_prefix(prefix, 64);
STRCMP_EQUAL(expected_str5, str);
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "flush buffers and locks");
}

Expand All @@ -148,9 +174,9 @@ TEST(trace, active_level_all_ipv6)
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL);

uint8_t arr[] = { 0x20, 0x01, 0xd, 0xb8, 0,0,0,0,0,1,0,0,0,0,0,1 };
ip6tos_output_string = "2001:db8::1:0:0:1";
ip6tos_stub.output_string = "2001:db8::1:0:0:1";
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "my addr: %s", mbed_trace_ipv6(arr));
CHECK(memcmp(ip6tos_input_array, arr, 16) == 0);
CHECK(memcmp(ip6tos_stub.input_array, arr, 16) == 0);
STRCMP_EQUAL("[DBG ][mygr]: my addr: 2001:db8::1:0:0:1", buf);
}
#endif //YOTTA_CFG_MBED_TRACE_FEA_IPV6
Expand Down
13 changes: 0 additions & 13 deletions test/stubs/common_functions_stub.c

This file was deleted.

18 changes: 0 additions & 18 deletions test/stubs/common_functions_stub.h

This file was deleted.

22 changes: 16 additions & 6 deletions test/stubs/ip6tos_stub.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
/*
* Copyright (c) 2016 ARM Limited. All rights reserved.
*/
#include "ip6string_stub.h"
#include "ip6string.h"
#include <string.h>
#include <stdio.h>
#include "common_functions.h"

const char *ip6tos_output_string;
extern uint8_t ip6tos_input_array[16];
#include "ip6tos_stub.h"

void ip6tos(const void *ip6addr, char *p)
ip6tos_stub_def_t ip6tos_stub; // extern variable

uint8_t ip6tos(const void *ip6addr, char *p)
{
memcpy(ip6tos_stub.input_array, ip6addr, 16);
strcpy(p, ip6tos_stub.output_string);
return strlen(p);
}

uint_fast8_t ip6_prefix_tos(const void *prefix, uint_fast8_t prefix_len, char *p)
{
memcpy(ip6tos_input_array, ip6addr, 16);
strcpy(p, ip6tos_output_string);
bitcopy(ip6tos_stub.input_array, prefix, prefix_len);
strcpy(p, ip6tos_stub.output_string);
return strlen(p);
}
8 changes: 5 additions & 3 deletions test/stubs/ip6string_stub.h → test/stubs/ip6tos_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ extern "C" {

#include <inttypes.h>

extern const char *ip6tos_output_string;
extern uint8_t ip6tos_input_array[16];
typedef struct {
const char *output_string;
uint8_t input_array[16];
} ip6tos_stub_def_t;

void ip6tos(const void *ip6addr, char *p);
extern ip6tos_stub_def_t ip6tos_stub;

#ifdef __cplusplus
}
Expand Down

0 comments on commit 5f114b3

Please # to comment.