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