From dd280e25fa56ecc05f38bae9e59b6d61adcdda0b Mon Sep 17 00:00:00 2001 From: "matthias.ringwald@gmail.com" Date: Fri, 24 May 2013 20:29:02 +0000 Subject: [PATCH] unit test for sdp rfcomm queries and sdp parser --- test/sdp_client/Makefile | 27 ++++++ test/sdp_client/config.h | 13 +++ test/sdp_client/test_sdp.c | 165 +++++++++++++++++++++++++++++++++++++ 3 files changed, 205 insertions(+) create mode 100644 test/sdp_client/Makefile create mode 100644 test/sdp_client/config.h create mode 100644 test/sdp_client/test_sdp.c diff --git a/test/sdp_client/Makefile b/test/sdp_client/Makefile new file mode 100644 index 000000000..f63e4e367 --- /dev/null +++ b/test/sdp_client/Makefile @@ -0,0 +1,27 @@ +CC = g++ + +# Requirements: http://www.cpputest.org/ should be placed in btstack/test + +BTSTACK_ROOT = ../.. +CPPUTEST_HOME = ${BTSTACK_ROOT}/test/cpputest + +CFLAGS = -g -Wall -I. -I${BTSTACK_ROOT}/src -I${BTSTACK_ROOT}/include -I$(CPPUTEST_HOME)/include +LDFLAGS += -L$(CPPUTEST_HOME) -lCppUTest -lCppUTestExt + +COMMON = \ + ${BTSTACK_ROOT}/src/sdp_util.c \ + ${BTSTACK_ROOT}/src/sdp_parser.c \ + ${BTSTACK_ROOT}/src/sdp_query_rfcomm.c \ + ${BTSTACK_ROOT}/src/utils.c \ + +COMMON_OBJ = $(COMMON:.c=.o) + +all: test-sdp + +test-sdp: ${COMMON_OBJ} test_sdp.c + ${CC} $^ ${CFLAGS} ${LDFLAGS} -o $@ + +clean: + rm -f test-sdp *.o ${BTSTACK_ROOT}/src/*.o + rm -rf *.dSYM + \ No newline at end of file diff --git a/test/sdp_client/config.h b/test/sdp_client/config.h new file mode 100644 index 000000000..20d4d787d --- /dev/null +++ b/test/sdp_client/config.h @@ -0,0 +1,13 @@ +// config.h created by configure for BTstack Sun Sep 25 16:06:59 CEST 2011 +#pragma once +#define HAVE_TRANSPORT_H4 +#define UART_DEVICE "/dev/tty.usbserial-A600eIDu" +#define UART_SPEED 115200 +#define USE_POSIX_RUN_LOOP +#define HAVE_TIME +#define HAVE_MALLOC +#define HAVE_BZERO +#define ENABLE_LOG_INFO +#define ENABLE_LOG_ERROR +#define HCI_ACL_PAYLOAD_SIZE 1021 +// #define HCI_ACL_PAYLOAD_SIZE 52 diff --git a/test/sdp_client/test_sdp.c b/test/sdp_client/test_sdp.c new file mode 100644 index 000000000..617ebc3e6 --- /dev/null +++ b/test/sdp_client/test_sdp.c @@ -0,0 +1,165 @@ + +//***************************************************************************** +// +// test rfcomm query tests +// +//***************************************************************************** + +#include "config.h" + +#include +#include +#include +#include + +#include "sdp_query_rfcomm.h" +#include +#include + +#include "hci.h" +#include "btstack_memory.h" +#include "hci_dump.h" +#include "l2cap.h" + +#include "CppUTest/TestHarness.h" +#include "CppUTest/CommandLineTestRunner.h" + +static uint8_t service_index; +static uint8_t channel_nr[10]; +static char* service_name[10]; + +static uint8_t sdp_test_record_list[] = { 0x36, 0x02, 0xE7, 0x35, 0x48, +0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x0A, 0x09, 0x00, 0x04, 0x35, 0x10, 0x35, 0x06, 0x19, +0x01, 0x00, 0x09, 0x00, 0x19, 0x35, 0x06, 0x19, 0x00, 0x19, 0x09, 0x01, 0x00, 0x09, 0x00, 0x05, +0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x0D, 0x09, +0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x11, 0x41, 0x32, 0x44, 0x50, 0x20, 0x41, 0x75, 0x64, 0x69, +0x6F, 0x20, 0x53, 0x6F, 0x75, 0x72, 0x63, 0x65, 0x35, 0x43, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, +0x11, 0x0C, 0x09, 0x00, 0x04, 0x35, 0x10, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x17, 0x35, +0x06, 0x19, 0x00, 0x17, 0x09, 0x01, 0x00, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, +0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x0E, 0x09, 0x01, 0x03, 0x09, 0x01, 0x00, 0x25, +0x0C, 0x41, 0x56, 0x52, 0x43, 0x50, 0x20, 0x54, 0x61, 0x72, 0x67, 0x65, 0x74, 0x35, 0x56, 0x09, +0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x05, 0x09, 0x00, 0x04, 0x35, 0x11, 0x35, 0x03, 0x19, 0x01, +0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x0A, 0x35, 0x03, 0x19, 0x00, 0x08, 0x09, 0x00, 0x05, +0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, +0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x05, 0x09, 0x01, 0x00, +0x09, 0x01, 0x00, 0x25, 0x10, 0x4F, 0x42, 0x45, 0x58, 0x20, 0x4F, 0x62, 0x6A, 0x65, 0x63, 0x74, +0x20, 0x50, 0x75, 0x73, 0x68, 0x35, 0x5A, 0x09, 0x00, 0x01, 0x35, 0x06, 0x19, 0x11, 0x1F, 0x19, +0x12, 0x03, 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, +0x03, 0x08, 0x02, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, +0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x06, 0x19, 0x11, +0x1E, +0x09, 0x01, 0x05, 0x09, 0x01, +0x00, 0x25, 0x18, 0x48, 0x61, 0x6E, 0x64, 0x73, 0x20, 0x46, 0x72, 0x65, 0x65, 0x20, 0x41, 0x75, +0x64, 0x69, 0x6F, 0x20, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x35, 0x27, 0x09, 0x00, 0x05, +0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x01, 0x00, 0x25, 0x1A, 0x41, 0x70, 0x70, 0x6C, 0x65, 0x20, +0x4D, 0x61, 0x63, 0x69, 0x6E, 0x74, 0x6F, 0x73, 0x68, 0x20, 0x41, 0x74, 0x74, 0x72, 0x69, 0x62, +0x75, 0x74, 0x65, 0x73, 0x35, 0x58, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x06, 0x09, 0x00, +0x04, 0x35, 0x11, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x0F, 0x35, +0x03, 0x19, 0x00, 0x08, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, +0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, +0x06, 0x19, 0x11, 0x06, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x12, 0x4F, 0x42, 0x45, 0x58, +0x20, 0x46, 0x69, 0x6C, 0x65, 0x20, 0x54, 0x72, 0x61, 0x6E, 0x73, 0x66, 0x65, 0x72, 0x35, 0x53, +0x09, 0x00, 0x01, 0x35, 0x03, 0x19, 0x11, 0x01, 0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19, +0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, 0x03, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, +0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, +0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x01, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, +0x12, 0x42, 0x6C, 0x75, 0x65, 0x74, 0x6F, 0x6F, 0x74, 0x68, 0x2D, 0x50, 0x44, 0x41, 0x2D, 0x53, +0x79, 0x6E, 0x63, 0x35, 0x59, 0x09, 0x00, 0x01, 0x35, 0x06, 0x19, 0x11, 0x12, 0x19, 0x12, 0x03, +0x09, 0x00, 0x04, 0x35, 0x0C, 0x35, 0x03, 0x19, 0x01, 0x00, 0x35, 0x05, 0x19, 0x00, 0x03, 0x08, +0x04, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, 0x35, 0x09, 0x09, 0x65, +0x6E, +0x09, 0x00, 0x6A, 0x09, 0x01, +0x00, 0x09, 0x00, 0x09, 0x35, 0x08, 0x35, 0x06, 0x19, 0x11, 0x08, 0x09, 0x01, 0x02, 0x09, 0x01, +0x00, 0x25, 0x15, 0x48, 0x65, 0x61, 0x64, 0x73, 0x65, 0x74, 0x20, 0x41, 0x75, 0x64, 0x69, 0x6F, +0x20, 0x47, 0x61, 0x74, 0x65, 0x77, 0x61, 0x79, 0x35, 0x6F, 0x09, 0x00, 0x01, 0x35, 0x03, 0x19, +0x11, 0x17, 0x09, 0x00, 0x04, 0x35, 0x1E, 0x35, 0x06, 0x19, 0x01, 0x00, 0x09, 0x00, 0x0F, 0x35, +0x14, 0x19, 0x00, 0x0F, 0x09, 0x01, 0x00, 0x35, 0x0C, 0x09, 0x08, 0x00, 0x09, 0x08, 0x06, 0x09, +0x86, 0xDD, 0x09, 0x88, 0x0B, 0x09, 0x00, 0x05, 0x35, 0x03, 0x19, 0x10, 0x02, 0x09, 0x00, 0x06, +0x35, 0x09, 0x09, 0x65, 0x6E, 0x09, 0x00, 0x6A, 0x09, 0x01, 0x00, 0x09, 0x00, 0x09, 0x35, 0x08, +0x35, 0x06, 0x19, 0x11, 0x17, 0x09, 0x01, 0x00, 0x09, 0x01, 0x00, 0x25, 0x1C, 0x47, 0x72, 0x6F, +0x75, 0x70, 0x20, 0x41, 0x64, 0x2D, 0x68, 0x6F, 0x63, 0x20, 0x4E, 0x65, 0x74, 0x77, 0x6F, 0x72, +0x6B, 0x20, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65 +}; + +// dummy function to allow compile without the stack +extern "C" void sdp_client_query(bd_addr_t remote, uint8_t * des_serviceSearchPattern, uint8_t * des_attributeIDList){ +} + + +void handle_query_rfcomm_event(sdp_query_event_t * event, void * context){ + sdp_query_rfcomm_service_event_t * ve; + sdp_query_complete_event_t * ce; + + switch (event->type){ + case SDP_QUERY_RFCOMM_SERVICE: + ve = (sdp_query_rfcomm_service_event_t*) event; + channel_nr[service_index] = ve->channel_nr; + service_name[service_index] = (char*) malloc(SDP_SERVICE_NAME_LEN+1); + strncpy(service_name[service_index], (char*) ve->service_name, SDP_SERVICE_NAME_LEN); + // printf("CALLBACK: Service name: '%s', RFCOMM port %u, service index %d\n", service_name[service_index], channel_nr[service_index], service_index); + service_index++; + break; + case SDP_QUERY_COMPLETE: + ce = (sdp_query_complete_event_t*) event; + // printf("CALLBACK: Client query response done. \n"); + break; + } +} + + +TEST_GROUP(SDPClient){ + uint8_t spp_buffer[sizeof(sdp_test_record_list)]; + + void setup(){ + service_index = 0; + sdp_query_rfcomm_register_callback(handle_query_rfcomm_event, NULL); + sdp_parser_init(); + sdp_query_rfcomm_init(); + } +}; + + +TEST(SDPClient, QueryRFCOMMWithSyntheticData){ + + const char* expected_name[] = {"SDP Response Test1", "SDP Response Test2", "SDP Response Test3"}; + uint8_t expected_channel[] = {10, 11, 12}; + int record_nr = sizeof(expected_channel)/sizeof(uint8_t); + int i; + + de_create_sequence(spp_buffer); + for (i=0; i