Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * tst_uuid.c --- test program from the UUID library |
| 3 | * |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 4 | * Copyright (C) 1996, 1997, 1998 Theodore Ts'o. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 5 | * |
| 6 | * %Begin-Header% |
Theodore Ts'o | 1bbfec6 | 2004-03-20 14:02:24 -0500 | [diff] [blame] | 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. |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 19 | * |
Theodore Ts'o | 1bbfec6 | 2004-03-20 14:02:24 -0500 | [diff] [blame] | 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'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 32 | * %End-Header% |
| 33 | */ |
| 34 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 35 | #include "config.h" |
| 36 | |
Theodore Ts'o | 6ec9ef1 | 2008-08-22 12:15:56 -0400 | [diff] [blame] | 37 | #ifdef _WIN32 |
| 38 | #define _WIN32_WINNT 0x0500 |
| 39 | #include <windows.h> |
| 40 | #define UUID MYUUID |
| 41 | #endif |
| 42 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 43 | #include <stdio.h> |
Matthias Andree | b969b1b | 2003-12-28 13:04:35 +0100 | [diff] [blame] | 44 | #include <stdlib.h> |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 45 | |
Theodore Ts'o | e1f0850 | 2009-10-04 14:45:08 -0400 | [diff] [blame] | 46 | #include <uuid/uuid.h> |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 47 | |
Theodore Ts'o | 8d7f458 | 2002-07-15 23:49:57 -0400 | [diff] [blame] | 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; |
Matthias Andree | b969b1b | 2003-12-28 13:04:35 +0100 | [diff] [blame] | 55 | |
Theodore Ts'o | 8d7f458 | 2002-07-15 23:49:57 -0400 | [diff] [blame] | 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 | |
Theodore Ts'o | 96f4bb1 | 2008-06-07 11:53:20 -0400 | [diff] [blame] | 65 | #ifdef __GNUC__ |
| 66 | #define ATTR(x) __attribute__(x) |
| 67 | #else |
| 68 | #define ATTR(x) |
| 69 | #endif |
| 70 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 71 | int |
Theodore Ts'o | 96f4bb1 | 2008-06-07 11:53:20 -0400 | [diff] [blame] | 72 | main(int argc ATTR((unused)) , char **argv ATTR((unused))) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 73 | { |
| 74 | uuid_t buf, tst; |
| 75 | char str[100]; |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 76 | struct timeval tv; |
Andreas Dilger | f797cf3 | 2012-11-29 05:47:51 -0700 | [diff] [blame] | 77 | time_t time_reg, time_gen; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 78 | unsigned char *cp; |
| 79 | int i; |
| 80 | int failed = 0; |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 81 | int type, variant; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 82 | |
| 83 | uuid_generate(buf); |
| 84 | uuid_unparse(buf, str); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 85 | printf("UUID generate = %s\n", str); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 86 | printf("UUID: "); |
| 87 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
| 88 | printf("%02x", *cp++); |
| 89 | } |
| 90 | printf("\n"); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 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"); |
Andreas Dilger | f797cf3 | 2012-11-29 05:47:51 -0700 | [diff] [blame] | 107 | type = uuid_type(buf); |
| 108 | variant = uuid_variant(buf); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 109 | printf("UUID type = %d, UUID variant = %d\n", type, variant); |
| 110 | if (variant != UUID_VARIANT_DCE) { |
| 111 | printf("Incorrect UUID Variant; was expecting DCE!\n"); |
| 112 | failed++; |
| 113 | } |
| 114 | if (type != 4) { |
| 115 | printf("Incorrect UUID type; was expecting " |
| 116 | "4 (random type)!\n"); |
| 117 | failed++; |
| 118 | } |
| 119 | printf("\n"); |
Matthias Andree | b969b1b | 2003-12-28 13:04:35 +0100 | [diff] [blame] | 120 | |
Andreas Dilger | f797cf3 | 2012-11-29 05:47:51 -0700 | [diff] [blame] | 121 | time_gen = time(0); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 122 | uuid_generate_time(buf); |
| 123 | uuid_unparse(buf, str); |
| 124 | printf("UUID string = %s\n", str); |
| 125 | printf("UUID time: "); |
| 126 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
| 127 | printf("%02x", *cp++); |
| 128 | } |
| 129 | printf("\n"); |
Andreas Dilger | f797cf3 | 2012-11-29 05:47:51 -0700 | [diff] [blame] | 130 | type = uuid_type(buf); |
| 131 | variant = uuid_variant(buf); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 132 | printf("UUID type = %d, UUID variant = %d\n", type, variant); |
| 133 | if (variant != UUID_VARIANT_DCE) { |
| 134 | printf("Incorrect UUID Variant; was expecting DCE!\n"); |
| 135 | failed++; |
| 136 | } |
| 137 | if (type != 1) { |
| 138 | printf("Incorrect UUID type; was expecting " |
| 139 | "1 (time-based type)!\\n"); |
| 140 | failed++; |
| 141 | } |
Andreas Dilger | f797cf3 | 2012-11-29 05:47:51 -0700 | [diff] [blame] | 142 | |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 143 | tv.tv_sec = 0; |
| 144 | tv.tv_usec = 0; |
| 145 | time_reg = uuid_time(buf, &tv); |
Andreas Dilger | f797cf3 | 2012-11-29 05:47:51 -0700 | [diff] [blame] | 146 | printf("UUID generated at %lu reports %lu (%ld.%ld)\n", |
| 147 | time_gen, time_reg, tv.tv_sec, tv.tv_usec); |
| 148 | /* allow 1s margin in case of rollover between sampling |
| 149 | * the current time and when the UUID is generated. */ |
| 150 | if (time_reg > time_gen + 1) { |
| 151 | printf("UUID time comparison failed!\n"); |
| 152 | failed++; |
| 153 | } else { |
| 154 | printf("UUID time comparison succeeded.\n"); |
| 155 | } |
| 156 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 157 | uuid_parse(str, tst); |
Andreas Dilger | f797cf3 | 2012-11-29 05:47:51 -0700 | [diff] [blame] | 158 | if (!uuid_compare(buf, tst)) { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 159 | printf("UUID parse and compare succeeded.\n"); |
Andreas Dilger | f797cf3 | 2012-11-29 05:47:51 -0700 | [diff] [blame] | 160 | } else { |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 161 | printf("UUID parse and compare failed!\n"); |
| 162 | failed++; |
| 163 | } |
| 164 | uuid_clear(tst); |
| 165 | if (uuid_is_null(tst)) |
| 166 | printf("UUID clear and is null succeeded.\n"); |
| 167 | else { |
| 168 | printf("UUID clear and is null failed!\n"); |
| 169 | failed++; |
| 170 | } |
| 171 | uuid_copy(buf, tst); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 172 | if (!uuid_compare(buf, tst)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 173 | printf("UUID copy and compare succeeded.\n"); |
| 174 | else { |
| 175 | printf("UUID copy and compare failed!\n"); |
| 176 | failed++; |
| 177 | } |
Andreas Dilger | f797cf3 | 2012-11-29 05:47:51 -0700 | [diff] [blame] | 178 | |
Theodore Ts'o | 8d7f458 | 2002-07-15 23:49:57 -0400 | [diff] [blame] | 179 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1); |
| 180 | failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1); |
| 181 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0); |
| 182 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0); |
| 183 | failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0); |
| 184 | failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0); |
| 185 | failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0); |
| 186 | failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0); |
| 187 | failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0); |
| 188 | failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0); |
| 189 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0); |
Matthias Andree | b969b1b | 2003-12-28 13:04:35 +0100 | [diff] [blame] | 190 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 191 | if (failed) { |
| 192 | printf("%d failures.\n", failed); |
| 193 | exit(1); |
| 194 | } |
| 195 | return 0; |
| 196 | } |