blob: e1284574c3fe306e276efd321cdf7b98855d2683 [file] [log] [blame]
Jeremy Kerr3d36ee22019-05-30 11:15:37 +08001/* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
Jeremy Kerrba076942019-03-13 16:20:34 +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 Kerrba076942019-03-13 16:20:34 +080010#include "test-utils.h"
11
Younghyun Parkfcb65ab2022-07-07 00:17:07 +000012#ifndef ARRAY_SIZE
Jeremy Kerrba076942019-03-13 16:20:34 +080013#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
Younghyun Parkfcb65ab2022-07-07 00:17:07 +000014#endif
Jeremy Kerrba076942019-03-13 16:20:34 +080015
16struct test_ctx {
17 struct mctp *mctp;
18 struct mctp_binding_test *binding;
19 int rx_count;
20 uint8_t rx_data[4];
21 size_t rx_len;
22};
23
Sumanth Bhatf39c3852022-01-10 17:04:10 +053024static void
25test_rx(uint8_t eid __unused, bool tag_owner __unused,
26 uint8_t msg_tag __unused, void *data, void *msg, size_t len)
Jeremy Kerrba076942019-03-13 16:20:34 +080027{
28 struct test_ctx *ctx = data;
29
Jeremy Kerrba076942019-03-13 16:20:34 +080030 ctx->rx_count++;
31
32 /* append incoming message data to the existing rx_data */
33 assert(len <= sizeof(ctx->rx_data));
34 assert(ctx->rx_len + len <= sizeof(ctx->rx_data));
35
36 memcpy(ctx->rx_data + ctx->rx_len, msg, len);
37 ctx->rx_len += len;
38}
39
40#define SEQ(x) (x << MCTP_HDR_SEQ_SHIFT)
41
42struct test {
43 int n_packets;
44 uint8_t flags_seq_tags[4];
45 int exp_rx_count;
46 size_t exp_rx_len;
47} tests[] = {
48 {
49 /* single packet */
50 .n_packets = 1,
51 .flags_seq_tags = {
52 SEQ(1) | MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM,
53 },
54 .exp_rx_count = 1,
55 .exp_rx_len = 1,
56 },
Jeremy Kerr8ab4a6c2019-03-13 16:31:33 +080057 {
58 /* two packets: one start, one end */
59 .n_packets = 2,
60 .flags_seq_tags = {
61 SEQ(1) | MCTP_HDR_FLAG_SOM,
62 SEQ(2) | MCTP_HDR_FLAG_EOM,
63 },
64 .exp_rx_count = 1,
65 .exp_rx_len = 2,
66 },
67 {
68 /* three packets: one start, one no flags, one end */
69 .n_packets = 3,
70 .flags_seq_tags = {
71 SEQ(1) | MCTP_HDR_FLAG_SOM,
72 SEQ(2),
73 SEQ(3) | MCTP_HDR_FLAG_EOM,
74 },
75 .exp_rx_count = 1,
76 .exp_rx_len = 3,
77 },
Jeremy Kerr6328ffb2019-03-13 16:34:41 +080078 {
79 /* two packets, wrapping sequence numbers */
80 .n_packets = 2,
81 .flags_seq_tags = {
82 SEQ(3) | MCTP_HDR_FLAG_SOM,
83 SEQ(0) | MCTP_HDR_FLAG_EOM,
84 },
85 .exp_rx_count = 1,
86 .exp_rx_len = 2,
87 },
88 {
89 /* two packets, invalid sequence number */
90 .n_packets = 2,
91 .flags_seq_tags = {
92 SEQ(1) | MCTP_HDR_FLAG_SOM,
93 SEQ(3) | MCTP_HDR_FLAG_EOM,
94 },
95 .exp_rx_count = 0,
96 .exp_rx_len = 0,
97 },
Jeremy Kerrba076942019-03-13 16:20:34 +080098};
99
100static void run_one_test(struct test_ctx *ctx, struct test *test)
101{
102 const mctp_eid_t local_eid = 8;
103 const mctp_eid_t remote_eid = 9;
104 struct {
105 struct mctp_hdr hdr;
106 uint8_t payload[1];
107 } pktbuf;
108 int i;
109
110 ctx->rx_count = 0;
111 ctx->rx_len = 0;
112
113 mctp_test_stack_init(&ctx->mctp, &ctx->binding, local_eid);
114
115 mctp_set_rx_all(ctx->mctp, test_rx, ctx);
116
117 for (i = 0; i < test->n_packets; i++) {
118 memset(&pktbuf, 0, sizeof(pktbuf));
119 pktbuf.hdr.dest = local_eid;
120 pktbuf.hdr.src = remote_eid;
121 pktbuf.hdr.flags_seq_tag = test->flags_seq_tags[i];
122 pktbuf.payload[0] = i;
123
124 mctp_binding_test_rx_raw(ctx->binding,
125 &pktbuf, sizeof(pktbuf));
126 }
127
128 assert(ctx->rx_count == test->exp_rx_count);
129 assert(ctx->rx_len == test->exp_rx_len);
130
131 /* ensure the payload data was reconstructed correctly */
132 for (i = 0; i < (int)ctx->rx_len; i++)
133 assert(ctx->rx_data[i] == i);
Andrew Jefferyc68d96e2020-03-10 23:24:58 +1030134
135 mctp_binding_test_destroy(ctx->binding);
136 mctp_destroy(ctx->mctp);
Jeremy Kerrba076942019-03-13 16:20:34 +0800137}
138
139
140int main(void)
141{
142 struct test_ctx ctx;
143 unsigned int i;
144
145 for (i = 0; i < ARRAY_SIZE(tests); i++)
146 run_one_test(&ctx, &tests[i]);
147
148 return EXIT_SUCCESS;
149}