Debian Codegraph @

unit/test-crf.c

raw
1 /*
2 * Copyright (c) 2018, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <stdarg.h>
29 #include <stddef.h>
30 #include <setjmp.h>
31 #include <cmocka.h>
32 #include <arpa/inet.h>
33
34 #include "avtp.h"
35 #include "avtp_crf.h"
36
37 static void crf_get_field_null_pdu(void **state)
38 {
39 int res;
40 uint64_t val;
41
42 res = avtp_crf_pdu_get(NULL, AVTP_CRF_FIELD_SV, &val);
43
44 assert_int_equal(res, -EINVAL);
45 }
46
47 static void crf_get_field_null_val(void **state)
48 {
49 int res;
50 struct avtp_crf_pdu pdu = { 0 };
51
52 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_SV, NULL);
53
54 assert_int_equal(res, -EINVAL);
55 }
56
57 static void crf_get_field_invalid_field(void **state)
58 {
59 int res;
60 uint64_t val;
61 struct avtp_crf_pdu pdu = { 0 };
62
63 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_MAX, &val);
64
65 assert_int_equal(res, -EINVAL);
66 }
67
68 static void crf_get_field_sv(void **state)
69 {
70 int res;
71 uint64_t val;
72 struct avtp_crf_pdu pdu = { 0 };
73
74 /* Set the 'sv' field to 1 */
75 pdu.subtype_data = htonl(0x00800000);
76
77 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_SV, &val);
78
79 assert_int_equal(res, 0);
80 assert_true(val == 1);
81 }
82
83 static void crf_get_field_mr(void **state)
84 {
85 int res;
86 uint64_t val;
87 struct avtp_crf_pdu pdu = { 0 };
88
89 /* Set the 'mr' field to 1 */
90 pdu.subtype_data = htonl(0x00080000);
91
92 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_MR, &val);
93
94 assert_int_equal(res, 0);
95 assert_true(val == 1);
96 }
97
98 static void ctf_get_field_fs(void **state)
99 {
100 int res;
101 uint64_t val;
102 struct avtp_crf_pdu pdu = { 0 };
103
104 /* Set the 'fs' field to 1 */
105 pdu.subtype_data = htonl(0x00020000);
106
107 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_FS, &val);
108
109 assert_int_equal(res, 0);
110 assert_true(val == 1);
111 }
112
113 static void crf_get_field_tu(void **state)
114 {
115 int res;
116 uint64_t val;
117 struct avtp_crf_pdu pdu = { 0 };
118
119 /* Set the 'tu' field to 1 */
120 pdu.subtype_data = htonl(0x00010000);
121
122 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_TU, &val);
123
124 assert_int_equal(res, 0);
125 assert_true(val == 1);
126 }
127
128 static void crf_get_field_seq_num(void **state)
129 {
130 int res;
131 uint64_t val;
132 struct avtp_crf_pdu pdu = { 0 };
133
134 /* Set the 'seq_num' field to 0xBB */
135 pdu.subtype_data = htonl(0x0000BB00);
136
137 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_SEQ_NUM, &val);
138
139 assert_int_equal(res, 0);
140 assert_true(val == 0xBB);
141 }
142
143 static void crf_get_field_type(void **state)
144 {
145 int res;
146 uint64_t val;
147 struct avtp_crf_pdu pdu = { 0 };
148
149 /* Set the 'type' field to AVTP_CRF_TYPE_VIDEO_LINE */
150 pdu.subtype_data = htonl(0x00000003);
151
152 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_TYPE, &val);
153
154 assert_int_equal(res, 0);
155 assert_true(val == AVTP_CRF_TYPE_VIDEO_LINE);
156 }
157
158 static void crf_get_field_stream_id(void **state)
159 {
160 int res;
161 uint64_t val;
162 struct avtp_crf_pdu pdu = { 0 };
163
164 /* Set the 'stream_id' field to 0xAABBCCDDEEFF0002 */
165 pdu.stream_id = htobe64(0xAABBCCDDEEFF0002);
166
167 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_STREAM_ID, &val);
168
169 assert_int_equal(res, 0);
170 assert_true(val == 0xAABBCCDDEEFF0002);
171 }
172
173 static void crf_get_field_pull(void **state)
174 {
175 int res;
176 uint64_t val;
177 struct avtp_crf_pdu pdu = { 0 };
178
179 /* Set the 'pull' field to AVTP_CRF_PULL_MULT_BY_1_001 */
180 pdu.packet_info = htobe64(0x4000000000000000);
181
182 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_PULL, &val);
183
184 assert_int_equal(res, 0);
185 assert_true(val == AVTP_CRF_PULL_MULT_BY_1_001);
186 }
187
188 static void crf_get_field_base_freq(void **state)
189 {
190 int res;
191 uint64_t val;
192 struct avtp_crf_pdu pdu = { 0 };
193
194 /* Set the 'base_freq' field to 0x1FFFFFFF */
195 pdu.packet_info = htobe64(0x1FFFFFFF00000000);
196
197 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_BASE_FREQ, &val);
198
199 assert_int_equal(res, 0);
200 assert_true(val == 0x1FFFFFFF);
201 }
202
203 static void crf_get_field_crf_data_len(void **state)
204 {
205 int res;
206 uint64_t val;
207 struct avtp_crf_pdu pdu = { 0 };
208
209 /* Set the 'crf_data_len' field to 0xABCD */
210 pdu.packet_info = htobe64(0x00000000ABCD0000);
211
212 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_CRF_DATA_LEN, &val);
213
214 assert_int_equal(res, 0);
215 assert_true(val == 0xABCD);
216 }
217
218 static void crf_get_field_timestamp_interval(void **state)
219 {
220 int res;
221 uint64_t val;
222 struct avtp_crf_pdu pdu = { 0 };
223
224 /* Set the 'timestamp_interval' field to 0xABCD */
225 pdu.packet_info = htobe64(0x000000000000ABCD);
226
227 res = avtp_crf_pdu_get(&pdu, AVTP_CRF_FIELD_TIMESTAMP_INTERVAL, &val);
228
229 assert_int_equal(res, 0);
230 assert_true(val == 0xABCD);
231 }
232
233 static void crf_set_field_null_pdu(void **state)
234 {
235 int res;
236
237 res = avtp_crf_pdu_set(NULL, AVTP_CRF_FIELD_SV, 1);
238
239 assert_int_equal(res, -EINVAL);
240 }
241
242 static void crf_set_field_invalid_field(void **state)
243 {
244 int res;
245 struct avtp_crf_pdu pdu = { 0 };
246
247 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_MAX, 1);
248
249 assert_int_equal(res, -EINVAL);
250 }
251
252 static void crf_set_field_sv(void **state)
253 {
254 int res;
255 struct avtp_crf_pdu pdu = { 0 };
256
257 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_SV, 1);
258
259 assert_int_equal(res, 0);
260 assert_true(ntohl(pdu.subtype_data) == 0x00800000);
261 assert_true(pdu.stream_id == 0);
262 assert_true(pdu.packet_info == 0);
263 }
264
265 static void crf_set_field_mr(void **state)
266 {
267 int res;
268 struct avtp_crf_pdu pdu = { 0 };
269
270 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_MR, 1);
271
272 assert_int_equal(res, 0);
273 assert_true(ntohl(pdu.subtype_data) == 0x00080000);
274 assert_true(pdu.stream_id == 0);
275 assert_true(pdu.packet_info == 0);
276 }
277
278 static void crf_set_field_fs(void **state)
279 {
280 int res;
281 struct avtp_crf_pdu pdu = { 0 };
282
283 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_FS, 1);
284
285 assert_int_equal(res, 0);
286 assert_true(ntohl(pdu.subtype_data) == 0x00020000);
287 assert_true(pdu.stream_id == 0);
288 assert_true(pdu.packet_info == 0);
289 }
290
291 static void crf_set_field_tu(void **state)
292 {
293 int res;
294 struct avtp_crf_pdu pdu = { 0 };
295
296 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_TU, 1);
297
298 assert_int_equal(res, 0);
299 assert_true(ntohl(pdu.subtype_data) == 0x00010000);
300 assert_true(pdu.stream_id == 0);
301 assert_true(pdu.packet_info == 0);
302 }
303
304 static void crf_set_field_seq_num(void **state)
305 {
306 int res;
307 struct avtp_crf_pdu pdu = { 0 };
308
309 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_SEQ_NUM, 0xAA);
310
311 assert_int_equal(res, 0);
312 assert_true(ntohl(pdu.subtype_data) == 0x0000AA00);
313 assert_true(pdu.stream_id == 0);
314 assert_true(pdu.packet_info == 0);
315 }
316
317 static void crf_set_field_type(void **state)
318 {
319 int res;
320 struct avtp_crf_pdu pdu = { 0 };
321
322 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_TYPE,
323 AVTP_CRF_TYPE_AUDIO_SAMPLE);
324
325 assert_int_equal(res, 0);
326 assert_true(ntohl(pdu.subtype_data) == 0x00000001);
327 assert_true(pdu.stream_id == 0);
328 assert_true(pdu.packet_info == 0);
329 }
330
331 static void crf_set_field_stream_id(void **state)
332 {
333 int res;
334 struct avtp_crf_pdu pdu = { 0 };
335
336 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_STREAM_ID,
337 0xAABBCCDDEEFF0002);
338
339 assert_int_equal(res, 0);
340 assert_true(be64toh(pdu.stream_id) == 0xAABBCCDDEEFF0002);
341 assert_true(pdu.subtype_data == 0);
342 assert_true(pdu.packet_info == 0);
343 }
344
345 static void crf_set_field_pull(void **state)
346 {
347 int res;
348 struct avtp_crf_pdu pdu = { 0 };
349
350 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_PULL,
351 AVTP_CRF_PULL_MULT_BY_1_001);
352
353 assert_int_equal(res, 0);
354 assert_true(pdu.subtype_data == 0);
355 assert_true(pdu.stream_id == 0);
356 assert_true(be64toh(pdu.packet_info) == 0x4000000000000000);
357 }
358
359 static void crf_set_field_base_freq(void **state)
360 {
361 int res;
362 struct avtp_crf_pdu pdu = { 0 };
363
364 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_BASE_FREQ, 0x1FFFFFFF);
365
366 assert_int_equal(res, 0);
367 assert_true(pdu.subtype_data == 0);
368 assert_true(pdu.stream_id == 0);
369 assert_true(be64toh(pdu.packet_info) == 0x1FFFFFFF00000000);
370 }
371
372 static void crf_set_field_crf_data_len(void **state)
373 {
374 int res;
375 struct avtp_crf_pdu pdu = { 0 };
376
377 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_CRF_DATA_LEN, 0xABCD);
378
379 assert_int_equal(res, 0);
380 assert_true(be64toh(pdu.packet_info) == 0x00000000ABCD0000);
381 assert_true(pdu.subtype_data == 0);
382 assert_true(pdu.stream_id == 0);
383 }
384
385 static void crf_set_field_timestamp_interval(void **state)
386 {
387 int res;
388 struct avtp_crf_pdu pdu = { 0 };
389
390 res = avtp_crf_pdu_set(&pdu, AVTP_CRF_FIELD_TIMESTAMP_INTERVAL, 0xABCD);
391
392 assert_int_equal(res, 0);
393 assert_true(be64toh(pdu.packet_info) == 0x000000000000ABCD);
394 assert_true(pdu.subtype_data == 0);
395 assert_true(pdu.stream_id == 0);
396 }
397
398 static void crf_pdu_init_null_pdu(void **state)
399 {
400 int res;
401
402 res = avtp_crf_pdu_init(NULL);
403
404 assert_int_equal(res, -EINVAL);
405 }
406
407 static void crf_pdu_init(void **state)
408 {
409 int res;
410 struct avtp_crf_pdu pdu;
411
412 res = avtp_crf_pdu_init(&pdu);
413
414 assert_int_equal(res, 0);
415 assert_true(ntohl(pdu.subtype_data) == 0x04800000);
416 assert_true(pdu.stream_id == 0);
417 assert_true(pdu.packet_info == 0);
418 }
419
420 int main(void)
421 {
422 const struct CMUnitTest tests[] = {
423 cmocka_unit_test(crf_get_field_null_pdu),
424 cmocka_unit_test(crf_get_field_null_val),
425 cmocka_unit_test(crf_get_field_invalid_field),
426 cmocka_unit_test(crf_get_field_sv),
427 cmocka_unit_test(crf_get_field_mr),
428 cmocka_unit_test(ctf_get_field_fs),
429 cmocka_unit_test(crf_get_field_tu),
430 cmocka_unit_test(crf_get_field_seq_num),
431 cmocka_unit_test(crf_get_field_type),
432 cmocka_unit_test(crf_get_field_stream_id),
433 cmocka_unit_test(crf_get_field_pull),
434 cmocka_unit_test(crf_get_field_base_freq),
435 cmocka_unit_test(crf_get_field_crf_data_len),
436 cmocka_unit_test(crf_get_field_timestamp_interval),
437 cmocka_unit_test(crf_set_field_null_pdu),
438 cmocka_unit_test(crf_set_field_invalid_field),
439 cmocka_unit_test(crf_set_field_sv),
440 cmocka_unit_test(crf_set_field_mr),
441 cmocka_unit_test(crf_set_field_fs),
442 cmocka_unit_test(crf_set_field_tu),
443 cmocka_unit_test(crf_set_field_seq_num),
444 cmocka_unit_test(crf_set_field_type),
445 cmocka_unit_test(crf_set_field_stream_id),
446 cmocka_unit_test(crf_set_field_pull),
447 cmocka_unit_test(crf_set_field_base_freq),
448 cmocka_unit_test(crf_set_field_crf_data_len),
449 cmocka_unit_test(crf_set_field_timestamp_interval),
450 cmocka_unit_test(crf_pdu_init_null_pdu),
451 cmocka_unit_test(crf_pdu_init),
452 };
453
454 return cmocka_run_group_tests(tests, NULL, NULL);
455 }