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> |
| 13 | #include <linux/ext2_fs.h> |
| 14 | |
| 15 | #include "uuid.h" |
| 16 | |
| 17 | int |
Theodore Ts'o | 818180c | 1998-06-27 05:11:14 +0000 | [diff] [blame] | 18 | main(int argc, char **argv) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 19 | { |
| 20 | uuid_t buf, tst; |
| 21 | char str[100]; |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 22 | struct timeval tv; |
| 23 | time_t time_reg; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 24 | unsigned char *cp; |
| 25 | int i; |
| 26 | int failed = 0; |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame^] | 27 | int type, variant; |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 28 | |
| 29 | uuid_generate(buf); |
| 30 | uuid_unparse(buf, str); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame^] | 31 | printf("UUID generate = %s\n", str); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 32 | printf("UUID: "); |
| 33 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
| 34 | printf("%02x", *cp++); |
| 35 | } |
| 36 | printf("\n"); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame^] | 37 | type = uuid_type(buf); variant = uuid_variant(buf); |
| 38 | printf("UUID type = %d, UUID variant = %d\n", type, variant); |
| 39 | if (variant != UUID_VARIANT_DCE) { |
| 40 | printf("Incorrect UUID Variant; was expecting DCE!\n"); |
| 41 | failed++; |
| 42 | } |
| 43 | printf("\n"); |
| 44 | |
| 45 | uuid_generate_random(buf); |
| 46 | uuid_unparse(buf, str); |
| 47 | printf("UUID random string = %s\n", str); |
| 48 | printf("UUID: "); |
| 49 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
| 50 | printf("%02x", *cp++); |
| 51 | } |
| 52 | printf("\n"); |
| 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 | if (type != 4) { |
| 60 | printf("Incorrect UUID type; was expecting " |
| 61 | "4 (random type)!\n"); |
| 62 | failed++; |
| 63 | } |
| 64 | printf("\n"); |
| 65 | |
| 66 | uuid_generate_time(buf); |
| 67 | uuid_unparse(buf, str); |
| 68 | printf("UUID string = %s\n", str); |
| 69 | printf("UUID time: "); |
| 70 | for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { |
| 71 | printf("%02x", *cp++); |
| 72 | } |
| 73 | printf("\n"); |
| 74 | type = uuid_type(buf); variant = uuid_variant(buf); |
| 75 | printf("UUID type = %d, UUID variant = %d\n", type, variant); |
| 76 | if (variant != UUID_VARIANT_DCE) { |
| 77 | printf("Incorrect UUID Variant; was expecting DCE!\n"); |
| 78 | failed++; |
| 79 | } |
| 80 | if (type != 1) { |
| 81 | printf("Incorrect UUID type; was expecting " |
| 82 | "1 (time-based type)!\\n"); |
| 83 | failed++; |
| 84 | } |
Theodore Ts'o | 1e0a221 | 1998-12-04 08:13:52 +0000 | [diff] [blame] | 85 | tv.tv_sec = 0; |
| 86 | tv.tv_usec = 0; |
| 87 | time_reg = uuid_time(buf, &tv); |
| 88 | printf("UUID time is: (%d, %d): %s\n", tv.tv_sec, tv.tv_usec, |
| 89 | ctime(&time_reg)); |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 90 | uuid_parse(str, tst); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame^] | 91 | if (!uuid_compare(buf, tst)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 92 | printf("UUID parse and compare succeeded.\n"); |
| 93 | else { |
| 94 | printf("UUID parse and compare failed!\n"); |
| 95 | failed++; |
| 96 | } |
| 97 | uuid_clear(tst); |
| 98 | if (uuid_is_null(tst)) |
| 99 | printf("UUID clear and is null succeeded.\n"); |
| 100 | else { |
| 101 | printf("UUID clear and is null failed!\n"); |
| 102 | failed++; |
| 103 | } |
| 104 | uuid_copy(buf, tst); |
Theodore Ts'o | b19d1a9 | 1999-06-18 00:32:03 +0000 | [diff] [blame^] | 105 | if (!uuid_compare(buf, tst)) |
Theodore Ts'o | 1e3472c | 1997-04-29 14:53:37 +0000 | [diff] [blame] | 106 | printf("UUID copy and compare succeeded.\n"); |
| 107 | else { |
| 108 | printf("UUID copy and compare failed!\n"); |
| 109 | failed++; |
| 110 | } |
| 111 | if (failed) { |
| 112 | printf("%d failures.\n", failed); |
| 113 | exit(1); |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |