blob: f1f56f16a07ab63ba5c8da727099c76ca2c4cbcd [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%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000012#include <stdio.h>
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000013
14#include "uuid.h"
15
Theodore Ts'o8d7f4582002-07-15 23:49:57 -040016static 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
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000033int
Theodore Ts'o818180c1998-06-27 05:11:14 +000034main(int argc, char **argv)
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000035{
36 uuid_t buf, tst;
37 char str[100];
Theodore Ts'o1e0a2211998-12-04 08:13:52 +000038 struct timeval tv;
39 time_t time_reg;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000040 unsigned char *cp;
41 int i;
42 int failed = 0;
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000043 int type, variant;
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000044
45 uuid_generate(buf);
46 uuid_unparse(buf, str);
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000047 printf("UUID generate = %s\n", str);
Theodore Ts'o1e3472c1997-04-29 14:53:37 +000048 printf("UUID: ");
49 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
50 printf("%02x", *cp++);
51 }
52 printf("\n");
Theodore Ts'ob19d1a91999-06-18 00:32:03 +000053 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 }
Theodore Ts'o1e0a2211998-12-04 08:13:52 +0000101 tv.tv_sec = 0;
102 tv.tv_usec = 0;
103 time_reg = uuid_time(buf, &tv);
Theodore Ts'o96394d12001-01-12 18:30:54 +0000104 printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
Theodore Ts'o1e0a2211998-12-04 08:13:52 +0000105 ctime(&time_reg));
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000106 uuid_parse(str, tst);
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000107 if (!uuid_compare(buf, tst))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000108 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);
Theodore Ts'ob19d1a91999-06-18 00:32:03 +0000121 if (!uuid_compare(buf, tst))
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000122 printf("UUID copy and compare succeeded.\n");
123 else {
124 printf("UUID copy and compare failed!\n");
125 failed++;
126 }
Theodore Ts'o8d7f4582002-07-15 23:49:57 -0400127 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
Theodore Ts'o1e3472c1997-04-29 14:53:37 +0000139 if (failed) {
140 printf("%d failures.\n", failed);
141 exit(1);
142 }
143 return 0;
144}
145
146
147