summaryrefslogtreecommitdiff
path: root/tests/test_channel.c
blob: 854aff782dd0ef7be98eb3ac7ff2a2a425c2993c (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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2017, 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 Channel unit tests
 *
 * \author Joshua Colp <jcolp@digium.com>
 *
 */

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

#include "asterisk.h"

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

AST_TEST_DEFINE(set_fd_grow)
{
	struct ast_channel *mock_channel;
	enum ast_test_result_state res = AST_TEST_PASS;
	int pos;

	switch (cmd) {
	case TEST_INIT:
		info->name = "set_fd_grow";
		info->category = "/main/channel/";
		info->summary = "channel setting file descriptor with growth test";
		info->description =
			"Test that setting a file descriptor on a high position of a channel results in -1 set on any new positions";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	mock_channel = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, "TestChannel");
	ast_test_validate_cleanup(test, mock_channel, res, done);

	ast_channel_set_fd(mock_channel, AST_EXTENDED_FDS + 10, 1);
	ast_test_validate_cleanup(test, ast_channel_fd_count(mock_channel) == AST_EXTENDED_FDS + 11, res, done);

	for (pos = AST_EXTENDED_FDS; (pos < AST_EXTENDED_FDS + 10); pos++) {
		ast_test_validate_cleanup(test, ast_channel_fd(mock_channel, pos) == -1, res, done);
	}

done:
	ast_hangup(mock_channel);

	return res;
}

AST_TEST_DEFINE(add_fd)
{
	struct ast_channel *mock_channel;
	enum ast_test_result_state res = AST_TEST_PASS;
	int pos;

	switch (cmd) {
	case TEST_INIT:
		info->name = "add_fd";
		info->category = "/main/channel/";
		info->summary = "channel adding file descriptor test";
		info->description =
			"Test that adding a file descriptor to a channel places it in the expected position";
		return AST_TEST_NOT_RUN;
	case TEST_EXECUTE:
		break;
	}

	mock_channel = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, "TestChannel");
	ast_test_validate_cleanup(test, mock_channel, res, done);

	pos = ast_channel_fd_add(mock_channel, 1);
	ast_test_validate_cleanup(test, pos == AST_EXTENDED_FDS, res, done);

	ast_channel_set_fd(mock_channel, pos, -1);
	ast_test_validate_cleanup(test, ast_channel_fd(mock_channel, pos) == -1, res, done);

done:
	ast_hangup(mock_channel);

	return res;
}

static int unload_module(void)
{
	AST_TEST_UNREGISTER(set_fd_grow);
	AST_TEST_UNREGISTER(add_fd);
	return 0;
}

static int load_module(void)
{
	AST_TEST_REGISTER(set_fd_grow);
	AST_TEST_REGISTER(add_fd);
	return AST_MODULE_LOAD_SUCCESS;
}

AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Channel Unit Tests");