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