]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blame - lib/uuid/tst_uuid.c
Don't use in-tree header files if using system uuid or blkid libraries
[thirdparty/e2fsprogs.git] / lib / uuid / tst_uuid.c
CommitLineData
19c78dc0
TT
1/*
2 * tst_uuid.c --- test program from the UUID library
3 *
1e0a221b 4 * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
19c78dc0
TT
5 *
6 * %Begin-Header%
1bbfec62
TT
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.
efc6f628 19 *
1bbfec62
TT
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.
19c78dc0
TT
32 * %End-Header%
33 */
34
6ec9ef18
TT
35#ifdef _WIN32
36#define _WIN32_WINNT 0x0500
37#include <windows.h>
38#define UUID MYUUID
39#endif
40
1e3472c5 41#include <stdio.h>
b969b1b8 42#include <stdlib.h>
1e3472c5 43
e1f08507 44#include <uuid/uuid.h>
1e3472c5 45
8d7f4587
TT
46static int test_uuid(const char * uuid, int isValid)
47{
48 static const char * validStr[2] = {"invalid", "valid"};
49 uuid_t uuidBits;
50 int parsedOk;
51
52 parsedOk = uuid_parse(uuid, uuidBits) == 0;
b969b1b8 53
8d7f4587
TT
54 printf("%s is %s", uuid, validStr[isValid]);
55 if (parsedOk != isValid) {
56 printf(" but uuid_parse says %s\n", validStr[parsedOk]);
57 return 1;
58 }
59 printf(", OK\n");
60 return 0;
61}
62
96f4bb1f
TT
63#ifdef __GNUC__
64#define ATTR(x) __attribute__(x)
65#else
66#define ATTR(x)
67#endif
68
1e3472c5 69int
96f4bb1f 70main(int argc ATTR((unused)) , char **argv ATTR((unused)))
1e3472c5
TT
71{
72 uuid_t buf, tst;
73 char str[100];
1e0a221b
TT
74 struct timeval tv;
75 time_t time_reg;
1e3472c5
TT
76 unsigned char *cp;
77 int i;
78 int failed = 0;
b19d1a95 79 int type, variant;
1e3472c5
TT
80
81 uuid_generate(buf);
82 uuid_unparse(buf, str);
b19d1a95
TT
83 printf("UUID generate = %s\n", str);
84 printf("UUID: ");
85 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
86 printf("%02x", *cp++);
87 }
88 printf("\n");
89 type = uuid_type(buf); variant = uuid_variant(buf);
90 printf("UUID type = %d, UUID variant = %d\n", type, variant);
91 if (variant != UUID_VARIANT_DCE) {
92 printf("Incorrect UUID Variant; was expecting DCE!\n");
93 failed++;
94 }
95 printf("\n");
96
97 uuid_generate_random(buf);
98 uuid_unparse(buf, str);
99 printf("UUID random string = %s\n", str);
1e3472c5
TT
100 printf("UUID: ");
101 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
102 printf("%02x", *cp++);
103 }
104 printf("\n");
b19d1a95
TT
105 type = uuid_type(buf); variant = uuid_variant(buf);
106 printf("UUID type = %d, UUID variant = %d\n", type, variant);
107 if (variant != UUID_VARIANT_DCE) {
108 printf("Incorrect UUID Variant; was expecting DCE!\n");
109 failed++;
110 }
111 if (type != 4) {
112 printf("Incorrect UUID type; was expecting "
113 "4 (random type)!\n");
114 failed++;
115 }
116 printf("\n");
b969b1b8 117
b19d1a95
TT
118 uuid_generate_time(buf);
119 uuid_unparse(buf, str);
120 printf("UUID string = %s\n", str);
121 printf("UUID time: ");
122 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
123 printf("%02x", *cp++);
124 }
125 printf("\n");
126 type = uuid_type(buf); variant = uuid_variant(buf);
127 printf("UUID type = %d, UUID variant = %d\n", type, variant);
128 if (variant != UUID_VARIANT_DCE) {
129 printf("Incorrect UUID Variant; was expecting DCE!\n");
130 failed++;
131 }
132 if (type != 1) {
133 printf("Incorrect UUID type; was expecting "
134 "1 (time-based type)!\\n");
135 failed++;
136 }
1e0a221b
TT
137 tv.tv_sec = 0;
138 tv.tv_usec = 0;
139 time_reg = uuid_time(buf, &tv);
96394d11 140 printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
1e0a221b 141 ctime(&time_reg));
1e3472c5 142 uuid_parse(str, tst);
b19d1a95 143 if (!uuid_compare(buf, tst))
1e3472c5
TT
144 printf("UUID parse and compare succeeded.\n");
145 else {
146 printf("UUID parse and compare failed!\n");
147 failed++;
148 }
149 uuid_clear(tst);
150 if (uuid_is_null(tst))
151 printf("UUID clear and is null succeeded.\n");
152 else {
153 printf("UUID clear and is null failed!\n");
154 failed++;
155 }
156 uuid_copy(buf, tst);
b19d1a95 157 if (!uuid_compare(buf, tst))
1e3472c5
TT
158 printf("UUID copy and compare succeeded.\n");
159 else {
160 printf("UUID copy and compare failed!\n");
161 failed++;
162 }
8d7f4587
TT
163 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
164 failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
165 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
166 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
167 failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
168 failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
169 failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
170 failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
171 failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
172 failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
173 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
b969b1b8 174
1e3472c5
TT
175 if (failed) {
176 printf("%d failures.\n", failed);
177 exit(1);
178 }
179 return 0;
180}