summaryrefslogtreecommitdiff
path: root/tests/session.c
blob: 431ab5ec32bd3c4bf9753f8edfd8a7c66b675b93 (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
/*
 * This file is part of the libsigrokdecode project.
 *
 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <config.h>
#include "../libsigrokdecode-internal.h" /* First, to avoid compiler warning. */
#include "../libsigrokdecode.h"
#include <stdint.h>
#include <stdlib.h>
#include <check.h>
#include "lib.h"

/*
 * Check whether srd_session_new() works.
 * If it returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_session_new)
{
	int ret;
	struct srd_session *sess;

	srd_init(NULL);
	ret = srd_session_new(&sess);
	fail_unless(ret == SRD_OK, "srd_session_new() failed: %d.", ret);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_session_new() fails for bogus parameters.
 * If it returns SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_session_new_bogus)
{
	int ret;

	srd_init(NULL);
	ret = srd_session_new(NULL);
	fail_unless(ret != SRD_OK, "srd_session_new(NULL) worked.");
	srd_exit();
}
END_TEST

/*
 * Check whether multiple srd_session_new() calls work.
 * If any call returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_session_new_multiple)
{
	int ret;
	struct srd_session *sess1, *sess2, *sess3;

	sess1 = sess2 = sess3 = NULL;

	srd_init(NULL);

	/* Multiple srd_session_new() calls must work. */
	ret = srd_session_new(&sess1);
	fail_unless(ret == SRD_OK, "srd_session_new() 1 failed: %d.", ret);
	ret = srd_session_new(&sess2);
	fail_unless(ret == SRD_OK, "srd_session_new() 2 failed: %d.", ret);
	ret = srd_session_new(&sess3);
	fail_unless(ret == SRD_OK, "srd_session_new() 3 failed: %d.", ret);

	/* The returned session pointers must all be non-NULL. */
	fail_unless(sess1 != NULL);
	fail_unless(sess2 != NULL);
	fail_unless(sess3 != NULL);

	/* The returned session pointers must not be the same. */
	fail_unless(sess1 != sess2);
	fail_unless(sess1 != sess3);
	fail_unless(sess2 != sess3);

	/* Each session must have another ID than any other session. */
	fail_unless(sess1->session_id != sess2->session_id);
	fail_unless(sess1->session_id != sess3->session_id);
	fail_unless(sess2->session_id != sess3->session_id);

	/* Destroying any of the sessions must work. */
	ret = srd_session_destroy(sess1);
	fail_unless(ret == SRD_OK, "srd_session_destroy() 1 failed: %d.", ret);
	ret = srd_session_destroy(sess2);
	fail_unless(ret == SRD_OK, "srd_session_destroy() 2 failed: %d.", ret);
	ret = srd_session_destroy(sess3);
	fail_unless(ret == SRD_OK, "srd_session_destroy() 3 failed: %d.", ret);

	srd_exit();
}
END_TEST

/*
 * Check whether srd_session_destroy() works.
 * If it returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_session_destroy)
{
	int ret;
	struct srd_session *sess;

	srd_init(NULL);
	srd_session_new(&sess);
	ret = srd_session_destroy(sess);
	fail_unless(ret == SRD_OK, "srd_session_destroy() failed: %d.", ret);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_session_destroy() fails for bogus sessions.
 * If it returns SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_session_destroy_bogus)
{
	int ret;

	srd_init(NULL);
	ret = srd_session_destroy(NULL);
	fail_unless(ret != SRD_OK, "srd_session_destroy() failed: %d.", ret);
	srd_exit();
}
END_TEST

static void conf_check_ok(struct srd_session *sess, int key, uint64_t x)
{
	int ret;

	ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
	fail_unless(ret == SRD_OK, "srd_session_metadata_set(%p, %d, %"
		PRIu64 ") failed: %d.", sess, key, x, ret);
}

static void conf_check_fail(struct srd_session *sess, int key, uint64_t x)
{
	int ret;

	ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
	fail_unless(ret != SRD_OK, "srd_session_metadata_set(%p, %d, %"
		PRIu64 ") worked.", sess, key, x);
}

static void conf_check_fail_null(struct srd_session *sess, int key)
{
	int ret;

	ret = srd_session_metadata_set(sess, key, NULL);
	fail_unless(ret != SRD_OK,
		"srd_session_metadata_set(NULL) for key %d worked.", key);
}

static void conf_check_fail_str(struct srd_session *sess, int key, const char *s)
{
	int ret;

	ret = srd_session_metadata_set(sess, key, g_variant_new_string(s));
	fail_unless(ret != SRD_OK, "srd_session_metadata_set() for key %d "
		"failed: %d.", key, ret);
}

/*
 * Check whether srd_session_metadata_set() works.
 * If it returns != SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_session_metadata_set)
{
	uint64_t i;
	struct srd_session *sess;

	srd_init(NULL);
	srd_session_new(&sess);
	/* Try a bunch of values. */
	for (i = 0; i < 1000; i++)
		conf_check_ok(sess, SRD_CONF_SAMPLERATE, i);
	/* Try the max. possible value. */
	conf_check_ok(sess, SRD_CONF_SAMPLERATE, UINT64_MAX);
	srd_session_destroy(sess);
	srd_exit();
}
END_TEST

/*
 * Check whether srd_session_metadata_set() fails with invalid input.
 * If it returns SRD_OK (or segfaults) this test will fail.
 */
START_TEST(test_session_metadata_set_bogus)
{
	struct srd_session *sess;

	srd_init(NULL);
	srd_session_new(&sess);

	/* Incorrect GVariant type (currently only uint64 is used). */
	conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "");
	conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo");

	/* NULL data pointer. */
	conf_check_fail_null(sess, SRD_CONF_SAMPLERATE);

	/* NULL session. */
	conf_check_fail(NULL, SRD_CONF_SAMPLERATE, 0);

	/* Invalid keys. */
	conf_check_fail(sess, -1, 0);
	conf_check_fail(sess, 9, 0);
	conf_check_fail(sess, 123, 0);

	srd_session_destroy(sess);
	srd_exit();
}
END_TEST

Suite *suite_session(void)
{
	Suite *s;
	TCase *tc;

	s = suite_create("session");

	tc = tcase_create("new_destroy");
	tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
	tcase_add_test(tc, test_session_new);
	tcase_add_test(tc, test_session_new_bogus);
	tcase_add_test(tc, test_session_new_multiple);
	tcase_add_test(tc, test_session_destroy);
	tcase_add_test(tc, test_session_destroy_bogus);
	suite_add_tcase(s, tc);

	tc = tcase_create("config");
	tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
	tcase_add_test(tc, test_session_metadata_set);
	tcase_add_test(tc, test_session_metadata_set_bogus);
	suite_add_tcase(s, tc);

	return s;
}