summaryrefslogtreecommitdiff
path: root/tests/test_uuid.c
blob: 70769ef95350e37f55b7eca5f1fb558220a4715e (plain)
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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012, Digium, Inc.
 *
 * Mark Michelson <mmmichelson@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 * \brief Universally unique identifier tests
 */

/*** MODULEINFO
	<depend>TEST_FRAMEWORK</depend>
	<support_level>core</support_level>
 ***/

#include "asterisk.h"
#include "asterisk/test.h"
#include "asterisk/uuid.h"
#include "asterisk/module.h"

AST_TEST_DEFINE(uuid)
{
	struct ast_uuid *uuid1 = NULL;
	struct ast_uuid *uuid2 = NULL;
	struct ast_uuid *uuid3 = NULL;
	char uuid_str[AST_UUID_STR_LEN];
	enum ast_test_result_state res = AST_TEST_FAIL;

	switch (cmd) {
	case TEST_INIT:
		info->name = "uuid";
		info->category = "/main/uuid/";
		info->summary = "UUID unit test";
		info->description =
			"This tests basic UUID operations to ensure they work properly";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	/* Use method of generating UUID directly as a string. */
	ast_uuid_generate_str(uuid_str, sizeof(uuid_str));
	if (strlen(uuid_str) != (AST_UUID_STR_LEN - 1)) {
		ast_test_status_update(test, "Failed to directly generate UUID string\n");
		goto end;
	}
	ast_test_status_update(test, "Generate UUID direct to string, got %s\n", uuid_str);

	/* Now convert the direct UUID string to a UUID */
	uuid1 = ast_str_to_uuid(uuid_str);
	if (!uuid1) {
		ast_test_status_update(test, "Unable to convert direct UUID string %s to UUID\n", uuid_str);
		goto end;
	}
	ast_free(uuid1);

	/* Make sure that we can generate a UUID */
	uuid1 = ast_uuid_generate();
	if (!uuid1) {
		ast_test_status_update(test, "Unable to generate a UUID\n");
		goto end;
	}

	/* Make sure we're not generating nil UUIDs */
	if (ast_uuid_is_nil(uuid1)) {
		ast_test_status_update(test, "We generated a nil UUID. Something is wrong\n");
		goto end;
	}

	/* Convert it to a string */
	ast_uuid_to_str(uuid1, uuid_str, sizeof(uuid_str));

	if (strlen(uuid_str) != (AST_UUID_STR_LEN - 1)) {
		ast_test_status_update(test, "Failed to convert the UUID to a string\n");
		goto end;
	}

	ast_test_status_update(test, "Second generated UUID converted to string, got %s\n", uuid_str);

	/* Now convert the string back to a UUID */
	uuid2 = ast_str_to_uuid(uuid_str);
	if (!uuid2) {
		ast_test_status_update(test, "Unable to convert string %s to UUID\n", uuid_str);
		goto end;
	}

	/* Make sure the UUIDs are identical */
	if (ast_uuid_compare(uuid1, uuid2) != 0) {
		ast_test_status_update(test, "UUIDs that should be identical are different\n");
		goto end;
	}

	/* Try copying a UUID */
	uuid3 = ast_uuid_copy(uuid1);
	if (!uuid3) {
		ast_test_status_update(test, "Unable to copy UUID\n");
		goto end;
	}

	/* Make sure copied UUIDs are identical */
	if (ast_uuid_compare(uuid1, uuid3) != 0) {
		ast_test_status_update(test, "UUIDs that should be identical are different\n");
		goto end;
	}

	if (ast_uuid_compare(uuid2, uuid3) != 0) {
		ast_test_status_update(test, "UUIDs that should be identical are different\n");
		goto end;
	}

	/* Clear a UUID and ensure that it registers as nil */
	ast_uuid_clear(uuid1);

	if (!ast_uuid_is_nil(uuid1)) {
		ast_test_status_update(test, "UUID that was cleared does not appear to be nil\n");
		goto end;
	}

	res = AST_TEST_PASS;

end:
	ast_free(uuid1);
	ast_free(uuid2);
	ast_free(uuid3);
	return res;
}

static int unload_module(void)
{
	AST_TEST_UNREGISTER(uuid);
	return 0;
}

static int load_module(void)
{
	AST_TEST_REGISTER(uuid);
	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "UUID test module");