blob: 13c5cb9578cafedaccf85ed79019460dabd50cfb [file] [log] [blame]
Jeremy Kerrba076942019-03-13 16:20:34 +08001
2#include <assert.h>
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6
7#include <libmctp.h>
8
9#include "test-utils.h"
10
11#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
12
13struct test_ctx {
14 struct mctp *mctp;
15 struct mctp_binding_test *binding;
16 int rx_count;
17 uint8_t rx_data[4];
18 size_t rx_len;
19};
20
21static void test_rx(uint8_t eid, void *data, void *msg, size_t len)
22{
23 struct test_ctx *ctx = data;
24
25 (void)eid;
26
27 ctx->rx_count++;
28
29 /* append incoming message data to the existing rx_data */
30 assert(len <= sizeof(ctx->rx_data));
31 assert(ctx->rx_len + len <= sizeof(ctx->rx_data));
32
33 memcpy(ctx->rx_data + ctx->rx_len, msg, len);
34 ctx->rx_len += len;
35}
36
37#define SEQ(x) (x << MCTP_HDR_SEQ_SHIFT)
38
39struct test {
40 int n_packets;
41 uint8_t flags_seq_tags[4];
42 int exp_rx_count;
43 size_t exp_rx_len;
44} tests[] = {
45 {
46 /* single packet */
47 .n_packets = 1,
48 .flags_seq_tags = {
49 SEQ(1) | MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM,
50 },
51 .exp_rx_count = 1,
52 .exp_rx_len = 1,
53 },
Jeremy Kerr8ab4a6c2019-03-13 16:31:33 +080054 {
55 /* two packets: one start, one end */
56 .n_packets = 2,
57 .flags_seq_tags = {
58 SEQ(1) | MCTP_HDR_FLAG_SOM,
59 SEQ(2) | MCTP_HDR_FLAG_EOM,
60 },
61 .exp_rx_count = 1,
62 .exp_rx_len = 2,
63 },
64 {
65 /* three packets: one start, one no flags, one end */
66 .n_packets = 3,
67 .flags_seq_tags = {
68 SEQ(1) | MCTP_HDR_FLAG_SOM,
69 SEQ(2),
70 SEQ(3) | MCTP_HDR_FLAG_EOM,
71 },
72 .exp_rx_count = 1,
73 .exp_rx_len = 3,
74 },
Jeremy Kerr6328ffb2019-03-13 16:34:41 +080075 {
76 /* two packets, wrapping sequence numbers */
77 .n_packets = 2,
78 .flags_seq_tags = {
79 SEQ(3) | MCTP_HDR_FLAG_SOM,
80 SEQ(0) | MCTP_HDR_FLAG_EOM,
81 },
82 .exp_rx_count = 1,
83 .exp_rx_len = 2,
84 },
85 {
86 /* two packets, invalid sequence number */
87 .n_packets = 2,
88 .flags_seq_tags = {
89 SEQ(1) | MCTP_HDR_FLAG_SOM,
90 SEQ(3) | MCTP_HDR_FLAG_EOM,
91 },
92 .exp_rx_count = 0,
93 .exp_rx_len = 0,
94 },
Jeremy Kerrba076942019-03-13 16:20:34 +080095};
96
97static void run_one_test(struct test_ctx *ctx, struct test *test)
98{
99 const mctp_eid_t local_eid = 8;
100 const mctp_eid_t remote_eid = 9;
101 struct {
102 struct mctp_hdr hdr;
103 uint8_t payload[1];
104 } pktbuf;
105 int i;
106
107 ctx->rx_count = 0;
108 ctx->rx_len = 0;
109
110 mctp_test_stack_init(&ctx->mctp, &ctx->binding, local_eid);
111
112 mctp_set_rx_all(ctx->mctp, test_rx, ctx);
113
114 for (i = 0; i < test->n_packets; i++) {
115 memset(&pktbuf, 0, sizeof(pktbuf));
116 pktbuf.hdr.dest = local_eid;
117 pktbuf.hdr.src = remote_eid;
118 pktbuf.hdr.flags_seq_tag = test->flags_seq_tags[i];
119 pktbuf.payload[0] = i;
120
121 mctp_binding_test_rx_raw(ctx->binding,
122 &pktbuf, sizeof(pktbuf));
123 }
124
125 assert(ctx->rx_count == test->exp_rx_count);
126 assert(ctx->rx_len == test->exp_rx_len);
127
128 /* ensure the payload data was reconstructed correctly */
129 for (i = 0; i < (int)ctx->rx_len; i++)
130 assert(ctx->rx_data[i] == i);
131}
132
133
134int main(void)
135{
136 struct test_ctx ctx;
137 unsigned int i;
138
139 for (i = 0; i < ARRAY_SIZE(tests); i++)
140 run_one_test(&ctx, &tests[i]);
141
142 return EXIT_SUCCESS;
143}