tst_uuid.c

00001 /*
00002  * tst_uuid.c --- test program from the UUID library
00003  *
00004  * Copyright (C) 1996, 1997, 1998 Theodore Ts'o.
00005  *
00006  * %Begin-Header%
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, and the entire permission notice in its entirety,
00012  *    including the disclaimer of warranties.
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in the
00015  *    documentation and/or other materials provided with the distribution.
00016  * 3. The name of the author may not be used to endorse or promote
00017  *    products derived from this software without specific prior
00018  *    written permission.
00019  * 
00020  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00021  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00022  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
00023  * WHICH ARE HEREBY DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE
00024  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
00026  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
00027  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00028  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00030  * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
00031  * DAMAGE.
00032  * %End-Header%
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 }

Generated on Sun Sep 24 20:10:47 2006 for WvStreams by  doxygen 1.4.7