summaryrefslogtreecommitdiff
path: root/tests/test_uuid.c
diff options
context:
space:
mode:
authorMark Michelson <mmichelson@digium.com>2012-12-11 21:04:45 +0000
committerMark Michelson <mmichelson@digium.com>2012-12-11 21:04:45 +0000
commit8cb156bfc1533fa7442f9824b00b15c09ad62d19 (patch)
treeca373aceb97237a8414f774389d71cb7a02e95e0 /tests/test_uuid.c
parent619de7f01222e36aac4bd997678dcf2bc15afe70 (diff)
Add UUID support to Asterisk.
This provides a common API for dealing with unique identifiers. The API provides methods to create, parse, copy, and stringify UUIDs. An accompanying unit test is provided that tests all operations. (closes issue ASTERISK-20726) reported by Matt Jordan Review: https://reviewboard.asterisk.org/r/2217 git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@377846 65c4cc65-6c06-0410-ace0-fbb531ad65f3
Diffstat (limited to 'tests/test_uuid.c')
-rw-r--r--tests/test_uuid.c136
1 files changed, 136 insertions, 0 deletions
diff --git a/tests/test_uuid.c b/tests/test_uuid.c
new file mode 100644
index 000000000..a76ec8b39
--- /dev/null
+++ b/tests/test_uuid.c
@@ -0,0 +1,136 @@
+/*
+ * 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;
+ }
+
+ /* First, 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, "Converted uuid 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");