blob: 2fbbd5a7ffe3c60a81e184cb066d8660b3df891d [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 {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060013 struct mctp *mctp;
14 struct mctp_binding_test *binding;
15 int rx_count;
16 mctp_eid_t src_eid;
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080017};
18
Patrick Williamsa721c2d2022-12-04 14:30:26 -060019static void test_rx(uint8_t eid, bool tag_owner __unused,
20 uint8_t msg_tag __unused, void *data, void *msg __unused,
21 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
Patrick Williamsa721c2d2022-12-04 14:30:26 -060032static void create_packet(struct mctp_hdr *pkt, mctp_eid_t src, mctp_eid_t dest)
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080033{
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
40int 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 {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060047 struct mctp_hdr hdr;
48 uint8_t payload[1];
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080049 } 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
Andrew Jeffery7c738012020-03-10 23:28:13 +103074 mctp_binding_test_destroy(ctx->binding);
75 mctp_destroy(ctx->mctp);
76
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080077 return EXIT_SUCCESS;
78}