00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037
00038 #include <uuid/uuid.h>
00039
00040 static int test_uuid(const char * uuid, int isValid)
00041 {
00042 static const char * validStr[2] = {"invalid", "valid"};
00043 uuid_t uuidBits;
00044 int parsedOk;
00045
00046 parsedOk = uuid_parse(uuid, uuidBits) == 0;
00047
00048 printf("%s is %s", uuid, validStr[isValid]);
00049 if (parsedOk != isValid) {
00050 printf(" but uuid_parse says %s\n", validStr[parsedOk]);
00051 return 1;
00052 }
00053 printf(", OK\n");
00054 return 0;
00055 }
00056
00057 int
00058 main(int argc, char **argv)
00059 {
00060 uuid_t buf, tst;
00061 char str[100];
00062 struct timeval tv;
00063 time_t time_reg;
00064 unsigned char *cp;
00065 int i;
00066 int failed = 0;
00067 int type, variant;
00068
00069 uuid_generate(buf);
00070 uuid_unparse(buf, str);
00071 printf("UUID generate = %s\n", str);
00072 printf("UUID: ");
00073 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
00074 printf("%02x", *cp++);
00075 }
00076 printf("\n");
00077 type = uuid_type(buf); variant = uuid_variant(buf);
00078 printf("UUID type = %d, UUID variant = %d\n", type, variant);
00079 if (variant != UUID_VARIANT_DCE) {
00080 printf("Incorrect UUID Variant; was expecting DCE!\n");
00081 failed++;
00082 }
00083 printf("\n");
00084
00085 uuid_generate_random(buf);
00086 uuid_unparse(buf, str);
00087 printf("UUID random string = %s\n", str);
00088 printf("UUID: ");
00089 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
00090 printf("%02x", *cp++);
00091 }
00092 printf("\n");
00093 type = uuid_type(buf); variant = uuid_variant(buf);
00094 printf("UUID type = %d, UUID variant = %d\n", type, variant);
00095 if (variant != UUID_VARIANT_DCE) {
00096 printf("Incorrect UUID Variant; was expecting DCE!\n");
00097 failed++;
00098 }
00099 if (type != 4) {
00100 printf("Incorrect UUID type; was expecting "
00101 "4 (random type)!\n");
00102 failed++;
00103 }
00104 printf("\n");
00105
00106 uuid_generate_time(buf);
00107 uuid_unparse(buf, str);
00108 printf("UUID string = %s\n", str);
00109 printf("UUID time: ");
00110 for (i=0, cp = (unsigned char *) &buf; i < 16; i++) {
00111 printf("%02x", *cp++);
00112 }
00113 printf("\n");
00114 type = uuid_type(buf); variant = uuid_variant(buf);
00115 printf("UUID type = %d, UUID variant = %d\n", type, variant);
00116 if (variant != UUID_VARIANT_DCE) {
00117 printf("Incorrect UUID Variant; was expecting DCE!\n");
00118 failed++;
00119 }
00120 if (type != 1) {
00121 printf("Incorrect UUID type; was expecting "
00122 "1 (time-based type)!\\n");
00123 failed++;
00124 }
00125 tv.tv_sec = 0;
00126 tv.tv_usec = 0;
00127 time_reg = uuid_time(buf, &tv);
00128 printf("UUID time is: (%ld, %ld): %s\n", tv.tv_sec, tv.tv_usec,
00129 ctime(&time_reg));
00130 uuid_parse(str, tst);
00131 if (!uuid_compare(buf, tst))
00132 printf("UUID parse and compare succeeded.\n");
00133 else {
00134 printf("UUID parse and compare failed!\n");
00135 failed++;
00136 }
00137 uuid_clear(tst);
00138 if (uuid_is_null(tst))
00139 printf("UUID clear and is null succeeded.\n");
00140 else {
00141 printf("UUID clear and is null failed!\n");
00142 failed++;
00143 }
00144 uuid_copy(buf, tst);
00145 if (!uuid_compare(buf, tst))
00146 printf("UUID copy and compare succeeded.\n");
00147 else {
00148 printf("UUID copy and compare failed!\n");
00149 failed++;
00150 }
00151 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
00152 failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
00153 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
00154 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
00155 failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
00156 failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
00157 failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
00158 failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
00159 failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
00160 failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
00161 failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
00162
00163 if (failed) {
00164 printf("%d failures.\n", failed);
00165 exit(1);
00166 }
00167 return 0;
00168 }