Debian Codegraph @

unit/test-cvf.c

raw
1 /*
2 * Copyright (c) 2019, 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 <alloca.h>
29 #include <stdarg.h>
30 #include <stddef.h>
31 #include <string.h>
32 #include <setjmp.h>
33 #include <cmocka.h>
34 #include <arpa/inet.h>
35
36 #include "avtp.h"
37 #include "avtp_cvf.h"
38
39 static void cvf_get_field_null_pdu(void **state)
40 {
41 int res;
42 uint64_t val = 1;
43
44 res = avtp_cvf_pdu_get(NULL, AVTP_CVF_FIELD_SV, &val);
45
46 assert_int_equal(res, -EINVAL);
47 }
48
49 static void cvf_get_field_null_val(void **state)
50 {
51 int res;
52 struct avtp_stream_pdu pdu = { 0 };
53
54 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_SV, NULL);
55
56 assert_int_equal(res, -EINVAL);
57 }
58
59 static void cvf_get_field_invalid_field(void **state)
60 {
61 int res;
62 uint64_t val = 1;
63 struct avtp_stream_pdu pdu = { 0 };
64
65 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_MAX, &val);
66
67 assert_int_equal(res, -EINVAL);
68 }
69
70 static void cvf_get_field_sv(void **state)
71 {
72 int res;
73 uint64_t val;
74 struct avtp_stream_pdu pdu = { 0 };
75
76 /* Set 'sv' field to 1. */
77 pdu.subtype_data = htonl(0x00800000);
78
79 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_SV, &val);
80
81 assert_int_equal(res, 0);
82 assert_true(val == 1);
83 }
84
85 static void cvf_get_field_mr(void **state)
86 {
87 int res;
88 uint64_t val;
89 struct avtp_stream_pdu pdu = { 0 };
90
91 /* Set 'mr' field to 1. */
92 pdu.subtype_data = htonl(0x00080000);
93
94 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_MR, &val);
95
96 assert_int_equal(res, 0);
97 assert_true(val == 1);
98 }
99
100 static void cvf_get_field_tv(void **state)
101 {
102 int res;
103 uint64_t val;
104 struct avtp_stream_pdu pdu = { 0 };
105
106 /* Set 'tv' field to 1. */
107 pdu.subtype_data = htonl(0x00010000);
108
109 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_TV, &val);
110
111 assert_int_equal(res, 0);
112 assert_true(val == 1);
113 }
114
115 static void cvf_get_field_seq_num(void **state)
116 {
117 int res;
118 uint64_t val;
119 struct avtp_stream_pdu pdu = { 0 };
120
121 /* Set 'sequence_num' field to 0x55. */
122 pdu.subtype_data = htonl(0x00005500);
123
124 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_SEQ_NUM, &val);
125
126 assert_int_equal(res, 0);
127 assert_true(val == 0x55);
128 }
129
130 static void cvf_get_field_tu(void **state)
131 {
132 int res;
133 uint64_t val;
134 struct avtp_stream_pdu pdu = { 0 };
135
136 /* Set 'tu' field to 1. */
137 pdu.subtype_data = htonl(0x00000001);
138
139 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_TU, &val);
140
141 assert_int_equal(res, 0);
142 assert_true(val == 1);
143 }
144
145 static void cvf_get_field_stream_id(void **state)
146 {
147 int res;
148 uint64_t val;
149 struct avtp_stream_pdu pdu = { 0 };
150
151 /* Set 'stream_id' field to 0xAABBCCDDEEFF0001. */
152 pdu.stream_id = htobe64(0xAABBCCDDEEFF0001);
153
154 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_STREAM_ID, &val);
155
156 assert_int_equal(res, 0);
157 assert_true(val == 0xAABBCCDDEEFF0001);
158 }
159
160 static void cvf_get_field_timestamp(void **state)
161 {
162 int res;
163 uint64_t val;
164 struct avtp_stream_pdu pdu = { 0 };
165
166 /* Set 'avtp_timestamp' field to 0x80C0FFEE. */
167 pdu.avtp_time = htonl(0x80C0FFEE);
168
169 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_TIMESTAMP, &val);
170
171 assert_int_equal(res, 0);
172 assert_true(val == 0x80C0FFEE);
173 }
174
175 static void cvf_get_field_format(void **state)
176 {
177 int res;
178 uint64_t val;
179 struct avtp_stream_pdu pdu = { 0 };
180
181 /* Set 'format' field to AVTP_CVF_FORMAT_RFC. */
182 pdu.format_specific = htonl(0x02000000);
183
184 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_FORMAT, &val);
185
186 assert_int_equal(res, 0);
187 assert_true(val == AVTP_CVF_FORMAT_RFC);
188 }
189
190 static void cvf_get_field_format_subtype(void **state)
191 {
192 int res;
193 uint64_t val;
194 struct avtp_stream_pdu pdu = { 0 };
195
196 /* Set 'format_subtype' field to AVTP_CVF_FORMAT_SUBTYPE_H264. */
197 pdu.format_specific = htonl(0x00010000);
198
199 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_FORMAT_SUBTYPE, &val);
200
201 assert_int_equal(res, 0);
202 assert_true(val == AVTP_CVF_FORMAT_SUBTYPE_H264);
203 }
204
205 static void cvf_get_field_data_len(void **state)
206 {
207 int res;
208 uint64_t val;
209 struct avtp_stream_pdu pdu = { 0 };
210
211 /* Set 'stream_data_length' field to 0xAAAA. */
212 pdu.packet_info = htonl(0xAAAA0000);
213
214 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_STREAM_DATA_LEN, &val);
215
216 assert_int_equal(res, 0);
217 assert_true(val == 0xAAAA);
218 }
219
220 static void cvf_get_field_m(void **state)
221 {
222 int res;
223 uint64_t val;
224 struct avtp_stream_pdu pdu = { 0 };
225
226 /* Set 'M' field to 0x1. */
227 pdu.packet_info = htonl(0x00001000);
228
229 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_M, &val);
230
231 assert_int_equal(res, 0);
232 assert_true(val == 0x1);
233 }
234
235 static void cvf_get_field_evt(void **state)
236 {
237 int res;
238 uint64_t val;
239 struct avtp_stream_pdu pdu = { 0 };
240
241 /* Set 'evt' field to 0xA. */
242 pdu.packet_info = htonl(0x00000A00);
243
244 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_EVT, &val);
245
246 assert_int_equal(res, 0);
247 assert_true(val == 0xA);
248 }
249
250 static void cvf_set_field_null_pdu(void **state)
251 {
252 int res;
253
254 res = avtp_cvf_pdu_set(NULL, AVTP_CVF_FIELD_SV, 1);
255
256 assert_int_equal(res, -EINVAL);
257 }
258
259 static void cvf_set_field_invalid_field(void **state)
260 {
261 int res;
262 struct avtp_stream_pdu pdu = { 0 };
263
264 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_MAX, 1);
265
266 assert_int_equal(res, -EINVAL);
267 }
268
269 static void cvf_set_field_sv(void **state)
270 {
271 int res;
272 struct avtp_stream_pdu pdu = { 0 };
273
274 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_SV, 1);
275
276 assert_int_equal(res, 0);
277 assert_true(ntohl(pdu.subtype_data) == 0x00800000);
278 assert_true(pdu.stream_id == 0);
279 assert_true(pdu.avtp_time == 0);
280 assert_true(pdu.format_specific == 0);
281 assert_true(pdu.packet_info == 0);
282 }
283
284 static void cvf_set_field_mr(void **state)
285 {
286 int res;
287 struct avtp_stream_pdu pdu = { 0 };
288
289 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_MR, 1);
290
291 assert_int_equal(res, 0);
292 assert_true(ntohl(pdu.subtype_data) == 0x00080000);
293 assert_true(pdu.stream_id == 0);
294 assert_true(pdu.avtp_time == 0);
295 assert_true(pdu.format_specific == 0);
296 assert_true(pdu.packet_info == 0);
297 }
298
299 static void cvf_set_field_tv(void **state)
300 {
301 int res;
302 struct avtp_stream_pdu pdu = { 0 };
303
304 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_TV, 1);
305
306 assert_int_equal(res, 0);
307 assert_true(ntohl(pdu.subtype_data) == 0x00010000);
308 assert_true(pdu.stream_id == 0);
309 assert_true(pdu.avtp_time == 0);
310 assert_true(pdu.format_specific == 0);
311 assert_true(pdu.packet_info == 0);
312 }
313
314 static void cvf_set_field_seq_num(void **state)
315 {
316 int res;
317 struct avtp_stream_pdu pdu = { 0 };
318
319 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_SEQ_NUM, 0x55);
320
321 assert_int_equal(res, 0);
322 assert_true(ntohl(pdu.subtype_data) == 0x00005500);
323 assert_true(pdu.stream_id == 0);
324 assert_true(pdu.avtp_time == 0);
325 assert_true(pdu.format_specific == 0);
326 assert_true(pdu.packet_info == 0);
327 }
328
329 static void cvf_set_field_tu(void **state)
330 {
331 int res;
332 struct avtp_stream_pdu pdu = { 0 };
333
334 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_TU, 1);
335
336 assert_int_equal(res, 0);
337 assert_true(ntohl(pdu.subtype_data) == 0x00000001);
338 assert_true(pdu.stream_id == 0);
339 assert_true(pdu.avtp_time == 0);
340 assert_true(pdu.format_specific == 0);
341 assert_true(pdu.packet_info == 0);
342 }
343
344 static void cvf_set_field_stream_id(void **state)
345 {
346 int res;
347 struct avtp_stream_pdu pdu = { 0 };
348
349 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_STREAM_ID,
350 0xAABBCCDDEEFF0001);
351
352 assert_int_equal(res, 0);
353 assert_true(be64toh(pdu.stream_id) == 0xAABBCCDDEEFF0001);
354 assert_true(pdu.subtype_data == 0);
355 assert_true(pdu.avtp_time == 0);
356 assert_true(pdu.format_specific == 0);
357 assert_true(pdu.packet_info == 0);
358 }
359
360 static void cvf_set_field_timestamp(void **state)
361 {
362 int res;
363 struct avtp_stream_pdu pdu = { 0 };
364
365 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_TIMESTAMP, 0x80C0FFEE);
366
367 assert_int_equal(res, 0);
368 assert_true(ntohl(pdu.avtp_time) == 0x80C0FFEE);
369 assert_true(pdu.subtype_data == 0);
370 assert_true(pdu.stream_id == 0);
371 assert_true(pdu.format_specific == 0);
372 assert_true(pdu.packet_info == 0);
373 }
374
375 static void cvf_set_field_format(void **state)
376 {
377 int res;
378 struct avtp_stream_pdu pdu = { 0 };
379
380 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_FORMAT,
381 AVTP_CVF_FORMAT_RFC);
382
383 assert_int_equal(res, 0);
384 assert_true(ntohl(pdu.format_specific) == 0x02000000);
385 assert_true(pdu.subtype_data == 0);
386 assert_true(pdu.stream_id == 0);
387 assert_true(pdu.avtp_time == 0);
388 assert_true(pdu.packet_info == 0);
389 }
390
391 static void cvf_set_field_format_subtype(void **state)
392 {
393 int res;
394 struct avtp_stream_pdu pdu = { 0 };
395
396 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_FORMAT_SUBTYPE,
397 AVTP_CVF_FORMAT_SUBTYPE_H264);
398
399 assert_int_equal(res, 0);
400 assert_true(ntohl(pdu.format_specific) == 0x10000);
401 assert_true(pdu.subtype_data == 0);
402 assert_true(pdu.stream_id == 0);
403 assert_true(pdu.avtp_time == 0);
404 assert_true(pdu.packet_info == 0);
405 }
406
407 static void cvf_set_field_data_len(void **state)
408 {
409 int res;
410 struct avtp_stream_pdu pdu = { 0 };
411
412 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_STREAM_DATA_LEN, 0xAAAA);
413
414 assert_int_equal(res, 0);
415 assert_true(ntohl(pdu.packet_info) == 0xAAAA0000);
416 assert_true(pdu.subtype_data == 0);
417 assert_true(pdu.stream_id == 0);
418 assert_true(pdu.avtp_time == 0);
419 assert_true(pdu.format_specific == 0);
420 }
421
422 static void cvf_set_field_m(void **state)
423 {
424 int res;
425 struct avtp_stream_pdu pdu = { 0 };
426
427 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_M, 1);
428
429 assert_int_equal(res, 0);
430 assert_true(ntohl(pdu.packet_info) == 0x00001000);
431 assert_true(pdu.subtype_data == 0);
432 assert_true(pdu.stream_id == 0);
433 assert_true(pdu.avtp_time == 0);
434 assert_true(pdu.format_specific == 0);
435 }
436
437 static void cvf_set_field_evt(void **state)
438 {
439 int res;
440 struct avtp_stream_pdu pdu = { 0 };
441
442 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_EVT, 0xA);
443
444 assert_int_equal(res, 0);
445 assert_true(ntohl(pdu.packet_info) == 0x00000A00);
446 assert_true(pdu.subtype_data == 0);
447 assert_true(pdu.stream_id == 0);
448 assert_true(pdu.avtp_time == 0);
449 assert_true(pdu.format_specific == 0);
450 }
451
452 static void cvf_pdu_init_null_pdu(void **state)
453 {
454 int res;
455
456 res = avtp_cvf_pdu_init(NULL, AVTP_CVF_FORMAT_SUBTYPE_H264);
457 assert_int_equal(res, -EINVAL);
458 }
459
460 static void cvf_pdu_init_invalid_subtype(void **state)
461 {
462 int res;
463 struct avtp_stream_pdu pdu;
464
465 res = avtp_cvf_pdu_init(&pdu, AVTP_CVF_FORMAT_SUBTYPE_JPEG2000 + 1);
466 assert_int_equal(res, -EINVAL);
467 }
468
469 static void cvf_pdu_init(void **state)
470 {
471 int res;
472 struct avtp_stream_pdu pdu;
473
474 res = avtp_cvf_pdu_init(&pdu, AVTP_CVF_FORMAT_SUBTYPE_H264);
475
476 assert_int_equal(res, 0);
477 assert_true(ntohl(pdu.subtype_data) == 0x03800000);
478 assert_true(pdu.stream_id == 0);
479 assert_true(pdu.avtp_time == 0);
480 assert_true(ntohl(pdu.format_specific) == 0x2010000);
481 assert_true(pdu.packet_info == 0);
482 }
483
484 /**** Tests for H.264 fields ****/
485
486 static void cvf_get_field_h264_ptv(void **state)
487 {
488 int res;
489 uint64_t val;
490 struct avtp_stream_pdu pdu = { 0 };
491
492 /* Set 'ptv' field to 1. */
493 pdu.packet_info = htonl(0x00002000);
494
495 res = avtp_cvf_pdu_get(&pdu, AVTP_CVF_FIELD_H264_PTV, &val);
496
497 assert_int_equal(res, 0);
498 assert_true(val == 1);
499 }
500
501 static void cvf_get_field_h264_timestamp(void **state)
502 {
503 int res;
504 uint64_t val;
505 struct avtp_stream_pdu *pdu =
506 alloca(sizeof(struct avtp_stream_pdu) + sizeof(uint32_t));
507 struct avtp_cvf_h264_payload *pay =
508 (struct avtp_cvf_h264_payload *)pdu->avtp_payload;
509
510 /* Set 'h264_timestamp' field (which lives in h264_header) to
511 * 0x80C0FFEE. */
512 pay->h264_header = htonl(0x80C0FFEE);
513
514 res = avtp_cvf_pdu_get(pdu, AVTP_CVF_FIELD_H264_TIMESTAMP, &val);
515
516 assert_int_equal(res, 0);
517 assert_true(val == 0x80C0FFEE);
518 }
519
520 static void cvf_set_field_h264_ptv(void **state)
521 {
522 int res;
523 struct avtp_stream_pdu pdu = { 0 };
524
525 res = avtp_cvf_pdu_set(&pdu, AVTP_CVF_FIELD_H264_PTV, 1);
526
527 assert_int_equal(res, 0);
528 assert_true(pdu.subtype_data == 0);
529 assert_true(pdu.stream_id == 0);
530 assert_true(pdu.avtp_time == 0);
531 assert_true(pdu.format_specific == 0);
532 assert_true(ntohl(pdu.packet_info) == 0x00002000);
533 }
534
535 static void cvf_set_field_h264_timestamp(void **state)
536 {
537 int res;
538 struct avtp_stream_pdu *pdu =
539 alloca(sizeof(struct avtp_stream_pdu) + sizeof(uint32_t));
540 struct avtp_cvf_h264_payload *pay =
541 (struct avtp_cvf_h264_payload *)pdu->avtp_payload;
542 memset(pdu, 0, sizeof(struct avtp_stream_pdu) + sizeof(uint32_t));
543
544 res = avtp_cvf_pdu_set(pdu, AVTP_CVF_FIELD_H264_TIMESTAMP, 0x80C0FFEE);
545
546 assert_int_equal(res, 0);
547 assert_true(pdu->avtp_time == 0);
548 assert_true(pdu->subtype_data == 0);
549 assert_true(pdu->stream_id == 0);
550 assert_true(pdu->format_specific == 0);
551 assert_true(pdu->packet_info == 0);
552 assert_true(ntohl(pay->h264_header) == 0x80C0FFEE);
553 }
554
555 int main(void)
556 {
557 const struct CMUnitTest tests[] = {
558 cmocka_unit_test(cvf_get_field_null_pdu),
559 cmocka_unit_test(cvf_get_field_null_val),
560 cmocka_unit_test(cvf_get_field_invalid_field),
561 cmocka_unit_test(cvf_get_field_sv),
562 cmocka_unit_test(cvf_get_field_mr),
563 cmocka_unit_test(cvf_get_field_tv),
564 cmocka_unit_test(cvf_get_field_seq_num),
565 cmocka_unit_test(cvf_get_field_tu),
566 cmocka_unit_test(cvf_get_field_stream_id),
567 cmocka_unit_test(cvf_get_field_timestamp),
568 cmocka_unit_test(cvf_get_field_format),
569 cmocka_unit_test(cvf_get_field_format_subtype),
570 cmocka_unit_test(cvf_get_field_data_len),
571 cmocka_unit_test(cvf_get_field_m),
572 cmocka_unit_test(cvf_get_field_evt),
573 cmocka_unit_test(cvf_get_field_h264_ptv),
574 cmocka_unit_test(cvf_get_field_h264_timestamp),
575 cmocka_unit_test(cvf_set_field_null_pdu),
576 cmocka_unit_test(cvf_set_field_invalid_field),
577 cmocka_unit_test(cvf_set_field_sv),
578 cmocka_unit_test(cvf_set_field_mr),
579 cmocka_unit_test(cvf_set_field_tv),
580 cmocka_unit_test(cvf_set_field_seq_num),
581 cmocka_unit_test(cvf_set_field_tu),
582 cmocka_unit_test(cvf_set_field_stream_id),
583 cmocka_unit_test(cvf_set_field_timestamp),
584 cmocka_unit_test(cvf_set_field_format),
585 cmocka_unit_test(cvf_set_field_format_subtype),
586 cmocka_unit_test(cvf_set_field_data_len),
587 cmocka_unit_test(cvf_set_field_m),
588 cmocka_unit_test(cvf_set_field_evt),
589 cmocka_unit_test(cvf_set_field_h264_ptv),
590 cmocka_unit_test(cvf_set_field_h264_timestamp),
591 cmocka_unit_test(cvf_pdu_init_null_pdu),
592 cmocka_unit_test(cvf_pdu_init_invalid_subtype),
593 cmocka_unit_test(cvf_pdu_init),
594 };
595
596 return cmocka_run_group_tests(tests, NULL, NULL);
597 }