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 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12#include <stdio.h>
13
14#include "uuid.h"
15
16static int test_uuid(const char * uuid, int isValid)
17{
18	static const char * validStr[2] = {"invalid", "valid"};
19	uuid_t uuidBits;
20	int parsedOk;
21
22	parsedOk = uuid_parse(uuid, uuidBits) == 0;
23
24	printf("%s is %s", uuid, validStr[isValid]);
25	if (parsedOk != isValid) {
26		printf(" but uuid_parse says %s\n", validStr[parsedOk]);
27		return 1;
28	}
29	printf(", OK\n");
30	return 0;
31}
32
33int
34main(int argc, char **argv)
35{
36	uuid_t		buf, tst;
37	char		str[100];
38	struct timeval	tv;
39	time_t		time_reg;
40	unsigned char	*cp;
41	int i;
42	int failed = 0;
43	int type, variant;
44
45	uuid_generate(buf);
46	uuid_unparse(buf, str);
47	printf("UUID generate = %s\n", str);
48	printf("UUID: ");
49	for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
50		printf("%02x", *cp++);
51	}
52	printf("\n");
53	type = uuid_type(buf); 	variant = uuid_variant(buf);
54	printf("UUID type = %d, UUID variant = %d\n", type, variant);
55	if (variant != UUID_VARIANT_DCE) {
56		printf("Incorrect UUID Variant; was expecting DCE!\n");
57		failed++;
58	}
59	printf("\n");
60
61	uuid_generate_random(buf);
62	uuid_unparse(buf, str);
63	printf("UUID random string = %s\n", str);
64	printf("UUID: ");
65	for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
66		printf("%02x", *cp++);
67	}
68	printf("\n");
69	type = uuid_type(buf); 	variant = uuid_variant(buf);
70	printf("UUID type = %d, UUID variant = %d\n", type, variant);
71	if (variant != UUID_VARIANT_DCE) {
72		printf("Incorrect UUID Variant; was expecting DCE!\n");
73		failed++;
74	}
75	if (type != 4) {
76		printf("Incorrect UUID type; was expecting "
77		       "4 (random type)!\n");
78		failed++;
79	}
80	printf("\n");
81
82	uuid_generate_time(buf);
83	uuid_unparse(buf, str);
84	printf("UUID string = %s\n", str);
85	printf("UUID time: ");
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	if (type != 1) {
97		printf("Incorrect UUID type; was expecting "
98		       "1 (time-based type)!\\n");
99		failed++;
100	}
101	tv.tv_sec = 0;
102	tv.tv_usec = 0;
103	time_reg = uuid_time(buf, &tv);
104	printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
105	       ctime(&time_reg));
106	uuid_parse(str, tst);
107	if (!uuid_compare(buf, tst))
108		printf("UUID parse and compare succeeded.\n");
109	else {
110		printf("UUID parse and compare failed!\n");
111		failed++;
112	}
113	uuid_clear(tst);
114	if (uuid_is_null(tst))
115		printf("UUID clear and is null succeeded.\n");
116	else {
117		printf("UUID clear and is null failed!\n");
118		failed++;
119	}
120	uuid_copy(buf, tst);
121	if (!uuid_compare(buf, tst))
122		printf("UUID copy and compare succeeded.\n");
123	else {
124		printf("UUID copy and compare failed!\n");
125		failed++;
126	}
127	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
128	failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
129	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
130	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
131	failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
132	failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
133	failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
134	failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
135	failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
136	failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
137	failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
138
139	if (failed) {
140		printf("%d failures.\n", failed);
141		exit(1);
142	}
143	return 0;
144}
145
146
147
148