]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Added unit tests for guid_128_*()
authorTimo Sirainen <tss@iki.fi>
Wed, 22 Oct 2014 02:40:44 +0000 (05:40 +0300)
committerTimo Sirainen <tss@iki.fi>
Wed, 22 Oct 2014 02:40:44 +0000 (05:40 +0300)
src/lib/Makefile.am
src/lib/test-guid.c [new file with mode: 0644]
src/lib/test-lib.c
src/lib/test-lib.h

index d607d185c05b4da4a02defbacd7acfca3caa7102..d1d0edc51c15c21ae457ed07f55c0038a8e3bab2 100644 (file)
@@ -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 (file)
index 0000000..5657d6c
--- /dev/null
@@ -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();
+}
index 0dba0902cbe6bd240410073e6660e7094fc2fef0..693b9fde34fca8875bb19a6fa9353b6629723645 100644 (file)
@@ -13,6 +13,7 @@ int main(void)
                test_buffer,
                test_crc32,
                test_data_stack,
+               test_guid,
                test_hash,
                test_hash_format,
                test_hash_method,
index 6786f0246a1fe5f4f7744da2e42bbc3b10453123..b1d5b4828843f792d4f65cbfb786759e604342c1 100644 (file)
@@ -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);