Asterisk - The Open Source Telephony Project GIT-master-7e7a603
test_format_cache.c
Go to the documentation of this file.
1/*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Joshua Colp <jcolp@digium.com>
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/*!
20 * \file
21 * \brief Format Cache API Unit Tests
22 *
23 * \author Joshua Colp <jcolp@digium.com>
24 *
25 */
26
27/*** MODULEINFO
28 <depend>TEST_FRAMEWORK</depend>
29 <support_level>core</support_level>
30 ***/
31
32#include "asterisk.h"
33
34#include "asterisk/test.h"
35#include "asterisk/module.h"
36#include "asterisk/codec.h"
37#include "asterisk/format.h"
39
40AST_TEST_DEFINE(format_cache_set)
41{
42 RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
43 RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
44
45 switch (cmd) {
46 case TEST_INIT:
47 info->name = "format_cache_set";
48 info->category = "/main/format_cache/";
49 info->summary = "format cache add unit test";
50 info->description =
51 "Test that adding of a cached format succeeds";
52 return AST_TEST_NOT_RUN;
53 case TEST_EXECUTE:
54 break;
55 }
56
57 codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
58 if (!codec) {
59 ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
60 return AST_TEST_FAIL;
61 }
62
63 format = ast_format_create_named("ulaw@20_1", codec);
64 if (!format) {
65 ast_test_status_update(test, "Could not create format using built-in codec\n");
66 return AST_TEST_FAIL;
67 }
68
69 if (ast_format_cache_set(format)) {
70 ast_test_status_update(test, "Could not add just created format to cache\n");
71 return AST_TEST_FAIL;
72 }
73
74 return AST_TEST_PASS;
75}
76
77AST_TEST_DEFINE(format_cache_set_duplicate)
78{
79 RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
80 RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
81
82 switch (cmd) {
83 case TEST_INIT:
84 info->name = "format_cache_set_duplicate";
85 info->category = "/main/format_cache/";
86 info->summary = "format cache add unit test";
87 info->description =
88 "Test that adding of a cached format multiple times succeeds";
89 return AST_TEST_NOT_RUN;
90 case TEST_EXECUTE:
91 break;
92 }
93
94 codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
95 if (!codec) {
96 ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
97 return AST_TEST_FAIL;
98 }
99
100 format = ast_format_create_named("ulaw@20_2", codec);
101 if (!format) {
102 ast_test_status_update(test, "Could not create format using built-in codec\n");
103 return AST_TEST_FAIL;
104 }
105
106 if (ast_format_cache_set(format)) {
107 ast_test_status_update(test, "Could not add just created format to cache\n");
108 return AST_TEST_FAIL;
109 }
110
111 if (ast_format_cache_set(format)) {
112 ast_test_status_update(test, "Failed to update cached format\n");
113 return AST_TEST_FAIL;
114 }
115
116 return AST_TEST_PASS;
117}
118
119AST_TEST_DEFINE(format_cache_set_null)
120{
121 RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
122 RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
123
124 switch (cmd) {
125 case TEST_INIT:
126 info->name = "format_cache_set_null";
127 info->category = "/main/format_cache/";
128 info->summary = "format cache add unit test";
129 info->description =
130 "Test that adding a NULL or empty format to the cache does not succeed";
131 return AST_TEST_NOT_RUN;
132 case TEST_EXECUTE:
133 break;
134 }
135
136 codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
137 if (!codec) {
138 ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
139 return AST_TEST_FAIL;
140 }
141
142 format = ast_format_create_named("", codec);
143 if (!format) {
144 ast_test_status_update(test, "Could not create format using built-in codec\n");
145 return AST_TEST_FAIL;
146 }
147
148 if (!ast_format_cache_set(format)) {
149 ast_test_status_update(test, "Successfully cached a format with an empty name\n");
150 return AST_TEST_FAIL;
151 }
152
153 return AST_TEST_PASS;
154}
155
156AST_TEST_DEFINE(format_cache_get)
157{
158 RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
159 RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
160 RAII_VAR(struct ast_format *, cached, NULL, ao2_cleanup);
161
162 switch (cmd) {
163 case TEST_INIT:
164 info->name = "format_cache_get";
165 info->category = "/main/format_cache/";
166 info->summary = "format cache get unit test";
167 info->description =
168 "Test that getting of a cached format succeeds";
169 return AST_TEST_NOT_RUN;
170 case TEST_EXECUTE:
171 break;
172 }
173
174 codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
175 if (!codec) {
176 ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
177 return AST_TEST_FAIL;
178 }
179
180 format = ast_format_create_named("ulaw@20", codec);
181 if (!format) {
182 ast_test_status_update(test, "Could not create format using built-in codec\n");
183 return AST_TEST_FAIL;
184 }
185
186 if (ast_format_cache_set(format)) {
187 ast_test_status_update(test, "Could not add just created format to cache\n");
188 return AST_TEST_FAIL;
189 }
190
191 cached = ast_format_cache_get("ulaw@20");
192 if (!cached) {
193 ast_test_status_update(test, "Failed to retrieve a format we just cached\n");
194 return AST_TEST_FAIL;
195 } else if (cached != format) {
196 ast_test_status_update(test, "Returned cached format does not match format we just added\n");
197 return AST_TEST_FAIL;
198 }
199
200 return AST_TEST_PASS;
201}
202
203AST_TEST_DEFINE(format_cache_get_nonexistent)
204{
205 RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
206 RAII_VAR(struct ast_format *, format, NULL, ao2_cleanup);
207 RAII_VAR(struct ast_format *, cached, NULL, ao2_cleanup);
208
209 switch (cmd) {
210 case TEST_INIT:
211 info->name = "format_cache_get_nonxistent";
212 info->category = "/main/format_cache/";
213 info->summary = "format cache get unit test";
214 info->description =
215 "Test that getting of a non-existent cached format does not succeed";
216 return AST_TEST_NOT_RUN;
217 case TEST_EXECUTE:
218 break;
219 }
220
221 codec = ast_codec_get("ulaw", AST_MEDIA_TYPE_AUDIO, 8000);
222 if (!codec) {
223 ast_test_status_update(test, "Could not retrieve built-in ulaw codec\n");
224 return AST_TEST_FAIL;
225 }
226
227 format = ast_format_create_named("ulaw@40", codec);
228 if (!format) {
229 ast_test_status_update(test, "Could not create format using built-in codec\n");
230 return AST_TEST_FAIL;
231 }
232
233 if (ast_format_cache_set(format)) {
234 ast_test_status_update(test, "Could not add just created format to cache\n");
235 return AST_TEST_FAIL;
236 }
237
238 cached = ast_format_cache_get("ulaw@60");
239 if (cached) {
240 ast_test_status_update(test, "Retrieved a cached format when one should not have existed\n");
241 return AST_TEST_FAIL;
242 }
243
244 cached = ast_format_cache_get("");
245 if (cached) {
246 ast_test_status_update(test, "Retrieved a cached format when we provided an empty name\n");
247 return AST_TEST_FAIL;
248 }
249
250 cached = ast_format_cache_get(NULL);
251 if (cached) {
252 ast_test_status_update(test, "Retrieved a cached format when we provided a NULL name\n");
253 return AST_TEST_FAIL;
254 }
255
256 return AST_TEST_PASS;
257}
258
259static int unload_module(void)
260{
261 AST_TEST_UNREGISTER(format_cache_set);
262 AST_TEST_UNREGISTER(format_cache_set_duplicate);
263 AST_TEST_UNREGISTER(format_cache_set_null);
264 AST_TEST_UNREGISTER(format_cache_get);
265 AST_TEST_UNREGISTER(format_cache_get_nonexistent);
266 return 0;
267}
268
269static int load_module(void)
270{
271 AST_TEST_REGISTER(format_cache_set);
272 AST_TEST_REGISTER(format_cache_set_duplicate);
273 AST_TEST_REGISTER(format_cache_set_null);
274 AST_TEST_REGISTER(format_cache_get);
275 AST_TEST_REGISTER(format_cache_get_nonexistent);
277}
278
279AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Format cache API test module");
Asterisk main include file. File version handling, generic pbx functions.
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
Codec API.
@ AST_MEDIA_TYPE_AUDIO
Definition: codec.h:32
struct ast_codec * ast_codec_get(const char *name, enum ast_media_type type, unsigned int sample_rate)
Retrieve a codec given a name, type, and sample rate.
Definition: codec.c:327
Media Format API.
struct ast_format * ast_format_create_named(const char *format_name, struct ast_codec *codec)
Create a new media format with a specific name.
Definition: format.c:157
Media Format Cache API.
int ast_format_cache_set(struct ast_format *format)
Set a named format cache entry.
Definition: format_cache.c:474
#define ast_format_cache_get(name)
Retrieve a named format from the cache.
Definition: format_cache.h:278
Asterisk module definitions.
#define AST_MODULE_INFO_STANDARD(keystr, desc)
Definition: module.h:567
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:46
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
def info(msg)
#define NULL
Definition: resample.c:96
Represents a media codec within Asterisk.
Definition: codec.h:42
Definition of a media format.
Definition: format.c:43
Test Framework API.
@ TEST_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define AST_TEST_REGISTER(cb)
Definition: test.h:127
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_FAIL
Definition: test.h:196
@ AST_TEST_NOT_RUN
Definition: test.h:194
static int load_module(void)
static int unload_module(void)
AST_TEST_DEFINE(format_cache_set)
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941