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. |
| 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. |
Theodore Ts'o | 19c78dc | 1997-04-29 16:17:09 +0000 | [diff] [blame] | 32 | * %End-Header% |
| 33 | */ |
| 34 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
Matthias Andree | b969b1b | 2003-12-28 13:04:35 +0100 | [diff] [blame] | 36 | #include <stdlib.h> |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 37 | |
| 38 | #include "uuid.h" |
| 39 | |
Theodore Ts'o | 8d7f458 | 2002-07-15 23:49:57 -0400 | [diff] [blame] | 40 | static int test_uuid(const char * uuid, int isValid) |
| 41 | { |
| 42 | static const char * validStr[2] = {"invalid", "valid"}; |
| 43 | uuid_t uuidBits; |
| 44 | int parsedOk; |
| 45 | |
| 46 | parsedOk = uuid_parse(uuid, uuidBits) == 0; |
Matthias Andree | b969b1b | 2003-12-28 13:04:35 +0100 | [diff] [blame] | 47 | |
Theodore Ts'o | 8d7f458 | 2002-07-15 23:49:57 -0400 | [diff] [blame] | 48 | printf("%s is %s", uuid, validStr[isValid]); |
| 49 | if (parsedOk != isValid) { |
| 50 | printf(" but uuid_parse says %s\n", validStr[parsedOk]); |
| 51 | return 1; |
| 52 | } |
| 53 | printf(", OK\n"); |
| 54 | return 0; |
| 55 | } |
| 56 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 57 | int |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 58 | main(int argc, char **argv) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 59 | { |
| 60 | uuid_t buf, tst; |
| 61 | char str[100]; |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 62 | struct timeval tv; |
| 63 | time_t time_reg; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 64 | unsigned char *cp; |
| 65 | int i; |
| 66 | int failed = 0; |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 67 | int type, variant; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 68 | |
| 69 | uuid_generate(buf); |
| 70 | uuid_unparse(buf, str); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 71 | printf("UUID generate = %s\n", str); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 72 | printf("UUID: "); |
| 73 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
| 74 | printf("%02x", *cp++); |
| 75 | } |
| 76 | printf("\n"); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 77 | type = uuid_type(buf); variant = uuid_variant(buf); |
| 78 | printf("UUID type = %d, UUID variant = %d\n", type, variant); |
| 79 | if (variant != UUID_VARIANT_DCE) { |
| 80 | printf("Incorrect UUID Variant; was expecting DCE!\n"); |
| 81 | failed++; |
| 82 | } |
| 83 | printf("\n"); |
| 84 | |
| 85 | uuid_generate_random(buf); |
| 86 | uuid_unparse(buf, str); |
| 87 | printf("UUID random string = %s\n", str); |
| 88 | printf("UUID: "); |
| 89 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
| 90 | printf("%02x", *cp++); |
| 91 | } |
| 92 | printf("\n"); |
| 93 | type = uuid_type(buf); variant = uuid_variant(buf); |
| 94 | printf("UUID type = %d, UUID variant = %d\n", type, variant); |
| 95 | if (variant != UUID_VARIANT_DCE) { |
| 96 | printf("Incorrect UUID Variant; was expecting DCE!\n"); |
| 97 | failed++; |
| 98 | } |
| 99 | if (type != 4) { |
| 100 | printf("Incorrect UUID type; was expecting " |
| 101 | "4 (random type)!\n"); |
| 102 | failed++; |
| 103 | } |
| 104 | printf("\n"); |
Matthias Andree | b969b1b | 2003-12-28 13:04:35 +0100 | [diff] [blame] | 105 | |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 106 | uuid_generate_time(buf); |
| 107 | uuid_unparse(buf, str); |
| 108 | printf("UUID string = %s\n", str); |
| 109 | printf("UUID time: "); |
| 110 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
| 111 | printf("%02x", *cp++); |
| 112 | } |
| 113 | printf("\n"); |
| 114 | type = uuid_type(buf); variant = uuid_variant(buf); |
| 115 | printf("UUID type = %d, UUID variant = %d\n", type, variant); |
| 116 | if (variant != UUID_VARIANT_DCE) { |
| 117 | printf("Incorrect UUID Variant; was expecting DCE!\n"); |
| 118 | failed++; |
| 119 | } |
| 120 | if (type != 1) { |
| 121 | printf("Incorrect UUID type; was expecting " |
| 122 | "1 (time-based type)!\\n"); |
| 123 | failed++; |
| 124 | } |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 125 | tv.tv_sec = 0; |
| 126 | tv.tv_usec = 0; |
| 127 | time_reg = uuid_time(buf, &tv); |
Theodore Ts'o | 96394d1 | 2001-01-12 18:30:54 +0000 | [diff] [blame] | 128 | printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec, |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 129 | ctime(&time_reg)); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 130 | uuid_parse(str, tst); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 131 | if (!uuid_compare(buf, tst)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 132 | printf("UUID parse and compare succeeded.\n"); |
| 133 | else { |
| 134 | printf("UUID parse and compare failed!\n"); |
| 135 | failed++; |
| 136 | } |
| 137 | uuid_clear(tst); |
| 138 | if (uuid_is_null(tst)) |
| 139 | printf("UUID clear and is null succeeded.\n"); |
| 140 | else { |
| 141 | printf("UUID clear and is null failed!\n"); |
| 142 | failed++; |
| 143 | } |
| 144 | uuid_copy(buf, tst); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame] | 145 | if (!uuid_compare(buf, tst)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 146 | printf("UUID copy and compare succeeded.\n"); |
| 147 | else { |
| 148 | printf("UUID copy and compare failed!\n"); |
| 149 | failed++; |
| 150 | } |
Theodore Ts'o | 8d7f458 | 2002-07-15 23:49:57 -0400 | [diff] [blame] | 151 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1); |
| 152 | failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1); |
| 153 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0); |
| 154 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0); |
| 155 | failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0); |
| 156 | failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0); |
| 157 | failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0); |
| 158 | failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0); |
| 159 | failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0); |
| 160 | failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0); |
| 161 | failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0); |
Matthias Andree | b969b1b | 2003-12-28 13:04:35 +0100 | [diff] [blame] | 162 | |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 163 | if (failed) { |
| 164 | printf("%d failures.\n", failed); |
| 165 | exit(1); |
| 166 | } |
| 167 | return 0; |
| 168 | } |