]> sigrok.org Git - libsigrokdecode.git/blob - tests/session.c
avr_isp: Add more parts
[libsigrokdecode.git] / tests / session.c
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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <libsigrokdecode-internal.h> /* First, to avoid compiler warning. */
22 #include <libsigrokdecode.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <check.h>
26 #include "lib.h"
27
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
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, %"
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;
151         GVariant *value = g_variant_new_uint64(x);
152
153         ret = srd_session_metadata_set(sess, key, value);
154         fail_unless(ret != SRD_OK, "srd_session_metadata_set(%p, %d, %"
155                 PRIu64 ") worked.", sess, key, x);
156         if (ret != SRD_OK)
157                 g_variant_unref(value);
158 }
159
160 static void conf_check_fail_null(struct srd_session *sess, int key)
161 {
162         int ret;
163
164         ret = srd_session_metadata_set(sess, key, NULL);
165         fail_unless(ret != SRD_OK,
166                 "srd_session_metadata_set(NULL) for key %d worked.", key);
167 }
168
169 static void conf_check_fail_str(struct srd_session *sess, int key, const char *s)
170 {
171         int ret;
172         GVariant *value = g_variant_new_string(s);
173
174         ret = srd_session_metadata_set(sess, key, value);
175         fail_unless(ret != SRD_OK, "srd_session_metadata_set() for key %d "
176                 "failed: %d.", key, ret);
177         if (ret != SRD_OK)
178                 g_variant_unref(value);
179 }
180
181 /*
182  * Check whether srd_session_metadata_set() works.
183  * If it returns != SRD_OK (or segfaults) this test will fail.
184  */
185 START_TEST(test_session_metadata_set)
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. */
193         for (i = 0; i < 1000; i++)
194                 conf_check_ok(sess, SRD_CONF_SAMPLERATE, i);
195         /* Try the max. possible value. */
196         conf_check_ok(sess, SRD_CONF_SAMPLERATE, UINT64_MAX);
197         srd_session_destroy(sess);
198         srd_exit();
199 }
200 END_TEST
201
202 /*
203  * Check whether srd_session_metadata_set() fails with invalid input.
204  * If it returns SRD_OK (or segfaults) this test will fail.
205  */
206 START_TEST(test_session_metadata_set_bogus)
207 {
208         struct srd_session *sess;
209
210         srd_init(NULL);
211         srd_session_new(&sess);
212
213         /* Incorrect GVariant type (currently only uint64 is used). */
214         conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "");
215         conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo");
216
217         /* NULL data pointer. */
218         conf_check_fail_null(sess, SRD_CONF_SAMPLERATE);
219
220         /* NULL session. */
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
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
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");
271         tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
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");
280         tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
281         tcase_add_test(tc, test_session_metadata_set);
282         tcase_add_test(tc, test_session_metadata_set_bogus);
283         suite_add_tcase(s, tc);
284
285         tc = tcase_create("reset");
286         tcase_add_test(tc, test_session_reset_nodata);
287         suite_add_tcase(s, tc);
288
289         return s;
290 }