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