le_device_db_tlv_test: move test from flash_tlv

This commit is contained in:
Milanka Ringwald 2020-09-24 14:41:36 +02:00
parent 27c566a6f0
commit 21c2455c51
8 changed files with 273 additions and 136 deletions

View File

@ -16,6 +16,7 @@ SUBDIRS = \
hfp \
hid_parser \
linked_list \
le_device_db_tlv \
map_test \
mesh \
obex \
@ -42,6 +43,7 @@ SUBDIRS_BLE = \
gatt_client \
gap \
hid_parser \
le_device_db_tlv \
linked_list \
ring_buffer \
security_manager \

View File

@ -1,4 +1,3 @@
tlv_test
*.pklg
tlv_le_test
tlv_le_test.pklg

View File

@ -29,7 +29,7 @@ CFLAGS = \
CFLAGS += -fprofile-arcs -ftest-coverage -fsanitize=address,undefined
LDFLAGS += -lCppUTest -lCppUTestExt
TESTS = tlv_test tlv_le_test
TESTS = tlv_test
all: ${TESTS}
@ -40,9 +40,6 @@ clean:
tlv_test: ${COMMON_OBJ} btstack_link_key_db_tlv.o tlv_test.o
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
tlv_le_test: ${COMMON_OBJ} le_device_db_tlv.o tlv_le_test.o
${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@
test: all
@echo Run all test
@set -e; \

View File

@ -1,130 +0,0 @@
#include "CppUTest/TestHarness.h"
#include "CppUTest/CommandLineTestRunner.h"
#include "hal_flash_bank.h"
#include "hal_flash_bank_memory.h"
#include "btstack_tlv.h"
#include "btstack_tlv_flash_bank.h"
#include "hci_dump.h"
#include "ble/le_device_db.h"
#include "ble/le_device_db_tlv.h"
#include "btstack_util.h"
#include "btstack_config.h"
#include "btstack_debug.h"
#define HAL_FLASH_BANK_MEMORY_STORAGE_SIZE 512
static uint8_t hal_flash_bank_memory_storage[HAL_FLASH_BANK_MEMORY_STORAGE_SIZE];
static void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
int i;
for (i=0; i<size; i++){
if (expected[i] != actual[i]) {
printf("offset %u wrong\n", i);
printf("expected: "); printf_hexdump(expected, size);
printf("actual: "); printf_hexdump(actual, size);
}
BYTES_EQUAL(expected[i], actual[i]);
}
}
//
TEST_GROUP(LE_DEVICE_DB){
const hal_flash_bank_t * hal_flash_bank_impl;
hal_flash_bank_memory_t hal_flash_bank_context;
const btstack_tlv_t * btstack_tlv_impl;
btstack_tlv_flash_bank_t btstack_tlv_context;
bd_addr_t addr_aa, addr_bb, addr_cc;
sm_key_t sm_key_aa, sm_key_bb, sm_key_cc;
void setup(void){
// hal_flash_bank
hal_flash_bank_impl = hal_flash_bank_memory_init_instance(&hal_flash_bank_context, hal_flash_bank_memory_storage, HAL_FLASH_BANK_MEMORY_STORAGE_SIZE);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 0);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 1);
// btstack_tlv
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
// le_device_db_tlv
le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_context);
le_device_db_init();
bd_addr_t addr_1 = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
bd_addr_t addr_2 = { 0xbb, 0xbb, 0xbb, 0xbb, 0xbb, 0xbb };
bd_addr_t addr_3 = { 0xcc, 0xcc, 0xcc, 0xcc, 0xcc, 0xcc };
bd_addr_copy(addr_aa, addr_1);
bd_addr_copy(addr_bb, addr_2);
bd_addr_copy(addr_cc, addr_3);
memset(sm_key_aa, 0xaa, 16);
memset(sm_key_bb, 0xbb, 16);
memset(sm_key_cc, 0xcc, 16);
}
};
TEST(LE_DEVICE_DB, Empty){
CHECK_EQUAL(0, le_device_db_count());
}
TEST(LE_DEVICE_DB, AddOne){
int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
CHECK_TRUE(index >= 0);
CHECK_EQUAL(1, le_device_db_count());
}
TEST(LE_DEVICE_DB, RetrieveOne){
int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
CHECK_TRUE(index >= 0);
CHECK_EQUAL(1, le_device_db_count());
bd_addr_t addr;
sm_key_t sm_key;
int addr_type;
le_device_db_info((uint16_t) index, &addr_type, addr, sm_key);
CHECK_EQUAL_ARRAY(sm_key_aa, sm_key, 16);
CHECK_EQUAL_ARRAY(addr_aa, addr, 6);
}
TEST(LE_DEVICE_DB, AddTwo){
le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_bb, sm_key_bb);
CHECK_EQUAL(2, le_device_db_count());
}
TEST(LE_DEVICE_DB, AddOTwoRemoveOne){
int index_a = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
CHECK_TRUE(index_a >= 0);
int index_b = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_bb, sm_key_bb);
CHECK_TRUE(index_b >= 0);
le_device_db_remove((uint16_t) index_a);
CHECK_EQUAL(1, le_device_db_count());
bd_addr_t addr;
sm_key_t sm_key;
int addr_type;
le_device_db_info((uint16_t) index_b, &addr_type, addr, sm_key);
CHECK_EQUAL_ARRAY(sm_key_bb, sm_key, 16);
CHECK_EQUAL_ARRAY(addr_bb, addr, 6);
}
TEST(LE_DEVICE_DB, AddTwoRemoveOneAddOne){
int index_a = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
CHECK_TRUE(index_a >= 0);
int index_b = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_bb, sm_key_bb);
CHECK_TRUE(index_b >= 0);
le_device_db_remove((uint16_t) index_a);
int index_c = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_cc, sm_key_cc);
CHECK_TRUE(index_c >= 0);
CHECK_EQUAL(2, le_device_db_count());
bd_addr_t addr;
sm_key_t sm_key;
int addr_type;
le_device_db_info((uint16_t) index_c, &addr_type, addr, sm_key);
CHECK_EQUAL_ARRAY(sm_key_cc, sm_key, 16);
CHECK_EQUAL_ARRAY(addr_cc, addr, 6);
}
int main (int argc, const char * argv[]){
hci_dump_open("tlv_le_test.pklg", HCI_DUMP_PACKETLOGGER);
return CommandLineTestRunner::RunAllTests(argc, argv);
}

2
test/le_device_db_tlv/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
le_device_db_tlv_test
le_device_db_tlv_test.pklg

View File

@ -0,0 +1,44 @@
CC = g++
# Requirements: cpputest.github.io
BTSTACK_ROOT = ../..
CFLAGS = -DUNIT_TEST -x c++ -g -Wall -Wnarrowing -Wconversion-null
CFLAGS += -I.
CFLAGS += -I${BTSTACK_ROOT}/src
CFLAGS += -I${BTSTACK_ROOT}/platform/embedded
# CFLAGS += -fprofile-arcs -ftest-coverage -fsanitize=address
LDFLAGS += -lCppUTest -lCppUTestExt
VPATH += ${BTSTACK_ROOT}/src
VPATH += ${BTSTACK_ROOT}/src/ble
VPATH += ${BTSTACK_ROOT}/platform/posix
VPATH += ${BTSTACK_ROOT}/platform/embedded
COMMON = \
btstack_linked_list.c \
btstack_memory.c \
btstack_memory_pool.c \
btstack_util.c \
hci_dump.c \
le_device_db_tlv.c \
btstack_tlv_flash_bank.c \
hal_flash_bank_memory.c \
COMMON_OBJ = $(COMMON:.c=.o)
all: le_device_db_tlv_test
le_device_db_tlv_test: ${COMMON_OBJ} le_device_db_tlv_test.o
${CC} ${COMMON_OBJ} le_device_db_tlv_test.o ${CFLAGS} ${LDFLAGS} -o $@
test: all
./le_device_db_tlv_test
clean:
rm -f le_device_db_tlv_test
rm -f *.o
rm -rf *.dSYM
rm -f *.gcno *.gcda

View File

@ -0,0 +1,40 @@
//
// btstack_config.h for most tests
//
#ifndef __BTSTACK_CONFIG
#define __BTSTACK_CONFIG
// Port related features
#define HAVE_MALLOC
#define HAVE_POSIX_TIME
#define HAVE_POSIX_FILE_IO
#define HAVE_BTSTACK_STDIN
// BTstack features that can be enabled
#define ENABLE_BLE
#define ENABLE_CLASSIC
// #define ENABLE_LOG_DEBUG
#define ENABLE_LOG_ERROR
#define ENABLE_LOG_INFO
#define ENABLE_SDP_DES_DUMP
#define ENABLE_SDP_EXTRA_QUERIES
// #define ENABLE_LE_SECURE_CONNECTIONS
#define ENABLE_LE_SIGNED_WRITE
#define ENABLE_LE_PERIPHERAL
#define ENABLE_LE_CENTRAL
#define ENABLE_SDP_EXTRA_QUERIES
#define ENABLE_L2CAP_ENHANCED_RETRANSMISSION_MODE
// Link Key DB and LE Device DB using TLV on top of Flash Sector interface
#define NVM_NUM_DEVICE_DB_ENTRIES 16
// BTstack configuration. buffers, sizes, ...
#define HCI_ACL_PAYLOAD_SIZE 52
#define HCI_INCOMING_PRE_BUFFER_SIZE 4
#define MAX_NR_LE_DEVICE_DB_ENTRIES 4
#define NVM_NUM_LINK_KEYS 2
#endif

View File

@ -0,0 +1,183 @@
/*
* Copyright (C) 2014 BlueKitchen GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
* 4. Any redistribution, use, or modification is done solely for
* personal benefit and not for any commercial purpose or for
* monetary gain.
*
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* Please inquire about commercial licensing options at
* contact@bluekitchen-gmbh.com
*
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "CppUTest/TestHarness.h"
#include "CppUTest/CommandLineTestRunner.h"
#include "ble/le_device_db.h"
#include "ble/le_device_db_tlv.h"
#include "btstack_util.h"
#include "bluetooth.h"
#include "btstack_tlv_flash_bank.h"
#include "hal_flash_bank_memory.h"
#define HAL_FLASH_BANK_MEMORY_STORAGE_SIZE 4096
static uint8_t hal_flash_bank_memory_storage[HAL_FLASH_BANK_MEMORY_STORAGE_SIZE];
TEST_GROUP(LE_DEVICE_DB_TLV){
const hal_flash_bank_t * hal_flash_bank_impl;
hal_flash_bank_memory_t hal_flash_bank_context;
const btstack_tlv_t * btstack_tlv_impl;
btstack_tlv_flash_bank_t btstack_tlv_context;
bd_addr_t addr_aa, addr_bb, addr_cc;
sm_key_t sm_key_aa, sm_key_bb, sm_key_cc;
bd_addr_t addr;
sm_key_t sm_key;
void init_addr_and_key(uint8_t a){
uint8_t value = 0;
if (a <= 0x0F){
value = 0x11 * a;
}
memset(sm_key, value, 16);
memset(addr, value, 6);
}
void setup(void){
// hal_flash_bank
hal_flash_bank_impl = hal_flash_bank_memory_init_instance(&hal_flash_bank_context, hal_flash_bank_memory_storage, HAL_FLASH_BANK_MEMORY_STORAGE_SIZE);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 0);
hal_flash_bank_impl->erase(&hal_flash_bank_context, 1);
// btstack_tlv
btstack_tlv_impl = btstack_tlv_flash_bank_init_instance(&btstack_tlv_context, hal_flash_bank_impl, &hal_flash_bank_context);
// le_device_db_tlv
le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_context);
le_device_db_init();
memset(addr_aa, 0xaa, 6);
memset(addr_bb, 0xbb, 6);
memset(addr_cc, 0xcc, 6);
memset(sm_key_aa, 0xaa, 16);
memset(sm_key_bb, 0xbb, 16);
memset(sm_key_cc, 0xcc, 16);
}
};
TEST(LE_DEVICE_DB_TLV, Empty){
CHECK_EQUAL(0, le_device_db_count());
}
TEST(LE_DEVICE_DB_TLV, AddZero){
int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
CHECK_TRUE(index >= 0);
CHECK_EQUAL(1, le_device_db_count());
}
TEST(LE_DEVICE_DB_TLV, AddOne){
int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
CHECK_TRUE(index >= 0);
CHECK_EQUAL(1, le_device_db_count());
}
TEST(LE_DEVICE_DB_TLV, RetrieveOne){
int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
CHECK_TRUE(index >= 0);
CHECK_EQUAL(1, le_device_db_count());
bd_addr_t addr;
sm_key_t sm_key;
int addr_type;
le_device_db_info((uint16_t) index, &addr_type, addr, sm_key);
MEMCMP_EQUAL(sm_key_aa, sm_key, 16);
MEMCMP_EQUAL(addr_aa, addr, 6);
}
TEST(LE_DEVICE_DB_TLV, AddTwo){
le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_bb, sm_key_bb);
CHECK_EQUAL(2, le_device_db_count());
}
TEST(LE_DEVICE_DB_TLV, AddOTwoRemoveOne){
int index_a = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
CHECK_TRUE(index_a >= 0);
int index_b = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_bb, sm_key_bb);
CHECK_TRUE(index_b >= 0);
le_device_db_remove((uint16_t) index_a);
CHECK_EQUAL(1, le_device_db_count());
bd_addr_t addr;
sm_key_t sm_key;
int addr_type;
le_device_db_info((uint16_t) index_b, &addr_type, addr, sm_key);
MEMCMP_EQUAL(sm_key_bb, sm_key, 16);
MEMCMP_EQUAL(addr_bb, addr, 6);
}
TEST(LE_DEVICE_DB_TLV, AddTwoRemoveOneAddOne){
int index_a = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_aa, sm_key_aa);
CHECK_TRUE(index_a >= 0);
int index_b = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_bb, sm_key_bb);
CHECK_TRUE(index_b >= 0);
le_device_db_remove((uint16_t) index_a);
int index_c = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr_cc, sm_key_cc);
CHECK_TRUE(index_c >= 0);
CHECK_EQUAL(2, le_device_db_count());
bd_addr_t addr;
sm_key_t sm_key;
int addr_type;
le_device_db_info((uint16_t) index_c, &addr_type, addr, sm_key);
MEMCMP_EQUAL(sm_key_cc, sm_key, 16);
MEMCMP_EQUAL(addr_cc, addr, 6);
}
TEST(LE_DEVICE_DB_TLV, le_device_db_add){
uint8_t i;
for (i=0; i < NVM_NUM_DEVICE_DB_ENTRIES; i++){
init_addr_and_key(i);
int index = le_device_db_add(BD_ADDR_TYPE_LE_PUBLIC, addr, sm_key);
CHECK_TRUE(index >= 0);
CHECK_EQUAL(i+1, le_device_db_count());
}
}
int main (int argc, const char * argv[]){
return CommandLineTestRunner::RunAllTests(argc, argv);
}