| 1 |
/* |
| 2 |
* Copyright (c) 2017, 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 |
|
| 36 |
static void get_field_null_pdu(void **state) |
| 37 |
{ |
| 38 |
int res; |
| 39 |
uint32_t val = AVTP_SUBTYPE_MAAP; |
| 40 |
|
| 41 |
res = avtp_pdu_get(NULL, AVTP_FIELD_SUBTYPE, &val); |
| 42 |
|
| 43 |
assert_int_equal(res, -EINVAL); |
| 44 |
} |
| 45 |
|
| 46 |
static void get_field_null_val(void **state) |
| 47 |
{ |
| 48 |
int res; |
| 49 |
struct avtp_common_pdu pdu = { 0 }; |
| 50 |
|
| 51 |
res = avtp_pdu_get(&pdu, AVTP_FIELD_SUBTYPE, NULL); |
| 52 |
|
| 53 |
assert_int_equal(res, -EINVAL); |
| 54 |
} |
| 55 |
|
| 56 |
static void get_field_invalid_field(void **state) |
| 57 |
{ |
| 58 |
int res; |
| 59 |
uint32_t val = AVTP_SUBTYPE_MAAP; |
| 60 |
struct avtp_common_pdu pdu = { 0 }; |
| 61 |
|
| 62 |
res = avtp_pdu_get(&pdu, AVTP_FIELD_MAX, &val); |
| 63 |
|
| 64 |
assert_int_equal(res, -EINVAL); |
| 65 |
} |
| 66 |
|
| 67 |
static void get_field_subtype(void **state) |
| 68 |
{ |
| 69 |
int res; |
| 70 |
uint32_t val; |
| 71 |
struct avtp_common_pdu pdu = { 0 }; |
| 72 |
|
| 73 |
/* Set 'subtype' field to 0xFE (AVTP_SUBTYPE_MAAP). */ |
| 74 |
pdu.subtype_data = htonl(0xFE000000); |
| 75 |
|
| 76 |
res = avtp_pdu_get(&pdu, AVTP_FIELD_SUBTYPE, &val); |
| 77 |
|
| 78 |
assert_int_equal(res, 0); |
| 79 |
assert_true(val == AVTP_SUBTYPE_MAAP); |
| 80 |
} |
| 81 |
|
| 82 |
static void get_field_version(void **state) |
| 83 |
{ |
| 84 |
int res; |
| 85 |
uint32_t val; |
| 86 |
struct avtp_common_pdu pdu = { 0 }; |
| 87 |
|
| 88 |
/* Set 'version' field to 5. */ |
| 89 |
pdu.subtype_data = htonl(0x00500000); |
| 90 |
|
| 91 |
res = avtp_pdu_get(&pdu, AVTP_FIELD_VERSION, &val); |
| 92 |
|
| 93 |
assert_int_equal(res, 0); |
| 94 |
assert_true(val == 5); |
| 95 |
} |
| 96 |
|
| 97 |
static void set_field_null_pdu(void **state) |
| 98 |
{ |
| 99 |
int res; |
| 100 |
|
| 101 |
res = avtp_pdu_set(NULL, AVTP_FIELD_SUBTYPE, AVTP_SUBTYPE_MAAP); |
| 102 |
|
| 103 |
assert_int_equal(res, -EINVAL); |
| 104 |
} |
| 105 |
|
| 106 |
static void set_field_invalid_field(void **state) |
| 107 |
{ |
| 108 |
int res; |
| 109 |
struct avtp_common_pdu pdu = { 0 }; |
| 110 |
|
| 111 |
res = avtp_pdu_set(&pdu, AVTP_FIELD_MAX, 1); |
| 112 |
|
| 113 |
assert_int_equal(res, -EINVAL); |
| 114 |
} |
| 115 |
|
| 116 |
static void set_field_subtype(void **state) |
| 117 |
{ |
| 118 |
int res; |
| 119 |
struct avtp_common_pdu pdu = { 0 }; |
| 120 |
|
| 121 |
res = avtp_pdu_set(&pdu, AVTP_FIELD_SUBTYPE, AVTP_SUBTYPE_MAAP); |
| 122 |
|
| 123 |
assert_int_equal(res, 0); |
| 124 |
assert_true(ntohl(pdu.subtype_data) == 0xFE000000); |
| 125 |
} |
| 126 |
|
| 127 |
static void set_field_version(void **state) |
| 128 |
{ |
| 129 |
int res; |
| 130 |
struct avtp_common_pdu pdu = { 0 }; |
| 131 |
|
| 132 |
res = avtp_pdu_set(&pdu, AVTP_FIELD_VERSION, 5); |
| 133 |
|
| 134 |
assert_int_equal(res, 0); |
| 135 |
assert_true(ntohl(pdu.subtype_data) == 0x00500000); |
| 136 |
} |
| 137 |
|
| 138 |
int main(void) |
| 139 |
{ |
| 140 |
const struct CMUnitTest tests[] = { |
| 141 |
cmocka_unit_test(get_field_null_pdu), |
| 142 |
cmocka_unit_test(get_field_null_val), |
| 143 |
cmocka_unit_test(get_field_invalid_field), |
| 144 |
cmocka_unit_test(get_field_subtype), |
| 145 |
cmocka_unit_test(get_field_version), |
| 146 |
cmocka_unit_test(set_field_null_pdu), |
| 147 |
cmocka_unit_test(set_field_invalid_field), |
| 148 |
cmocka_unit_test(set_field_subtype), |
| 149 |
cmocka_unit_test(set_field_version), |
| 150 |
}; |
| 151 |
|
| 152 |
return cmocka_run_group_tests(tests, NULL, NULL); |
| 153 |
} |