Andrew Jeffery | efb4006 | 2023-11-10 13:48:39 +1030 | [diff] [blame] | 1 | #include <libpldm/transport.h> |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 2 | |
| 3 | #include "array.h" |
| 4 | #include "transport/test.h" |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 8 | TEST(Transport, create) |
| 9 | { |
| 10 | struct pldm_transport_test* test = NULL; |
| 11 | |
| 12 | EXPECT_EQ(pldm_transport_test_init(&test, NULL, 0), 0); |
| 13 | EXPECT_NE(pldm_transport_test_core(test), nullptr); |
| 14 | pldm_transport_test_destroy(test); |
| 15 | } |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 16 | |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 17 | TEST(Transport, send_one) |
| 18 | { |
| 19 | const uint8_t msg[] = {0x81, 0x00, 0x01, 0x01}; |
| 20 | const struct pldm_transport_test_descriptor seq[] = { |
| 21 | { |
| 22 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_SEND, |
| 23 | .send_msg = |
| 24 | { |
| 25 | .dst = 1, |
| 26 | .msg = msg, |
| 27 | .len = sizeof(msg), |
| 28 | }, |
| 29 | }, |
| 30 | }; |
| 31 | struct pldm_transport_test* test = NULL; |
| 32 | struct pldm_transport* ctx; |
| 33 | int rc; |
| 34 | |
| 35 | EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0); |
| 36 | ctx = pldm_transport_test_core(test); |
| 37 | rc = pldm_transport_send_msg(ctx, 1, msg, sizeof(msg)); |
| 38 | EXPECT_EQ(rc, PLDM_REQUESTER_SUCCESS); |
| 39 | pldm_transport_test_destroy(test); |
| 40 | } |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 41 | |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 42 | TEST(Transport, recv_one) |
| 43 | { |
| 44 | uint8_t msg[] = {0x01, 0x00, 0x01, 0x00}; |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 45 | const pldm_tid_t src_tid = 1; |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 46 | const struct pldm_transport_test_descriptor seq[] = { |
| 47 | { |
| 48 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV, |
| 49 | .recv_msg = |
| 50 | { |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 51 | .src = src_tid, |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 52 | .msg = msg, |
| 53 | .len = sizeof(msg), |
| 54 | }, |
| 55 | }, |
| 56 | }; |
| 57 | struct pldm_transport_test* test = NULL; |
| 58 | struct pldm_transport* ctx; |
| 59 | void* recvd; |
| 60 | size_t len; |
| 61 | int rc; |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 62 | pldm_tid_t tid; |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 63 | |
| 64 | EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0); |
| 65 | ctx = pldm_transport_test_core(test); |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 66 | rc = pldm_transport_recv_msg(ctx, &tid, &recvd, &len); |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 67 | EXPECT_EQ(rc, PLDM_REQUESTER_SUCCESS); |
| 68 | EXPECT_EQ(len, sizeof(msg)); |
| 69 | EXPECT_EQ(memcmp(recvd, msg, len), 0); |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 70 | EXPECT_EQ(tid, src_tid); |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 71 | free(recvd); |
| 72 | pldm_transport_test_destroy(test); |
| 73 | } |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 74 | |
Thu Nguyen | f56e4dc | 2023-07-24 10:39:47 +0700 | [diff] [blame] | 75 | TEST(Transport, send_recv_drain_one_unwanted) |
| 76 | { |
| 77 | uint8_t unwanted[] = {0x01, 0x00, 0x01, 0x01}; |
| 78 | uint8_t req[] = {0x81, 0x00, 0x01, 0x01}; |
| 79 | uint8_t resp[] = {0x01, 0x00, 0x01, 0x00}; |
| 80 | const struct pldm_transport_test_descriptor seq[] = { |
| 81 | { |
| 82 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV, |
| 83 | .recv_msg = |
| 84 | { |
| 85 | .src = 2, |
| 86 | .msg = unwanted, |
| 87 | .len = sizeof(unwanted), |
| 88 | }, |
| 89 | }, |
| 90 | { |
| 91 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_SEND, |
| 92 | .send_msg = |
| 93 | { |
| 94 | .dst = 1, |
| 95 | .msg = req, |
| 96 | .len = sizeof(req), |
| 97 | }, |
| 98 | }, |
| 99 | { |
| 100 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV, |
| 101 | .recv_msg = |
| 102 | { |
| 103 | .src = 1, |
| 104 | .msg = resp, |
| 105 | .len = sizeof(resp), |
| 106 | }, |
| 107 | }, |
| 108 | }; |
| 109 | struct pldm_transport_test* test = NULL; |
| 110 | struct pldm_transport* ctx; |
| 111 | size_t len; |
Andrew Jeffery | dc1edac | 2023-08-07 21:15:07 +0930 | [diff] [blame] | 112 | void* msg = NULL; |
Thu Nguyen | f56e4dc | 2023-07-24 10:39:47 +0700 | [diff] [blame] | 113 | int rc; |
| 114 | |
| 115 | EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0); |
| 116 | ctx = pldm_transport_test_core(test); |
| 117 | rc = pldm_transport_send_recv_msg(ctx, 1, req, sizeof(req), &msg, &len); |
Andrew Jeffery | dc1edac | 2023-08-07 21:15:07 +0930 | [diff] [blame] | 118 | ASSERT_EQ(rc, PLDM_REQUESTER_SUCCESS); |
Thu Nguyen | f56e4dc | 2023-07-24 10:39:47 +0700 | [diff] [blame] | 119 | EXPECT_NE(memcmp(msg, unwanted, len), 0); |
| 120 | EXPECT_EQ(memcmp(msg, resp, len), 0); |
| 121 | free(msg); |
| 122 | pldm_transport_test_destroy(test); |
| 123 | } |
Thu Nguyen | 16cd523 | 2023-08-23 06:47:19 +0700 | [diff] [blame] | 124 | |
Andrew Jeffery | 486d85d | 2023-08-31 11:12:08 +0930 | [diff] [blame] | 125 | TEST(Transport, send_recv_req_echo) |
| 126 | { |
| 127 | uint8_t req[] = {0x81, 0x00, 0x01, 0x01}; |
| 128 | uint8_t echo[] = {0x81, 0x00, 0x01, 0x01}; |
| 129 | uint8_t resp[] = {0x01, 0x00, 0x01, 0x00}; |
| 130 | const struct pldm_transport_test_descriptor seq[] = { |
| 131 | { |
| 132 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_SEND, |
| 133 | .send_msg = |
| 134 | { |
| 135 | .dst = 1, |
| 136 | .msg = req, |
| 137 | .len = sizeof(req), |
| 138 | }, |
| 139 | }, |
| 140 | { |
| 141 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV, |
| 142 | .recv_msg = |
| 143 | { |
| 144 | .src = 1, |
| 145 | .msg = echo, |
| 146 | .len = sizeof(echo), |
| 147 | }, |
| 148 | }, |
| 149 | { |
| 150 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV, |
| 151 | .recv_msg = |
| 152 | { |
| 153 | .src = 1, |
| 154 | .msg = resp, |
| 155 | .len = sizeof(resp), |
| 156 | }, |
| 157 | }, |
| 158 | }; |
| 159 | struct pldm_transport_test* test = NULL; |
| 160 | struct pldm_transport* ctx; |
| 161 | size_t len; |
| 162 | void* msg; |
| 163 | int rc; |
| 164 | |
| 165 | EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0); |
| 166 | ctx = pldm_transport_test_core(test); |
| 167 | rc = pldm_transport_send_recv_msg(ctx, 1, req, sizeof(req), &msg, &len); |
| 168 | ASSERT_EQ(rc, PLDM_REQUESTER_SUCCESS); |
| 169 | EXPECT_NE(memcmp(msg, echo, len), 0); |
| 170 | EXPECT_EQ(memcmp(msg, resp, len), 0); |
| 171 | free(msg); |
| 172 | pldm_transport_test_destroy(test); |
| 173 | } |