xref: /openbmc/libmctp/tests/test_eid.c (revision 5ab78259)
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 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 
30 static 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 
39 int 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 
73 	mctp_binding_test_destroy(ctx->binding);
74 	mctp_destroy(ctx->mctp);
75 
76 	return EXIT_SUCCESS;
77 }
78