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