]> sigrok.org Git - libsigrokdecode.git/blame - tests/check_session.c
nrf24l01: Check for CS# pin and only throw exceptions once.
[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
83df730d
UH
21#include "../libsigrokdecode-internal.h" /* First, to avoid compiler warning. */
22#include "../libsigrokdecode.h"
e94fdd57 23#include <stdint.h>
269b442d
UH
24#include <stdlib.h>
25#include <check.h>
b9223384 26#include "lib.h"
269b442d 27
269b442d
UH
28/*
29 * Check whether srd_session_new() works.
30 * If it returns != SRD_OK (or segfaults) this test will fail.
31 */
32START_TEST(test_session_new)
33{
34 int ret;
35 struct srd_session *sess;
36
37 srd_init(NULL);
38 ret = srd_session_new(&sess);
39 fail_unless(ret == SRD_OK, "srd_session_new() failed: %d.", ret);
40 srd_exit();
41}
42END_TEST
43
44/*
45 * Check whether srd_session_new() fails for bogus parameters.
46 * If it returns SRD_OK (or segfaults) this test will fail.
47 */
48START_TEST(test_session_new_bogus)
49{
50 int ret;
51
52 srd_init(NULL);
53 ret = srd_session_new(NULL);
54 fail_unless(ret != SRD_OK, "srd_session_new(NULL) worked.");
55 srd_exit();
56}
57END_TEST
58
59/*
60 * Check whether multiple srd_session_new() calls work.
61 * If any call returns != SRD_OK (or segfaults) this test will fail.
62 */
63START_TEST(test_session_new_multiple)
64{
65 int ret;
66 struct srd_session *sess1, *sess2, *sess3;
67
68 sess1 = sess2 = sess3 = NULL;
69
70 srd_init(NULL);
71
72 /* Multiple srd_session_new() calls must work. */
73 ret = srd_session_new(&sess1);
74 fail_unless(ret == SRD_OK, "srd_session_new() 1 failed: %d.", ret);
75 ret = srd_session_new(&sess2);
76 fail_unless(ret == SRD_OK, "srd_session_new() 2 failed: %d.", ret);
77 ret = srd_session_new(&sess3);
78 fail_unless(ret == SRD_OK, "srd_session_new() 3 failed: %d.", ret);
79
80 /* The returned session pointers must all be non-NULL. */
81 fail_unless(sess1 != NULL);
82 fail_unless(sess2 != NULL);
83 fail_unless(sess3 != NULL);
84
85 /* The returned session pointers must not be the same. */
86 fail_unless(sess1 != sess2);
87 fail_unless(sess1 != sess3);
88 fail_unless(sess2 != sess3);
89
90 /* Each session must have another ID than any other session. */
91 fail_unless(sess1->session_id != sess2->session_id);
92 fail_unless(sess1->session_id != sess3->session_id);
93 fail_unless(sess2->session_id != sess3->session_id);
94
95 /* Destroying any of the sessions must work. */
96 ret = srd_session_destroy(sess1);
97 fail_unless(ret == SRD_OK, "srd_session_destroy() 1 failed: %d.", ret);
98 ret = srd_session_destroy(sess2);
99 fail_unless(ret == SRD_OK, "srd_session_destroy() 2 failed: %d.", ret);
100 ret = srd_session_destroy(sess3);
101 fail_unless(ret == SRD_OK, "srd_session_destroy() 3 failed: %d.", ret);
102
103 srd_exit();
104}
105END_TEST
106
107/*
108 * Check whether srd_session_destroy() works.
109 * If it returns != SRD_OK (or segfaults) this test will fail.
110 */
111START_TEST(test_session_destroy)
112{
113 int ret;
114 struct srd_session *sess;
115
116 srd_init(NULL);
117 srd_session_new(&sess);
118 ret = srd_session_destroy(sess);
119 fail_unless(ret == SRD_OK, "srd_session_destroy() failed: %d.", ret);
120 srd_exit();
121}
122END_TEST
123
124/*
125 * Check whether srd_session_destroy() fails for bogus sessions.
126 * If it returns SRD_OK (or segfaults) this test will fail.
127 */
128START_TEST(test_session_destroy_bogus)
129{
130 int ret;
131
132 srd_init(NULL);
133 ret = srd_session_destroy(NULL);
134 fail_unless(ret != SRD_OK, "srd_session_destroy() failed: %d.", ret);
135 srd_exit();
136}
137END_TEST
138
139static void conf_check_ok(struct srd_session *sess, int key, uint64_t x)
140{
141 int ret;
142
98e0b79b
UH
143 ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
144 fail_unless(ret == SRD_OK, "srd_session_metadata_set(%p, %d, %"
269b442d
UH
145 PRIu64 ") failed: %d.", sess, key, x, ret);
146}
147
148static void conf_check_fail(struct srd_session *sess, int key, uint64_t x)
149{
150 int ret;
151
98e0b79b
UH
152 ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
153 fail_unless(ret != SRD_OK, "srd_session_metadata_set(%p, %d, %"
269b442d
UH
154 PRIu64 ") worked.", sess, key, x);
155}
156
157static void conf_check_fail_null(struct srd_session *sess, int key)
158{
159 int ret;
160
98e0b79b 161 ret = srd_session_metadata_set(sess, key, NULL);
269b442d 162 fail_unless(ret != SRD_OK,
98e0b79b 163 "srd_session_metadata_set(NULL) for key %d worked.", key);
269b442d
UH
164}
165
166static void conf_check_fail_str(struct srd_session *sess, int key, const char *s)
167{
168 int ret;
169
98e0b79b
UH
170 ret = srd_session_metadata_set(sess, key, g_variant_new_string(s));
171 fail_unless(ret != SRD_OK, "srd_session_metadata_set() for key %d "
269b442d
UH
172 "failed: %d.", key, ret);
173}
174
175/*
98e0b79b 176 * Check whether srd_session_metadata_set() works.
269b442d
UH
177 * If it returns != SRD_OK (or segfaults) this test will fail.
178 */
98e0b79b 179START_TEST(test_session_metadata_set)
269b442d
UH
180{
181 uint64_t i;
182 struct srd_session *sess;
183
184 srd_init(NULL);
185 srd_session_new(&sess);
186 /* Try a bunch of values. */
e94fdd57 187 for (i = 0; i < 1000; i++)
269b442d 188 conf_check_ok(sess, SRD_CONF_SAMPLERATE, i);
269b442d 189 /* Try the max. possible value. */
e94fdd57 190 conf_check_ok(sess, SRD_CONF_SAMPLERATE, UINT64_MAX);
269b442d
UH
191 srd_session_destroy(sess);
192 srd_exit();
193}
194END_TEST
195
196/*
98e0b79b 197 * Check whether srd_session_metadata_set() fails with invalid input.
269b442d
UH
198 * If it returns SRD_OK (or segfaults) this test will fail.
199 */
98e0b79b 200START_TEST(test_session_metadata_set_bogus)
269b442d
UH
201{
202 struct srd_session *sess;
203
204 srd_init(NULL);
205 srd_session_new(&sess);
206
207 /* Incorrect gvariant type (currently only uint64 is used). */
269b442d 208 conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "");
269b442d
UH
209 conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo");
210
211 /* NULL data pointer. */
269b442d
UH
212 conf_check_fail_null(sess, SRD_CONF_SAMPLERATE);
213
214 /* NULL session. */
269b442d
UH
215 conf_check_fail(NULL, SRD_CONF_SAMPLERATE, 0);
216
217 /* Invalid keys. */
218 conf_check_fail(sess, -1, 0);
219 conf_check_fail(sess, 9, 0);
220 conf_check_fail(sess, 123, 0);
221
222 srd_session_destroy(sess);
223 srd_exit();
224}
225END_TEST
226
227Suite *suite_session(void)
228{
229 Suite *s;
230 TCase *tc;
231
232 s = suite_create("session");
233
234 tc = tcase_create("new_destroy");
28c3c217 235 tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
269b442d
UH
236 tcase_add_test(tc, test_session_new);
237 tcase_add_test(tc, test_session_new_bogus);
238 tcase_add_test(tc, test_session_new_multiple);
239 tcase_add_test(tc, test_session_destroy);
240 tcase_add_test(tc, test_session_destroy_bogus);
241 suite_add_tcase(s, tc);
242
243 tc = tcase_create("config");
28c3c217 244 tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
98e0b79b
UH
245 tcase_add_test(tc, test_session_metadata_set);
246 tcase_add_test(tc, test_session_metadata_set_bogus);
269b442d
UH
247 suite_add_tcase(s, tc);
248
249 return s;
250}