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