xref: /openbmc/libmctp/tests/test_eid.c (revision f39c3857)
1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2 
3 #include <assert.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 
8 #include "compiler.h"
9 #include "libmctp.h"
10 #include "test-utils.h"
11 
12 struct test_ctx {
13 	struct mctp			*mctp;
14 	struct mctp_binding_test	*binding;
15 	int				rx_count;
16 	mctp_eid_t			src_eid;
17 };
18 
19 static void
20 test_rx(uint8_t eid, bool tag_owner __unused, uint8_t msg_tag __unused,
21 	void *data, void *msg __unused, size_t len __unused)
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 
32 static 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 
41 int 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 
75 	mctp_binding_test_destroy(ctx->binding);
76 	mctp_destroy(ctx->mctp);
77 
78 	return EXIT_SUCCESS;
79 }
80