1 /* guuid.c
2 *
3 * Copyright (C) 2013-2015, 2017 Red Hat, Inc.
4 *
5 * SPDX-License-Identifier: LGPL-2.1-or-later
6 *
7 * This is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include "config.h"
23
24 #undef G_DISABLE_ASSERT
25
26 #include <glib.h>
27 #include <string.h>
28
29 static void
30 test_guuid_string (void)
31 {
32 g_assert_false (g_uuid_string_is_valid ("00010203-0405-0607-0809"));
33 g_assert_false (g_uuid_string_is_valid ("zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz"));
34 g_assert_false (g_uuid_string_is_valid ("000102030405060708090a0b0c0d0e0f"));
35 g_assert_false (g_uuid_string_is_valid ("{urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6}"));
36 g_assert_false (g_uuid_string_is_valid ("urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6"));
37
38 g_assert_true (g_uuid_string_is_valid ("00010203-0405-0607-0809-0a0b0c0d0e0f"));
39 g_assert_true (g_uuid_string_is_valid ("7d444840-9dc0-11d1-b245-5ffdce74fad2"));
40 g_assert_true (g_uuid_string_is_valid ("e902893a-9d22-3c7e-a7b8-d6e313b71d9f"));
41 g_assert_true (g_uuid_string_is_valid ("6ba7b810-9dad-11d1-80b4-00c04fd430c8"));
42 }
43
44 static void
45 test_guuid_random (void)
46 {
47 gchar *str1, *str2;
48
49 str1 = g_uuid_string_random ();
50 g_assert_cmpuint (strlen (str1), ==, 36);
51 g_assert_true (g_uuid_string_is_valid (str1));
52
53 str2 = g_uuid_string_random ();
54 g_assert_cmpuint (strlen (str2), ==, 36);
55 g_assert_true (g_uuid_string_is_valid (str2));
56 g_assert_cmpstr (str1, !=, str2);
57
58 g_free (str1);
59 g_free (str2);
60 }
61
62 int
63 main (int argc, char **argv)
64 {
65 g_test_init (&argc, &argv, NULL);
66
67 /* GUuid Tests */
68 g_test_add_func ("/uuid/string", test_guuid_string);
69 g_test_add_func ("/uuid/random", test_guuid_random);
70
71 return g_test_run ();
72 }