blob: 083875111c7207e8879b6178d3f9787c1464df3f [file] [log] [blame]
Andrew Jeffery9a6ba892023-07-18 22:22:14 +09301#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
9TEST(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
20TEST(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
47TEST(Transport, recv_one)
48{
49 uint8_t msg[] = {0x01, 0x00, 0x01, 0x00};
Rashmica Gupta24576292023-07-31 14:02:41 +100050 const pldm_tid_t src_tid = 1;
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093051 const struct pldm_transport_test_descriptor seq[] = {
52 {
53 .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV,
54 .recv_msg =
55 {
Rashmica Gupta24576292023-07-31 14:02:41 +100056 .src = src_tid,
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093057 .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 Gupta24576292023-07-31 14:02:41 +100067 pldm_tid_t tid;
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093068
69 EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0);
70 ctx = pldm_transport_test_core(test);
Rashmica Gupta24576292023-07-31 14:02:41 +100071 rc = pldm_transport_recv_msg(ctx, &tid, &recvd, &len);
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093072 EXPECT_EQ(rc, PLDM_REQUESTER_SUCCESS);
73 EXPECT_EQ(len, sizeof(msg));
74 EXPECT_EQ(memcmp(recvd, msg, len), 0);
Rashmica Gupta24576292023-07-31 14:02:41 +100075 EXPECT_EQ(tid, src_tid);
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093076 free(recvd);
77 pldm_transport_test_destroy(test);
78}
79#endif
80
81#ifdef LIBPLDM_API_TESTING
82TEST(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
132TEST(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 Nguyenef485062023-07-14 23:36:21 +0700167
168#ifdef LIBPLDM_API_TESTING
169TEST(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 Nguyenf56e4dc2023-07-24 10:39:47 +0700222
223#ifdef LIBPLDM_API_TESTING
224TEST(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 Jefferydc1edac2023-08-07 21:15:07 +0930261 void* msg = NULL;
Thu Nguyenf56e4dc2023-07-24 10:39:47 +0700262 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 Jefferydc1edac2023-08-07 21:15:07 +0930267 ASSERT_EQ(rc, PLDM_REQUESTER_SUCCESS);
Thu Nguyenf56e4dc2023-07-24 10:39:47 +0700268 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