]> sigrok.org Git - libsigrokdecode.git/blob - tests/check_session.c
53b874b370bd44fd453576debdd8a09efa531b48
[libsigrokdecode.git] / tests / check_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, 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
26 static void setup(void)
27 {
28         /* Silence libsigrokdecode while the unit tests run. */
29         srd_log_loglevel_set(SRD_LOG_NONE);
30 }
31
32 static 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  */
40 START_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 }
50 END_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  */
56 START_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 }
65 END_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  */
71 START_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 }
113 END_TEST
114
115 /*
116  * Check whether srd_session_destroy() works.
117  * If it returns != SRD_OK (or segfaults) this test will fail.
118  */
119 START_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 }
130 END_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  */
136 START_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 }
145 END_TEST
146
147 static void conf_check_ok(struct srd_session *sess, int key, uint64_t x)
148 {
149         int ret;
150
151         ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
152         fail_unless(ret == SRD_OK, "srd_session_metadata_set(%p, %d, %"
153                 PRIu64 ") failed: %d.", sess, key, x, ret);
154 }
155
156 static void conf_check_fail(struct srd_session *sess, int key, uint64_t x)
157 {
158         int ret;
159
160         ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
161         fail_unless(ret != SRD_OK, "srd_session_metadata_set(%p, %d, %"
162                 PRIu64 ") worked.", sess, key, x);
163 }
164
165 static void conf_check_fail_null(struct srd_session *sess, int key)
166 {
167         int ret;
168
169         ret = srd_session_metadata_set(sess, key, NULL);
170         fail_unless(ret != SRD_OK,
171                 "srd_session_metadata_set(NULL) for key %d worked.", key);
172 }
173
174 static void conf_check_fail_str(struct srd_session *sess, int key, const char *s)
175 {
176         int ret;
177
178         ret = srd_session_metadata_set(sess, key, g_variant_new_string(s));
179         fail_unless(ret != SRD_OK, "srd_session_metadata_set() for key %d "
180                 "failed: %d.", key, ret);
181 }
182
183 /*
184  * Check whether srd_session_metadata_set() works.
185  * If it returns != SRD_OK (or segfaults) this test will fail.
186  */
187 START_TEST(test_session_metadata_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_SAMPLERATE, i);
197         }
198         /* Try the max. possible value. */
199         conf_check_ok(sess, SRD_CONF_SAMPLERATE, 18446744073709551615ULL);
200         srd_session_destroy(sess);
201         srd_exit();
202 }
203 END_TEST
204
205 /*
206  * Check whether srd_session_metadata_set() fails with invalid input.
207  * If it returns SRD_OK (or segfaults) this test will fail.
208  */
209 START_TEST(test_session_metadata_set_bogus)
210 {
211         struct srd_session *sess;
212
213         srd_init(NULL);
214         srd_session_new(&sess);
215
216         /* Incorrect gvariant type (currently only uint64 is used). */
217         conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "");
218         conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo");
219
220         /* NULL data pointer. */
221         conf_check_fail_null(sess, SRD_CONF_SAMPLERATE);
222
223         /* NULL session. */
224         conf_check_fail(NULL, SRD_CONF_SAMPLERATE, 0);
225
226         /* Invalid keys. */
227         conf_check_fail(sess, -1, 0);
228         conf_check_fail(sess, 9, 0);
229         conf_check_fail(sess, 123, 0);
230
231         srd_session_destroy(sess);
232         srd_exit();
233 }
234 END_TEST
235
236 Suite *suite_session(void)
237 {
238         Suite *s;
239         TCase *tc;
240
241         s = suite_create("session");
242
243         tc = tcase_create("new_destroy");
244         tcase_add_checked_fixture(tc, setup, teardown);
245         tcase_add_test(tc, test_session_new);
246         tcase_add_test(tc, test_session_new_bogus);
247         tcase_add_test(tc, test_session_new_multiple);
248         tcase_add_test(tc, test_session_destroy);
249         tcase_add_test(tc, test_session_destroy_bogus);
250         suite_add_tcase(s, tc);
251
252         tc = tcase_create("config");
253         tcase_add_checked_fixture(tc, setup, teardown);
254         tcase_add_test(tc, test_session_metadata_set);
255         tcase_add_test(tc, test_session_metadata_set_bogus);
256         suite_add_tcase(s, tc);
257
258         return s;
259 }