From 709e2292b646fb679958acd503bc183ed23f9fe5 Mon Sep 17 00:00:00 2001 From: Juri Glass Date: Fri, 8 Jul 2011 15:20:35 +0200 Subject: [PATCH] added more test cases, fixed negative integers bug --- sml/src/sml_number.c | 29 ++++++--- test/Makefile | 9 ++- test/src/sml_buffer_test.c | 34 +++++++++++ test/src/sml_number_test.c | 101 +++++++++++++++++++++++++++++++ test/src/sml_octet_string_test.c | 51 ++++++++++++++++ test/src/test_helper.c | 36 +++++++++++ test/src/test_helper.h | 7 +++ test/src/test_sml_octet_string.c | 26 -------- test/test_main.c | 2 + 9 files changed, 257 insertions(+), 38 deletions(-) create mode 100644 test/src/sml_buffer_test.c create mode 100644 test/src/sml_number_test.c create mode 100644 test/src/sml_octet_string_test.c create mode 100644 test/src/test_helper.c create mode 100644 test/src/test_helper.h delete mode 100644 test/src/test_sml_octet_string.c diff --git a/sml/src/sml_number.c b/sml/src/sml_number.c index 61d2bd2..84c5a34 100644 --- a/sml/src/sml_number.c +++ b/sml/src/sml_number.c @@ -22,8 +22,8 @@ #include u64 sml_number_parse(sml_buffer *buf, unsigned char type, int max_size) { - - int l, i; + int l, i, j; + unsigned char b; u64 n = 0; if (sml_buf_get_next_type(buf) != type) { buf->error = 1; @@ -36,13 +36,24 @@ u64 sml_number_parse(sml_buffer *buf, unsigned char type, int max_size) { return 0; } - // TODO: this doesn't work for integers (leading 1's) - // mathias runge: -> fixed bug in sml_value_parse for SML_TYPE_INTEGER -> see comment in sml_value.cpp - // maybe this one is fixed too? - for (i = 0; i < l; i++) { - n <<= 8; - n |= sml_buf_get_current_byte(buf); - sml_buf_update_bytes_read(buf, 1); + b = sml_buf_get_current_byte(buf); + // negative value with leading 1's + if (type == SML_TYPE_INTEGER && b & 128) { + n =~ n; + b = 0xFF; + for (i = 0; i < l; i++) { + n <<= 8; + n |= b; + n = (n & ~b) | sml_buf_get_current_byte(buf); + sml_buf_update_bytes_read(buf, 1); + } + } + else { + for (i = 0; i < l; i++) { + n <<= 8; + n |= sml_buf_get_current_byte(buf); + sml_buf_update_bytes_read(buf, 1); + } } return n; } diff --git a/test/Makefile b/test/Makefile index e2a3641..19e5cc9 100644 --- a/test/Makefile +++ b/test/Makefile @@ -6,12 +6,15 @@ UNITY = \ unity/unity_fixture.o OBJS = \ - src/test_sml_octet_string.o + src/test_helper.o \ + src/sml_octet_string_test.o \ + src/sml_buffer_test.o \ + src/sml_number_test.o test_run: libsml test @./test -test : $(UNITY) $(OBJS) +test : $(UNITY) $(OBJS) $(LIBSML) $(CC) $(CFLAGS) $(LIBSML) $^ test_main.c -o test .PHONY: code @@ -24,7 +27,7 @@ libsml : .PHONY: clean clean : @rm -f unity/*.o - @rm -f src/*.tst + @rm -f src/*.o diff --git a/test/src/sml_buffer_test.c b/test/src/sml_buffer_test.c new file mode 100644 index 0000000..5a7040c --- /dev/null +++ b/test/src/sml_buffer_test.c @@ -0,0 +1,34 @@ + +#include "../unity/unity_fixture.h" +#include + +TEST_GROUP(sml_buffer); + +int buffer_len = 512; +sml_buffer *buf; + +TEST_SETUP(sml_buffer) { + buf = sml_buffer_init(buffer_len); +} + +TEST_TEAR_DOWN(sml_buffer) { + +} + +TEST(sml_buffer, init_defaults) { + + TEST_ASSERT_NOT_NULL(buf); + TEST_ASSERT_NOT_NULL(buf->buffer); + TEST_ASSERT_EQUAL(buffer_len, buf->buffer_len); + TEST_ASSERT_EQUAL(0, buf->cursor); + TEST_ASSERT_EQUAL(0, buf->error); + TEST_ASSERT_NULL(buf->error_msg); +} + +TEST_GROUP_RUNNER(sml_buffer) { + RUN_TEST_CASE(sml_buffer, init_defaults); +} + + + + diff --git a/test/src/sml_number_test.c b/test/src/sml_number_test.c new file mode 100644 index 0000000..b4afeab --- /dev/null +++ b/test/src/sml_number_test.c @@ -0,0 +1,101 @@ + +#include "../unity/unity_fixture.h" +#include "test_helper.h" +#include + +TEST_GROUP(sml_number); + +sml_buffer *buf; + +TEST_SETUP(sml_number) { + buf = sml_buffer_init(512); +} + +TEST_TEAR_DOWN(sml_number) { + +} + +TEST(sml_number, parse_unsigned8) { + hex2binary("6201", sml_buf_get_current_buf(buf)); + u8 n = sml_u8_parse(buf); + TEST_ASSERT_EQUAL(1, n); +} + +TEST(sml_number, parse_unsigned16) { + hex2binary("630101", sml_buf_get_current_buf(buf)); + u16 n = sml_u16_parse(buf); + TEST_ASSERT_EQUAL(257, n); +} + +TEST(sml_number, parse_unsigned32) { + hex2binary("6500000001", sml_buf_get_current_buf(buf)); + u32 n = sml_u32_parse(buf); + TEST_ASSERT_EQUAL(1, n); +} + +TEST(sml_number, parse_unsigned32_fewer_bytes) { + hex2binary("64010001", sml_buf_get_current_buf(buf)); + u32 n = sml_u32_parse(buf); + TEST_ASSERT_EQUAL(65537, n); +} + +TEST(sml_number, parse_unsigned64) { + hex2binary("690000000000000001", sml_buf_get_current_buf(buf)); + u64 n = sml_u64_parse(buf); + TEST_ASSERT_EQUAL(1, n); +} + +TEST(sml_number, parse_unsigned64_fewer_bytes) { + hex2binary("67000000000001", sml_buf_get_current_buf(buf)); + u64 n = sml_u64_parse(buf); + TEST_ASSERT_EQUAL(1, n); +} + + +TEST(sml_number, parse_int8) { + hex2binary("52FF", sml_buf_get_current_buf(buf)); + i8 n = sml_i8_parse(buf); + TEST_ASSERT_EQUAL(-1, n); +} + +TEST(sml_number, parse_int16) { + hex2binary("53EC78", sml_buf_get_current_buf(buf)); + i16 n = sml_i16_parse(buf); + TEST_ASSERT_EQUAL(-5000, n); +} + +TEST(sml_number, parse_int32) { + hex2binary("55FFFFFFFF", sml_buf_get_current_buf(buf)); + i32 n = sml_i32_parse(buf); + TEST_ASSERT_EQUAL(-1, n); +} + +TEST(sml_number, parse_int64) { + hex2binary("59FFFFFFFFFFFFFFFF", sml_buf_get_current_buf(buf)); + i64 n = sml_i64_parse(buf); + TEST_ASSERT_EQUAL(-1, n); +} + +TEST(sml_number, parse_int64_fewer_bytes) { + hex2binary("58FFFFFFFFFFEC78", sml_buf_get_current_buf(buf)); + i64 n = sml_i64_parse(buf); + TEST_ASSERT_EQUAL(-5000, n); +} + +TEST_GROUP_RUNNER(sml_number) { + RUN_TEST_CASE(sml_number, parse_unsigned8); + RUN_TEST_CASE(sml_number, parse_unsigned16); + RUN_TEST_CASE(sml_number, parse_unsigned32); + RUN_TEST_CASE(sml_number, parse_unsigned64); + RUN_TEST_CASE(sml_number, parse_unsigned32_fewer_bytes); + RUN_TEST_CASE(sml_number, parse_unsigned64_fewer_bytes); + RUN_TEST_CASE(sml_number, parse_int8); + RUN_TEST_CASE(sml_number, parse_int16); + RUN_TEST_CASE(sml_number, parse_int32); + RUN_TEST_CASE(sml_number, parse_int64); + RUN_TEST_CASE(sml_number, parse_int64_fewer_bytes); +} + + + + diff --git a/test/src/sml_octet_string_test.c b/test/src/sml_octet_string_test.c new file mode 100644 index 0000000..37bd810 --- /dev/null +++ b/test/src/sml_octet_string_test.c @@ -0,0 +1,51 @@ + +#include "../unity/unity_fixture.h" +#include "test_helper.h" +#include + +TEST_GROUP(sml_octet_string); + +sml_buffer *buf; +void expected_octet_string(octet_string *str, char *content, int len) { + TEST_ASSERT_NOT_NULL(str); + TEST_ASSERT_EQUAL(len, str->len); + TEST_ASSERT_EQUAL_MEMORY(content, str->str, len); +} + +TEST_SETUP(sml_octet_string) { + buf = sml_buffer_init(512); +} + +TEST_TEAR_DOWN(sml_octet_string) { + +} + +TEST(sml_octet_string, init) { + octet_string *str = sml_octet_string_init((unsigned char *)"hallo", 5); + TEST_ASSERT_EQUAL(5, str->len); + TEST_ASSERT_EQUAL_MEMORY("hallo", str->str, 5); +} + +TEST(sml_octet_string, parse) { + hex2binary("0648616C6C6F", sml_buf_get_current_buf(buf)); + + octet_string *str = sml_octet_string_parse(buf); + expected_octet_string(str, "Hallo", 5); +} + +TEST(sml_octet_string, parse_multiple_tl_fields) { + hex2binary("8102616161616F6161616161616161616161", sml_buf_get_current_buf(buf)); + + octet_string *str = sml_octet_string_parse(buf); + expected_octet_string(str, "aaaaoaaaaaaaaaaa", 16); +} + +TEST_GROUP_RUNNER(sml_octet_string) { + RUN_TEST_CASE(sml_octet_string, init); + RUN_TEST_CASE(sml_octet_string, parse); + RUN_TEST_CASE(sml_octet_string, parse_multiple_tl_fields); +} + + + + diff --git a/test/src/test_helper.c b/test/src/test_helper.c new file mode 100644 index 0000000..809c260 --- /dev/null +++ b/test/src/test_helper.c @@ -0,0 +1,36 @@ + +#include "test_helper.h" +#include +#include + +uint8_t test_helper_ctoi(uint8_t c){ + uint8_t ret = 0; + + if((c >= '0') && (c <= '9')){ + ret = c - '0'; + } else if((c >= 'a') && (c <= 'f')){ + ret = c - 'a' + 10; + } else if((c >= 'A') && (c <= 'F')){ + ret = c - 'A' + 10; + } + + return ret; +} + +inline uint8_t test_helper_c2toi(uint8_t c1, uint8_t c2){ + return test_helper_ctoi(c1) << 4 | test_helper_ctoi(c2); +} + +inline uint8_t test_helper_c2ptoi(char* c){ + return test_helper_ctoi((uint8_t)c[0]) << 4 | test_helper_ctoi((uint8_t)c[1]); +} + +int hex2binary(char *hex, unsigned char *buf) { + int i; + int len = strlen(hex); + for (i = 0; i < (len /2); i++) { + buf[i] = test_helper_c2ptoi(&(hex[i * 2])); + } + return i; +} + diff --git a/test/src/test_helper.h b/test/src/test_helper.h new file mode 100644 index 0000000..7b582a6 --- /dev/null +++ b/test/src/test_helper.h @@ -0,0 +1,7 @@ + +#ifndef TEST_HELPER_H_ +#define TEST_HELPER_H_ + +int hex2binary(char *hex, unsigned char *buf); + +#endif diff --git a/test/src/test_sml_octet_string.c b/test/src/test_sml_octet_string.c deleted file mode 100644 index feba807..0000000 --- a/test/src/test_sml_octet_string.c +++ /dev/null @@ -1,26 +0,0 @@ - -#include "../unity/unity_fixture.h" -#include - -TEST_GROUP(sml_octet_string); - -TEST_SETUP(sml_octet_string) { - -} - -TEST_TEAR_DOWN(sml_octet_string) { - -} - -TEST(sml_octet_string, init) { - octet_string *str = sml_octet_string_init((unsigned char *)"hallo", 5); - TEST_ASSERT_TRUE(str->len == 5) -} - -TEST_GROUP_RUNNER(sml_octet_string) { - RUN_TEST_CASE(sml_octet_string, init); -} - - - - diff --git a/test/test_main.c b/test/test_main.c index fe0b339..58888fd 100644 --- a/test/test_main.c +++ b/test/test_main.c @@ -2,6 +2,8 @@ static void runAllTests() { RUN_TEST_GROUP(sml_octet_string); + RUN_TEST_GROUP(sml_buffer); + RUN_TEST_GROUP(sml_number); } int main(int argc, char * argv[]) {