Asterisk - The Open Source Telephony Project  18.5.0
test_uuid.c
Go to the documentation of this file.
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 
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];
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);
150 }
151 
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
Asterisk main include file. File version handling, generic pbx functions.
#define AST_UUID_STR_LEN
Definition: uuid.h:27
static int load_module(void)
Definition: test_uuid.c:146
struct ast_uuid * ast_uuid_copy(struct ast_uuid *src)
Make a copy of a UUID.
Definition: uuid.c:168
int ast_uuid_is_nil(struct ast_uuid *uuid)
Check if a UUID is a nil UUID (all 0s)
Definition: uuid.c:189
Universally unique identifier support.
Test Framework API.
AST_TEST_DEFINE(uuid)
Definition: test_uuid.c:33
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define NULL
Definition: resample.c:96
char * end
Definition: eagi_proxy.c:73
Definition: uuid.c:39
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
void ast_uuid_clear(struct ast_uuid *uuid)
Clear a UUID by setting it to be a nil UUID (all 0s)
Definition: uuid.c:184
struct ast_uuid * ast_str_to_uuid(char *str)
Convert a string to a UUID.
Definition: uuid.c:151
static int unload_module(void)
Definition: test_uuid.c:140
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
def info(msg)
struct ast_uuid * ast_uuid_generate(void)
Generate a UUID.
Definition: uuid.c:125
#define ast_free(a)
Definition: astmm.h:182
char * ast_uuid_generate_str(char *buf, size_t size)
Generate a UUID string.
Definition: uuid.c:143
int ast_uuid_compare(struct ast_uuid *left, struct ast_uuid *right)
Compare two UUIDs.
Definition: uuid.c:179
char * ast_uuid_to_str(struct ast_uuid *uuid, char *buf, size_t size)
Convert a UUID to a string.
Definition: uuid.c:136
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
Asterisk module definitions.
ast_test_result_state
Definition: test.h:200