| 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 <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_stream.h" |
| 36 |
|
| 37 |
static void stream_get_field_null_pdu(void **state) |
| 38 |
{ |
| 39 |
int res; |
| 40 |
uint64_t val; |
| 41 |
|
| 42 |
res = avtp_stream_pdu_get(NULL, AVTP_STREAM_FIELD_SV, &val); |
| 43 |
|
| 44 |
assert_int_equal(res, -EINVAL); |
| 45 |
} |
| 46 |
|
| 47 |
static void stream_get_field_null_val(void **state) |
| 48 |
{ |
| 49 |
int res; |
| 50 |
struct avtp_stream_pdu pdu = { 0 }; |
| 51 |
|
| 52 |
res = avtp_stream_pdu_get(&pdu, AVTP_STREAM_FIELD_SV, NULL); |
| 53 |
|
| 54 |
assert_int_equal(res, -EINVAL); |
| 55 |
} |
| 56 |
|
| 57 |
static void stream_get_field_invalid_field(void **state) |
| 58 |
{ |
| 59 |
int res; |
| 60 |
uint64_t val; |
| 61 |
struct avtp_stream_pdu pdu = { 0 }; |
| 62 |
|
| 63 |
res = avtp_stream_pdu_get(&pdu, AVTP_STREAM_FIELD_MAX, &val); |
| 64 |
|
| 65 |
assert_int_equal(res, -EINVAL); |
| 66 |
} |
| 67 |
|
| 68 |
static void stream_get_field_sv(void **state) |
| 69 |
{ |
| 70 |
int res; |
| 71 |
uint64_t val; |
| 72 |
struct avtp_stream_pdu pdu = { 0 }; |
| 73 |
|
| 74 |
/* stream_set 'sv' field to 1. */ |
| 75 |
pdu.subtype_data = htonl(0x00800000); |
| 76 |
|
| 77 |
res = avtp_stream_pdu_get(&pdu, AVTP_STREAM_FIELD_SV, &val); |
| 78 |
|
| 79 |
assert_int_equal(res, 0); |
| 80 |
assert_true(val == 1); |
| 81 |
} |
| 82 |
|
| 83 |
static void stream_get_field_mr(void **state) |
| 84 |
{ |
| 85 |
int res; |
| 86 |
uint64_t val; |
| 87 |
struct avtp_stream_pdu pdu = { 0 }; |
| 88 |
|
| 89 |
/* stream_set 'mr' field to 1. */ |
| 90 |
pdu.subtype_data = htonl(0x00080000); |
| 91 |
|
| 92 |
res = avtp_stream_pdu_get(&pdu, AVTP_STREAM_FIELD_MR, &val); |
| 93 |
|
| 94 |
assert_int_equal(res, 0); |
| 95 |
assert_true(val == 1); |
| 96 |
} |
| 97 |
|
| 98 |
static void stream_get_field_tv(void **state) |
| 99 |
{ |
| 100 |
int res; |
| 101 |
uint64_t val; |
| 102 |
struct avtp_stream_pdu pdu = { 0 }; |
| 103 |
|
| 104 |
/* stream_set 'tv' field to 1. */ |
| 105 |
pdu.subtype_data = htonl(0x00010000); |
| 106 |
|
| 107 |
res = avtp_stream_pdu_get(&pdu, AVTP_STREAM_FIELD_TV, &val); |
| 108 |
|
| 109 |
assert_int_equal(res, 0); |
| 110 |
assert_true(val == 1); |
| 111 |
} |
| 112 |
|
| 113 |
static void stream_get_field_seq_num(void **state) |
| 114 |
{ |
| 115 |
int res; |
| 116 |
uint64_t val; |
| 117 |
struct avtp_stream_pdu pdu = { 0 }; |
| 118 |
|
| 119 |
/* stream_set 'sequence_num' field to 0x55. */ |
| 120 |
pdu.subtype_data = htonl(0x00005500); |
| 121 |
|
| 122 |
res = avtp_stream_pdu_get(&pdu, AVTP_STREAM_FIELD_SEQ_NUM, &val); |
| 123 |
|
| 124 |
assert_int_equal(res, 0); |
| 125 |
assert_true(val == 0x55); |
| 126 |
} |
| 127 |
|
| 128 |
static void stream_get_field_tu(void **state) |
| 129 |
{ |
| 130 |
int res; |
| 131 |
uint64_t val; |
| 132 |
struct avtp_stream_pdu pdu = { 0 }; |
| 133 |
|
| 134 |
/* stream_set 'tu' field to 1. */ |
| 135 |
pdu.subtype_data = htonl(0x00000001); |
| 136 |
|
| 137 |
res = avtp_stream_pdu_get(&pdu, AVTP_STREAM_FIELD_TU, &val); |
| 138 |
|
| 139 |
assert_int_equal(res, 0); |
| 140 |
assert_true(val == 1); |
| 141 |
} |
| 142 |
|
| 143 |
static void stream_get_field_stream_id(void **state) |
| 144 |
{ |
| 145 |
int res; |
| 146 |
uint64_t val; |
| 147 |
struct avtp_stream_pdu pdu = { 0 }; |
| 148 |
|
| 149 |
/* stream_set 'stream_id' field to 0xAABBCCDDEEFF0001. */ |
| 150 |
pdu.stream_id = htobe64(0xAABBCCDDEEFF0001); |
| 151 |
|
| 152 |
res = avtp_stream_pdu_get(&pdu, AVTP_STREAM_FIELD_STREAM_ID, &val); |
| 153 |
|
| 154 |
assert_int_equal(res, 0); |
| 155 |
assert_true(val == 0xAABBCCDDEEFF0001); |
| 156 |
} |
| 157 |
|
| 158 |
static void stream_get_field_timestamp(void **state) |
| 159 |
{ |
| 160 |
int res; |
| 161 |
uint64_t val; |
| 162 |
struct avtp_stream_pdu pdu = { 0 }; |
| 163 |
|
| 164 |
/* stream_set 'avtp_timestamp' field to 0x80C0FFEE. */ |
| 165 |
pdu.avtp_time = htonl(0x80C0FFEE); |
| 166 |
|
| 167 |
res = avtp_stream_pdu_get(&pdu, AVTP_STREAM_FIELD_TIMESTAMP, &val); |
| 168 |
|
| 169 |
assert_int_equal(res, 0); |
| 170 |
assert_true(val == 0x80C0FFEE); |
| 171 |
} |
| 172 |
|
| 173 |
static void stream_get_field_data_len(void **state) |
| 174 |
{ |
| 175 |
int res; |
| 176 |
uint64_t val; |
| 177 |
struct avtp_stream_pdu pdu = { 0 }; |
| 178 |
|
| 179 |
/* stream_set 'stream_data_length' field to 0xAAAA. */ |
| 180 |
pdu.packet_info = htonl(0xAAAA0000); |
| 181 |
|
| 182 |
res = avtp_stream_pdu_get(&pdu, AVTP_STREAM_FIELD_STREAM_DATA_LEN, |
| 183 |
&val); |
| 184 |
|
| 185 |
assert_int_equal(res, 0); |
| 186 |
assert_true(val == 0xAAAA); |
| 187 |
} |
| 188 |
|
| 189 |
static void stream_set_field_null_pdu(void **state) |
| 190 |
{ |
| 191 |
int res; |
| 192 |
|
| 193 |
res = avtp_stream_pdu_set(NULL, AVTP_STREAM_FIELD_SV, 0); |
| 194 |
|
| 195 |
assert_int_equal(res, -EINVAL); |
| 196 |
} |
| 197 |
|
| 198 |
static void stream_set_field_invalid_field(void **state) |
| 199 |
{ |
| 200 |
int res; |
| 201 |
struct avtp_stream_pdu pdu = { 0 }; |
| 202 |
|
| 203 |
res = avtp_stream_pdu_set(&pdu, AVTP_STREAM_FIELD_MAX, 1); |
| 204 |
|
| 205 |
assert_int_equal(res, -EINVAL); |
| 206 |
} |
| 207 |
|
| 208 |
static void stream_set_field_sv(void **state) |
| 209 |
{ |
| 210 |
int res; |
| 211 |
struct avtp_stream_pdu pdu = { 0 }; |
| 212 |
|
| 213 |
res = avtp_stream_pdu_set(&pdu, AVTP_STREAM_FIELD_SV, 1); |
| 214 |
|
| 215 |
assert_int_equal(res, 0); |
| 216 |
assert_true(ntohl(pdu.subtype_data) == 0x00800000); |
| 217 |
assert_true(pdu.stream_id == 0); |
| 218 |
assert_true(pdu.avtp_time == 0); |
| 219 |
assert_true(pdu.format_specific == 0); |
| 220 |
assert_true(pdu.packet_info == 0); |
| 221 |
} |
| 222 |
|
| 223 |
static void stream_set_field_mr(void **state) |
| 224 |
{ |
| 225 |
int res; |
| 226 |
struct avtp_stream_pdu pdu = { 0 }; |
| 227 |
|
| 228 |
res = avtp_stream_pdu_set(&pdu, AVTP_STREAM_FIELD_MR, 1); |
| 229 |
|
| 230 |
assert_int_equal(res, 0); |
| 231 |
assert_true(ntohl(pdu.subtype_data) == 0x00080000); |
| 232 |
assert_true(pdu.stream_id == 0); |
| 233 |
assert_true(pdu.avtp_time == 0); |
| 234 |
assert_true(pdu.format_specific == 0); |
| 235 |
assert_true(pdu.packet_info == 0); |
| 236 |
} |
| 237 |
|
| 238 |
static void stream_set_field_tv(void **state) |
| 239 |
{ |
| 240 |
int res; |
| 241 |
struct avtp_stream_pdu pdu = { 0 }; |
| 242 |
|
| 243 |
res = avtp_stream_pdu_set(&pdu, AVTP_STREAM_FIELD_TV, 1); |
| 244 |
|
| 245 |
assert_int_equal(res, 0); |
| 246 |
assert_true(ntohl(pdu.subtype_data) == 0x00010000); |
| 247 |
assert_true(pdu.stream_id == 0); |
| 248 |
assert_true(pdu.avtp_time == 0); |
| 249 |
assert_true(pdu.format_specific == 0); |
| 250 |
assert_true(pdu.packet_info == 0); |
| 251 |
} |
| 252 |
|
| 253 |
static void stream_set_field_seq_num(void **state) |
| 254 |
{ |
| 255 |
int res; |
| 256 |
struct avtp_stream_pdu pdu = { 0 }; |
| 257 |
|
| 258 |
res = avtp_stream_pdu_set(&pdu, AVTP_STREAM_FIELD_SEQ_NUM, 0x55); |
| 259 |
|
| 260 |
assert_int_equal(res, 0); |
| 261 |
assert_true(ntohl(pdu.subtype_data) == 0x00005500); |
| 262 |
assert_true(pdu.stream_id == 0); |
| 263 |
assert_true(pdu.avtp_time == 0); |
| 264 |
assert_true(pdu.format_specific == 0); |
| 265 |
assert_true(pdu.packet_info == 0); |
| 266 |
} |
| 267 |
|
| 268 |
static void stream_set_field_tu(void **state) |
| 269 |
{ |
| 270 |
int res; |
| 271 |
struct avtp_stream_pdu pdu = { 0 }; |
| 272 |
|
| 273 |
res = avtp_stream_pdu_set(&pdu, AVTP_STREAM_FIELD_TU, 1); |
| 274 |
|
| 275 |
assert_int_equal(res, 0); |
| 276 |
assert_true(ntohl(pdu.subtype_data) == 0x00000001); |
| 277 |
assert_true(pdu.stream_id == 0); |
| 278 |
assert_true(pdu.avtp_time == 0); |
| 279 |
assert_true(pdu.format_specific == 0); |
| 280 |
assert_true(pdu.packet_info == 0); |
| 281 |
} |
| 282 |
|
| 283 |
static void stream_set_field_stream_id(void **state) |
| 284 |
{ |
| 285 |
int res; |
| 286 |
struct avtp_stream_pdu pdu = { 0 }; |
| 287 |
|
| 288 |
res = avtp_stream_pdu_set(&pdu, AVTP_STREAM_FIELD_STREAM_ID, |
| 289 |
0xAABBCCDDEEFF0001); |
| 290 |
|
| 291 |
assert_int_equal(res, 0); |
| 292 |
assert_true(be64toh(pdu.stream_id) == 0xAABBCCDDEEFF0001); |
| 293 |
assert_true(pdu.subtype_data == 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 stream_set_field_timestamp(void **state) |
| 300 |
{ |
| 301 |
int res; |
| 302 |
struct avtp_stream_pdu pdu = { 0 }; |
| 303 |
|
| 304 |
res = avtp_stream_pdu_set(&pdu, AVTP_STREAM_FIELD_TIMESTAMP, |
| 305 |
0x80C0FFEE); |
| 306 |
|
| 307 |
assert_int_equal(res, 0); |
| 308 |
assert_true(ntohl(pdu.avtp_time) == 0x80C0FFEE); |
| 309 |
assert_true(pdu.subtype_data == 0); |
| 310 |
assert_true(pdu.stream_id == 0); |
| 311 |
assert_true(pdu.format_specific == 0); |
| 312 |
assert_true(pdu.packet_info == 0); |
| 313 |
} |
| 314 |
|
| 315 |
static void stream_set_field_data_len(void **state) |
| 316 |
{ |
| 317 |
int res; |
| 318 |
struct avtp_stream_pdu pdu = { 0 }; |
| 319 |
|
| 320 |
res = avtp_stream_pdu_set(&pdu, AVTP_STREAM_FIELD_STREAM_DATA_LEN, |
| 321 |
0xAAAA); |
| 322 |
|
| 323 |
assert_int_equal(res, 0); |
| 324 |
assert_true(ntohl(pdu.packet_info) == 0xAAAA0000); |
| 325 |
assert_true(pdu.subtype_data == 0); |
| 326 |
assert_true(pdu.stream_id == 0); |
| 327 |
assert_true(pdu.avtp_time == 0); |
| 328 |
assert_true(pdu.format_specific == 0); |
| 329 |
} |
| 330 |
|
| 331 |
int main(void) |
| 332 |
{ |
| 333 |
const struct CMUnitTest tests[] = { |
| 334 |
cmocka_unit_test(stream_get_field_null_pdu), |
| 335 |
cmocka_unit_test(stream_get_field_null_val), |
| 336 |
cmocka_unit_test(stream_get_field_invalid_field), |
| 337 |
cmocka_unit_test(stream_get_field_sv), |
| 338 |
cmocka_unit_test(stream_get_field_mr), |
| 339 |
cmocka_unit_test(stream_get_field_tv), |
| 340 |
cmocka_unit_test(stream_get_field_seq_num), |
| 341 |
cmocka_unit_test(stream_get_field_tu), |
| 342 |
cmocka_unit_test(stream_get_field_stream_id), |
| 343 |
cmocka_unit_test(stream_get_field_timestamp), |
| 344 |
cmocka_unit_test(stream_get_field_data_len), |
| 345 |
cmocka_unit_test(stream_set_field_null_pdu), |
| 346 |
cmocka_unit_test(stream_set_field_invalid_field), |
| 347 |
cmocka_unit_test(stream_set_field_sv), |
| 348 |
cmocka_unit_test(stream_set_field_mr), |
| 349 |
cmocka_unit_test(stream_set_field_tv), |
| 350 |
cmocka_unit_test(stream_set_field_seq_num), |
| 351 |
cmocka_unit_test(stream_set_field_tu), |
| 352 |
cmocka_unit_test(stream_set_field_stream_id), |
| 353 |
cmocka_unit_test(stream_set_field_timestamp), |
| 354 |
cmocka_unit_test(stream_set_field_data_len), |
| 355 |
}; |
| 356 |
|
| 357 |
return cmocka_run_group_tests(tests, NULL, NULL); |
| 358 |
} |