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