bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen#include "test-lib.h"
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen#include "guid.h"
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek#include "ioloop.h"
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek/*
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * We want earlier timestamps to compare as < with later timestamps, but
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * guid_128_cmp() doesn't do that because the timestamps in the guid are
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * stored in little-endian byte order.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipekstatic int reverse_guid_128_cmp(const guid_128_t a, const guid_128_t b)
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek{
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek int i;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek for (i = GUID_128_SIZE - 1; i >= 0; i--)
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek if (a[i] != b[i])
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek return (int)a[i] - (int)b[i];
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek return 0;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek}
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipekstatic bool guid_128_has_sane_nsecs(const guid_128_t g)
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek{
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek unsigned long nsecs;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
b9e830a81455faf3c0dadfc9dbf0c7dc8aca955cJosef 'Jeff' Sipek nsecs = le32_to_cpu_unaligned(g);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek return nsecs < 1000000000UL;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek}
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipekstatic inline void set_fake_time(time_t sec, long usec)
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek{
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek ioloop_timeval.tv_sec = sec;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek ioloop_timeval.tv_usec = usec;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek}
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek/*
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * We muck with the ioloop_timeval in various ways and make sure that the
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * guids that get generated make sense. To make sure that the guid
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * generation code takes up our faked timestamp, we use a far-away time (Jan
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * 1 2038) as the base time. We don't want to go beyond 32-bit signed
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * time_t for the base time to avoid issues on systems with 32-bit signed
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * time_t.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek *
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * While guids really only need to be unique, here we actually enforce that
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * they are increasing (as defined by reverse_guid_128_cmp()). If guids are
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * always increasing, they will always be unique.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipekstatic void test_ioloop_guid_128_generate(void)
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek{
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek const time_t basetime = 2145909600; /* Jan 1 2038 */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek struct timeval saved_ioloop_timeval;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek guid_128_t guids[2];
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek int i;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /* save the ioloop_timeval before we start messing with it */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek saved_ioloop_timeval = ioloop_timeval;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /*
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * Generating multiple guids within a microsecond should keep
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * incrementing them.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() increasing guid within a usec");
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek set_fake_time(basetime, 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek guid_128_generate(guids[1]);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek for (i = 0; i < 10; i++) {
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek const int this = i % 2;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek const int prev = 1 - this;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek guid_128_generate(guids[this]);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(reverse_guid_128_cmp(guids[prev], guids[this]) < 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(guid_128_has_sane_nsecs(guids[this]));
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek }
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_end();
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /*
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * If the current time changes by +1 usec, so should the guids.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() increasing guid with usec fast-forward");
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek for (i = 0; i < 10; i++) {
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek const int this = i % 2;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek const int prev = 1 - this;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek set_fake_time(basetime, 1 + i);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek guid_128_generate(guids[this]);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(reverse_guid_128_cmp(guids[prev], guids[this]) < 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(guid_128_has_sane_nsecs(guids[this]));
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek }
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_end();
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /*
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * If the current time changes by +1 sec, so should the guids.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() increasing guid with sec fast-forward");
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek for (i = 0; i < 10; i++) {
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek const int this = i % 2;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek const int prev = 1 - this;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek set_fake_time(basetime + 1 + i, 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek guid_128_generate(guids[this]);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(reverse_guid_128_cmp(guids[prev], guids[this]) < 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(guid_128_has_sane_nsecs(guids[this]));
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek }
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_end();
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /*
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * Requesting enough guids should increment the seconds but always
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * produce valid nsecs.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek *
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * (Set a time that leaves us 1000 guids before seconds overflow and
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * then ask for 2500 guids.)
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() proper guid nsec overflow");
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek set_fake_time(basetime + 11, 999999L);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek for (i = 0; i < 2500; i++) {
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek const int this = i % 2;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek const int prev = 1 - this;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek guid_128_generate(guids[this]);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(reverse_guid_128_cmp(guids[prev], guids[this]) < 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(guid_128_has_sane_nsecs(guids[this]));
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek }
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_end();
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /*
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * When ahead by 1500 guids (see previous test), +1 usec shouldn't
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * have any effect.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() no effect with increasing time when ahead");
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek set_fake_time(basetime + 12, 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek guid_128_generate(guids[0]);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(reverse_guid_128_cmp(guids[1], guids[0]) < 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(guid_128_has_sane_nsecs(guids[0]));
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_end();
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /* not a test - just set a more convenient time */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek set_fake_time(basetime + 15, 500);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek guid_128_generate(guids[1]);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(reverse_guid_128_cmp(guids[0], guids[1]) < 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(guid_128_has_sane_nsecs(guids[1]));
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /*
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * Time going backwards by 1 usec should have no effect on guids.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() usec time-travel still increasing");
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek set_fake_time(basetime + 15, 499);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek guid_128_generate(guids[0]);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(reverse_guid_128_cmp(guids[1], guids[0]) < 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(guid_128_has_sane_nsecs(guids[0]));
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_end();
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /*
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * Time going backwards by 1 sec should have no effect on guids.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() sec time-travel still increasing");
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek set_fake_time(basetime + 14, 499);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek guid_128_generate(guids[1]);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(reverse_guid_128_cmp(guids[0], guids[1]) < 0);
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_assert(guid_128_has_sane_nsecs(guids[1]));
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_end();
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /* restore the previously saved value just in case */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek ioloop_timeval = saved_ioloop_timeval;
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek}
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainenvoid test_guid(void)
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen{
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen static const guid_128_t test_guid =
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen { 0x01, 0x23, 0x45, 0x67, 0x89,
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen 0xab, 0xcd, 0xef,
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen 0xAB, 0xCD, 0xEF,
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen 0x00, 0x00, 0x00, 0x00, 0x00 };
33750ba29b605a925af5aed58d3f3735422b1e25Phil Carmody guid_128_t guid1, guid2, guid3;
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen const char *str;
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen char guidbuf[GUID_128_SIZE*2 + 2];
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen unsigned int i;
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_begin("guid_128_generate()");
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guid_128_generate(guid1);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guid_128_generate(guid2);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(!guid_128_equals(guid1, guid2));
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_cmp(guid1, guid2) != 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_end();
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_begin("guid_128_is_empty()");
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(!guid_128_is_empty(guid1));
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(!guid_128_is_empty(guid2));
33750ba29b605a925af5aed58d3f3735422b1e25Phil Carmody guid_128_generate(guid3);
33750ba29b605a925af5aed58d3f3735422b1e25Phil Carmody guid_128_empty(guid3);
33750ba29b605a925af5aed58d3f3735422b1e25Phil Carmody test_assert(guid_128_is_empty(guid3));
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_end();
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_begin("guid_128_copy()");
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guid_128_copy(guid3, guid1);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_equals(guid3, guid1));
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(!guid_128_equals(guid3, guid2));
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guid_128_copy(guid3, guid2);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(!guid_128_equals(guid3, guid1));
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_equals(guid3, guid2));
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_end();
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_begin("guid_128_to_string()");
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen str = guid_128_to_string(guid1);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(str, guid3) == 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_equals(guid3, guid1));
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_end();
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_begin("guid_128_from_string()");
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen /* empty */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen memset(guidbuf, '0', GUID_128_SIZE*2);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[GUID_128_SIZE*2] = '\0';
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[GUID_128_SIZE*2+1] = '\0';
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) == 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_is_empty(guid3));
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen /* too large */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[GUID_128_SIZE*2] = '0';
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) < 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen /* too small */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[GUID_128_SIZE*2-1] = '\0';
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) < 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen /* reset to normal */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[GUID_128_SIZE*2-1] = '0';
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[GUID_128_SIZE*2] = '\0';
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) == 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen /* upper + lowercase hex chars */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen i_assert(GUID_128_SIZE*2 > 16 + 6);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen for (i = 0; i < 10; i++)
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[i] = '0' + i;
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen for (i = 0; i < 6; i++)
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[10 + i] = 'a' + i;
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen for (i = 0; i < 6; i++)
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[16 + i] = 'A' + i;
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) == 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_equals(guid3, test_guid));
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen /* non-hex chars */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[0] = 'g';
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) < 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen guidbuf[0] = ' ';
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) < 0);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi test_assert(guid_128_from_uuid_string("fee0ceac-0327-11e7-ad39-52540078f374", guid3) == 0);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi test_assert(guid_128_from_uuid_string("fee0ceac032711e7ad3952540078f374", guid2) == 0);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi test_assert(guid_128_cmp(guid3, guid2) == 0);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi test_assert(guid_128_from_uuid_string("{fee0ceac-0327-11e7-ad39-52540078f374}", guid2) == 0);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi test_assert(guid_128_cmp(guid3, guid2) == 0);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi test_assert(strcmp(guid_128_to_uuid_string(guid3, FORMAT_RECORD), "fee0ceac-0327-11e7-ad39-52540078f374")==0);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi test_assert(strcmp(guid_128_to_uuid_string(guid3, FORMAT_COMPACT), "fee0ceac032711e7ad3952540078f374")==0);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi test_assert(strcmp(guid_128_to_uuid_string(guid3, FORMAT_MICROSOFT), "{fee0ceac-0327-11e7-ad39-52540078f374}")==0);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi /* failure test */
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi test_assert(guid_128_from_uuid_string("fe-e0ceac-0327-11e7-ad39-52540078f374", guid3) < 0);
7c681809344b880aae61e99b6ab74ae19945ff9eAki Tuomi
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_end();
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_ioloop_guid_128_generate();
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen}