Asterisk - The Open Source Telephony Project GIT-master-7e7a603
Functions | Variables
test_format_cache.c File Reference

Format Cache API Unit Tests. More...

#include "asterisk.h"
#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/codec.h"
#include "asterisk/format.h"
#include "asterisk/format_cache.h"
Include dependency graph for test_format_cache.c:

Go to the source code of this file.

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (void)
 
 AST_TEST_DEFINE (format_cache_get)
 
 AST_TEST_DEFINE (format_cache_get_nonexistent)
 
 AST_TEST_DEFINE (format_cache_set)
 
 AST_TEST_DEFINE (format_cache_set_duplicate)
 
 AST_TEST_DEFINE (format_cache_set_null)
 
static int load_module (void)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Format cache API test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
 
static const struct ast_module_infoast_module_info = &__mod_info
 

Detailed Description

Format Cache API Unit Tests.

Author
Joshua Colp jcolp.nosp@m.@dig.nosp@m.ium.c.nosp@m.om

Definition in file test_format_cache.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 279 of file test_format_cache.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 279 of file test_format_cache.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module * AST_MODULE_SELF_SYM ( void  )

Definition at line 279 of file test_format_cache.c.

◆ AST_TEST_DEFINE() [1/5]

AST_TEST_DEFINE ( format_cache_get  )

Definition at line 156 of file test_format_cache.c.

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}
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
@ 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
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
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
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_INIT
Definition: test.h:200
@ TEST_EXECUTE
Definition: test.h:201
#define ast_test_status_update(a, b, c...)
Definition: test.h:129
@ AST_TEST_PASS
Definition: test.h:195
@ AST_TEST_FAIL
Definition: test.h:196
@ AST_TEST_NOT_RUN
Definition: test.h:194
#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

References ao2_cleanup, ast_codec_get(), ast_format_cache_get, ast_format_cache_set(), ast_format_create_named(), AST_MEDIA_TYPE_AUDIO, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, sip_to_pjsip::info(), NULL, RAII_VAR, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [2/5]

AST_TEST_DEFINE ( format_cache_get_nonexistent  )

Definition at line 203 of file test_format_cache.c.

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}

References ao2_cleanup, ast_codec_get(), ast_format_cache_get, ast_format_cache_set(), ast_format_create_named(), AST_MEDIA_TYPE_AUDIO, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, sip_to_pjsip::info(), NULL, RAII_VAR, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [3/5]

AST_TEST_DEFINE ( format_cache_set  )

Definition at line 40 of file test_format_cache.c.

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}

References ao2_cleanup, ast_codec_get(), ast_format_cache_set(), ast_format_create_named(), AST_MEDIA_TYPE_AUDIO, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, sip_to_pjsip::info(), NULL, RAII_VAR, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [4/5]

AST_TEST_DEFINE ( format_cache_set_duplicate  )

Definition at line 77 of file test_format_cache.c.

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}

References ao2_cleanup, ast_codec_get(), ast_format_cache_set(), ast_format_create_named(), AST_MEDIA_TYPE_AUDIO, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, sip_to_pjsip::info(), NULL, RAII_VAR, TEST_EXECUTE, and TEST_INIT.

◆ AST_TEST_DEFINE() [5/5]

AST_TEST_DEFINE ( format_cache_set_null  )

Definition at line 119 of file test_format_cache.c.

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}

References ao2_cleanup, ast_codec_get(), ast_format_cache_set(), ast_format_create_named(), AST_MEDIA_TYPE_AUDIO, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, sip_to_pjsip::info(), NULL, RAII_VAR, TEST_EXECUTE, and TEST_INIT.

◆ load_module()

static int load_module ( void  )
static

Definition at line 269 of file test_format_cache.c.

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}
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
#define AST_TEST_REGISTER(cb)
Definition: test.h:127

References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 259 of file test_format_cache.c.

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}
#define AST_TEST_UNREGISTER(cb)
Definition: test.h:128

References AST_TEST_UNREGISTER.

Variable Documentation

◆ __mod_info

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "Format cache API test module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_DEFAULT, .support_level = AST_MODULE_SUPPORT_CORE, }
static

Definition at line 279 of file test_format_cache.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 279 of file test_format_cache.c.