Debian Codegraph @

tests/test_uuid.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2012, Digium, Inc.
5 *
6 * Mark Michelson <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*! \file
20 * \brief Universally unique identifier tests
21 */
22
23 /*** MODULEINFO
24 <depend>TEST_FRAMEWORK</depend>
25 <support_level>core</support_level>
26 ***/
27
28 #include "asterisk.h"
29 #include "asterisk/test.h"
30 #include "asterisk/uuid.h"
31 #include "asterisk/module.h"
32
33 AST_TEST_DEFINE(uuid)
34 {
35 struct ast_uuid *uuid1 = NULL;
36 struct ast_uuid *uuid2 = NULL;
37 struct ast_uuid *uuid3 = NULL;
38 char uuid_str[AST_UUID_STR_LEN];
39 enum ast_test_result_state res = AST_TEST_FAIL;
40
41 switch (cmd) {
42 case TEST_INIT:
43 info->name = "uuid";
44 info->category = "/main/uuid/";
45 info->summary = "UUID unit test";
46 info->description =
47 "This tests basic UUID operations to ensure they work properly";
48 return AST_TEST_NOT_RUN;
49 case TEST_EXECUTE:
50 break;
51 }
52
53 /* Use method of generating UUID directly as a string. */
54 ast_uuid_generate_str(uuid_str, sizeof(uuid_str));
55 if (strlen(uuid_str) != (AST_UUID_STR_LEN - 1)) {
56 ast_test_status_update(test, "Failed to directly generate UUID string\n");
57 goto end;
58 }
59 ast_test_status_update(test, "Generate UUID direct to string, got %s\n", uuid_str);
60
61 /* Now convert the direct UUID string to a UUID */
62 uuid1 = ast_str_to_uuid(uuid_str);
63 if (!uuid1) {
64 ast_test_status_update(test, "Unable to convert direct UUID string %s to UUID\n", uuid_str);
65 goto end;
66 }
67 ast_free(uuid1);
68
69 /* Make sure that we can generate a UUID */
70 uuid1 = ast_uuid_generate();
71 if (!uuid1) {
72 ast_test_status_update(test, "Unable to generate a UUID\n");
73 goto end;
74 }
75
76 /* Make sure we're not generating nil UUIDs */
77 if (ast_uuid_is_nil(uuid1)) {
78 ast_test_status_update(test, "We generated a nil UUID. Something is wrong\n");
79 goto end;
80 }
81
82 /* Convert it to a string */
83 ast_uuid_to_str(uuid1, uuid_str, sizeof(uuid_str));
84
85 if (strlen(uuid_str) != (AST_UUID_STR_LEN - 1)) {
86 ast_test_status_update(test, "Failed to convert the UUID to a string\n");
87 goto end;
88 }
89
90 ast_test_status_update(test, "Second generated UUID converted to string, got %s\n", uuid_str);
91
92 /* Now convert the string back to a UUID */
93 uuid2 = ast_str_to_uuid(uuid_str);
94 if (!uuid2) {
95 ast_test_status_update(test, "Unable to convert string %s to UUID\n", uuid_str);
96 goto end;
97 }
98
99 /* Make sure the UUIDs are identical */
100 if (ast_uuid_compare(uuid1, uuid2) != 0) {
101 ast_test_status_update(test, "UUIDs that should be identical are different\n");
102 goto end;
103 }
104
105 /* Try copying a UUID */
106 uuid3 = ast_uuid_copy(uuid1);
107 if (!uuid3) {
108 ast_test_status_update(test, "Unable to copy UUID\n");
109 goto end;
110 }
111
112 /* Make sure copied UUIDs are identical */
113 if (ast_uuid_compare(uuid1, uuid3) != 0) {
114 ast_test_status_update(test, "UUIDs that should be identical are different\n");
115 goto end;
116 }
117
118 if (ast_uuid_compare(uuid2, uuid3) != 0) {
119 ast_test_status_update(test, "UUIDs that should be identical are different\n");
120 goto end;
121 }
122
123 /* Clear a UUID and ensure that it registers as nil */
124 ast_uuid_clear(uuid1);
125
126 if (!ast_uuid_is_nil(uuid1)) {
127 ast_test_status_update(test, "UUID that was cleared does not appear to be nil\n");
128 goto end;
129 }
130
131 res = AST_TEST_PASS;
132
133 end:
134 ast_free(uuid1);
135 ast_free(uuid2);
136 ast_free(uuid3);
137 return res;
138 }
139
140 static int unload_module(void)
141 {
142 AST_TEST_UNREGISTER(uuid);
143 return 0;
144 }
145
146 static int load_module(void)
147 {
148 AST_TEST_REGISTER(uuid);
149 return AST_MODULE_LOAD_SUCCESS;
150 }
151
152 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "UUID test module");