Whamcloud - gitweb
5884ad92704469c9db84448accd3086d022897f9
[tools/e2fsprogs.git] / lib / uuid / tst_uuid.c
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 #include "config.h"
36
37 #ifdef _WIN32
38 #define _WIN32_WINNT 0x0500
39 #include <windows.h>
40 #define UUID MYUUID
41 #endif
42
43 #include <stdio.h>
44 #include <stdlib.h>
45
46 #include <uuid/uuid.h>
47
48 static int test_uuid(const char * uuid, int isValid)
49 {
50         static const char * validStr[2] = {"invalid", "valid"};
51         uuid_t uuidBits;
52         int parsedOk;
53
54         parsedOk = uuid_parse(uuid, uuidBits) == 0;
55
56         printf("%s is %s", uuid, validStr[isValid]);
57         if (parsedOk != isValid) {
58                 printf(" but uuid_parse says %s\n", validStr[parsedOk]);
59                 return 1;
60         }
61         printf(", OK\n");
62         return 0;
63 }
64
65 #ifdef __GNUC__
66 #define ATTR(x) __attribute__(x)
67 #else
68 #define ATTR(x)
69 #endif
70
71 int
72 main(int argc ATTR((unused)) , char **argv ATTR((unused)))
73 {
74         uuid_t          buf, tst;
75         char            str[100];
76         struct timeval  tv;
77         time_t          time_reg;
78         unsigned char   *cp;
79         int i;
80         int failed = 0;
81         int type, variant;
82
83         uuid_generate(buf);
84         uuid_unparse(buf, str);
85         printf("UUID generate = %s\n", str);
86         printf("UUID: ");
87         for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
88                 printf("%02x", *cp++);
89         }
90         printf("\n");
91         type = uuid_type(buf);  variant = uuid_variant(buf);
92         printf("UUID type = %d, UUID variant = %d\n", type, variant);
93         if (variant != UUID_VARIANT_DCE) {
94                 printf("Incorrect UUID Variant; was expecting DCE!\n");
95                 failed++;
96         }
97         printf("\n");
98
99         uuid_generate_random(buf);
100         uuid_unparse(buf, str);
101         printf("UUID random string = %s\n", str);
102         printf("UUID: ");
103         for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
104                 printf("%02x", *cp++);
105         }
106         printf("\n");
107         type = uuid_type(buf);  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         uuid_generate_time(buf);
121         uuid_unparse(buf, str);
122         printf("UUID string = %s\n", str);
123         printf("UUID time: ");
124         for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
125                 printf("%02x", *cp++);
126         }
127         printf("\n");
128         type = uuid_type(buf);  variant = uuid_variant(buf);
129         printf("UUID type = %d, UUID variant = %d\n", type, variant);
130         if (variant != UUID_VARIANT_DCE) {
131                 printf("Incorrect UUID Variant; was expecting DCE!\n");
132                 failed++;
133         }
134         if (type != 1) {
135                 printf("Incorrect UUID type; was expecting "
136                        "1 (time-based type)!\\n");
137                 failed++;
138         }
139         tv.tv_sec = 0;
140         tv.tv_usec = 0;
141         time_reg = uuid_time(buf, &tv);
142         printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
143                ctime(&time_reg));
144         uuid_parse(str, tst);
145         if (!uuid_compare(buf, tst))
146                 printf("UUID parse and compare succeeded.\n");
147         else {
148                 printf("UUID parse and compare failed!\n");
149                 failed++;
150         }
151         uuid_clear(tst);
152         if (uuid_is_null(tst))
153                 printf("UUID clear and is null succeeded.\n");
154         else {
155                 printf("UUID clear and is null failed!\n");
156                 failed++;
157         }
158         uuid_copy(buf, tst);
159         if (!uuid_compare(buf, tst))
160                 printf("UUID copy and compare succeeded.\n");
161         else {
162                 printf("UUID copy and compare failed!\n");
163                 failed++;
164         }
165         failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
166         failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
167         failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
168         failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
169         failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
170         failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
171         failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
172         failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
173         failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
174         failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
175         failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
176
177         if (failed) {
178                 printf("%d failures.\n", failed);
179                 exit(1);
180         }
181         return 0;
182 }