Jeremy Kerr | ba07694 | 2019-03-13 16:20:34 +0800 | [diff] [blame^] | 1 | |
| 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 | |
| 13 | struct 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 | |
| 21 | static 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 | |
| 39 | struct 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 | }, |
| 54 | }; |
| 55 | |
| 56 | static void run_one_test(struct test_ctx *ctx, struct test *test) |
| 57 | { |
| 58 | const mctp_eid_t local_eid = 8; |
| 59 | const mctp_eid_t remote_eid = 9; |
| 60 | struct { |
| 61 | struct mctp_hdr hdr; |
| 62 | uint8_t payload[1]; |
| 63 | } pktbuf; |
| 64 | int i; |
| 65 | |
| 66 | ctx->rx_count = 0; |
| 67 | ctx->rx_len = 0; |
| 68 | |
| 69 | mctp_test_stack_init(&ctx->mctp, &ctx->binding, local_eid); |
| 70 | |
| 71 | mctp_set_rx_all(ctx->mctp, test_rx, ctx); |
| 72 | |
| 73 | for (i = 0; i < test->n_packets; i++) { |
| 74 | memset(&pktbuf, 0, sizeof(pktbuf)); |
| 75 | pktbuf.hdr.dest = local_eid; |
| 76 | pktbuf.hdr.src = remote_eid; |
| 77 | pktbuf.hdr.flags_seq_tag = test->flags_seq_tags[i]; |
| 78 | pktbuf.payload[0] = i; |
| 79 | |
| 80 | mctp_binding_test_rx_raw(ctx->binding, |
| 81 | &pktbuf, sizeof(pktbuf)); |
| 82 | } |
| 83 | |
| 84 | assert(ctx->rx_count == test->exp_rx_count); |
| 85 | assert(ctx->rx_len == test->exp_rx_len); |
| 86 | |
| 87 | /* ensure the payload data was reconstructed correctly */ |
| 88 | for (i = 0; i < (int)ctx->rx_len; i++) |
| 89 | assert(ctx->rx_data[i] == i); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | int main(void) |
| 94 | { |
| 95 | struct test_ctx ctx; |
| 96 | unsigned int i; |
| 97 | |
| 98 | for (i = 0; i < ARRAY_SIZE(tests); i++) |
| 99 | run_one_test(&ctx, &tests[i]); |
| 100 | |
| 101 | return EXIT_SUCCESS; |
| 102 | } |