Jeremy Kerr | 3d36ee2 | 2019-05-30 11:15:37 +0800 | [diff] [blame] | 1 | /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */ |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 2 | |
| 3 | #include <assert.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | |
Andrew Jeffery | 5ab7825 | 2022-02-17 21:04:59 +1030 | [diff] [blame] | 8 | #include "compiler.h" |
| 9 | #include "libmctp.h" |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 10 | #include "test-utils.h" |
| 11 | |
Younghyun Park | fcb65ab | 2022-07-07 00:17:07 +0000 | [diff] [blame] | 12 | #ifndef ARRAY_SIZE |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 13 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
Younghyun Park | fcb65ab | 2022-07-07 00:17:07 +0000 | [diff] [blame] | 14 | #endif |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 15 | |
| 16 | struct 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 Bhat | f39c385 | 2022-01-10 17:04:10 +0530 | [diff] [blame] | 24 | static void |
| 25 | test_rx(uint8_t eid __unused, bool tag_owner __unused, |
| 26 | uint8_t msg_tag __unused, void *data, void *msg, size_t len) |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 27 | { |
| 28 | struct test_ctx *ctx = data; |
| 29 | |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 30 | 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 | |
| 42 | struct 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 Kerr | 8ab4a6c | 2019-03-13 16:31:33 +0800 | [diff] [blame] | 57 | { |
| 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 Kerr | 6328ffb | 2019-03-13 16:34:41 +0800 | [diff] [blame] | 78 | { |
| 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 Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static 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 Jeffery | c68d96e | 2020-03-10 23:24:58 +1030 | [diff] [blame] | 134 | |
| 135 | mctp_binding_test_destroy(ctx->binding); |
| 136 | mctp_destroy(ctx->mctp); |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| 140 | int 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 | } |