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