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