]> sigrok.org Git - libsigrokdecode.git/blame - tests/check_session.c
testsuite: Add some decoder instance related tests.
[libsigrokdecode.git] / tests / check_session.c
CommitLineData
269b442d
UH
1/*
2 * This file is part of the libsigrokdecode project.
3 *
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "../libsigrokdecode.h" /* First, to avoid compiler warning. */
22#include "../libsigrokdecode-internal.h"
23#include <stdlib.h>
24#include <check.h>
25
26static void setup(void)
27{
28 /* Silence libsigrokdecode while the unit tests run. */
29 srd_log_loglevel_set(SRD_LOG_NONE);
30}
31
32static void teardown(void)
33{
34}
35
36/*
37 * Check whether srd_session_new() works.
38 * If it returns != SRD_OK (or segfaults) this test will fail.
39 */
40START_TEST(test_session_new)
41{
42 int ret;
43 struct srd_session *sess;
44
45 srd_init(NULL);
46 ret = srd_session_new(&sess);
47 fail_unless(ret == SRD_OK, "srd_session_new() failed: %d.", ret);
48 srd_exit();
49}
50END_TEST
51
52/*
53 * Check whether srd_session_new() fails for bogus parameters.
54 * If it returns SRD_OK (or segfaults) this test will fail.
55 */
56START_TEST(test_session_new_bogus)
57{
58 int ret;
59
60 srd_init(NULL);
61 ret = srd_session_new(NULL);
62 fail_unless(ret != SRD_OK, "srd_session_new(NULL) worked.");
63 srd_exit();
64}
65END_TEST
66
67/*
68 * Check whether multiple srd_session_new() calls work.
69 * If any call returns != SRD_OK (or segfaults) this test will fail.
70 */
71START_TEST(test_session_new_multiple)
72{
73 int ret;
74 struct srd_session *sess1, *sess2, *sess3;
75
76 sess1 = sess2 = sess3 = NULL;
77
78 srd_init(NULL);
79
80 /* Multiple srd_session_new() calls must work. */
81 ret = srd_session_new(&sess1);
82 fail_unless(ret == SRD_OK, "srd_session_new() 1 failed: %d.", ret);
83 ret = srd_session_new(&sess2);
84 fail_unless(ret == SRD_OK, "srd_session_new() 2 failed: %d.", ret);
85 ret = srd_session_new(&sess3);
86 fail_unless(ret == SRD_OK, "srd_session_new() 3 failed: %d.", ret);
87
88 /* The returned session pointers must all be non-NULL. */
89 fail_unless(sess1 != NULL);
90 fail_unless(sess2 != NULL);
91 fail_unless(sess3 != NULL);
92
93 /* The returned session pointers must not be the same. */
94 fail_unless(sess1 != sess2);
95 fail_unless(sess1 != sess3);
96 fail_unless(sess2 != sess3);
97
98 /* Each session must have another ID than any other session. */
99 fail_unless(sess1->session_id != sess2->session_id);
100 fail_unless(sess1->session_id != sess3->session_id);
101 fail_unless(sess2->session_id != sess3->session_id);
102
103 /* Destroying any of the sessions must work. */
104 ret = srd_session_destroy(sess1);
105 fail_unless(ret == SRD_OK, "srd_session_destroy() 1 failed: %d.", ret);
106 ret = srd_session_destroy(sess2);
107 fail_unless(ret == SRD_OK, "srd_session_destroy() 2 failed: %d.", ret);
108 ret = srd_session_destroy(sess3);
109 fail_unless(ret == SRD_OK, "srd_session_destroy() 3 failed: %d.", ret);
110
111 srd_exit();
112}
113END_TEST
114
115/*
116 * Check whether srd_session_destroy() works.
117 * If it returns != SRD_OK (or segfaults) this test will fail.
118 */
119START_TEST(test_session_destroy)
120{
121 int ret;
122 struct srd_session *sess;
123
124 srd_init(NULL);
125 srd_session_new(&sess);
126 ret = srd_session_destroy(sess);
127 fail_unless(ret == SRD_OK, "srd_session_destroy() failed: %d.", ret);
128 srd_exit();
129}
130END_TEST
131
132/*
133 * Check whether srd_session_destroy() fails for bogus sessions.
134 * If it returns SRD_OK (or segfaults) this test will fail.
135 */
136START_TEST(test_session_destroy_bogus)
137{
138 int ret;
139
140 srd_init(NULL);
141 ret = srd_session_destroy(NULL);
142 fail_unless(ret != SRD_OK, "srd_session_destroy() failed: %d.", ret);
143 srd_exit();
144}
145END_TEST
146
147static void conf_check_ok(struct srd_session *sess, int key, uint64_t x)
148{
149 int ret;
150
151 ret = srd_session_config_set(sess, key, g_variant_new_uint64(x));
152 fail_unless(ret == SRD_OK, "srd_session_config_set(%p, %d, %"
153 PRIu64 ") failed: %d.", sess, key, x, ret);
154}
155
156static void conf_check_fail(struct srd_session *sess, int key, uint64_t x)
157{
158 int ret;
159
160 ret = srd_session_config_set(sess, key, g_variant_new_uint64(x));
161 fail_unless(ret != SRD_OK, "srd_session_config_set(%p, %d, %"
162 PRIu64 ") worked.", sess, key, x);
163}
164
165static void conf_check_fail_null(struct srd_session *sess, int key)
166{
167 int ret;
168
169 ret = srd_session_config_set(sess, key, NULL);
170 fail_unless(ret != SRD_OK,
171 "srd_session_config_set(NULL) for key %d worked.", key);
172}
173
174static void conf_check_fail_str(struct srd_session *sess, int key, const char *s)
175{
176 int ret;
177
178 ret = srd_session_config_set(sess, key, g_variant_new_string(s));
179 fail_unless(ret != SRD_OK, "srd_session_config_set for key %d "
180 "failed: %d.", key, ret);
181}
182
183/*
184 * Check whether srd_session_config_set() works.
185 * If it returns != SRD_OK (or segfaults) this test will fail.
186 */
187START_TEST(test_session_config_set)
188{
189 uint64_t i;
190 struct srd_session *sess;
191
192 srd_init(NULL);
193 srd_session_new(&sess);
194 /* Try a bunch of values. */
195 for (i = 0; i < 1000; i++) {
196 conf_check_ok(sess, SRD_CONF_NUM_PROBES, i);
197 conf_check_ok(sess, SRD_CONF_UNITSIZE, i);
198 conf_check_ok(sess, SRD_CONF_SAMPLERATE, i);
199 }
200 /* Try the max. possible value. */
201 conf_check_ok(sess, SRD_CONF_NUM_PROBES, 18446744073709551615ULL);
202 conf_check_ok(sess, SRD_CONF_UNITSIZE, 18446744073709551615ULL);
203 conf_check_ok(sess, SRD_CONF_SAMPLERATE, 18446744073709551615ULL);
204 srd_session_destroy(sess);
205 srd_exit();
206}
207END_TEST
208
209/*
210 * Check whether srd_session_config_set() fails with invalid input.
211 * If it returns SRD_OK (or segfaults) this test will fail.
212 */
213START_TEST(test_session_config_set_bogus)
214{
215 struct srd_session *sess;
216
217 srd_init(NULL);
218 srd_session_new(&sess);
219
220 /* Incorrect gvariant type (currently only uint64 is used). */
221 conf_check_fail_str(sess, SRD_CONF_NUM_PROBES, "");
222 conf_check_fail_str(sess, SRD_CONF_UNITSIZE, "");
223 conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "");
224 conf_check_fail_str(sess, SRD_CONF_NUM_PROBES, "Foo");
225 conf_check_fail_str(sess, SRD_CONF_UNITSIZE, "Foo");
226 conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo");
227
228 /* NULL data pointer. */
229 conf_check_fail_null(sess, SRD_CONF_NUM_PROBES);
230 conf_check_fail_null(sess, SRD_CONF_UNITSIZE);
231 conf_check_fail_null(sess, SRD_CONF_SAMPLERATE);
232
233 /* NULL session. */
234 conf_check_fail(NULL, SRD_CONF_NUM_PROBES, 0);
235 conf_check_fail(NULL, SRD_CONF_UNITSIZE, 0);
236 conf_check_fail(NULL, SRD_CONF_SAMPLERATE, 0);
237
238 /* Invalid keys. */
239 conf_check_fail(sess, -1, 0);
240 conf_check_fail(sess, 9, 0);
241 conf_check_fail(sess, 123, 0);
242
243 srd_session_destroy(sess);
244 srd_exit();
245}
246END_TEST
247
248Suite *suite_session(void)
249{
250 Suite *s;
251 TCase *tc;
252
253 s = suite_create("session");
254
255 tc = tcase_create("new_destroy");
256 tcase_add_checked_fixture(tc, setup, teardown);
257 tcase_add_test(tc, test_session_new);
258 tcase_add_test(tc, test_session_new_bogus);
259 tcase_add_test(tc, test_session_new_multiple);
260 tcase_add_test(tc, test_session_destroy);
261 tcase_add_test(tc, test_session_destroy_bogus);
262 suite_add_tcase(s, tc);
263
264 tc = tcase_create("config");
265 tcase_add_checked_fixture(tc, setup, teardown);
266 tcase_add_test(tc, test_session_config_set);
267 tcase_add_test(tc, test_session_config_set_bogus);
268 suite_add_tcase(s, tc);
269
270 return s;
271}