xref: /openbmc/libmctp/tests/test-utils.c (revision 0a96544ab28c3763ccbdca71ab4865871e71e991)
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 /* mctp_binding_test can be used for loopback in tests. Senders must use
16  * the local EID as the destination */
17 struct mctp_binding_test {
18 	struct mctp_binding binding;
19 	uint8_t tx_storage[MCTP_PKTBUF_SIZE(MCTP_BTU)];
20 };
21 
mctp_binding_test_tx(struct mctp_binding * b,struct mctp_pktbuf * pkt)22 static int mctp_binding_test_tx(struct mctp_binding *b, struct mctp_pktbuf *pkt)
23 {
24 	mctp_bus_rx(b, pkt);
25 	return 0;
26 }
27 
mctp_binding_test_init(void)28 struct mctp_binding_test *mctp_binding_test_init(void)
29 {
30 	struct mctp_binding_test *test;
31 	test = __mctp_alloc(sizeof(*test));
32 	memset(test, '\0', sizeof(*test));
33 	test->binding.name = "test";
34 	test->binding.version = 1;
35 	test->binding.tx = mctp_binding_test_tx;
36 	test->binding.pkt_size = MCTP_PACKET_SIZE(MCTP_BTU);
37 	test->binding.pkt_header = 0;
38 	test->binding.pkt_trailer = 0;
39 	test->binding.tx_storage = test->tx_storage;
40 	return test;
41 }
42 
mctp_binding_test_destroy(struct mctp_binding_test * test)43 void mctp_binding_test_destroy(struct mctp_binding_test *test)
44 {
45 	__mctp_free(test);
46 }
47 
mctp_binding_test_rx_raw(struct mctp_binding_test * test,void * buf,size_t len)48 void mctp_binding_test_rx_raw(struct mctp_binding_test *test, void *buf,
49 			      size_t len)
50 {
51 	struct mctp_pktbuf *pkt;
52 
53 	pkt = mctp_pktbuf_alloc(&test->binding, len);
54 	assert(pkt);
55 	memcpy(mctp_pktbuf_hdr(pkt), buf, len);
56 	mctp_bus_rx(&test->binding, pkt);
57 	mctp_pktbuf_free(pkt);
58 }
59 
mctp_binding_test_register_bus(struct mctp_binding_test * binding,struct mctp * mctp,mctp_eid_t eid)60 void mctp_binding_test_register_bus(struct mctp_binding_test *binding,
61 				    struct mctp *mctp, mctp_eid_t eid)
62 {
63 	mctp_register_bus(mctp, &binding->binding, eid);
64 }
65 
mctp_test_stack_init(struct mctp ** mctp,struct mctp_binding_test ** binding,mctp_eid_t eid)66 void mctp_test_stack_init(struct mctp **mctp,
67 			  struct mctp_binding_test **binding, mctp_eid_t eid)
68 {
69 	*mctp = mctp_init();
70 	assert(*mctp);
71 
72 	*binding = mctp_binding_test_init();
73 	assert(*binding);
74 
75 	mctp_binding_test_register_bus(*binding, *mctp, eid);
76 	mctp_binding_set_tx_enabled(&(*binding)->binding, true);
77 }
78