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 fd, ret = 0;
      70  	size_t sz;
      71  	char str[UUID_STR_LEN];
      72  	uuid_t uuidBits;
      73  
      74  	if ((fd = open(file, O_RDONLY)) < 0) {
      75  		warn("%s", file);
      76  		return 1;
      77  	}
      78  	while ((sz = read(fd, str, sizeof(str))) != 0) {
      79  		str[sizeof(str) - 1] = '\0';
      80  		if (uuid_parse(str, uuidBits)) {
      81  			warnx("%s: %s", file, str);
      82  			ret++;
      83  		}
      84  	}
      85  
      86  	close(fd);
      87  	return ret;
      88  }
      89  
      90  int
      91  main(int argc, char **argv)
      92  {
      93  	int failed = 0;
      94  
      95  	if (argc < 2) {
      96  		failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1);
      97  		failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1);
      98  		failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0);
      99  		failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0);
     100  		failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0);
     101  		failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0);
     102  		failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0);
     103  		failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0);
     104  		failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0);
     105  		failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0);
     106  		failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0);
     107  		failed += test_uuid("00000000-0000-0000-0000-000000000000", 1);
     108  		failed += test_uuid("01234567-89ab-cdef-0134-567890abcedf", 1);
     109  		failed += test_uuid("ffffffff-ffff-ffff-ffff-ffffffffffff", 1);
     110  	} else {
     111  		int i;
     112  
     113  		for (i = 1; i < argc; i++)
     114  			failed += check_uuids_in_file(argv[i]);
     115  	}
     116  	if (failed) {
     117  		printf("%d failures.\n", failed);
     118  		exit(1);
     119  	}
     120  	return 0;
     121  }