blob: 4687cb8cd2fa7382c11fc8da9e1a910f524dc9ac [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
Rashmica Guptae915aad2022-08-10 16:28:59 +10003#ifdef NDEBUG
4#undef NDEBUG
5#endif
6
Jeremy Kerr4dc8f952019-03-13 15:55:06 +08007#include <assert.h>
8#include <stdlib.h>
9#include <stdio.h>
10#include <string.h>
11
Andrew Jeffery5ab78252022-02-17 21:04:59 +103012#include "compiler.h"
13#include "libmctp.h"
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080014#include "test-utils.h"
15
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080016struct test_ctx {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060017 struct mctp *mctp;
18 struct mctp_binding_test *binding;
19 int rx_count;
20 mctp_eid_t src_eid;
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080021};
22
Patrick Williamsa721c2d2022-12-04 14:30:26 -060023static 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)
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080026{
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
Patrick Williamsa721c2d2022-12-04 14:30:26 -060036static void create_packet(struct mctp_hdr *pkt, mctp_eid_t src, mctp_eid_t dest)
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080037{
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
44int 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 {
Patrick Williamsa721c2d2022-12-04 14:30:26 -060051 struct mctp_hdr hdr;
52 uint8_t payload[1];
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080053 } 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
Andrew Jeffery7c738012020-03-10 23:28:13 +103078 mctp_binding_test_destroy(ctx->binding);
79 mctp_destroy(ctx->mctp);
80
Jeremy Kerr4dc8f952019-03-13 15:55:06 +080081 return EXIT_SUCCESS;
82}