Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 1 | #include "libpldm/transport.h" |
| 2 | |
| 3 | #include "array.h" |
| 4 | #include "transport/test.h" |
| 5 | |
| 6 | #include <gtest/gtest.h> |
| 7 | |
| 8 | #ifdef LIBPLDM_API_TESTING |
| 9 | TEST(Transport, create) |
| 10 | { |
| 11 | struct pldm_transport_test* test = NULL; |
| 12 | |
| 13 | EXPECT_EQ(pldm_transport_test_init(&test, NULL, 0), 0); |
| 14 | EXPECT_NE(pldm_transport_test_core(test), nullptr); |
| 15 | pldm_transport_test_destroy(test); |
| 16 | } |
| 17 | #endif |
| 18 | |
| 19 | #ifdef LIBPLDM_API_TESTING |
| 20 | TEST(Transport, send_one) |
| 21 | { |
| 22 | const uint8_t msg[] = {0x81, 0x00, 0x01, 0x01}; |
| 23 | const struct pldm_transport_test_descriptor seq[] = { |
| 24 | { |
| 25 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_SEND, |
| 26 | .send_msg = |
| 27 | { |
| 28 | .dst = 1, |
| 29 | .msg = msg, |
| 30 | .len = sizeof(msg), |
| 31 | }, |
| 32 | }, |
| 33 | }; |
| 34 | struct pldm_transport_test* test = NULL; |
| 35 | struct pldm_transport* ctx; |
| 36 | int rc; |
| 37 | |
| 38 | EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0); |
| 39 | ctx = pldm_transport_test_core(test); |
| 40 | rc = pldm_transport_send_msg(ctx, 1, msg, sizeof(msg)); |
| 41 | EXPECT_EQ(rc, PLDM_REQUESTER_SUCCESS); |
| 42 | pldm_transport_test_destroy(test); |
| 43 | } |
| 44 | #endif |
| 45 | |
| 46 | #ifdef LIBPLDM_API_TESTING |
| 47 | TEST(Transport, recv_one) |
| 48 | { |
| 49 | uint8_t msg[] = {0x01, 0x00, 0x01, 0x00}; |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 50 | const pldm_tid_t src_tid = 1; |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 51 | const struct pldm_transport_test_descriptor seq[] = { |
| 52 | { |
| 53 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV, |
| 54 | .recv_msg = |
| 55 | { |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 56 | .src = src_tid, |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 57 | .msg = msg, |
| 58 | .len = sizeof(msg), |
| 59 | }, |
| 60 | }, |
| 61 | }; |
| 62 | struct pldm_transport_test* test = NULL; |
| 63 | struct pldm_transport* ctx; |
| 64 | void* recvd; |
| 65 | size_t len; |
| 66 | int rc; |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 67 | pldm_tid_t tid; |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 68 | |
| 69 | EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0); |
| 70 | ctx = pldm_transport_test_core(test); |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 71 | rc = pldm_transport_recv_msg(ctx, &tid, &recvd, &len); |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 72 | EXPECT_EQ(rc, PLDM_REQUESTER_SUCCESS); |
| 73 | EXPECT_EQ(len, sizeof(msg)); |
| 74 | EXPECT_EQ(memcmp(recvd, msg, len), 0); |
Rashmica Gupta | 2457629 | 2023-07-31 14:02:41 +1000 | [diff] [blame] | 75 | EXPECT_EQ(tid, src_tid); |
Andrew Jeffery | 9a6ba89 | 2023-07-18 22:22:14 +0930 | [diff] [blame] | 76 | free(recvd); |
| 77 | pldm_transport_test_destroy(test); |
| 78 | } |
| 79 | #endif |
| 80 | |
| 81 | #ifdef LIBPLDM_API_TESTING |
| 82 | TEST(Transport, send_recv_one) |
| 83 | { |
| 84 | uint8_t req[] = {0x81, 0x00, 0x01, 0x01}; |
| 85 | uint8_t resp[] = {0x01, 0x00, 0x01, 0x00}; |
| 86 | const struct pldm_transport_test_descriptor seq[] = { |
| 87 | { |
| 88 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_SEND, |
| 89 | .send_msg = |
| 90 | { |
| 91 | .dst = 1, |
| 92 | .msg = req, |
| 93 | .len = sizeof(req), |
| 94 | }, |
| 95 | }, |
| 96 | { |
| 97 | .type = PLDM_TRANSPORT_TEST_ELEMENT_LATENCY, |
| 98 | .latency = |
| 99 | { |
| 100 | .it_interval = {0, 0}, |
| 101 | .it_value = {1, 0}, |
| 102 | }, |
| 103 | }, |
| 104 | { |
| 105 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV, |
| 106 | .recv_msg = |
| 107 | { |
| 108 | .src = 1, |
| 109 | .msg = resp, |
| 110 | .len = sizeof(resp), |
| 111 | }, |
| 112 | }, |
| 113 | }; |
| 114 | struct pldm_transport_test* test = NULL; |
| 115 | struct pldm_transport* ctx; |
| 116 | size_t len; |
| 117 | void* msg; |
| 118 | int rc; |
| 119 | |
| 120 | EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0); |
| 121 | ctx = pldm_transport_test_core(test); |
| 122 | rc = pldm_transport_send_recv_msg(ctx, 1, req, sizeof(req), &msg, &len); |
| 123 | EXPECT_EQ(rc, PLDM_REQUESTER_SUCCESS); |
| 124 | EXPECT_EQ(len, sizeof(resp)); |
| 125 | EXPECT_EQ(memcmp(msg, resp, len), 0); |
| 126 | free(msg); |
| 127 | pldm_transport_test_destroy(test); |
| 128 | } |
| 129 | #endif |
| 130 | |
| 131 | #ifdef LIBPLDM_API_TESTING |
| 132 | TEST(Transport, send_recv_timeout) |
| 133 | { |
| 134 | uint8_t req[] = {0x81, 0x00, 0x01, 0x01}; |
| 135 | const struct pldm_transport_test_descriptor seq[] = { |
| 136 | { |
| 137 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_SEND, |
| 138 | .send_msg = |
| 139 | { |
| 140 | .dst = 1, |
| 141 | .msg = req, |
| 142 | .len = sizeof(req), |
| 143 | }, |
| 144 | }, |
| 145 | { |
| 146 | .type = PLDM_TRANSPORT_TEST_ELEMENT_LATENCY, |
| 147 | .latency = |
| 148 | { |
| 149 | .it_interval = {0, 0}, |
| 150 | .it_value = {5, 0}, |
| 151 | }, |
| 152 | }, |
| 153 | }; |
| 154 | struct pldm_transport_test* test = NULL; |
| 155 | struct pldm_transport* ctx; |
| 156 | size_t len; |
| 157 | void* msg; |
| 158 | int rc; |
| 159 | |
| 160 | EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0); |
| 161 | ctx = pldm_transport_test_core(test); |
| 162 | rc = pldm_transport_send_recv_msg(ctx, 1, req, sizeof(req), &msg, &len); |
| 163 | EXPECT_EQ(rc, PLDM_REQUESTER_RECV_FAIL); |
| 164 | pldm_transport_test_destroy(test); |
| 165 | } |
| 166 | #endif |
Thu Nguyen | ef48506 | 2023-07-14 23:36:21 +0700 | [diff] [blame] | 167 | |
| 168 | #ifdef LIBPLDM_API_TESTING |
| 169 | TEST(Transport, send_recv_unwanted) |
| 170 | { |
| 171 | uint8_t req[] = {0x81, 0x00, 0x01, 0x01}; |
| 172 | uint8_t resp[] = {0x02, 0x00, 0x01, 0x00}; |
| 173 | const struct pldm_transport_test_descriptor seq[] = { |
| 174 | { |
| 175 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_SEND, |
| 176 | .send_msg = |
| 177 | { |
| 178 | .dst = 1, |
| 179 | .msg = req, |
| 180 | .len = sizeof(req), |
| 181 | }, |
| 182 | }, |
| 183 | { |
| 184 | .type = PLDM_TRANSPORT_TEST_ELEMENT_LATENCY, |
| 185 | .latency = |
| 186 | { |
| 187 | .it_interval = {0, 0}, |
| 188 | .it_value = {1, 0}, |
| 189 | }, |
| 190 | }, |
| 191 | { |
| 192 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV, |
| 193 | .recv_msg = |
| 194 | { |
| 195 | .src = 2, |
| 196 | .msg = resp, |
| 197 | .len = sizeof(resp), |
| 198 | }, |
| 199 | }, |
| 200 | { |
| 201 | .type = PLDM_TRANSPORT_TEST_ELEMENT_LATENCY, |
| 202 | .latency = |
| 203 | { |
| 204 | .it_interval = {0, 0}, |
| 205 | .it_value = {4, 0}, |
| 206 | }, |
| 207 | }, |
| 208 | }; |
| 209 | struct pldm_transport_test* test = NULL; |
| 210 | struct pldm_transport* ctx; |
| 211 | size_t len; |
| 212 | void* msg; |
| 213 | int rc; |
| 214 | |
| 215 | EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0); |
| 216 | ctx = pldm_transport_test_core(test); |
| 217 | rc = pldm_transport_send_recv_msg(ctx, 1, req, sizeof(req), &msg, &len); |
| 218 | EXPECT_EQ(rc, PLDM_REQUESTER_RECV_FAIL); |
| 219 | pldm_transport_test_destroy(test); |
| 220 | } |
| 221 | #endif |
Thu Nguyen | f56e4dc | 2023-07-24 10:39:47 +0700 | [diff] [blame] | 222 | |
| 223 | #ifdef LIBPLDM_API_TESTING |
| 224 | TEST(Transport, send_recv_drain_one_unwanted) |
| 225 | { |
| 226 | uint8_t unwanted[] = {0x01, 0x00, 0x01, 0x01}; |
| 227 | uint8_t req[] = {0x81, 0x00, 0x01, 0x01}; |
| 228 | uint8_t resp[] = {0x01, 0x00, 0x01, 0x00}; |
| 229 | const struct pldm_transport_test_descriptor seq[] = { |
| 230 | { |
| 231 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV, |
| 232 | .recv_msg = |
| 233 | { |
| 234 | .src = 2, |
| 235 | .msg = unwanted, |
| 236 | .len = sizeof(unwanted), |
| 237 | }, |
| 238 | }, |
| 239 | { |
| 240 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_SEND, |
| 241 | .send_msg = |
| 242 | { |
| 243 | .dst = 1, |
| 244 | .msg = req, |
| 245 | .len = sizeof(req), |
| 246 | }, |
| 247 | }, |
| 248 | { |
| 249 | .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV, |
| 250 | .recv_msg = |
| 251 | { |
| 252 | .src = 1, |
| 253 | .msg = resp, |
| 254 | .len = sizeof(resp), |
| 255 | }, |
| 256 | }, |
| 257 | }; |
| 258 | struct pldm_transport_test* test = NULL; |
| 259 | struct pldm_transport* ctx; |
| 260 | size_t len; |
Andrew Jeffery | dc1edac | 2023-08-07 21:15:07 +0930 | [diff] [blame] | 261 | void* msg = NULL; |
Thu Nguyen | f56e4dc | 2023-07-24 10:39:47 +0700 | [diff] [blame] | 262 | int rc; |
| 263 | |
| 264 | EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0); |
| 265 | ctx = pldm_transport_test_core(test); |
| 266 | 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] | 267 | ASSERT_EQ(rc, PLDM_REQUESTER_SUCCESS); |
Thu Nguyen | f56e4dc | 2023-07-24 10:39:47 +0700 | [diff] [blame] | 268 | EXPECT_NE(memcmp(msg, unwanted, len), 0); |
| 269 | EXPECT_EQ(memcmp(msg, resp, len), 0); |
| 270 | free(msg); |
| 271 | pldm_transport_test_destroy(test); |
| 272 | } |
| 273 | #endif |