blob: 47ff06cb5c7a1ba796f53dfc6c407ad2bff6e639 [file] [log] [blame]
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00001/*
2 * tst_uuid.c --- test program from the UUID library
3 *
Theodore Ts'o1e0a2211998-12-04 08:13:52 +00004 * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +00005 *
6 * %Begin-Header%
Theodore Ts'o1bbfec62004-03-20 14:02:24 -05007 * 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.
Theodore Ts'o19c78dc1997-04-29 16:17:09 +000032 * %End-Header%
33 */
34
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000035#include <stdio.h>
Matthias Andreeb969b1b2003-12-28 13:04:35 +010036#include <stdlib.h>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000037
38#include "uuid.h"
39
Theodore Ts'o8d7f4582002-07-15 23:49:57 -040040static int test_uuid(const char * uuid, int isValid)
41{
42 static const char * validStr[2] = {"invalid", "valid"};
43 uuid_t uuidBits;
44 int parsedOk;
45
46 parsedOk = uuid_parse(uuid, uuidBits) == 0;
Matthias Andreeb969b1b2003-12-28 13:04:35 +010047
Theodore Ts'o8d7f4582002-07-15 23:49:57 -040048 printf("%s is %s", uuid, validStr[isValid]);
49 if (parsedOk != isValid) {
50 printf(" but uuid_parse says %s\n", validStr[parsedOk]);
51 return 1;
52 }
53 printf(", OK\n");
54 return 0;
55}
56
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000057int
Theodore Ts'o818180c1998-06-27 05:11:14 +000058main(int argc, char **argv)
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000059{
60 uuid_t buf, tst;
61 char str[100];
Theodore Ts'o1e0a2211998-12-04 08:13:52 +000062 struct timeval tv;
63 time_t time_reg;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000064 unsigned char *cp;
65 int i;
66 int failed = 0;
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000067 int type, variant;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000068
69 uuid_generate(buf);
70 uuid_unparse(buf, str);
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000071 printf("UUID generate = %s\n", str);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000072 printf("UUID: ");
73 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
74 printf("%02x", *cp++);
75 }
76 printf("\n");
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000077 type = uuid_type(buf); variant = uuid_variant(buf);
78 printf("UUID type = %d, UUID variant = %d\n", type, variant);
79 if (variant != UUID_VARIANT_DCE) {
80 printf("Incorrect UUID Variant; was expecting DCE!\n");
81 failed++;
82 }
83 printf("\n");
84
85 uuid_generate_random(buf);
86 uuid_unparse(buf, str);
87 printf("UUID random string = %s\n", str);
88 printf("UUID: ");
89 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
90 printf("%02x", *cp++);
91 }
92 printf("\n");
93 type = uuid_type(buf); variant = uuid_variant(buf);
94 printf("UUID type = %d, UUID variant = %d\n", type, variant);
95 if (variant != UUID_VARIANT_DCE) {
96 printf("Incorrect UUID Variant; was expecting DCE!\n");
97 failed++;
98 }
99 if (type != 4) {
100 printf("Incorrect UUID type; was expecting "
101 "4 (random type)!\n");
102 failed++;
103 }
104 printf("\n");
Matthias Andreeb969b1b2003-12-28 13:04:35 +0100105
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000106 uuid_generate_time(buf);
107 uuid_unparse(buf, str);
108 printf("UUID string = %s\n", str);
109 printf("UUID time: ");
110 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
111 printf("%02x", *cp++);
112 }
113 printf("\n");
114 type = uuid_type(buf); variant = uuid_variant(buf);
115 printf("UUID type = %d, UUID variant = %d\n", type, variant);
116 if (variant != UUID_VARIANT_DCE) {
117 printf("Incorrect UUID Variant; was expecting DCE!\n");
118 failed++;
119 }
120 if (type != 1) {
121 printf("Incorrect UUID type; was expecting "
122 "1 (time-based type)!\\n");
123 failed++;
124 }
Theodore Ts'o1e0a2211998-12-04 08:13:52 +0000125 tv.tv_sec = 0;
126 tv.tv_usec = 0;
127 time_reg = uuid_time(buf, &tv);
Theodore Ts'o96394d12001-01-12 18:30:54 +0000128 printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
Theodore Ts'o1e0a2211998-12-04 08:13:52 +0000129 ctime(&time_reg));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000130 uuid_parse(str, tst);
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000131 if (!uuid_compare(buf, tst))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000132 printf("UUID parse and compare succeeded.\n");
133 else {
134 printf("UUID parse and compare failed!\n");
135 failed++;
136 }
137 uuid_clear(tst);
138 if (uuid_is_null(tst))
139 printf("UUID clear and is null succeeded.\n");
140 else {
141 printf("UUID clear and is null failed!\n");
142 failed++;
143 }
144 uuid_copy(buf, tst);
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000145 if (!uuid_compare(buf, tst))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000146 printf("UUID copy and compare succeeded.\n");
147 else {
148 printf("UUID copy and compare failed!\n");
149 failed++;
150 }
Theodore Ts'o8d7f4582002-07-15 23:49:57 -0400151 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
152 failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
153 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
154 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
155 failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
156 failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
157 failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
158 failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
159 failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
160 failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
161 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
Matthias Andreeb969b1b2003-12-28 13:04:35 +0100162
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000163 if (failed) {
164 printf("%d failures.\n", failed);
165 exit(1);
166 }
167 return 0;
168}