| 1 |
/* |
| 2 |
* tst_uuid.c --- test program from the UUID library |
| 3 |
* |
| 4 |
* Copyright (C) 1996, 1997, 1998 Theodore Ts'o. |
| 5 |
* |
| 6 |
* %Begin-Header% |
| 7 |
* Redistribution and use in source and binary forms, with or without |
| 8 |
* modification, are permitted provided that the following conditions |
| 9 |
* are met: |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, and the entire permission notice in its entirety, |
| 12 |
* including the disclaimer of warranties. |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* 3. The name of the author may not be used to endorse or promote |
| 17 |
* products derived from this software without specific prior |
| 18 |
* written permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 21 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 22 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF |
| 23 |
* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE |
| 24 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT |
| 26 |
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 |
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 30 |
* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH |
| 31 |
* DAMAGE. |
| 32 |
* %End-Header% |
| 33 |
*/ |
| 34 |
|
| 35 |
#ifdef _WIN32 |
| 36 |
#define _WIN32_WINNT 0x0500 |
| 37 |
#include <windows.h> |
| 38 |
#define UUID MYUUID |
| 39 |
#endif |
| 40 |
|
| 41 |
#include <ctype.h> |
| 42 |
#include <fcntl.h> |
| 43 |
#include <stdio.h> |
| 44 |
#include <stdlib.h> |
| 45 |
#include <sys/stat.h> |
| 46 |
|
| 47 |
#include "c.h" |
| 48 |
#include "uuid.h" |
| 49 |
|
| 50 |
static int test_uuid(const char * uuid, int isValid) |
| 51 |
{ |
| 52 |
static const char * validStr[2] = {"invalid", "valid"}; |
| 53 |
uuid_t uuidBits; |
| 54 |
int parsedOk; |
| 55 |
|
| 56 |
parsedOk = uuid_parse(uuid, uuidBits) == 0; |
| 57 |
|
| 58 |
printf("%s is %s", uuid, validStr[isValid]); |
| 59 |
if (parsedOk != isValid) { |
| 60 |
printf(" but uuid_parse says %s\n", validStr[parsedOk]); |
| 61 |
return 1; |
| 62 |
} |
| 63 |
printf(", OK\n"); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
static int check_uuids_in_file(const char *file) |
| 68 |
{ |
| 69 |
int ret = 0; |
| 70 |
size_t alloc = 0; |
| 71 |
ssize_t sz; |
| 72 |
char *str = NULL; |
| 73 |
FILE *f; |
| 74 |
uuid_t uuidBits; |
| 75 |
|
| 76 |
if ((f = fopen(file, "r")) == NULL) { |
| 77 |
warn("%s", file); |
| 78 |
return 1; |
| 79 |
} |
| 80 |
while ((sz = getline(&str, &alloc, f)) != -1) { |
| 81 |
if (sz == 0 || str[0] == '\n') |
| 82 |
continue; |
| 83 |
if (str[sz - 1] == '\n') |
| 84 |
str[sz - 1] = '\0'; |
| 85 |
if (uuid_parse(str, uuidBits)) { |
| 86 |
warnx("%s: %s", file, str); |
| 87 |
ret++; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
fclose(f); |
| 92 |
free(str); |
| 93 |
return ret; |
| 94 |
} |
| 95 |
|
| 96 |
int |
| 97 |
main(int argc, char **argv) |
| 98 |
{ |
| 99 |
int failed = 0; |
| 100 |
|
| 101 |
if (argc < 2) { |
| 102 |
failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1); |
| 103 |
failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1); |
| 104 |
failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0); |
| 105 |
failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0); |
| 106 |
failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0); |
| 107 |
failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0); |
| 108 |
failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0); |
| 109 |
failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0); |
| 110 |
failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0); |
| 111 |
failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0); |
| 112 |
failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0); |
| 113 |
failed += test_uuid("00000000-0000-0000-0000-000000000000", 1); |
| 114 |
failed += test_uuid("01234567-89ab-cdef-0134-567890abcedf", 1); |
| 115 |
failed += test_uuid("ffffffff-ffff-ffff-ffff-ffffffffffff", 1); |
| 116 |
} else { |
| 117 |
int i; |
| 118 |
|
| 119 |
for (i = 1; i < argc; i++) |
| 120 |
failed += check_uuids_in_file(argv[i]); |
| 121 |
} |
| 122 |
if (failed) { |
| 123 |
printf("%d failures.\n", failed); |
| 124 |
exit(1); |
| 125 |
} |
| 126 |
return 0; |
| 127 |
} |