bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */
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' Sipekstatic int reverse_guid_128_cmp(const guid_128_t a, const guid_128_t b)
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek if (a[i] != b[i])
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek return (int)a[i] - (int)b[i];
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipekstatic bool guid_128_has_sane_nsecs(const guid_128_t g)
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipekstatic inline void set_fake_time(time_t sec, long usec)
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 * 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' Sipekstatic void test_ioloop_guid_128_generate(void)
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek const time_t basetime = 2145909600; /* Jan 1 2038 */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek /* save the ioloop_timeval before we start messing with it */
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * Generating multiple guids within a microsecond should keep
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * incrementing them.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() increasing guid within a usec");
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek for (i = 0; i < 10; i++) {
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 * If the current time changes by +1 usec, so should the guids.
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 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 * If the current time changes by +1 sec, so should the guids.
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 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 * Requesting enough guids should increment the seconds but always
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * produce valid nsecs.
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 test_begin("guid_128_generate() proper guid nsec overflow");
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek for (i = 0; i < 2500; i++) {
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 * When ahead by 1500 guids (see previous test), +1 usec shouldn't
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek * have any effect.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() no effect with increasing time when ahead");
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 /* not a test - just set a more convenient time */
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 * Time going backwards by 1 usec should have no effect on guids.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() usec time-travel still increasing");
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 * Time going backwards by 1 sec should have no effect on guids.
c536bf22651c97ad0369864897f1920625e53ea9Josef 'Jeff' Sipek test_begin("guid_128_generate() sec time-travel still increasing");
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 /* restore the previously saved value just in case */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen unsigned int i;
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(str, guid3) == 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) == 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen /* too large */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) < 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen /* too small */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) < 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen /* reset to normal */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) == 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen /* upper + lowercase hex chars */
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen for (i = 0; i < 10; i++)
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen for (i = 0; i < 6; i++)
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen for (i = 0; i < 6; 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 test_assert(guid_128_from_string(guidbuf, guid3) < 0);
3bb59b47d47cba85a92df67823b0e49d2c383307Timo Sirainen test_assert(guid_128_from_string(guidbuf, guid3) < 0);
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_from_uuid_string("{fee0ceac-0327-11e7-ad39-52540078f374}", 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);