From: Timo Sirainen Date: Wed, 22 Oct 2014 02:40:44 +0000 (+0300) Subject: lib: Added unit tests for guid_128_*() X-Git-Tag: 2.2.15~36 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3bb59b47d47cba85a92df67823b0e49d2c383307;p=thirdparty%2Fdovecot%2Fcore.git lib: Added unit tests for guid_128_*() --- diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index d607d185c0..d1d0edc51c 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -282,6 +282,7 @@ test_lib_SOURCES = \ test-buffer.c \ test-crc32.c \ test-data-stack.c \ + test-guid.c \ test-hash.c \ test-hash-format.c \ test-hash-method.c \ diff --git a/src/lib/test-guid.c b/src/lib/test-guid.c new file mode 100644 index 0000000000..5657d6c6b2 --- /dev/null +++ b/src/lib/test-guid.c @@ -0,0 +1,81 @@ +/* Copyright (c) 2014 Dovecot authors, see the included COPYING file */ + +#include "test-lib.h" +#include "guid.h" + +void test_guid(void) +{ + static const guid_128_t test_guid = + { 0x01, 0x23, 0x45, 0x67, 0x89, + 0xab, 0xcd, 0xef, + 0xAB, 0xCD, 0xEF, + 0x00, 0x00, 0x00, 0x00, 0x00 }; + guid_128_t guid1, guid2, guid3, empty_guid; + const char *str; + char guidbuf[GUID_128_SIZE*2 + 2]; + unsigned int i; + + memset(empty_guid, 0, sizeof(empty_guid)); + + test_begin("guid_128_generate()"); + guid_128_generate(guid1); + guid_128_generate(guid2); + test_assert(!guid_128_equals(guid1, guid2)); + test_assert(guid_128_cmp(guid1, guid2) != 0); + test_end(); + + test_begin("guid_128_is_empty()"); + test_assert(!guid_128_is_empty(guid1)); + test_assert(!guid_128_is_empty(guid2)); + test_assert(guid_128_is_empty(empty_guid)); + test_end(); + + test_begin("guid_128_copy()"); + guid_128_copy(guid3, guid1); + test_assert(guid_128_equals(guid3, guid1)); + test_assert(!guid_128_equals(guid3, guid2)); + guid_128_copy(guid3, guid2); + test_assert(!guid_128_equals(guid3, guid1)); + test_assert(guid_128_equals(guid3, guid2)); + test_end(); + + test_begin("guid_128_to_string()"); + str = guid_128_to_string(guid1); + test_assert(guid_128_from_string(str, guid3) == 0); + test_assert(guid_128_equals(guid3, guid1)); + test_end(); + + test_begin("guid_128_from_string()"); + /* empty */ + memset(guidbuf, '0', GUID_128_SIZE*2); + guidbuf[GUID_128_SIZE*2] = '\0'; + guidbuf[GUID_128_SIZE*2+1] = '\0'; + test_assert(guid_128_from_string(guidbuf, guid3) == 0); + test_assert(guid_128_is_empty(guid3)); + /* too large */ + guidbuf[GUID_128_SIZE*2] = '0'; + test_assert(guid_128_from_string(guidbuf, guid3) < 0); + /* too small */ + guidbuf[GUID_128_SIZE*2-1] = '\0'; + test_assert(guid_128_from_string(guidbuf, guid3) < 0); + /* reset to normal */ + guidbuf[GUID_128_SIZE*2-1] = '0'; + guidbuf[GUID_128_SIZE*2] = '\0'; + test_assert(guid_128_from_string(guidbuf, guid3) == 0); + /* upper + lowercase hex chars */ + i_assert(GUID_128_SIZE*2 > 16 + 6); + for (i = 0; i < 10; i++) + guidbuf[i] = '0' + i; + for (i = 0; i < 6; i++) + guidbuf[10 + i] = 'a' + i; + for (i = 0; i < 6; i++) + guidbuf[16 + i] = 'A' + i; + test_assert(guid_128_from_string(guidbuf, guid3) == 0); + test_assert(guid_128_equals(guid3, test_guid)); + /* non-hex chars */ + guidbuf[0] = 'g'; + test_assert(guid_128_from_string(guidbuf, guid3) < 0); + guidbuf[0] = ' '; + test_assert(guid_128_from_string(guidbuf, guid3) < 0); + test_end(); +} diff --git a/src/lib/test-lib.c b/src/lib/test-lib.c index 0dba0902cb..693b9fde34 100644 --- a/src/lib/test-lib.c +++ b/src/lib/test-lib.c @@ -13,6 +13,7 @@ int main(void) test_buffer, test_crc32, test_data_stack, + test_guid, test_hash, test_hash_format, test_hash_method, diff --git a/src/lib/test-lib.h b/src/lib/test-lib.h index 6786f0246a..b1d5b48288 100644 --- a/src/lib/test-lib.h +++ b/src/lib/test-lib.h @@ -13,6 +13,7 @@ void test_buffer(void); void test_crc32(void); void test_data_stack(void); enum fatal_test_state fatal_data_stack(int); +void test_guid(void); void test_hash(void); void test_hash_format(void); void test_hash_method(void);