blob: 5c089addf5a0445bd2380ab9a233097856316c4b [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
Sumanth Bhatf39c3852022-01-10 17:04:10 +053019static void
20test_rx(uint8_t eid, bool tag_owner __unused, uint8_t msg_tag __unused,
21 void *data, void *msg __unused, size_t len __unused)
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080022{
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
32static void create_packet(struct mctp_hdr *pkt,
33 mctp_eid_t src, mctp_eid_t dest)
34{
35 memset(pkt, 0, sizeof(*pkt));
36 pkt->src = src;
37 pkt->dest = dest;
38 pkt->flags_seq_tag = MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM;
39}
40
41int main(void)
42{
43 struct test_ctx _ctx, *ctx = &_ctx;
44 const mctp_eid_t local_eid = 8;
45 const mctp_eid_t remote_eid = 9;
46 const mctp_eid_t other_eid = 10;
47 struct {
48 struct mctp_hdr hdr;
49 uint8_t payload[1];
50 } pktbuf;
51
52 mctp_test_stack_init(&ctx->mctp, &ctx->binding, local_eid);
53
54 mctp_set_rx_all(ctx->mctp, test_rx, ctx);
55
56 /* check a message addressed to us is received */
57 ctx->rx_count = 0;
58
59 create_packet(&pktbuf.hdr, remote_eid, local_eid);
60
61 mctp_binding_test_rx_raw(ctx->binding, &pktbuf, sizeof(pktbuf));
62
63 assert(ctx->rx_count == 1);
64 assert(ctx->src_eid == remote_eid);
65
66 /* check a message not addressed to us is not received */
67 ctx->rx_count = 0;
68
69 create_packet(&pktbuf.hdr, remote_eid, other_eid);
70
71 mctp_binding_test_rx_raw(ctx->binding, &pktbuf, sizeof(pktbuf));
72
73 assert(ctx->rx_count == 0);
74
Andrew Jeffery7c738012020-03-10 23:28:13 +103075 mctp_binding_test_destroy(ctx->binding);
76 mctp_destroy(ctx->mctp);
77
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080078 return EXIT_SUCCESS;
79}