[88e7dc5] | 1 | /*
|
---|
| 2 | * Copyright (c) 2019 Matthieu Riolo
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <pcut/pcut.h>
|
---|
| 30 | #include <uuid.h>
|
---|
| 31 | #include <stdbool.h>
|
---|
| 32 | #include <stdio.h>
|
---|
| 33 | #include <str.h>
|
---|
| 34 | #include <ctype.h>
|
---|
| 35 |
|
---|
| 36 | static size_t MAX_SUB_TESTS = 10;
|
---|
| 37 | static size_t uuids_len = 6;
|
---|
| 38 | static const char *uuids[] = {
|
---|
| 39 | /* uppercase */
|
---|
| 40 | "F81163AE-299A-4DA2-BED1-0E096C59F3AB",
|
---|
| 41 | "4A0CE2A3-FD1C-4951-972E-AAA13A703078",
|
---|
| 42 | "69C7DB62-8309-4C58-831B-8C4E4161E8AC",
|
---|
| 43 |
|
---|
[d1582b50] | 44 | /* lower case */
|
---|
[88e7dc5] | 45 | "c511bf24-70cb-422e-933b-2a74ab699a56",
|
---|
| 46 | "7b1abd05-456f-4661-ab62-917685069343",
|
---|
| 47 | "5b00f76b-4a16-4dce-a1fc-b78c60324d89"
|
---|
| 48 | };
|
---|
| 49 |
|
---|
| 50 | static bool uuid_valid(uuid_t uuid)
|
---|
| 51 | {
|
---|
[d85d4f2] | 52 | if (!((uuid.time_hi_and_version & 0xf000) & 0x4000)) {
|
---|
[88e7dc5] | 53 | return false;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[d85d4f2] | 56 | int f = (uuid.clock_seq_hi_and_reserved & 0x80) || (uuid.clock_seq_hi_and_reserved & 0x90);
|
---|
| 57 | f = f || (uuid.clock_seq_hi_and_reserved & 0xA0) || (uuid.clock_seq_hi_and_reserved & 0xB0);
|
---|
[88e7dc5] | 58 | if (!f) {
|
---|
| 59 | return false;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | return true;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | PCUT_INIT;
|
---|
| 66 |
|
---|
| 67 | PCUT_TEST_SUITE(uuid);
|
---|
| 68 |
|
---|
| 69 | PCUT_TEST(uuid_generate)
|
---|
| 70 | {
|
---|
| 71 | uuid_t uuid;
|
---|
| 72 | size_t i;
|
---|
| 73 |
|
---|
| 74 | for (i = 0; i < MAX_SUB_TESTS; i++) {
|
---|
| 75 | errno_t ret = uuid_generate(&uuid);
|
---|
| 76 | PCUT_ASSERT_ERRNO_VAL(EOK, ret);
|
---|
| 77 | PCUT_ASSERT_TRUE(uuid_valid(uuid));
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | PCUT_TEST(uuid_parse)
|
---|
| 82 | {
|
---|
| 83 | uuid_t uuid;
|
---|
| 84 | errno_t ret;
|
---|
| 85 |
|
---|
| 86 | for (size_t i = 0; i < uuids_len; i++) {
|
---|
| 87 | ret = uuid_parse(uuids[i], &uuid, NULL);
|
---|
| 88 | PCUT_ASSERT_ERRNO_VAL(EOK, ret);
|
---|
| 89 | PCUT_ASSERT_TRUE(uuid_valid(uuid));
|
---|
| 90 | }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | PCUT_TEST(uuid_parse_in_text)
|
---|
| 94 | {
|
---|
| 95 | uuid_t uuid;
|
---|
| 96 | errno_t ret;
|
---|
| 97 | const char *endptr;
|
---|
| 98 | const char *uuid_in_text = "7b1abd05-456f-4661-ab62-917685069343hello world!";
|
---|
| 99 |
|
---|
| 100 | ret = uuid_parse(uuid_in_text, &uuid, &endptr);
|
---|
| 101 |
|
---|
| 102 | PCUT_ASSERT_ERRNO_VAL(EOK, ret);
|
---|
| 103 | PCUT_ASSERT_TRUE(uuid_valid(uuid));
|
---|
| 104 | PCUT_ASSERT_STR_EQUALS("hello world!", endptr);
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | PCUT_TEST(uuid_format_generated)
|
---|
| 108 | {
|
---|
| 109 | uuid_t uuid;
|
---|
| 110 | size_t i;
|
---|
| 111 | char *rstr;
|
---|
| 112 |
|
---|
| 113 | for (i = 0; i < MAX_SUB_TESTS; i++) {
|
---|
| 114 | errno_t ret = uuid_generate(&uuid);
|
---|
| 115 | PCUT_ASSERT_ERRNO_VAL(EOK, ret);
|
---|
| 116 | PCUT_ASSERT_TRUE(uuid_valid(uuid));
|
---|
| 117 |
|
---|
| 118 | ret = uuid_format(&uuid, &rstr, true);
|
---|
| 119 | PCUT_ASSERT_ERRNO_VAL(EOK, ret);
|
---|
| 120 | PCUT_ASSERT_INT_EQUALS('\0', rstr[37]);
|
---|
| 121 | PCUT_ASSERT_INT_EQUALS(36, str_length(rstr));
|
---|
| 122 | PCUT_ASSERT_INT_EQUALS('4', rstr[14]);
|
---|
| 123 |
|
---|
| 124 | int f = rstr[19] == '8' || rstr[19] == '9';
|
---|
| 125 | f = f || toupper(rstr[19]) == 'A' || toupper(rstr[19]) == 'B';
|
---|
| 126 | PCUT_ASSERT_TRUE(f);
|
---|
| 127 |
|
---|
| 128 | free(rstr);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | PCUT_TEST(uuid_format_parsed)
|
---|
| 133 | {
|
---|
| 134 | uuid_t uuid;
|
---|
| 135 | char *rstr;
|
---|
| 136 | errno_t ret;
|
---|
| 137 |
|
---|
| 138 | for (size_t i = 0; i < uuids_len; i++) {
|
---|
| 139 | ret = uuid_parse(uuids[i], &uuid, NULL);
|
---|
| 140 | PCUT_ASSERT_ERRNO_VAL(EOK, ret);
|
---|
| 141 | PCUT_ASSERT_TRUE(uuid_valid(uuid));
|
---|
| 142 |
|
---|
| 143 | ret = uuid_format(&uuid, &rstr, true);
|
---|
| 144 | PCUT_ASSERT_ERRNO_VAL(EOK, ret);
|
---|
| 145 | PCUT_ASSERT_INT_EQUALS('\0', rstr[37]);
|
---|
| 146 | PCUT_ASSERT_INT_EQUALS(36, str_length(rstr));
|
---|
| 147 | PCUT_ASSERT_INT_EQUALS(0, str_casecmp(uuids[i], rstr));
|
---|
| 148 |
|
---|
| 149 | free(rstr);
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | PCUT_EXPORT(uuid);
|
---|