Home | History | Annotate | Download | only in uuid
      1 /*
      2  * tst_uuid.c --- test program from the UUID library
      3  *
      4  * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
      5  *
      6  * %Begin-Header%
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, and the entire permission notice in its entirety,
     12  *    including the disclaimer of warranties.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. The name of the author may not be used to endorse or promote
     17  *    products derived from this software without specific prior
     18  *    written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
     21  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
     23  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
     24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
     26  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     27  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
     28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
     30  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
     31  * DAMAGE.
     32  * %End-Header%
     33  */
     34 
     35 
     36 #ifdef _WIN32
     37 #define _WIN32_WINNT 0x0500
     38 #include <windows.h>
     39 #define UUID MYUUID
     40 #endif
     41 
     42 #include <stdio.h>
     43 #include <stdlib.h>
     44 
     45 #include <uuid/uuid.h>
     46 
     47 static int test_uuid(const char * uuid, int isValid)
     48 {
     49 	static const char * validStr[2] = {"invalid", "valid"};
     50 	uuid_t uuidBits;
     51 	int parsedOk;
     52 
     53 	parsedOk = uuid_parse(uuid, uuidBits) == 0;
     54 
     55 	printf("%s is %s", uuid, validStr[isValid]);
     56 	if (parsedOk != isValid) {
     57 		printf(" but uuid_parse says %s\n", validStr[parsedOk]);
     58 		return 1;
     59 	}
     60 	printf(", OK\n");
     61 	return 0;
     62 }
     63 
     64 #ifdef __GNUC__
     65 #define ATTR(x) __attribute__(x)
     66 #else
     67 #define ATTR(x)
     68 #endif
     69 
     70 int
     71 main(int argc ATTR((unused)) , char **argv ATTR((unused)))
     72 {
     73 	uuid_t		buf, tst;
     74 	char		str[100];
     75 	struct timeval	tv;
     76 	time_t		time_reg, time_gen;
     77 	unsigned char	*cp;
     78 	int i;
     79 	int failed = 0;
     80 	int type, variant;
     81 
     82 	uuid_generate(buf);
     83 	uuid_unparse(buf, str);
     84 	printf("UUID generate = %s\n", str);
     85 	printf("UUID: ");
     86 	for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
     87 		printf("%02x", *cp++);
     88 	}
     89 	printf("\n");
     90 	type = uuid_type(buf); 	variant = uuid_variant(buf);
     91 	printf("UUID type = %d, UUID variant = %d\n", type, variant);
     92 	if (variant != UUID_VARIANT_DCE) {
     93 		printf("Incorrect UUID Variant; was expecting DCE!\n");
     94 		failed++;
     95 	}
     96 	printf("\n");
     97 
     98 	uuid_generate_random(buf);
     99 	uuid_unparse(buf, str);
    100 	printf("UUID random string = %s\n", str);
    101 	printf("UUID: ");
    102 	for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
    103 		printf("%02x", *cp++);
    104 	}
    105 	printf("\n");
    106 	type = uuid_type(buf);
    107 	variant = uuid_variant(buf);
    108 	printf("UUID type = %d, UUID variant = %d\n", type, variant);
    109 	if (variant != UUID_VARIANT_DCE) {
    110 		printf("Incorrect UUID Variant; was expecting DCE!\n");
    111 		failed++;
    112 	}
    113 	if (type != 4) {
    114 		printf("Incorrect UUID type; was expecting "
    115 		       "4 (random type)!\n");
    116 		failed++;
    117 	}
    118 	printf("\n");
    119 
    120 	time_gen = time(0);
    121 	uuid_generate_time(buf);
    122 	uuid_unparse(buf, str);
    123 	printf("UUID string = %s\n", str);
    124 	printf("UUID time: ");
    125 	for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
    126 		printf("%02x", *cp++);
    127 	}
    128 	printf("\n");
    129 	type = uuid_type(buf);
    130 	variant = uuid_variant(buf);
    131 	printf("UUID type = %d, UUID variant = %d\n", type, variant);
    132 	if (variant != UUID_VARIANT_DCE) {
    133 		printf("Incorrect UUID Variant; was expecting DCE!\n");
    134 		failed++;
    135 	}
    136 	if (type != 1) {
    137 		printf("Incorrect UUID type; was expecting "
    138 		       "1 (time-based type)!\\n");
    139 		failed++;
    140 	}
    141 
    142 	tv.tv_sec = 0;
    143 	tv.tv_usec = 0;
    144 	time_reg = uuid_time(buf, &tv);
    145 	printf("UUID generated at %lu reports %lu (%ld.%ld)\n",
    146 	       time_gen, time_reg, tv.tv_sec, (long)tv.tv_usec);
    147 	/* allow 1s margin in case of rollover between sampling
    148 	 * the current time and when the UUID is generated. */
    149 	if (time_reg > time_gen + 1) {
    150 		printf("UUID time comparison failed!\n");
    151 		failed++;
    152 	} else {
    153 		printf("UUID time comparison succeeded.\n");
    154 	}
    155 
    156 	uuid_parse(str, tst);
    157 	if (!uuid_compare(buf, tst)) {
    158 		printf("UUID parse and compare succeeded.\n");
    159 	} else {
    160 		printf("UUID parse and compare failed!\n");
    161 		failed++;
    162 	}
    163 	uuid_clear(tst);
    164 	if (uuid_is_null(tst))
    165 		printf("UUID clear and is null succeeded.\n");
    166 	else {
    167 		printf("UUID clear and is null failed!\n");
    168 		failed++;
    169 	}
    170 	uuid_copy(buf, tst);
    171 	if (!uuid_compare(buf, tst))
    172 		printf("UUID copy and compare succeeded.\n");
    173 	else {
    174 		printf("UUID copy and compare failed!\n");
    175 		failed++;
    176 	}
    177 
    178 	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
    179 	failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
    180 	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
    181 	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
    182 	failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
    183 	failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
    184 	failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
    185 	failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
    186 	failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
    187 	failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
    188 	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
    189 
    190 	if (failed) {
    191 		printf("%d failures.\n", failed);
    192 		exit(1);
    193 	}
    194 	return 0;
    195 }
    196