paul@181 | 1 | /* |
paul@181 | 2 | * tst_uuid.c --- test program from the UUID library |
paul@181 | 3 | * |
paul@181 | 4 | * Copyright (C) 1996, 1997, 1998 Theodore Ts'o. |
paul@181 | 5 | * |
paul@181 | 6 | * %Begin-Header% |
paul@181 | 7 | * Redistribution and use in source and binary forms, with or without |
paul@181 | 8 | * modification, are permitted provided that the following conditions |
paul@181 | 9 | * are met: |
paul@181 | 10 | * 1. Redistributions of source code must retain the above copyright |
paul@181 | 11 | * notice, and the entire permission notice in its entirety, |
paul@181 | 12 | * including the disclaimer of warranties. |
paul@181 | 13 | * 2. Redistributions in binary form must reproduce the above copyright |
paul@181 | 14 | * notice, this list of conditions and the following disclaimer in the |
paul@181 | 15 | * documentation and/or other materials provided with the distribution. |
paul@181 | 16 | * 3. The name of the author may not be used to endorse or promote |
paul@181 | 17 | * products derived from this software without specific prior |
paul@181 | 18 | * written permission. |
paul@181 | 19 | * |
paul@181 | 20 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
paul@181 | 21 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
paul@181 | 22 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
paul@181 | 23 | * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
paul@181 | 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
paul@181 | 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
paul@181 | 26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
paul@181 | 27 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
paul@181 | 28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
paul@181 | 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
paul@181 | 30 | * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
paul@181 | 31 | * DAMAGE. |
paul@181 | 32 | * %End-Header% |
paul@181 | 33 | */ |
paul@181 | 34 | |
paul@181 | 35 | #include "config.h" |
paul@181 | 36 | |
paul@181 | 37 | #ifdef _WIN32 |
paul@181 | 38 | #define _WIN32_WINNT 0x0500 |
paul@181 | 39 | #include <windows.h> |
paul@181 | 40 | #define UUID MYUUID |
paul@181 | 41 | #endif |
paul@181 | 42 | |
paul@181 | 43 | #include <stdio.h> |
paul@181 | 44 | #include <stdlib.h> |
paul@181 | 45 | |
paul@181 | 46 | #include <uuid/uuid.h> |
paul@181 | 47 | |
paul@181 | 48 | static int test_uuid(const char * uuid, int isValid) |
paul@181 | 49 | { |
paul@181 | 50 | static const char * validStr[2] = {"invalid", "valid"}; |
paul@181 | 51 | uuid_t uuidBits; |
paul@181 | 52 | int parsedOk; |
paul@181 | 53 | |
paul@181 | 54 | parsedOk = uuid_parse(uuid, uuidBits) == 0; |
paul@181 | 55 | |
paul@181 | 56 | printf("%s is %s", uuid, validStr[isValid]); |
paul@181 | 57 | if (parsedOk != isValid) { |
paul@181 | 58 | printf(" but uuid_parse says %s\n", validStr[parsedOk]); |
paul@181 | 59 | return 1; |
paul@181 | 60 | } |
paul@181 | 61 | printf(", OK\n"); |
paul@181 | 62 | return 0; |
paul@181 | 63 | } |
paul@181 | 64 | |
paul@181 | 65 | #ifdef __GNUC__ |
paul@181 | 66 | #define ATTR(x) __attribute__(x) |
paul@181 | 67 | #else |
paul@181 | 68 | #define ATTR(x) |
paul@181 | 69 | #endif |
paul@181 | 70 | |
paul@181 | 71 | int |
paul@181 | 72 | main(int argc ATTR((unused)) , char **argv ATTR((unused))) |
paul@181 | 73 | { |
paul@181 | 74 | uuid_t buf, tst; |
paul@181 | 75 | char str[100]; |
paul@181 | 76 | struct timeval tv; |
paul@181 | 77 | time_t time_reg, time_gen; |
paul@181 | 78 | unsigned char *cp; |
paul@181 | 79 | int i; |
paul@181 | 80 | int failed = 0; |
paul@181 | 81 | int type, variant; |
paul@181 | 82 | |
paul@181 | 83 | uuid_generate(buf); |
paul@181 | 84 | uuid_unparse(buf, str); |
paul@181 | 85 | printf("UUID generate = %s\n", str); |
paul@181 | 86 | printf("UUID: "); |
paul@181 | 87 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
paul@181 | 88 | printf("%02x", *cp++); |
paul@181 | 89 | } |
paul@181 | 90 | printf("\n"); |
paul@181 | 91 | type = uuid_type(buf); variant = uuid_variant(buf); |
paul@181 | 92 | printf("UUID type = %d, UUID variant = %d\n", type, variant); |
paul@181 | 93 | if (variant != UUID_VARIANT_DCE) { |
paul@181 | 94 | printf("Incorrect UUID Variant; was expecting DCE!\n"); |
paul@181 | 95 | failed++; |
paul@181 | 96 | } |
paul@181 | 97 | printf("\n"); |
paul@181 | 98 | |
paul@181 | 99 | uuid_generate_random(buf); |
paul@181 | 100 | uuid_unparse(buf, str); |
paul@181 | 101 | printf("UUID random string = %s\n", str); |
paul@181 | 102 | printf("UUID: "); |
paul@181 | 103 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
paul@181 | 104 | printf("%02x", *cp++); |
paul@181 | 105 | } |
paul@181 | 106 | printf("\n"); |
paul@181 | 107 | type = uuid_type(buf); |
paul@181 | 108 | variant = uuid_variant(buf); |
paul@181 | 109 | printf("UUID type = %d, UUID variant = %d\n", type, variant); |
paul@181 | 110 | if (variant != UUID_VARIANT_DCE) { |
paul@181 | 111 | printf("Incorrect UUID Variant; was expecting DCE!\n"); |
paul@181 | 112 | failed++; |
paul@181 | 113 | } |
paul@181 | 114 | if (type != 4) { |
paul@181 | 115 | printf("Incorrect UUID type; was expecting " |
paul@181 | 116 | "4 (random type)!\n"); |
paul@181 | 117 | failed++; |
paul@181 | 118 | } |
paul@181 | 119 | printf("\n"); |
paul@181 | 120 | |
paul@181 | 121 | time_gen = time(0); |
paul@181 | 122 | uuid_generate_time(buf); |
paul@181 | 123 | uuid_unparse(buf, str); |
paul@181 | 124 | printf("UUID string = %s\n", str); |
paul@181 | 125 | printf("UUID time: "); |
paul@181 | 126 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
paul@181 | 127 | printf("%02x", *cp++); |
paul@181 | 128 | } |
paul@181 | 129 | printf("\n"); |
paul@181 | 130 | type = uuid_type(buf); |
paul@181 | 131 | variant = uuid_variant(buf); |
paul@181 | 132 | printf("UUID type = %d, UUID variant = %d\n", type, variant); |
paul@181 | 133 | if (variant != UUID_VARIANT_DCE) { |
paul@181 | 134 | printf("Incorrect UUID Variant; was expecting DCE!\n"); |
paul@181 | 135 | failed++; |
paul@181 | 136 | } |
paul@181 | 137 | if (type != 1) { |
paul@181 | 138 | printf("Incorrect UUID type; was expecting " |
paul@181 | 139 | "1 (time-based type)!\\n"); |
paul@181 | 140 | failed++; |
paul@181 | 141 | } |
paul@181 | 142 | |
paul@181 | 143 | tv.tv_sec = 0; |
paul@181 | 144 | tv.tv_usec = 0; |
paul@181 | 145 | time_reg = uuid_time(buf, &tv); |
paul@181 | 146 | printf("UUID generated at %lu reports %lu (%ld.%ld)\n", |
paul@181 | 147 | time_gen, time_reg, tv.tv_sec, (long)tv.tv_usec); |
paul@181 | 148 | /* allow 1s margin in case of rollover between sampling |
paul@181 | 149 | * the current time and when the UUID is generated. */ |
paul@181 | 150 | if (time_reg > time_gen + 1) { |
paul@181 | 151 | printf("UUID time comparison failed!\n"); |
paul@181 | 152 | failed++; |
paul@181 | 153 | } else { |
paul@181 | 154 | printf("UUID time comparison succeeded.\n"); |
paul@181 | 155 | } |
paul@181 | 156 | |
paul@181 | 157 | if (uuid_parse(str, tst) < 0) { |
paul@181 | 158 | printf("UUID parse failed\n"); |
paul@181 | 159 | failed++; |
paul@181 | 160 | } |
paul@181 | 161 | if (!uuid_compare(buf, tst)) { |
paul@181 | 162 | printf("UUID parse and compare succeeded.\n"); |
paul@181 | 163 | } else { |
paul@181 | 164 | printf("UUID parse and compare failed!\n"); |
paul@181 | 165 | failed++; |
paul@181 | 166 | } |
paul@181 | 167 | uuid_clear(tst); |
paul@181 | 168 | if (uuid_is_null(tst)) |
paul@181 | 169 | printf("UUID clear and is null succeeded.\n"); |
paul@181 | 170 | else { |
paul@181 | 171 | printf("UUID clear and is null failed!\n"); |
paul@181 | 172 | failed++; |
paul@181 | 173 | } |
paul@181 | 174 | uuid_copy(buf, tst); |
paul@181 | 175 | if (!uuid_compare(buf, tst)) |
paul@181 | 176 | printf("UUID copy and compare succeeded.\n"); |
paul@181 | 177 | else { |
paul@181 | 178 | printf("UUID copy and compare failed!\n"); |
paul@181 | 179 | failed++; |
paul@181 | 180 | } |
paul@181 | 181 | |
paul@181 | 182 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1); |
paul@181 | 183 | failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1); |
paul@181 | 184 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0); |
paul@181 | 185 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0); |
paul@181 | 186 | failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0); |
paul@181 | 187 | failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0); |
paul@181 | 188 | failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0); |
paul@181 | 189 | failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0); |
paul@181 | 190 | failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0); |
paul@181 | 191 | failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0); |
paul@181 | 192 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0); |
paul@181 | 193 | |
paul@181 | 194 | if (failed) { |
paul@181 | 195 | printf("%d failures.\n", failed); |
paul@181 | 196 | exit(1); |
paul@181 | 197 | } |
paul@181 | 198 | return 0; |
paul@181 | 199 | } |