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