]>
Commit | Line | Data |
---|---|---|
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 | |
4539e9ca | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
269b442d UH |
18 | */ |
19 | ||
36784362 | 20 | #include <config.h> |
b480383d DE |
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 | */ | |
32 | START_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 | } | |
42 | END_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 | */ | |
48 | START_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 | } | |
57 | END_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 | */ | |
63 | START_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 | } | |
105 | END_TEST | |
106 | ||
107 | /* | |
108 | * Check whether srd_session_destroy() works. | |
109 | * If it returns != SRD_OK (or segfaults) this test will fail. | |
110 | */ | |
111 | START_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 | } | |
122 | END_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 | */ | |
128 | START_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 | } | |
137 | END_TEST | |
138 | ||
139 | static 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 | ||
148 | static void conf_check_fail(struct srd_session *sess, int key, uint64_t x) | |
149 | { | |
150 | int ret; | |
08cbe922 | 151 | GVariant *value = g_variant_new_uint64(x); |
269b442d | 152 | |
08cbe922 | 153 | ret = srd_session_metadata_set(sess, key, value); |
98e0b79b | 154 | fail_unless(ret != SRD_OK, "srd_session_metadata_set(%p, %d, %" |
269b442d | 155 | PRIu64 ") worked.", sess, key, x); |
08cbe922 JB |
156 | if (ret != SRD_OK) |
157 | g_variant_unref(value); | |
269b442d UH |
158 | } |
159 | ||
160 | static void conf_check_fail_null(struct srd_session *sess, int key) | |
161 | { | |
162 | int ret; | |
163 | ||
98e0b79b | 164 | ret = srd_session_metadata_set(sess, key, NULL); |
269b442d | 165 | fail_unless(ret != SRD_OK, |
98e0b79b | 166 | "srd_session_metadata_set(NULL) for key %d worked.", key); |
269b442d UH |
167 | } |
168 | ||
169 | static void conf_check_fail_str(struct srd_session *sess, int key, const char *s) | |
170 | { | |
171 | int ret; | |
08cbe922 | 172 | GVariant *value = g_variant_new_string(s); |
269b442d | 173 | |
08cbe922 | 174 | ret = srd_session_metadata_set(sess, key, value); |
98e0b79b | 175 | fail_unless(ret != SRD_OK, "srd_session_metadata_set() for key %d " |
269b442d | 176 | "failed: %d.", key, ret); |
08cbe922 JB |
177 | if (ret != SRD_OK) |
178 | g_variant_unref(value); | |
269b442d UH |
179 | } |
180 | ||
181 | /* | |
98e0b79b | 182 | * Check whether srd_session_metadata_set() works. |
269b442d UH |
183 | * If it returns != SRD_OK (or segfaults) this test will fail. |
184 | */ | |
98e0b79b | 185 | START_TEST(test_session_metadata_set) |
269b442d UH |
186 | { |
187 | uint64_t i; | |
188 | struct srd_session *sess; | |
189 | ||
190 | srd_init(NULL); | |
191 | srd_session_new(&sess); | |
192 | /* Try a bunch of values. */ | |
e94fdd57 | 193 | for (i = 0; i < 1000; i++) |
269b442d | 194 | conf_check_ok(sess, SRD_CONF_SAMPLERATE, i); |
269b442d | 195 | /* Try the max. possible value. */ |
e94fdd57 | 196 | conf_check_ok(sess, SRD_CONF_SAMPLERATE, UINT64_MAX); |
269b442d UH |
197 | srd_session_destroy(sess); |
198 | srd_exit(); | |
199 | } | |
200 | END_TEST | |
201 | ||
202 | /* | |
98e0b79b | 203 | * Check whether srd_session_metadata_set() fails with invalid input. |
269b442d UH |
204 | * If it returns SRD_OK (or segfaults) this test will fail. |
205 | */ | |
98e0b79b | 206 | START_TEST(test_session_metadata_set_bogus) |
269b442d UH |
207 | { |
208 | struct srd_session *sess; | |
209 | ||
210 | srd_init(NULL); | |
211 | srd_session_new(&sess); | |
212 | ||
868fd207 | 213 | /* Incorrect GVariant type (currently only uint64 is used). */ |
269b442d | 214 | conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, ""); |
269b442d UH |
215 | conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo"); |
216 | ||
217 | /* NULL data pointer. */ | |
269b442d UH |
218 | conf_check_fail_null(sess, SRD_CONF_SAMPLERATE); |
219 | ||
220 | /* NULL session. */ | |
269b442d UH |
221 | conf_check_fail(NULL, SRD_CONF_SAMPLERATE, 0); |
222 | ||
223 | /* Invalid keys. */ | |
224 | conf_check_fail(sess, -1, 0); | |
225 | conf_check_fail(sess, 9, 0); | |
226 | conf_check_fail(sess, 123, 0); | |
227 | ||
228 | srd_session_destroy(sess); | |
229 | srd_exit(); | |
230 | } | |
231 | END_TEST | |
232 | ||
51c51ffb GS |
233 | /* |
234 | * Check whether srd_session_terminate_reset() succeeds on newly created | |
235 | * sessions, as well as after calling start() and meta(). No data is fed | |
236 | * to decoders here. | |
237 | */ | |
238 | START_TEST(test_session_reset_nodata) | |
239 | { | |
240 | struct srd_session *sess; | |
241 | int ret; | |
242 | GVariant *data; | |
243 | ||
244 | srd_init(NULL); | |
245 | srd_session_new(&sess); | |
246 | ret = srd_session_terminate_reset(sess); | |
247 | fail_unless(ret == SRD_OK, "srd_session_terminate_reset() failed: %d.", ret); | |
248 | ret = srd_session_start(sess); | |
249 | fail_unless(ret == SRD_OK, "srd_session_start() failed: %d.", ret); | |
250 | ret = srd_session_terminate_reset(sess); | |
251 | fail_unless(ret == SRD_OK, "srd_session_terminate_reset() failed: %d.", ret); | |
252 | data = g_variant_new_uint64(1000000); | |
253 | ret = srd_session_metadata_set(sess, SRD_CONF_SAMPLERATE, data); | |
254 | fail_unless(ret == SRD_OK, "srd_session_metadata_set() failed: %d.", ret); | |
255 | ret = srd_session_terminate_reset(sess); | |
256 | fail_unless(ret == SRD_OK, "srd_session_terminate_reset() failed: %d.", ret); | |
257 | ret = srd_session_destroy(sess); | |
258 | fail_unless(ret == SRD_OK, "srd_session_destroy() failed: %d.", ret); | |
259 | srd_exit(); | |
260 | } | |
261 | END_TEST | |
262 | ||
269b442d UH |
263 | Suite *suite_session(void) |
264 | { | |
265 | Suite *s; | |
266 | TCase *tc; | |
267 | ||
268 | s = suite_create("session"); | |
269 | ||
270 | tc = tcase_create("new_destroy"); | |
28c3c217 | 271 | tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown); |
269b442d UH |
272 | tcase_add_test(tc, test_session_new); |
273 | tcase_add_test(tc, test_session_new_bogus); | |
274 | tcase_add_test(tc, test_session_new_multiple); | |
275 | tcase_add_test(tc, test_session_destroy); | |
276 | tcase_add_test(tc, test_session_destroy_bogus); | |
277 | suite_add_tcase(s, tc); | |
278 | ||
279 | tc = tcase_create("config"); | |
28c3c217 | 280 | tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown); |
98e0b79b UH |
281 | tcase_add_test(tc, test_session_metadata_set); |
282 | tcase_add_test(tc, test_session_metadata_set_bogus); | |
269b442d UH |
283 | suite_add_tcase(s, tc); |
284 | ||
51c51ffb GS |
285 | tc = tcase_create("reset"); |
286 | tcase_add_test(tc, test_session_reset_nodata); | |
287 | suite_add_tcase(s, tc); | |
288 | ||
269b442d UH |
289 | return s; |
290 | } |