summaryrefslogtreecommitdiff
path: root/tests/test_core_codec.c
blob: 1da21cdc29610333baa9cdc6e41996c371f6ae2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2014, Digium, Inc.
 *
 * Joshua Colp <jcolp@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 Core Codec API Unit Tests
 *
 * \author Joshua Colp <jcolp@digium.com>
 *
 */

/*** MODULEINFO
	<depend>TEST_FRAMEWORK</depend>
	<support_level>core</support_level>
 ***/

#include "asterisk.h"

#include "asterisk/test.h"
#include "asterisk/module.h"
#include "asterisk/codec.h"

static struct ast_codec known_unknown = {
	.name = "unit_test",
	.description = "Unit test codec",
	.type = AST_MEDIA_TYPE_AUDIO,
	.sample_rate = 8000,
	.minimum_ms = 10,
	.maximum_ms = 150,
	.default_ms = 20,
};

static struct ast_codec doubly = {
	.name = "unit_test_double",
	.description = "Unit test codec",
	.type = AST_MEDIA_TYPE_AUDIO,
	.sample_rate = 8000,
	.minimum_ms = 10,
	.maximum_ms = 150,
	.default_ms = 20,
};

static struct ast_codec unknown = {
	.name = "unit_test_unknown",
	.description = "Unit test codec",
	.type = AST_MEDIA_TYPE_UNKNOWN,
	.sample_rate = 8000,
	.minimum_ms = 10,
	.maximum_ms = 150,
	.default_ms = 20,
};

static struct ast_codec audio_without_rate = {
	.name = "unit_test_audio_without_rate",
	.description = "Unit test codec",
	.type = AST_MEDIA_TYPE_AUDIO,
	.minimum_ms = 10,
	.maximum_ms = 150,
	.default_ms = 20,
};

static struct ast_codec audio_get = {
	.name = "unit_test_audio_get",
	.description = "Unit test codec",
	.type = AST_MEDIA_TYPE_AUDIO,
	.sample_rate = 8000,
	.minimum_ms = 10,
	.maximum_ms = 150,
	.default_ms = 20,
};

static struct ast_codec audio_get_unknown = {
	.name = "unit_test_audio_get_unknown",
	.description = "Unit test codec",
	.type = AST_MEDIA_TYPE_AUDIO,
	.sample_rate = 8000,
	.minimum_ms = 10,
	.maximum_ms = 150,
	.default_ms = 20,
};

static struct ast_codec audio_get_id = {
	.name = "unit_test_audio_get_id",
	.description = "Unit test codec",
	.type = AST_MEDIA_TYPE_AUDIO,
	.sample_rate = 8000,
	.minimum_ms = 10,
	.maximum_ms = 150,
	.default_ms = 20,
};

AST_TEST_DEFINE(codec_register)
{
	switch (cmd) {
	case TEST_INIT:
		info->name = "codec_register";
		info->category = "/main/core_codec/";
		info->summary = "codec registration unit test";
		info->description =
			"Test registration of a core codec that is known to be unknown";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	if (ast_codec_register(&known_unknown)) {
		ast_test_status_update(test, "Unsuccessfully registered a codec that is known to be unknown\n");
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(codec_register_twice)
{
	switch (cmd) {
	case TEST_INIT:
		info->name = "codec_register_twice";
		info->category = "/main/core_codec/";
		info->summary = "codec registration unit test";
		info->description =
			"Test double registration of a core codec to confirm it fails";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	if (ast_codec_register(&doubly)) {
		ast_test_status_update(test, "Unsuccessfully registered a codec that is known to be unknown\n");
		return AST_TEST_FAIL;
	}

	if (!ast_codec_register(&doubly)) {
		ast_test_status_update(test, "Successfully registered a codec twice\n");
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(codec_register_unknown)
{
	switch (cmd) {
	case TEST_INIT:
		info->name = "codec_register_unknown";
		info->category = "/main/core_codec/";
		info->summary = "codec registration unit test";
		info->description =
			"Test that registration of an unknown codec type fails";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	if (!ast_codec_register(&unknown)) {
		ast_test_status_update(test, "Successfully registered a codec with an unknown media type\n");
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(codec_register_audio_no_sample_rate)
{
	switch (cmd) {
	case TEST_INIT:
		info->name = "codec_register_audio_no_sample_rate";
		info->category = "/main/core_codec/";
		info->summary = "codec registration unit test";
		info->description =
			"Test that registration of an audio codec without sample rate fails";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	if (!ast_codec_register(&audio_without_rate)) {
		ast_test_status_update(test, "Successfully registered an audio codec without a sample rate\n");
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(codec_get)
{
	RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);

	switch (cmd) {
	case TEST_INIT:
		info->name = "codec_get";
		info->category = "/main/core_codec/";
		info->summary = "codec get unit test";
		info->description =
			"Test that getting of a known codec succeeds";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	if (ast_codec_register(&audio_get)) {
		ast_test_status_update(test, "Unsucessfully registered a codec for getting\n");
		return AST_TEST_FAIL;
	}

	codec = ast_codec_get("unit_test_audio_get", AST_MEDIA_TYPE_AUDIO, 8000);
	if (!codec) {
		ast_test_status_update(test, "Unsuccessfully retrieved a codec we just registered\n");
		return AST_TEST_FAIL;
	} else if (strcmp(codec->name, audio_get.name)) {
		ast_test_status_update(test, "Name of retrieved codec does not match registered codec\n");
		return AST_TEST_FAIL;
	} else if (codec->type != audio_get.type) {
		ast_test_status_update(test, "Type of retrieved codec does not match registered codec\n");
		return AST_TEST_FAIL;
	} else if (codec->sample_rate != audio_get.sample_rate) {
		ast_test_status_update(test, "Sample rate of retrieved codec does not match registered codec\n");
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(codec_get_unregistered)
{
	RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);

	switch (cmd) {
	case TEST_INIT:
		info->name = "codec_get_unregistered";
		info->category = "/main/core_codec/";
		info->summary = "codec get unit test";
		info->description =
			"Test that getting of a codec that is not registered fails";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	codec = ast_codec_get("goats", AST_MEDIA_TYPE_AUDIO, 8000);
	if (codec) {
		ast_test_status_update(test, "Successfully got a codec named '%s' when getting a codec named 'goats'\n",
			codec->name);
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(codec_get_unknown)
{
	RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);

	switch (cmd) {
	case TEST_INIT:
		info->name = "codec_get_unknown";
		info->category = "/main/core_codec/";
		info->summary = "codec get unit test";
		info->description =
			"Test that getting of a known codec using name and unknown type succeeds";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	if (ast_codec_register(&audio_get_unknown)) {
		ast_test_status_update(test, "Unsucessfully registered a codec for getting\n");
		return AST_TEST_FAIL;
	}

	codec = ast_codec_get("unit_test_audio_get_unknown", AST_MEDIA_TYPE_UNKNOWN, 8000);
	if (!codec) {
		ast_test_status_update(test, "Unsuccessfully retrieved a codec we just registered\n");
		return AST_TEST_FAIL;
	} else if (strcmp(codec->name, audio_get_unknown.name)) {
		ast_test_status_update(test, "Name of retrieved codec does not match registered codec\n");
		return AST_TEST_FAIL;
	} else if (codec->type != audio_get_unknown.type) {
		ast_test_status_update(test, "Type of retrieved codec does not match registered codec\n");
		return AST_TEST_FAIL;
	} else if (codec->sample_rate != audio_get_unknown.sample_rate) {
		ast_test_status_update(test, "Sample rate of retrieved codec does not match registered codec\n");
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

AST_TEST_DEFINE(codec_get_id)
{
	RAII_VAR(struct ast_codec *, named, NULL, ao2_cleanup);
	RAII_VAR(struct ast_codec *, codec, NULL, ao2_cleanup);

	switch (cmd) {
	case TEST_INIT:
		info->name = "codec_get_unknown";
		info->category = "/main/core_codec/";
		info->summary = "codec get unit test";
		info->description =
			"Test that getting of a known codec using name and unknown type succeeds";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	if (ast_codec_register(&audio_get_id)) {
		ast_test_status_update(test, "Unsucessfully registered a codec for getting\n");
		return AST_TEST_FAIL;
	}

	named = ast_codec_get("unit_test_audio_get_id", AST_MEDIA_TYPE_AUDIO, 8000);
	if (!named) {
		ast_test_status_update(test, "Unsuccessfully retrieved a codec we just registered\n");
		return AST_TEST_FAIL;
	}

	codec = ast_codec_get_by_id(named->id);
	if (!codec) {
		ast_test_status_update(test, "Unsuccessfully retrieved a codec using id of a named codec we just got\n");
		return AST_TEST_FAIL;
	}

	return AST_TEST_PASS;
}

static int unload_module(void)
{
	AST_TEST_UNREGISTER(codec_register);
	AST_TEST_UNREGISTER(codec_register_twice);
	AST_TEST_UNREGISTER(codec_register_unknown);
	AST_TEST_UNREGISTER(codec_register_audio_no_sample_rate);
	AST_TEST_UNREGISTER(codec_get);
	AST_TEST_UNREGISTER(codec_get_unregistered);
	AST_TEST_UNREGISTER(codec_get_unknown);
	AST_TEST_UNREGISTER(codec_get_id);
	return 0;
}

static int load_module(void)
{
	AST_TEST_REGISTER(codec_register);
	AST_TEST_REGISTER(codec_register_twice);
	AST_TEST_REGISTER(codec_register_unknown);
	AST_TEST_REGISTER(codec_register_audio_no_sample_rate);
	AST_TEST_REGISTER(codec_get);
	AST_TEST_REGISTER(codec_get_unregistered);
	AST_TEST_REGISTER(codec_get_unknown);
	AST_TEST_REGISTER(codec_get_id);
	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Core codec API test module");