File: uuid.c

package info (click to toggle)
libigloo 0.9.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,968 kB
  • sloc: ansic: 5,476; sh: 4,996; makefile: 62
file content (173 lines) | stat: -rw-r--r-- 8,521 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Copyright (C) 2022       Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <igloo/tap.h>
#include <igloo/igloo.h>
#include <igloo/uuid.h>
#include <igloo/sp.h>

static igloo_ro_t g_instance;

static void check_uuid(const char *ref)
{
    size_t len = strlen(ref);
    igloo_tap_test("strlen(ref) == 36", len == 36);

    igloo_tap_diagnostic("Ref is:");
    igloo_tap_diagnostic(ref);

    if (len == 36) {
        igloo_tap_test_success("igloo_uuid_is_valid", igloo_uuid_is_valid(ref, igloo_UUID_STRICTNESS_STRICT));
    }
}

static void group_valid(void)
{
    static const struct {
        const char *str;
        igloo_uuid_strictness_t strictness;
        igloo_error_t error;
    } vectors[] = {
        /* invalid group length */
        {"551a9547-fe63-4cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-4cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-4cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_ILLSEQ},

        {"551a9547-fe63-45cf-a153-2801bac47", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-45cf-a153-2801bac47", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-45cf-a153-2801bac47", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_ILLSEQ},

        /* invalid characters */
        {"551a9547-fe63-4zcf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-4zcf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-4zcf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-4 cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-4 cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-4 cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_ILLSEQ},

        /* all fine */
        {"551a9547-fe63-45cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_NONE},
        {"551a9547-fe63-45cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_NONE},
        {"551a9547-fe63-45cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_NONE},

        /* upper case */
        {"551a9547-FE63-45cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {"551a9547-FE63-45cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_NONE},
        {"551a9547-FE63-45cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_NONE},

        /* spaces */
        {" 551a9547-fe63-45cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {" 551a9547-fe63-45cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {" 551a9547-fe63-45cf-a153-2801bac47ff8", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_NONE},
        {"551a9547-fe63-45cf-a153-2801bac47ff8 ", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-45cf-a153-2801bac47ff8 ", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-45cf-a153-2801bac47ff8 ", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_NONE},

        /* spaces and upper case */
        {" 551a9547-fe63-45CF-a153-2801bac47ff8", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {" 551a9547-fe63-45CF-a153-2801bac47ff8", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {" 551a9547-fe63-45CF-a153-2801bac47ff8", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_NONE},
        {"551a9547-fe63-45CF-a153-2801bac47ff8 ", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-45CF-a153-2801bac47ff8 ", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {"551a9547-fe63-45CF-a153-2801bac47ff8 ", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_NONE},

        /* fences */
        {"{551a9547-fe63-45cf-a153-2801bac47ff8}", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {"{551a9547-fe63-45cf-a153-2801bac47ff8}", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {"{551a9547-fe63-45cf-a153-2801bac47ff8}", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_NONE},
        {" {551a9547-fe63-45cf-a153-2801bac47ff8} ", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {" {551a9547-fe63-45cf-a153-2801bac47ff8} ", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {" {551a9547-fe63-45cf-a153-2801bac47ff8} ", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_NONE},
        {" { 551a9547-fe63-45cf-a153-2801bac47ff8} ", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {" { 551a9547-fe63-45cf-a153-2801bac47ff8} ", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {" { 551a9547-fe63-45cf-a153-2801bac47ff8} ", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_ILLSEQ},
        {" {551a9547-fe63-45cf-a153-2801bac47ff8 } ", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {" {551a9547-fe63-45cf-a153-2801bac47ff8 } ", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {" {551a9547-fe63-45cf-a153-2801bac47ff8 } ", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_ILLSEQ},
        {" {551a9547-fe63-45cf-a153-2801bac47ff8 ", igloo_UUID_STRICTNESS_STRICT, igloo_ERROR_ILLSEQ},
        {" {551a9547-fe63-45cf-a153-2801bac47ff8 ", igloo_UUID_STRICTNESS_RELAXED, igloo_ERROR_ILLSEQ},
        {" {551a9547-fe63-45cf-a153-2801bac47ff8 ", igloo_UUID_STRICTNESS_LOOSE, igloo_ERROR_ILLSEQ},
    };

    for (size_t i = 0; i < (sizeof(vectors)/sizeof(*vectors)); i++) {
        igloo_error_t ret = igloo_uuid_is_valid(vectors[i].str, vectors[i].strictness);
        const igloo_error_desc_t * error_desc = igloo_error_get_description(vectors[i].error);
        char buf[1024] = "XXX";

        switch (vectors[i].strictness) {
            case igloo_UUID_STRICTNESS_STRICT:
                snprintf(buf, sizeof(buf), "igloo_uuid_is_valid(\"%s\", igloo_UUID_STRICTNESS_STRICT) -> %s", vectors[i].str, error_desc->name);
                break;
            case igloo_UUID_STRICTNESS_RELAXED:
                snprintf(buf, sizeof(buf), "igloo_uuid_is_valid(\"%s\", igloo_UUID_STRICTNESS_RELAXED) -> %s", vectors[i].str, error_desc->name);
                break;
            case igloo_UUID_STRICTNESS_LOOSE:
                snprintf(buf, sizeof(buf), "igloo_uuid_is_valid(\"%s\", igloo_UUID_STRICTNESS_LOOSE) -> %s", vectors[i].str, error_desc->name);
                break;
        }

        igloo_tap_test_error(buf, vectors[i].error, ret);
    }
}

static void group_sp(void)
{
    const char *ref = NULL;

    igloo_tap_test_success("igloo_uuid_new_random_sp", igloo_uuid_new_random_sp(&ref, g_instance));
    igloo_tap_test("ref != NULL", ref != NULL);
    if (ref) {
        check_uuid(ref);
        igloo_tap_test_success("igloo_sp_unref", igloo_sp_unref(&ref, g_instance));
        igloo_tap_test("ref == NULL", ref == NULL);
    }
}

static void group_cstr(void)
{
    char *ref = NULL;

    igloo_tap_test_success("igloo_uuid_new_random_cstr", igloo_uuid_new_random_cstr(&ref, g_instance));
    igloo_tap_test("ref != NULL", ref != NULL);
    if (ref) {
        check_uuid(ref);
        free(ref);
    }
}

int main (void)
{
    igloo_tap_init();
    igloo_tap_exit_on(igloo_TAP_EXIT_ON_FIN, NULL);
    igloo_tap_test_success("igloo_initialize", igloo_initialize(&g_instance));
    igloo_tap_group_run("valid", group_valid);
    igloo_tap_group_run("sp", group_sp);
    igloo_tap_group_run("cstr", group_cstr);
    igloo_tap_test_success("unref instance", igloo_ro_unref(&g_instance));
    igloo_tap_fin();

    return EXIT_FAILURE; // return failure as we should never reach this point!
}