1997-04-29 20:17:09 +04:00
|
|
|
/*
|
|
|
|
* tst_uuid.c --- test program from the UUID library
|
|
|
|
*
|
1998-12-04 11:13:52 +03:00
|
|
|
* Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
|
1997-04-29 20:17:09 +04:00
|
|
|
*
|
|
|
|
* %Begin-Header%
|
2004-03-20 22:02:24 +03:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, and the entire permission notice in its entirety,
|
|
|
|
* including the disclaimer of warranties.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
|
|
|
|
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
|
|
|
|
* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
|
|
|
|
* DAMAGE.
|
1997-04-29 20:17:09 +04:00
|
|
|
* %End-Header%
|
|
|
|
*/
|
|
|
|
|
2008-08-22 20:15:56 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#define _WIN32_WINNT 0x0500
|
|
|
|
#include <windows.h>
|
|
|
|
#define UUID MYUUID
|
|
|
|
#endif
|
|
|
|
|
1997-04-29 18:53:37 +04:00
|
|
|
#include <stdio.h>
|
2003-12-28 15:04:35 +03:00
|
|
|
#include <stdlib.h>
|
1997-04-29 18:53:37 +04:00
|
|
|
|
|
|
|
#include "uuid.h"
|
|
|
|
|
2002-07-16 07:49:57 +04:00
|
|
|
static int test_uuid(const char * uuid, int isValid)
|
|
|
|
{
|
|
|
|
static const char * validStr[2] = {"invalid", "valid"};
|
|
|
|
uuid_t uuidBits;
|
|
|
|
int parsedOk;
|
|
|
|
|
|
|
|
parsedOk = uuid_parse(uuid, uuidBits) == 0;
|
2003-12-28 15:04:35 +03:00
|
|
|
|
2002-07-16 07:49:57 +04:00
|
|
|
printf("%s is %s", uuid, validStr[isValid]);
|
|
|
|
if (parsedOk != isValid) {
|
|
|
|
printf(" but uuid_parse says %s\n", validStr[parsedOk]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf(", OK\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-06-07 19:53:20 +04:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define ATTR(x) __attribute__(x)
|
|
|
|
#else
|
|
|
|
#define ATTR(x)
|
|
|
|
#endif
|
|
|
|
|
1997-04-29 18:53:37 +04:00
|
|
|
int
|
2008-06-07 19:53:20 +04:00
|
|
|
main(int argc ATTR((unused)) , char **argv ATTR((unused)))
|
1997-04-29 18:53:37 +04:00
|
|
|
{
|
|
|
|
uuid_t buf, tst;
|
|
|
|
char str[100];
|
1998-12-04 11:13:52 +03:00
|
|
|
struct timeval tv;
|
|
|
|
time_t time_reg;
|
1997-04-29 18:53:37 +04:00
|
|
|
unsigned char *cp;
|
|
|
|
int i;
|
|
|
|
int failed = 0;
|
1999-06-18 04:32:03 +04:00
|
|
|
int type, variant;
|
1997-04-29 18:53:37 +04:00
|
|
|
|
|
|
|
uuid_generate(buf);
|
|
|
|
uuid_unparse(buf, str);
|
1999-06-18 04:32:03 +04:00
|
|
|
printf("UUID generate = %s\n", str);
|
|
|
|
printf("UUID: ");
|
|
|
|
for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
|
|
|
|
printf("%02x", *cp++);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
type = uuid_type(buf); variant = uuid_variant(buf);
|
|
|
|
printf("UUID type = %d, UUID variant = %d\n", type, variant);
|
|
|
|
if (variant != UUID_VARIANT_DCE) {
|
|
|
|
printf("Incorrect UUID Variant; was expecting DCE!\n");
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
uuid_generate_random(buf);
|
|
|
|
uuid_unparse(buf, str);
|
|
|
|
printf("UUID random string = %s\n", str);
|
1997-04-29 18:53:37 +04:00
|
|
|
printf("UUID: ");
|
|
|
|
for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
|
|
|
|
printf("%02x", *cp++);
|
|
|
|
}
|
|
|
|
printf("\n");
|
1999-06-18 04:32:03 +04:00
|
|
|
type = uuid_type(buf); variant = uuid_variant(buf);
|
|
|
|
printf("UUID type = %d, UUID variant = %d\n", type, variant);
|
|
|
|
if (variant != UUID_VARIANT_DCE) {
|
|
|
|
printf("Incorrect UUID Variant; was expecting DCE!\n");
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
if (type != 4) {
|
|
|
|
printf("Incorrect UUID type; was expecting "
|
|
|
|
"4 (random type)!\n");
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
printf("\n");
|
2003-12-28 15:04:35 +03:00
|
|
|
|
1999-06-18 04:32:03 +04:00
|
|
|
uuid_generate_time(buf);
|
|
|
|
uuid_unparse(buf, str);
|
|
|
|
printf("UUID string = %s\n", str);
|
|
|
|
printf("UUID time: ");
|
|
|
|
for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
|
|
|
|
printf("%02x", *cp++);
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
type = uuid_type(buf); variant = uuid_variant(buf);
|
|
|
|
printf("UUID type = %d, UUID variant = %d\n", type, variant);
|
|
|
|
if (variant != UUID_VARIANT_DCE) {
|
|
|
|
printf("Incorrect UUID Variant; was expecting DCE!\n");
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
if (type != 1) {
|
|
|
|
printf("Incorrect UUID type; was expecting "
|
|
|
|
"1 (time-based type)!\\n");
|
|
|
|
failed++;
|
|
|
|
}
|
1998-12-04 11:13:52 +03:00
|
|
|
tv.tv_sec = 0;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
time_reg = uuid_time(buf, &tv);
|
2001-01-12 21:30:54 +03:00
|
|
|
printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
|
1998-12-04 11:13:52 +03:00
|
|
|
ctime(&time_reg));
|
1997-04-29 18:53:37 +04:00
|
|
|
uuid_parse(str, tst);
|
1999-06-18 04:32:03 +04:00
|
|
|
if (!uuid_compare(buf, tst))
|
1997-04-29 18:53:37 +04:00
|
|
|
printf("UUID parse and compare succeeded.\n");
|
|
|
|
else {
|
|
|
|
printf("UUID parse and compare failed!\n");
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
uuid_clear(tst);
|
|
|
|
if (uuid_is_null(tst))
|
|
|
|
printf("UUID clear and is null succeeded.\n");
|
|
|
|
else {
|
|
|
|
printf("UUID clear and is null failed!\n");
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
uuid_copy(buf, tst);
|
1999-06-18 04:32:03 +04:00
|
|
|
if (!uuid_compare(buf, tst))
|
1997-04-29 18:53:37 +04:00
|
|
|
printf("UUID copy and compare succeeded.\n");
|
|
|
|
else {
|
|
|
|
printf("UUID copy and compare failed!\n");
|
|
|
|
failed++;
|
|
|
|
}
|
2002-07-16 07:49:57 +04:00
|
|
|
failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
|
|
|
|
failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
|
|
|
|
failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
|
|
|
|
failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
|
|
|
|
failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
|
|
|
|
failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
|
|
|
|
failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
|
|
|
|
failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
|
|
|
|
failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
|
|
|
|
failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
|
|
|
|
failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
|
2003-12-28 15:04:35 +03:00
|
|
|
|
1997-04-29 18:53:37 +04:00
|
|
|
if (failed) {
|
|
|
|
printf("%d failures.\n", failed);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|