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