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