1 /* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later */
2
3 #ifdef NDEBUG
4 #undef NDEBUG
5 #endif
6
7 #include <string.h>
8 #include <assert.h>
9
10 #include <libmctp.h>
11 #include <libmctp-alloc.h>
12
13 #include "test-utils.h"
14
15 struct mctp_binding_test {
16 struct mctp_binding binding;
17 };
18
mctp_binding_test_tx(struct mctp_binding * b,struct mctp_pktbuf * pkt)19 static int mctp_binding_test_tx(struct mctp_binding *b __attribute__((unused)),
20 struct mctp_pktbuf *pkt __attribute__((unused)))
21 {
22 /* we are not expecting TX packets */
23 assert(0);
24 return 0;
25 }
26
mctp_binding_test_init(void)27 struct mctp_binding_test *mctp_binding_test_init(void)
28 {
29 struct mctp_binding_test *test;
30 test = __mctp_alloc(sizeof(*test));
31 memset(test, '\0', sizeof(*test));
32 test->binding.name = "test";
33 test->binding.version = 1;
34 test->binding.tx = mctp_binding_test_tx;
35 test->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
36 test->binding.pkt_header = 0;
37 test->binding.pkt_trailer = 0;
38 return test;
39 }
40
mctp_binding_test_destroy(struct mctp_binding_test * test)41 void mctp_binding_test_destroy(struct mctp_binding_test *test)
42 {
43 __mctp_free(test);
44 }
45
mctp_binding_test_rx_raw(struct mctp_binding_test * test,void * buf,size_t len)46 void mctp_binding_test_rx_raw(struct mctp_binding_test *test, void *buf,
47 size_t len)
48 {
49 struct mctp_pktbuf *pkt;
50
51 pkt = mctp_pktbuf_alloc(&test->binding, len);
52 assert(pkt);
53 memcpy(mctp_pktbuf_hdr(pkt), buf, len);
54 mctp_bus_rx(&test->binding, pkt);
55 }
56
mctp_binding_test_register_bus(struct mctp_binding_test * binding,struct mctp * mctp,mctp_eid_t eid)57 void mctp_binding_test_register_bus(struct mctp_binding_test *binding,
58 struct mctp *mctp, mctp_eid_t eid)
59 {
60 mctp_register_bus(mctp, &binding->binding, eid);
61 }
62
mctp_test_stack_init(struct mctp ** mctp,struct mctp_binding_test ** binding,mctp_eid_t eid)63 void mctp_test_stack_init(struct mctp **mctp,
64 struct mctp_binding_test **binding, mctp_eid_t eid)
65 {
66 *mctp = mctp_init();
67 assert(*mctp);
68
69 *binding = mctp_binding_test_init();
70 assert(*binding);
71
72 mctp_binding_test_register_bus(*binding, *mctp, eid);
73 }
74