119c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o/* 219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * tst_uuid.c --- test program from the UUID library 319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * 41e0a221b5d2cb1f82037e469c828c6db772b328eTheodore Ts'o * Copyright (C) 1996, 1997, 1998 Theodore Ts'o. 519c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * 619c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %Begin-Header% 71bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * Redistribution and use in source and binary forms, with or without 81bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * modification, are permitted provided that the following conditions 91bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * are met: 101bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * 1. Redistributions of source code must retain the above copyright 111bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * notice, and the entire permission notice in its entirety, 121bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * including the disclaimer of warranties. 131bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * 2. Redistributions in binary form must reproduce the above copyright 141bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * notice, this list of conditions and the following disclaimer in the 151bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * documentation and/or other materials provided with the distribution. 161bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * 3. The name of the author may not be used to endorse or promote 171bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * products derived from this software without specific prior 181bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * written permission. 19efc6f628e15de95bcd13e4f0ee223cb42115d520Theodore Ts'o * 201bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 211bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 221bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF 231bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE 241bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 251bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 261bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 271bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 281bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 291bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 301bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH 311bbfec624c4bbe767060a13762aa9a656536a4fdTheodore Ts'o * DAMAGE. 3219c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o * %End-Header% 3319c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o */ 3419c78dc07fce2d6f39b5e541562afc3ca1ea38ffTheodore Ts'o 35e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall 366ec9ef1881e636540cfe28e6f32c93e1781ad173Theodore Ts'o#ifdef _WIN32 376ec9ef1881e636540cfe28e6f32c93e1781ad173Theodore Ts'o#define _WIN32_WINNT 0x0500 386ec9ef1881e636540cfe28e6f32c93e1781ad173Theodore Ts'o#include <windows.h> 396ec9ef1881e636540cfe28e6f32c93e1781ad173Theodore Ts'o#define UUID MYUUID 406ec9ef1881e636540cfe28e6f32c93e1781ad173Theodore Ts'o#endif 416ec9ef1881e636540cfe28e6f32c93e1781ad173Theodore Ts'o 421e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o#include <stdio.h> 43b969b1b8a5c13992cadb026114731958644540d8Matthias Andree#include <stdlib.h> 441e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o 45e1f085079525c0ed4c5ab1f2abedaf9407a7b8daTheodore Ts'o#include <uuid/uuid.h> 461e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o 478d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'ostatic int test_uuid(const char * uuid, int isValid) 488d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o{ 498d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o static const char * validStr[2] = {"invalid", "valid"}; 508d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o uuid_t uuidBits; 518d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o int parsedOk; 528d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o 538d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o parsedOk = uuid_parse(uuid, uuidBits) == 0; 54b969b1b8a5c13992cadb026114731958644540d8Matthias Andree 558d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o printf("%s is %s", uuid, validStr[isValid]); 568d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o if (parsedOk != isValid) { 578d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o printf(" but uuid_parse says %s\n", validStr[parsedOk]); 588d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o return 1; 598d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o } 608d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o printf(", OK\n"); 618d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o return 0; 628d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o} 638d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o 6496f4bb1fbc763ff1236417a28e0de457ffc95a0cTheodore Ts'o#ifdef __GNUC__ 6596f4bb1fbc763ff1236417a28e0de457ffc95a0cTheodore Ts'o#define ATTR(x) __attribute__(x) 6696f4bb1fbc763ff1236417a28e0de457ffc95a0cTheodore Ts'o#else 6796f4bb1fbc763ff1236417a28e0de457ffc95a0cTheodore Ts'o#define ATTR(x) 6896f4bb1fbc763ff1236417a28e0de457ffc95a0cTheodore Ts'o#endif 6996f4bb1fbc763ff1236417a28e0de457ffc95a0cTheodore Ts'o 701e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'oint 7196f4bb1fbc763ff1236417a28e0de457ffc95a0cTheodore Ts'omain(int argc ATTR((unused)) , char **argv ATTR((unused))) 721e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o{ 731e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o uuid_t buf, tst; 741e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o char str[100]; 751e0a221b5d2cb1f82037e469c828c6db772b328eTheodore Ts'o struct timeval tv; 76e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall time_t time_reg, time_gen; 771e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o unsigned char *cp; 781e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o int i; 791e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o int failed = 0; 80b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o int type, variant; 811e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o 821e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o uuid_generate(buf); 831e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o uuid_unparse(buf, str); 84b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("UUID generate = %s\n", str); 85b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("UUID: "); 86b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { 87b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("%02x", *cp++); 88b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o } 89b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("\n"); 90b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o type = uuid_type(buf); variant = uuid_variant(buf); 91b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("UUID type = %d, UUID variant = %d\n", type, variant); 92b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o if (variant != UUID_VARIANT_DCE) { 93b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("Incorrect UUID Variant; was expecting DCE!\n"); 94b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o failed++; 95b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o } 96b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("\n"); 97b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o 98b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o uuid_generate_random(buf); 99b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o uuid_unparse(buf, str); 100b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("UUID random string = %s\n", str); 1011e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o printf("UUID: "); 1021e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { 1031e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o printf("%02x", *cp++); 1041e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o } 1051e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o printf("\n"); 106e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall type = uuid_type(buf); 107e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall variant = uuid_variant(buf); 108b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("UUID type = %d, UUID variant = %d\n", type, variant); 109b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o if (variant != UUID_VARIANT_DCE) { 110b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("Incorrect UUID Variant; was expecting DCE!\n"); 111b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o failed++; 112b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o } 113b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o if (type != 4) { 114b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("Incorrect UUID type; was expecting " 115b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o "4 (random type)!\n"); 116b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o failed++; 117b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o } 118b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("\n"); 119b969b1b8a5c13992cadb026114731958644540d8Matthias Andree 120e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall time_gen = time(0); 121b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o uuid_generate_time(buf); 122b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o uuid_unparse(buf, str); 123b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("UUID string = %s\n", str); 124b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("UUID time: "); 125b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o for (i=0, cp = (unsigned char *) &buf; i < 16; i++) { 126b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("%02x", *cp++); 127b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o } 128b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("\n"); 129e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall type = uuid_type(buf); 130e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall variant = uuid_variant(buf); 131b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("UUID type = %d, UUID variant = %d\n", type, variant); 132b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o if (variant != UUID_VARIANT_DCE) { 133b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("Incorrect UUID Variant; was expecting DCE!\n"); 134b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o failed++; 135b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o } 136b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o if (type != 1) { 137b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o printf("Incorrect UUID type; was expecting " 138b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o "1 (time-based type)!\\n"); 139b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o failed++; 140b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o } 141e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall 1421e0a221b5d2cb1f82037e469c828c6db772b328eTheodore Ts'o tv.tv_sec = 0; 1431e0a221b5d2cb1f82037e469c828c6db772b328eTheodore Ts'o tv.tv_usec = 0; 1441e0a221b5d2cb1f82037e469c828c6db772b328eTheodore Ts'o time_reg = uuid_time(buf, &tv); 145e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall printf("UUID generated at %lu reports %lu (%ld.%ld)\n", 146e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall time_gen, time_reg, tv.tv_sec, (long)tv.tv_usec); 147e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall /* allow 1s margin in case of rollover between sampling 148e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall * the current time and when the UUID is generated. */ 149e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall if (time_reg > time_gen + 1) { 150e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall printf("UUID time comparison failed!\n"); 151e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall failed++; 152e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall } else { 153e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall printf("UUID time comparison succeeded.\n"); 154e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall } 155e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall 1561e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o uuid_parse(str, tst); 157e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall if (!uuid_compare(buf, tst)) { 1581e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o printf("UUID parse and compare succeeded.\n"); 159e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall } else { 1601e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o printf("UUID parse and compare failed!\n"); 1611e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o failed++; 1621e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o } 1631e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o uuid_clear(tst); 1641e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o if (uuid_is_null(tst)) 1651e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o printf("UUID clear and is null succeeded.\n"); 1661e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o else { 1671e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o printf("UUID clear and is null failed!\n"); 1681e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o failed++; 1691e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o } 1701e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o uuid_copy(buf, tst); 171b19d1a959eeea17d6b899a5b994bf3f3691de947Theodore Ts'o if (!uuid_compare(buf, tst)) 1721e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o printf("UUID copy and compare succeeded.\n"); 1731e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o else { 1741e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o printf("UUID copy and compare failed!\n"); 1751e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o failed++; 1761e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o } 177e0ed7404719a9ddd2ba427a80db5365c8bad18c0JP Abgrall 1788d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981b", 1); 1798d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("84949CC5-4701-4A84-895B-354C584A981B", 1); 1808d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981bc", 0); 1818d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981", 0); 1828d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("84949cc5x4701-4a84-895b-354c584a981b", 0); 1838d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("84949cc504701-4a84-895b-354c584a981b", 0); 1848d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("84949cc5-470104a84-895b-354c584a981b", 0); 1858d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("84949cc5-4701-4a840895b-354c584a981b", 0); 1868d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("84949cc5-4701-4a84-895b0354c584a981b", 0); 1878d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("g4949cc5-4701-4a84-895b-354c584a981b", 0); 1888d7f458743c2e37f15f58a38a67a85c3a8bb5f3dTheodore Ts'o failed += test_uuid("84949cc5-4701-4a84-895b-354c584a981g", 0); 189b969b1b8a5c13992cadb026114731958644540d8Matthias Andree 1901e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o if (failed) { 1911e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o printf("%d failures.\n", failed); 1921e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o exit(1); 1931e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o } 1941e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o return 0; 1951e3472c5f37ca3686dd69b079d4d02a302f5798dTheodore Ts'o} 196