blob: 41ef42e15262005f39dde009219b9884546bc247 [file] [log] [blame]
Andrew Jefferyefb40062023-11-10 13:48:39 +10301#include <libpldm/transport.h>
Andrew Jeffery9a6ba892023-07-18 22:22:14 +09302
3#include "array.h"
4#include "transport/test.h"
5
6#include <gtest/gtest.h>
7
Andrew Jeffery9a6ba892023-07-18 22:22:14 +09308TEST(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 Jeffery9a6ba892023-07-18 22:22:14 +093016
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093017TEST(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 Jeffery9a6ba892023-07-18 22:22:14 +093041
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093042TEST(Transport, recv_one)
43{
44 uint8_t msg[] = {0x01, 0x00, 0x01, 0x00};
Rashmica Gupta24576292023-07-31 14:02:41 +100045 const pldm_tid_t src_tid = 1;
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093046 const struct pldm_transport_test_descriptor seq[] = {
47 {
48 .type = PLDM_TRANSPORT_TEST_ELEMENT_MSG_RECV,
49 .recv_msg =
50 {
Rashmica Gupta24576292023-07-31 14:02:41 +100051 .src = src_tid,
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093052 .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 Gupta24576292023-07-31 14:02:41 +100062 pldm_tid_t tid;
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093063
64 EXPECT_EQ(pldm_transport_test_init(&test, seq, ARRAY_SIZE(seq)), 0);
65 ctx = pldm_transport_test_core(test);
Rashmica Gupta24576292023-07-31 14:02:41 +100066 rc = pldm_transport_recv_msg(ctx, &tid, &recvd, &len);
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093067 EXPECT_EQ(rc, PLDM_REQUESTER_SUCCESS);
68 EXPECT_EQ(len, sizeof(msg));
69 EXPECT_EQ(memcmp(recvd, msg, len), 0);
Rashmica Gupta24576292023-07-31 14:02:41 +100070 EXPECT_EQ(tid, src_tid);
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093071 free(recvd);
72 pldm_transport_test_destroy(test);
73}
Andrew Jeffery9a6ba892023-07-18 22:22:14 +093074
Thu Nguyenf56e4dc2023-07-24 10:39:47 +070075TEST(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 Jefferydc1edac2023-08-07 21:15:07 +0930112 void* msg = NULL;
Thu Nguyenf56e4dc2023-07-24 10:39:47 +0700113 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 Jefferydc1edac2023-08-07 21:15:07 +0930118 ASSERT_EQ(rc, PLDM_REQUESTER_SUCCESS);
Thu Nguyenf56e4dc2023-07-24 10:39:47 +0700119 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 Nguyen16cd5232023-08-23 06:47:19 +0700124
Andrew Jeffery486d85d2023-08-31 11:12:08 +0930125TEST(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}