Debian Codegraph @

tests/test_core_codec.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2014, Digium, Inc.
5 *
6 * Joshua Colp <[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 /*!
20 * \file
21 * \brief Core Codec API Unit Tests
22 *
23 * \author Joshua Colp <[email protected]>
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
38 static struct ast_codec known_unknown = {
39 .name = "unit_test",
40 .description = "Unit test codec",
41 .type = AST_MEDIA_TYPE_AUDIO,
42 .sample_rate = 8000,
43 .minimum_ms = 10,
44 .maximum_ms = 150,
45 .default_ms = 20,
46 };
47
48 static struct ast_codec doubly = {
49 .name = "unit_test_double",
50 .description = "Unit test codec",
51 .type = AST_MEDIA_TYPE_AUDIO,
52 .sample_rate = 8000,
53 .minimum_ms = 10,
54 .maximum_ms = 150,
55 .default_ms = 20,
56 };
57
58 static struct ast_codec unknown = {
59 .name = "unit_test_unknown",
60 .description = "Unit test codec",
61 .type = AST_MEDIA_TYPE_UNKNOWN,
62 .sample_rate = 8000,
63 .minimum_ms = 10,
64 .maximum_ms = 150,
65 .default_ms = 20,
66 };
67
68 static struct ast_codec audio_without_rate = {
69 .name = "unit_test_audio_without_rate",
70 .description = "Unit test codec",
71 .type = AST_MEDIA_TYPE_AUDIO,
72 .minimum_ms = 10,
73 .maximum_ms = 150,
74 .default_ms = 20,
75 };
76
77 static struct ast_codec audio_get = {
78 .name = "unit_test_audio_get",
79 .description = "Unit test codec",
80 .type = AST_MEDIA_TYPE_AUDIO,
81 .sample_rate = 8000,
82 .minimum_ms = 10,
83 .maximum_ms = 150,
84 .default_ms = 20,
85 };
86
87 static struct ast_codec audio_get_unknown = {
88 .name = "unit_test_audio_get_unknown",
89 .description = "Unit test codec",
90 .type = AST_MEDIA_TYPE_AUDIO,
91 .sample_rate = 8000,
92 .minimum_ms = 10,
93 .maximum_ms = 150,
94 .default_ms = 20,
95 };
96
97 static struct ast_codec audio_get_id = {
98 .name = "unit_test_audio_get_id",
99 .description = "Unit test codec",
100 .type = AST_MEDIA_TYPE_AUDIO,
101 .sample_rate = 8000,
102 .minimum_ms = 10,
103 .maximum_ms = 150,
104 .default_ms = 20,
105 };
106
107 AST_TEST_DEFINE(codec_register)
108 {
109 switch (cmd) {
110 case TEST_INIT:
111 info->name = "codec_register";
112 info->category = "/main/core_codec/";
113 info->summary = "codec registration unit test";
114 info->description =
115 "Test registration of a core codec that is known to be unknown";
116 return AST_TEST_NOT_RUN;
117 case TEST_EXECUTE:
118 break;
119 }
120
121 if (ast_codec_register(&known_unknown)) {
122 ast_test_status_update(test, "Unsuccessfully registered a codec that is known to be unknown\n");
123 return AST_TEST_FAIL;
124 }
125
126 return AST_TEST_PASS;
127 }
128
129 AST_TEST_DEFINE(codec_register_twice)
130 {
131 switch (cmd) {
132 case TEST_INIT:
133 info->name = "codec_register_twice";
134 info->category = "/main/core_codec/";
135 info->summary = "codec registration unit test";
136 info->description =
137 "Test double registration of a core codec to confirm it fails";
138 return AST_TEST_NOT_RUN;
139 case TEST_EXECUTE:
140 break;
141 }
142
143 if (ast_codec_register(&doubly)) {
144 ast_test_status_update(test, "Unsuccessfully registered a codec that is known to be unknown\n");
145 return AST_TEST_FAIL;
146 }
147
148 if (!ast_codec_register(&doubly)) {
149 ast_test_status_update(test, "Successfully registered a codec twice\n");
150 return AST_TEST_FAIL;
151 }
152
153 return AST_TEST_PASS;
154 }
155
156 AST_TEST_DEFINE(codec_register_unknown)
157 {
158 switch (cmd) {
159 case TEST_INIT:
160 info->name = "codec_register_unknown";
161 info->category = "/main/core_codec/";
162 info->summary = "codec registration unit test";
163 info->description =
164 "Test that registration of an unknown codec type fails";
165 return AST_TEST_NOT_RUN;
166 case TEST_EXECUTE:
167 break;
168 }
169
170 if (!ast_codec_register(&unknown)) {
171 ast_test_status_update(test, "Successfully registered a codec with an unknown media type\n");
172 return AST_TEST_FAIL;
173 }
174
175 return AST_TEST_PASS;
176 }
177
178 AST_TEST_DEFINE(codec_register_audio_no_sample_rate)
179 {
180 switch (cmd) {
181 case TEST_INIT:
182 info->name = "codec_register_audio_no_sample_rate";
183 info->category = "/main/core_codec/";
184 info->summary = "codec registration unit test";
185 info->description =
186 "Test that registration of an audio codec without sample rate fails";
187 return AST_TEST_NOT_RUN;
188 case TEST_EXECUTE:
189 break;
190 }
191
192 if (!ast_codec_register(&audio_without_rate)) {
193 ast_test_status_update(test, "Successfully registered an audio codec without a sample rate\n");
194 return AST_TEST_FAIL;
195 }
196
197 return AST_TEST_PASS;
198 }
199
200 AST_TEST_DEFINE(codec_get)
201 {
202 RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
203
204 switch (cmd) {
205 case TEST_INIT:
206 info->name = "codec_get";
207 info->category = "/main/core_codec/";
208 info->summary = "codec get unit test";
209 info->description =
210 "Test that getting of a known codec succeeds";
211 return AST_TEST_NOT_RUN;
212 case TEST_EXECUTE:
213 break;
214 }
215
216 if (ast_codec_register(&audio_get)) {
217 ast_test_status_update(test, "Unsucessfully registered a codec for getting\n");
218 return AST_TEST_FAIL;
219 }
220
221 codec = ast_codec_get("unit_test_audio_get", AST_MEDIA_TYPE_AUDIO, 8000);
222 if (!codec) {
223 ast_test_status_update(test, "Unsuccessfully retrieved a codec we just registered\n");
224 return AST_TEST_FAIL;
225 } else if (strcmp(codec->name, audio_get.name)) {
226 ast_test_status_update(test, "Name of retrieved codec does not match registered codec\n");
227 return AST_TEST_FAIL;
228 } else if (codec->type != audio_get.type) {
229 ast_test_status_update(test, "Type of retrieved codec does not match registered codec\n");
230 return AST_TEST_FAIL;
231 } else if (codec->sample_rate != audio_get.sample_rate) {
232 ast_test_status_update(test, "Sample rate of retrieved codec does not match registered codec\n");
233 return AST_TEST_FAIL;
234 }
235
236 return AST_TEST_PASS;
237 }
238
239 AST_TEST_DEFINE(codec_get_unregistered)
240 {
241 RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
242
243 switch (cmd) {
244 case TEST_INIT:
245 info->name = "codec_get_unregistered";
246 info->category = "/main/core_codec/";
247 info->summary = "codec get unit test";
248 info->description =
249 "Test that getting of a codec that is not registered fails";
250 return AST_TEST_NOT_RUN;
251 case TEST_EXECUTE:
252 break;
253 }
254
255 codec = ast_codec_get("goats", AST_MEDIA_TYPE_AUDIO, 8000);
256 if (codec) {
257 ast_test_status_update(test, "Successfully got a codec named '%s' when getting a codec named 'goats'\n",
258 codec->name);
259 return AST_TEST_FAIL;
260 }
261
262 return AST_TEST_PASS;
263 }
264
265 AST_TEST_DEFINE(codec_get_unknown)
266 {
267 RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
268
269 switch (cmd) {
270 case TEST_INIT:
271 info->name = "codec_get_unknown";
272 info->category = "/main/core_codec/";
273 info->summary = "codec get unit test";
274 info->description =
275 "Test that getting of a known codec using name and unknown type succeeds";
276 return AST_TEST_NOT_RUN;
277 case TEST_EXECUTE:
278 break;
279 }
280
281 if (ast_codec_register(&audio_get_unknown)) {
282 ast_test_status_update(test, "Unsucessfully registered a codec for getting\n");
283 return AST_TEST_FAIL;
284 }
285
286 codec = ast_codec_get("unit_test_audio_get_unknown", AST_MEDIA_TYPE_UNKNOWN, 8000);
287 if (!codec) {
288 ast_test_status_update(test, "Unsuccessfully retrieved a codec we just registered\n");
289 return AST_TEST_FAIL;
290 } else if (strcmp(codec->name, audio_get_unknown.name)) {
291 ast_test_status_update(test, "Name of retrieved codec does not match registered codec\n");
292 return AST_TEST_FAIL;
293 } else if (codec->type != audio_get_unknown.type) {
294 ast_test_status_update(test, "Type of retrieved codec does not match registered codec\n");
295 return AST_TEST_FAIL;
296 } else if (codec->sample_rate != audio_get_unknown.sample_rate) {
297 ast_test_status_update(test, "Sample rate of retrieved codec does not match registered codec\n");
298 return AST_TEST_FAIL;
299 }
300
301 return AST_TEST_PASS;
302 }
303
304 AST_TEST_DEFINE(codec_get_id)
305 {
306 RAII_VAR(struct ast_codec *, named, NULL, ao2_cleanup);
307 RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);
308
309 switch (cmd) {
310 case TEST_INIT:
311 info->name = "codec_get_unknown";
312 info->category = "/main/core_codec/";
313 info->summary = "codec get unit test";
314 info->description =
315 "Test that getting of a known codec using name and unknown type succeeds";
316 return AST_TEST_NOT_RUN;
317 case TEST_EXECUTE:
318 break;
319 }
320
321 if (ast_codec_register(&audio_get_id)) {
322 ast_test_status_update(test, "Unsucessfully registered a codec for getting\n");
323 return AST_TEST_FAIL;
324 }
325
326 named = ast_codec_get("unit_test_audio_get_id", AST_MEDIA_TYPE_AUDIO, 8000);
327 if (!named) {
328 ast_test_status_update(test, "Unsuccessfully retrieved a codec we just registered\n");
329 return AST_TEST_FAIL;
330 }
331
332 codec = ast_codec_get_by_id(named->id);
333 if (!codec) {
334 ast_test_status_update(test, "Unsuccessfully retrieved a codec using id of a named codec we just got\n");
335 return AST_TEST_FAIL;
336 }
337
338 return AST_TEST_PASS;
339 }
340
341 static int unload_module(void)
342 {
343 AST_TEST_UNREGISTER(codec_register);
344 AST_TEST_UNREGISTER(codec_register_twice);
345 AST_TEST_UNREGISTER(codec_register_unknown);
346 AST_TEST_UNREGISTER(codec_register_audio_no_sample_rate);
347 AST_TEST_UNREGISTER(codec_get);
348 AST_TEST_UNREGISTER(codec_get_unregistered);
349 AST_TEST_UNREGISTER(codec_get_unknown);
350 AST_TEST_UNREGISTER(codec_get_id);
351 return 0;
352 }
353
354 static int load_module(void)
355 {
356 AST_TEST_REGISTER(codec_register);
357 AST_TEST_REGISTER(codec_register_twice);
358 AST_TEST_REGISTER(codec_register_unknown);
359 AST_TEST_REGISTER(codec_register_audio_no_sample_rate);
360 AST_TEST_REGISTER(codec_get);
361 AST_TEST_REGISTER(codec_get_unregistered);
362 AST_TEST_REGISTER(codec_get_unknown);
363 AST_TEST_REGISTER(codec_get_id);
364 return AST_MODULE_LOAD_SUCCESS;
365 }
366
367 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Core codec API test module");