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 | |
Rashmica Gupta | e915aad | 2022-08-10 16:28:59 +1000 | [diff] [blame] | 3 | #ifdef NDEBUG |
| 4 | #undef NDEBUG |
| 5 | #endif |
| 6 | |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <stdio.h> |
| 10 | #include <string.h> |
| 11 | |
Andrew Jeffery | 5ab7825 | 2022-02-17 21:04:59 +1030 | [diff] [blame] | 12 | #include "compiler.h" |
| 13 | #include "libmctp.h" |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 14 | #include "test-utils.h" |
| 15 | |
Younghyun Park | fcb65ab | 2022-07-07 00:17:07 +0000 | [diff] [blame] | 16 | #ifndef ARRAY_SIZE |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 17 | #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0])) |
Younghyun Park | fcb65ab | 2022-07-07 00:17:07 +0000 | [diff] [blame] | 18 | #endif |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 19 | |
| 20 | struct test_ctx { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 21 | struct mctp *mctp; |
| 22 | struct mctp_binding_test *binding; |
| 23 | int rx_count; |
| 24 | uint8_t rx_data[4]; |
| 25 | size_t rx_len; |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 26 | }; |
| 27 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 28 | static void test_rx(uint8_t eid __unused, bool tag_owner __unused, |
| 29 | uint8_t msg_tag __unused, void *data, void *msg, size_t len) |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 30 | { |
| 31 | struct test_ctx *ctx = data; |
| 32 | |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 33 | ctx->rx_count++; |
| 34 | |
| 35 | /* append incoming message data to the existing rx_data */ |
| 36 | assert(len <= sizeof(ctx->rx_data)); |
| 37 | assert(ctx->rx_len + len <= sizeof(ctx->rx_data)); |
| 38 | |
| 39 | memcpy(ctx->rx_data + ctx->rx_len, msg, len); |
| 40 | ctx->rx_len += len; |
| 41 | } |
| 42 | |
| 43 | #define SEQ(x) (x << MCTP_HDR_SEQ_SHIFT) |
| 44 | |
| 45 | struct test { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 46 | int n_packets; |
| 47 | uint8_t flags_seq_tags[4]; |
| 48 | int exp_rx_count; |
| 49 | size_t exp_rx_len; |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 50 | } tests[] = { |
| 51 | { |
| 52 | /* single packet */ |
| 53 | .n_packets = 1, |
| 54 | .flags_seq_tags = { |
| 55 | SEQ(1) | MCTP_HDR_FLAG_SOM | MCTP_HDR_FLAG_EOM, |
| 56 | }, |
| 57 | .exp_rx_count = 1, |
| 58 | .exp_rx_len = 1, |
| 59 | }, |
Jeremy Kerr | 8ab4a6c | 2019-03-13 16:31:33 +0800 | [diff] [blame] | 60 | { |
| 61 | /* two packets: one start, one end */ |
| 62 | .n_packets = 2, |
| 63 | .flags_seq_tags = { |
| 64 | SEQ(1) | MCTP_HDR_FLAG_SOM, |
| 65 | SEQ(2) | MCTP_HDR_FLAG_EOM, |
| 66 | }, |
| 67 | .exp_rx_count = 1, |
| 68 | .exp_rx_len = 2, |
| 69 | }, |
| 70 | { |
| 71 | /* three packets: one start, one no flags, one end */ |
| 72 | .n_packets = 3, |
| 73 | .flags_seq_tags = { |
| 74 | SEQ(1) | MCTP_HDR_FLAG_SOM, |
| 75 | SEQ(2), |
| 76 | SEQ(3) | MCTP_HDR_FLAG_EOM, |
| 77 | }, |
| 78 | .exp_rx_count = 1, |
| 79 | .exp_rx_len = 3, |
| 80 | }, |
Jeremy Kerr | 6328ffb | 2019-03-13 16:34:41 +0800 | [diff] [blame] | 81 | { |
| 82 | /* two packets, wrapping sequence numbers */ |
| 83 | .n_packets = 2, |
| 84 | .flags_seq_tags = { |
| 85 | SEQ(3) | MCTP_HDR_FLAG_SOM, |
| 86 | SEQ(0) | MCTP_HDR_FLAG_EOM, |
| 87 | }, |
| 88 | .exp_rx_count = 1, |
| 89 | .exp_rx_len = 2, |
| 90 | }, |
| 91 | { |
| 92 | /* two packets, invalid sequence number */ |
| 93 | .n_packets = 2, |
| 94 | .flags_seq_tags = { |
| 95 | SEQ(1) | MCTP_HDR_FLAG_SOM, |
| 96 | SEQ(3) | MCTP_HDR_FLAG_EOM, |
| 97 | }, |
| 98 | .exp_rx_count = 0, |
| 99 | .exp_rx_len = 0, |
| 100 | }, |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static void run_one_test(struct test_ctx *ctx, struct test *test) |
| 104 | { |
| 105 | const mctp_eid_t local_eid = 8; |
| 106 | const mctp_eid_t remote_eid = 9; |
| 107 | struct { |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 108 | struct mctp_hdr hdr; |
| 109 | uint8_t payload[1]; |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 110 | } pktbuf; |
| 111 | int i; |
| 112 | |
| 113 | ctx->rx_count = 0; |
| 114 | ctx->rx_len = 0; |
| 115 | |
| 116 | mctp_test_stack_init(&ctx->mctp, &ctx->binding, local_eid); |
| 117 | |
| 118 | mctp_set_rx_all(ctx->mctp, test_rx, ctx); |
| 119 | |
| 120 | for (i = 0; i < test->n_packets; i++) { |
| 121 | memset(&pktbuf, 0, sizeof(pktbuf)); |
| 122 | pktbuf.hdr.dest = local_eid; |
| 123 | pktbuf.hdr.src = remote_eid; |
| 124 | pktbuf.hdr.flags_seq_tag = test->flags_seq_tags[i]; |
| 125 | pktbuf.payload[0] = i; |
| 126 | |
Patrick Williams | a721c2d | 2022-12-04 14:30:26 -0600 | [diff] [blame] | 127 | mctp_binding_test_rx_raw(ctx->binding, &pktbuf, sizeof(pktbuf)); |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | assert(ctx->rx_count == test->exp_rx_count); |
| 131 | assert(ctx->rx_len == test->exp_rx_len); |
| 132 | |
| 133 | /* ensure the payload data was reconstructed correctly */ |
| 134 | for (i = 0; i < (int)ctx->rx_len; i++) |
| 135 | assert(ctx->rx_data[i] == i); |
Andrew Jeffery | c68d96e | 2020-03-10 23:24:58 +1030 | [diff] [blame] | 136 | |
| 137 | mctp_binding_test_destroy(ctx->binding); |
| 138 | mctp_destroy(ctx->mctp); |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 139 | } |
| 140 | |
Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame] | 141 | int main(void) |
| 142 | { |
| 143 | struct test_ctx ctx; |
| 144 | unsigned int i; |
| 145 | |
| 146 | for (i = 0; i < ARRAY_SIZE(tests); i++) |
| 147 | run_one_test(&ctx, &tests[i]); |
| 148 | |
| 149 | return EXIT_SUCCESS; |
| 150 | } |