Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Jeremy Kerr | 4dc8f95 | 2019-03-13 15:55:06 +0800 | [diff] [blame] | 2 | |
| 3 | #include <assert.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | |
Andrew Jeffery | 5ab7825 | 2022-02-17 21:04:59 +1030 | [diff] [blame] | 8 | #include "compiler.h" |
| 9 | #include "libmctp.h" |
Jeremy Kerr | 4dc8f95 | 2019-03-13 15:55:06 +0800 | [diff] [blame] | 10 | #include "test-utils.h" |
| 11 | |
Jeremy Kerr | 4dc8f95 | 2019-03-13 15:55:06 +0800 | [diff] [blame] | 12 | struct test_ctx { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 13 | struct mctp *mctp; |
| 14 | struct mctp_binding_test *binding; |
| 15 | int rx_count; |
| 16 | mctp_eid_t src_eid; |
Jeremy Kerr | 4dc8f95 | 2019-03-13 15:55:06 +0800 | [diff] [blame] | 17 | }; |
| 18 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 19 | static void test_rx(uint8_t eid, bool tag_owner __unused, |
| 20 | uint8_t msg_tag __unused, void *data, void *msg __unused, |
| 21 | size_t len __unused) |
Jeremy Kerr | 4dc8f95 | 2019-03-13 15:55:06 +0800 | [diff] [blame] | 22 | { |
| 23 | struct test_ctx *ctx = data; |
| 24 | |
| 25 | (void)msg; |
| 26 | (void)len; |
| 27 | |
| 28 | ctx->rx_count++; |
| 29 | ctx->src_eid = eid; |
| 30 | } |
| 31 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 32 | static void create_packet(struct mctp_hdr *pkt, mctp_eid_t src, mctp_eid_t dest) |
Jeremy Kerr | 4dc8f95 | 2019-03-13 15:55:06 +0800 | [diff] [blame] | 33 | { |
| 34 | memset(pkt, 0, sizeof(*pkt)); |
| 35 | pkt->src = src; |
| 36 | pkt->dest = dest; |
| 37 | pkt->flags_seq_tag = MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM; |
| 38 | } |
| 39 | |
| 40 | int main(void) |
| 41 | { |
| 42 | struct test_ctx _ctx, *ctx = &_ctx; |
| 43 | const mctp_eid_t local_eid = 8; |
| 44 | const mctp_eid_t remote_eid = 9; |
| 45 | const mctp_eid_t other_eid = 10; |
| 46 | struct { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 47 | struct mctp_hdr hdr; |
| 48 | uint8_t payload[1]; |
Jeremy Kerr | 4dc8f95 | 2019-03-13 15:55:06 +0800 | [diff] [blame] | 49 | } pktbuf; |
| 50 | |
| 51 | mctp_test_stack_init(&ctx->mctp, &ctx->binding, local_eid); |
| 52 | |
| 53 | mctp_set_rx_all(ctx->mctp, test_rx, ctx); |
| 54 | |
| 55 | /* check a message addressed to us is received */ |
| 56 | ctx->rx_count = 0; |
| 57 | |
| 58 | create_packet(&pktbuf.hdr, remote_eid, local_eid); |
| 59 | |
| 60 | mctp_binding_test_rx_raw(ctx->binding, &pktbuf, sizeof(pktbuf)); |
| 61 | |
| 62 | assert(ctx->rx_count == 1); |
| 63 | assert(ctx->src_eid == remote_eid); |
| 64 | |
| 65 | /* check a message not addressed to us is not received */ |
| 66 | ctx->rx_count = 0; |
| 67 | |
| 68 | create_packet(&pktbuf.hdr, remote_eid, other_eid); |
| 69 | |
| 70 | mctp_binding_test_rx_raw(ctx->binding, &pktbuf, sizeof(pktbuf)); |
| 71 | |
| 72 | assert(ctx->rx_count == 0); |
| 73 | |
Andrew Jeffery | 7c73801 | 2020-03-10 23:28:13 +1030 | [diff] [blame] | 74 | mctp_binding_test_destroy(ctx->binding); |
| 75 | mctp_destroy(ctx->mctp); |
| 76 | |
Jeremy Kerr | 4dc8f95 | 2019-03-13 15:55:06 +0800 | [diff] [blame] | 77 | return EXIT_SUCCESS; |
| 78 | } |