From 85742615a805d8b3a2057e9fe3b5a7d36c08e272 Mon Sep 17 00:00:00 2001 From: "mila@ringwald.ch" Date: Fri, 29 Aug 2014 15:20:27 +0000 Subject: [PATCH] des iterator done --- test/des_iterator/des_iterator.c | 38 +++++++++++++-- test/des_iterator/des_iterator.h | 8 ++- test/des_iterator/des_iterator_test.c | 70 +++++++++++++++++++++++---- 3 files changed, 101 insertions(+), 15 deletions(-) diff --git a/test/des_iterator/des_iterator.c b/test/des_iterator/des_iterator.c index ffc6e0393..e0fd2d5c1 100644 --- a/test/des_iterator/des_iterator.c +++ b/test/des_iterator/des_iterator.c @@ -1,5 +1,5 @@ #include "des_iterator.h" - + int des_iterator_init(des_iterator_t * it, uint8_t * element){ de_type_t type = de_get_element_type(element); if (type != DE_DES) return 0; @@ -7,6 +7,7 @@ int des_iterator_init(des_iterator_t * it, uint8_t * element){ it->element = element; it->pos = de_get_header_size(element); it->length = de_get_len(element); + // printf("des_iterator_init current pos %d, total len %d\n", it->pos, it->length); return 1; } @@ -20,10 +21,37 @@ uint16_t des_iterator_get_size (des_iterator_t * it){ return length - header_size; } -int des_iterator_has_more(des_iterator_t * it){ - return 0; +int des_iterator_has_more(des_iterator_t * it){ + return it->pos < it->length; } -uint8_t * des_iterator_get_value(des_iterator_t * it){ - return NULL; +uint8_t * des_iterator_get_element(des_iterator_t * it){ + if (!des_iterator_has_more(it)) return NULL; + return &it->element[it->pos]; } + +void des_iterator_next(des_iterator_t * it){ + int element_len = de_get_len(&it->element[it->pos]); + // printf("des_iterator_next element size %d, current pos %d, total len %d\n", element_len, it->pos, it->length); + it->pos += element_len; +} + + +// move to sdp_util.c + +// @returns OK, if UINT16 value was read +int de_element_get_uint16(uint8_t * element, uint16_t * value){ + if (de_get_size_type(element) != DE_SIZE_16) return 0; + *value = READ_NET_16(element, de_get_header_size(element)); + return 1; +} + +// @returns 0 if no UUID16 was present, and UUID otherwise +uint16_t de_element_get_uuid16(uint8_t * element){ + if (de_get_element_type(element) != DE_UUID) return 0; + uint16_t value = 0; + de_element_get_uint16(element, &value); + return value; +} + + diff --git a/test/des_iterator/des_iterator.h b/test/des_iterator/des_iterator.h index aebb7f237..5be797e19 100644 --- a/test/des_iterator/des_iterator.h +++ b/test/des_iterator/des_iterator.h @@ -26,7 +26,13 @@ int des_iterator_init(des_iterator_t * it, uint8_t * element); int des_iterator_has_more(des_iterator_t * it); de_type_t des_iterator_get_type (des_iterator_t * it); uint16_t des_iterator_get_size (des_iterator_t * it); -uint8_t * des_iterator_get_value(des_iterator_t * it); +uint8_t * des_iterator_get_element(des_iterator_t * it); +void des_iterator_next(des_iterator_t * it); + + +// move to sdp util +int de_element_get_uint16(uint8_t * element, uint16_t * value); +uint16_t de_element_get_uuid16(uint8_t * element); #if defined __cplusplus } diff --git a/test/des_iterator/des_iterator_test.c b/test/des_iterator/des_iterator_test.c index e97c32bf0..f98488a1b 100644 --- a/test/des_iterator/des_iterator_test.c +++ b/test/des_iterator/des_iterator_test.c @@ -16,24 +16,76 @@ #include "CppUTest/CommandLineTestRunner.h" -static uint8_t des_test[] = { +static uint8_t prot_des_list_des[] = { 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 }; - -TEST_GROUP(DESParser){ - des_iterator_t des_protcol_list_it; - int iterator_initialized; - - void setup(){ - iterator_initialized = des_iterator_init(&des_protcol_list_it, des_test); - } +static uint16_t expected_values[] = { + 0x0100, 0xf, 0x000f, 0x0100, 0x800, 0x806, 0x86dd, 0x880b }; + +TEST_GROUP(DESParser){ + int value_index; + + void setup(){ + value_index = 0; + } + + void CHECK_EQUAL_UINT16(des_iterator_t *it){ + uint16_t value; + uint8_t * element = des_iterator_get_element(it); + if (des_iterator_get_type(it) == DE_UUID){ + value = de_element_get_uuid16(element); + } else { + value = 0xffff; + de_element_get_uint16(element, &value); + } + CHECK_EQUAL(expected_values[value_index], value); + value_index++; + } +}; + +// DES { DES{0x0100, 0xf}, DES{0x000f, 0x0100, DES{0x800, 0x806, 0x86dd, 0x880b}}} TEST(DESParser, DESIterator){ + des_iterator_t des_protocol_list_it; + des_iterator_t prot_it; + des_iterator_t packet_it; + int iterator_initialized; + uint8_t * prot_des; + uint8_t * packet_des; + // printf("\n *** protocol list\n"); + iterator_initialized = des_iterator_init(&des_protocol_list_it, prot_des_list_des); CHECK_EQUAL(iterator_initialized, 1); + CHECK_EQUAL(des_iterator_get_type(&des_protocol_list_it), DE_DES); + + // printf("\n *** l2cap uuid, psm\n"); + prot_des = des_iterator_get_element(&des_protocol_list_it); + for (des_iterator_init(&prot_it, prot_des) ; des_iterator_has_more(&prot_it) ; des_iterator_next(&prot_it) ){ + CHECK_EQUAL_UINT16(&prot_it); + } + + des_iterator_next(&des_protocol_list_it); + CHECK_EQUAL(des_iterator_get_type(&des_protocol_list_it), DE_DES); + + // printf("\n *** bnep uuid, value\n"); + prot_des = des_iterator_get_element(&des_protocol_list_it); + + for (des_iterator_init(&prot_it, prot_des) ; des_iterator_has_more(&prot_it) ; des_iterator_next(&prot_it)){ + if (des_iterator_get_type(&prot_it) != DE_DES){ + CHECK_EQUAL_UINT16(&prot_it); + continue; + } + + packet_des = des_iterator_get_element(&prot_it); + for (des_iterator_init(&packet_it, packet_des) ; des_iterator_has_more(&packet_it) ; des_iterator_next(&packet_it)){ + CHECK_EQUAL_UINT16(&packet_it); + } + } + des_iterator_next(&des_protocol_list_it); + CHECK_EQUAL(value_index, 8); }