xref: /btstack/test/l2cap-cbm/l2cap_cbm_test.cpp (revision 3b40ed9800910dfbeffa8bec45333eab74cf5de7)
1af7c3ae6SMatthias Ringwald 
2af7c3ae6SMatthias Ringwald // hal_cpu
3af7c3ae6SMatthias Ringwald #include "hal_cpu.h"
hal_cpu_disable_irqs(void)4af7c3ae6SMatthias Ringwald void hal_cpu_disable_irqs(void){}
hal_cpu_enable_irqs(void)5af7c3ae6SMatthias Ringwald void hal_cpu_enable_irqs(void){}
hal_cpu_enable_irqs_and_sleep(void)6af7c3ae6SMatthias Ringwald void hal_cpu_enable_irqs_and_sleep(void){}
7af7c3ae6SMatthias Ringwald 
8af7c3ae6SMatthias Ringwald // mock_sm.c
9af7c3ae6SMatthias Ringwald #include "ble/sm.h"
sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler)10af7c3ae6SMatthias Ringwald void sm_add_event_handler(btstack_packet_callback_registration_t * callback_handler){}
sm_request_pairing(hci_con_handle_t con_handle)11af7c3ae6SMatthias Ringwald void sm_request_pairing(hci_con_handle_t con_handle){}
12af7c3ae6SMatthias Ringwald 
13af7c3ae6SMatthias Ringwald // mock_hci_transport.h
14af7c3ae6SMatthias Ringwald #include "hci_transport.h"
15af7c3ae6SMatthias Ringwald void mock_hci_transport_receive_packet(uint8_t packet_type, uint8_t * packet, uint16_t size);
16af7c3ae6SMatthias Ringwald const hci_transport_t * mock_hci_transport_mock_get_instance(void);
17af7c3ae6SMatthias Ringwald 
18af7c3ae6SMatthias Ringwald // mock_hci_transport.c
19af7c3ae6SMatthias Ringwald #include <stddef.h>
20af7c3ae6SMatthias Ringwald static uint8_t  mock_hci_transport_outgoing_packet_buffer[HCI_ACL_PAYLOAD_SIZE];
21af7c3ae6SMatthias Ringwald static uint16_t mock_hci_transport_outgoing_packet_size;
22af7c3ae6SMatthias Ringwald static uint8_t  mock_hci_transport_outgoing_packet_type;
23af7c3ae6SMatthias Ringwald 
24af7c3ae6SMatthias Ringwald static void (*mock_hci_transport_packet_handler)(uint8_t packet_type, uint8_t * packet, uint16_t size);
mock_hci_transport_register_packet_handler(void (* packet_handler)(uint8_t packet_type,uint8_t * packet,uint16_t size))25af7c3ae6SMatthias Ringwald static void mock_hci_transport_register_packet_handler(void (*packet_handler)(uint8_t packet_type, uint8_t * packet, uint16_t size)){
26af7c3ae6SMatthias Ringwald     mock_hci_transport_packet_handler = packet_handler;
27af7c3ae6SMatthias Ringwald }
mock_hci_transport_send_packet(uint8_t packet_type,uint8_t * packet,int size)28af7c3ae6SMatthias Ringwald static int mock_hci_transport_send_packet(uint8_t packet_type, uint8_t *packet, int size){
29af7c3ae6SMatthias Ringwald     mock_hci_transport_outgoing_packet_type = packet_type;
30af7c3ae6SMatthias Ringwald     mock_hci_transport_outgoing_packet_size = size;
31af7c3ae6SMatthias Ringwald     memcpy(mock_hci_transport_outgoing_packet_buffer, packet, size);
32af7c3ae6SMatthias Ringwald     return 0;
33af7c3ae6SMatthias Ringwald }
mock_hci_transport_mock_get_instance(void)34af7c3ae6SMatthias Ringwald const hci_transport_t * mock_hci_transport_mock_get_instance(void){
35af7c3ae6SMatthias Ringwald     static hci_transport_t mock_hci_transport = {
36af7c3ae6SMatthias Ringwald         /*  .transport.name                          = */  "mock",
37af7c3ae6SMatthias Ringwald         /*  .transport.init                          = */  NULL,
38af7c3ae6SMatthias Ringwald         /*  .transport.open                          = */  NULL,
39af7c3ae6SMatthias Ringwald         /*  .transport.close                         = */  NULL,
40af7c3ae6SMatthias Ringwald         /*  .transport.register_packet_handler       = */  &mock_hci_transport_register_packet_handler,
41af7c3ae6SMatthias Ringwald         /*  .transport.can_send_packet_now           = */  NULL,
42af7c3ae6SMatthias Ringwald         /*  .transport.send_packet                   = */  &mock_hci_transport_send_packet,
43af7c3ae6SMatthias Ringwald         /*  .transport.set_baudrate                  = */  NULL,
44af7c3ae6SMatthias Ringwald     };
45af7c3ae6SMatthias Ringwald     return &mock_hci_transport;
46af7c3ae6SMatthias Ringwald }
mock_hci_transport_receive_packet(uint8_t packet_type,const uint8_t * packet,uint16_t size)47af7c3ae6SMatthias Ringwald void mock_hci_transport_receive_packet(uint8_t packet_type, const uint8_t * packet, uint16_t size){
48af7c3ae6SMatthias Ringwald     (*mock_hci_transport_packet_handler)(packet_type, (uint8_t *) packet, size);
49af7c3ae6SMatthias Ringwald }
50af7c3ae6SMatthias Ringwald 
51af7c3ae6SMatthias Ringwald //
52af7c3ae6SMatthias Ringwald 
53af7c3ae6SMatthias Ringwald #include <stdint.h>
54af7c3ae6SMatthias Ringwald #include <stdio.h>
55af7c3ae6SMatthias Ringwald #include <stdlib.h>
56af7c3ae6SMatthias Ringwald #include <string.h>
57af7c3ae6SMatthias Ringwald 
58af7c3ae6SMatthias Ringwald #include "CppUTest/TestHarness.h"
59af7c3ae6SMatthias Ringwald #include "CppUTest/CommandLineTestRunner.h"
60af7c3ae6SMatthias Ringwald #include "CppUTestExt/MockSupport.h"
61af7c3ae6SMatthias Ringwald 
62af7c3ae6SMatthias Ringwald #include "hci_dump.h"
63af7c3ae6SMatthias Ringwald #include "btstack_debug.h"
64af7c3ae6SMatthias Ringwald #include "l2cap.h"
65af7c3ae6SMatthias Ringwald #include "btstack_memory.h"
66af7c3ae6SMatthias Ringwald #include "btstack_run_loop_embedded.h"
67af7c3ae6SMatthias Ringwald #include "hci_dump_posix_stdout.h"
68af7c3ae6SMatthias Ringwald #include "btstack_event.h"
69af7c3ae6SMatthias Ringwald 
70af7c3ae6SMatthias Ringwald #define TEST_PACKET_SIZE       100
71af7c3ae6SMatthias Ringwald #define HCI_CON_HANDLE_TEST_LE 0x0005
72af7c3ae6SMatthias Ringwald #define TEST_PSM 0x1001
73af7c3ae6SMatthias Ringwald 
74af7c3ae6SMatthias Ringwald static bool l2cap_channel_accept_incoming;
75af7c3ae6SMatthias Ringwald static uint16_t initial_credits = L2CAP_LE_AUTOMATIC_CREDITS;
76af7c3ae6SMatthias Ringwald static uint8_t data_channel_buffer[TEST_PACKET_SIZE];
77af7c3ae6SMatthias Ringwald static uint16_t l2cap_cid;
78af7c3ae6SMatthias Ringwald static bool l2cap_channel_opened;
79af7c3ae6SMatthias Ringwald static btstack_packet_callback_registration_t l2cap_event_callback_registration;
80af7c3ae6SMatthias Ringwald 
81*3b40ed98SMatthias Ringwald // l2cap fuzz api
82*3b40ed98SMatthias Ringwald extern "C" void l2cap_setup_test_channels_fuzz(void);
83*3b40ed98SMatthias Ringwald extern "C" void l2cap_free_channels_fuzz(void);
84*3b40ed98SMatthias Ringwald extern "C" l2cap_channel_t * l2cap_get_dynamic_channel_fuzz(void);
85*3b40ed98SMatthias Ringwald 
86*3b40ed98SMatthias Ringwald // l2cap random public function
87*3b40ed98SMatthias Ringwald extern "C"  void l2cap_finalize_channel_close(l2cap_channel_t * channel);
88*3b40ed98SMatthias Ringwald 
89af7c3ae6SMatthias Ringwald const uint8_t le_data_channel_conn_request_1[] = {
90af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x12, 0x00, 0x0e, 0x00, 0x05, 0x00, 0x14, 0x01, 0x0a, 0x00, 0x01, 0x10, 0x41, 0x00,
91af7c3ae6SMatthias Ringwald         0x64, 0x00, 0x30, 0x00, 0xff, 0xff
92af7c3ae6SMatthias Ringwald };
93af7c3ae6SMatthias Ringwald 
94af7c3ae6SMatthias Ringwald const uint8_t le_data_channel_invalid_pdu[] = {
95af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x12, 0x00, 0x0e, 0x00, 0x05, 0x00, 0x11, 0x01, 0x0a, 0x00, 0x01, 0x10, 0x41, 0x00,
96af7c3ae6SMatthias Ringwald         0x64, 0x00, 0x30, 0x00, 0xff, 0xff
97af7c3ae6SMatthias Ringwald };
98af7c3ae6SMatthias Ringwald 
99af7c3ae6SMatthias Ringwald const uint8_t le_data_channel_conn_response_1[] = {
100af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x12, 0x00, 0x0e, 0x00, 0x05, 0x00, 0x15, 0x01, 0x0a, 0x00, 0x41, 0x00, 0x64, 0x00,
101af7c3ae6SMatthias Ringwald         0x30, 0x00, 0xff, 0xff, 0x00, 0x00
102af7c3ae6SMatthias Ringwald };
103af7c3ae6SMatthias Ringwald 
104af7c3ae6SMatthias Ringwald const uint8_t le_data_channel_data_1[] = {
105af7c3ae6SMatthias Ringwald         0x05, 0x20, 0x04, 0x00, 0x00, 0x00, 0x41, 0x00
106af7c3ae6SMatthias Ringwald };
107af7c3ae6SMatthias Ringwald 
fix_boundary_flags(uint8_t * packet,uint16_t size)108af7c3ae6SMatthias Ringwald static void fix_boundary_flags(uint8_t * packet, uint16_t size){
109af7c3ae6SMatthias Ringwald     uint8_t acl_flags = packet[1] >> 4;
110af7c3ae6SMatthias Ringwald     if (acl_flags == 0){
111af7c3ae6SMatthias Ringwald         acl_flags = 2;  // first fragment
112af7c3ae6SMatthias Ringwald     }
113af7c3ae6SMatthias Ringwald     packet[1] = (packet[1] & 0x0f) | (acl_flags << 4);
114af7c3ae6SMatthias Ringwald }
115af7c3ae6SMatthias Ringwald 
print_acl(const char * name,const uint8_t * packet,uint16_t size)116af7c3ae6SMatthias Ringwald static void print_acl(const char * name, const uint8_t * packet, uint16_t size){
117af7c3ae6SMatthias Ringwald     printf("const uint8_t %s[] = {", name);
118af7c3ae6SMatthias Ringwald     uint16_t i;
119af7c3ae6SMatthias Ringwald     for (i=0;i<size;i++){
120af7c3ae6SMatthias Ringwald         if (i != 0){
121af7c3ae6SMatthias Ringwald             printf(", ");
122af7c3ae6SMatthias Ringwald         }
123af7c3ae6SMatthias Ringwald         if ((i % 16) == 0){
124af7c3ae6SMatthias Ringwald             printf("\n    ");
125af7c3ae6SMatthias Ringwald         }
126af7c3ae6SMatthias Ringwald         printf("0x%02x", packet[i]);
127af7c3ae6SMatthias Ringwald     }
128af7c3ae6SMatthias Ringwald     printf("\n};\n");
129af7c3ae6SMatthias Ringwald }
130af7c3ae6SMatthias Ringwald 
l2cap_channel_packet_handler(uint8_t packet_type,uint16_t channel,uint8_t * packet,uint16_t size)131af7c3ae6SMatthias Ringwald static void l2cap_channel_packet_handler(uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
132af7c3ae6SMatthias Ringwald     UNUSED(channel);
133af7c3ae6SMatthias Ringwald     UNUSED(size);
134af7c3ae6SMatthias Ringwald     uint16_t cid;
135af7c3ae6SMatthias Ringwald     switch (packet_type) {
136af7c3ae6SMatthias Ringwald         case HCI_EVENT_PACKET:
137af7c3ae6SMatthias Ringwald             switch (hci_event_packet_get_type(packet)) {
138af7c3ae6SMatthias Ringwald                 case L2CAP_EVENT_CBM_INCOMING_CONNECTION:
139af7c3ae6SMatthias Ringwald                     cid = l2cap_event_cbm_incoming_connection_get_local_cid(packet);
140af7c3ae6SMatthias Ringwald                     if (l2cap_channel_accept_incoming){
141af7c3ae6SMatthias Ringwald                         l2cap_cbm_accept_connection(cid, data_channel_buffer, sizeof(data_channel_buffer), initial_credits);
142af7c3ae6SMatthias Ringwald                     } else {
143af7c3ae6SMatthias Ringwald                         l2cap_cbm_decline_connection(cid, L2CAP_CBM_CONNECTION_RESULT_NO_RESOURCES_AVAILABLE);
144af7c3ae6SMatthias Ringwald                     }
145af7c3ae6SMatthias Ringwald                     break;
146af7c3ae6SMatthias Ringwald                 case L2CAP_EVENT_CBM_CHANNEL_OPENED:
147af7c3ae6SMatthias Ringwald                     l2cap_channel_opened = true;
148af7c3ae6SMatthias Ringwald                     break;
149af7c3ae6SMatthias Ringwald                 default:
150af7c3ae6SMatthias Ringwald                     break;
151af7c3ae6SMatthias Ringwald             }
152af7c3ae6SMatthias Ringwald             break;
153af7c3ae6SMatthias Ringwald         default:
154af7c3ae6SMatthias Ringwald             break;
155af7c3ae6SMatthias Ringwald     }
156af7c3ae6SMatthias Ringwald }
157af7c3ae6SMatthias Ringwald 
TEST_GROUP(L2CAP_CHANNELS)158af7c3ae6SMatthias Ringwald TEST_GROUP(L2CAP_CHANNELS){
159af7c3ae6SMatthias Ringwald     const hci_transport_t * hci_transport;
160af7c3ae6SMatthias Ringwald     void setup(void){
161af7c3ae6SMatthias Ringwald         btstack_memory_init();
162af7c3ae6SMatthias Ringwald         btstack_run_loop_init(btstack_run_loop_embedded_get_instance());
163af7c3ae6SMatthias Ringwald         hci_transport = mock_hci_transport_mock_get_instance();
164af7c3ae6SMatthias Ringwald         hci_init(hci_transport, NULL);
165af7c3ae6SMatthias Ringwald         l2cap_init();
166af7c3ae6SMatthias Ringwald         l2cap_event_callback_registration.callback = &l2cap_channel_packet_handler;
167af7c3ae6SMatthias Ringwald         l2cap_add_event_handler(&l2cap_event_callback_registration);
168af7c3ae6SMatthias Ringwald         l2cap_register_fixed_channel(&l2cap_channel_packet_handler, L2CAP_CID_ATTRIBUTE_PROTOCOL);
169af7c3ae6SMatthias Ringwald         hci_dump_init(hci_dump_posix_stdout_get_instance());
170af7c3ae6SMatthias Ringwald         l2cap_channel_opened = false;
171af7c3ae6SMatthias Ringwald     }
172af7c3ae6SMatthias Ringwald     void teardown(void){
1738edfa49cSMilanka Ringwald         l2cap_remove_event_handler(&l2cap_event_callback_registration);
174af7c3ae6SMatthias Ringwald         l2cap_deinit();
175af7c3ae6SMatthias Ringwald         hci_deinit();
176af7c3ae6SMatthias Ringwald         btstack_memory_deinit();
177af7c3ae6SMatthias Ringwald         btstack_run_loop_deinit();
178af7c3ae6SMatthias Ringwald     }
179af7c3ae6SMatthias Ringwald };
180af7c3ae6SMatthias Ringwald 
TEST(L2CAP_CHANNELS,fixed_channel)181af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, fixed_channel){
182af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
183af7c3ae6SMatthias Ringwald     // channel does not exist
184af7c3ae6SMatthias Ringwald     l2cap_request_can_send_fix_channel_now_event(HCI_CON_HANDLE_TEST_LE, 0x003f);
185af7c3ae6SMatthias Ringwald     // att
186af7c3ae6SMatthias Ringwald     l2cap_request_can_send_fix_channel_now_event(HCI_CON_HANDLE_TEST_LE, L2CAP_CID_ATTRIBUTE_PROTOCOL);
187af7c3ae6SMatthias Ringwald     //
188af7c3ae6SMatthias Ringwald     (void) l2cap_can_send_fixed_channel_packet_now(HCI_CON_HANDLE_TEST_LE, L2CAP_CID_ATTRIBUTE_PROTOCOL);
189af7c3ae6SMatthias Ringwald     // packet buffer not reserved
190af7c3ae6SMatthias Ringwald     log_info("ignore error in next line (calling .. without reserving buffer first");
191af7c3ae6SMatthias Ringwald     l2cap_send_prepared_connectionless(HCI_CON_HANDLE_TEST_LE, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5);
192af7c3ae6SMatthias Ringwald     // packet buffer reserved
193af7c3ae6SMatthias Ringwald     l2cap_reserve_packet_buffer();
194af7c3ae6SMatthias Ringwald     l2cap_send_prepared_connectionless(HCI_CON_HANDLE_TEST_LE, L2CAP_CID_ATTRIBUTE_PROTOCOL, 5);
195af7c3ae6SMatthias Ringwald     //
196af7c3ae6SMatthias Ringwald     l2cap_send_connectionless(HCI_CON_HANDLE_TEST_LE, L2CAP_CID_ATTRIBUTE_PROTOCOL, (uint8_t *) "hallo", 5);
197af7c3ae6SMatthias Ringwald }
198af7c3ae6SMatthias Ringwald 
TEST(L2CAP_CHANNELS,some_functions)199af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, some_functions){
200af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
201af7c3ae6SMatthias Ringwald     l2cap_reserve_packet_buffer();
202af7c3ae6SMatthias Ringwald     (void) l2cap_get_outgoing_buffer();
203af7c3ae6SMatthias Ringwald     l2cap_release_packet_buffer();
204af7c3ae6SMatthias Ringwald     l2cap_set_max_le_mtu(30);
205af7c3ae6SMatthias Ringwald     l2cap_set_max_le_mtu(30);
206af7c3ae6SMatthias Ringwald     l2cap_cbm_unregister_service(TEST_PSM);
207af7c3ae6SMatthias Ringwald     l2cap_cbm_accept_connection(0X01, NULL, 0, 0);
208af7c3ae6SMatthias Ringwald     l2cap_cbm_decline_connection(0x01, L2CAP_CBM_CONNECTION_RESULT_NO_RESOURCES_AVAILABLE);
209*3b40ed98SMatthias Ringwald     l2cap_cbm_available_credits(0x01);
210af7c3ae6SMatthias Ringwald     l2cap_disconnect(0x01);
2118edfa49cSMilanka Ringwald 
2128edfa49cSMilanka Ringwald     uint16_t credits = 10;
2138edfa49cSMilanka Ringwald     uint16_t mtu = 23;
2148edfa49cSMilanka Ringwald     uint8_t  buffer[10];
2158edfa49cSMilanka Ringwald     uint16_t out_local_cid;
2168edfa49cSMilanka Ringwald 
2178edfa49cSMilanka Ringwald     l2cap_le_register_service(&l2cap_channel_packet_handler, TEST_PSM, LEVEL_2);
2188edfa49cSMilanka Ringwald     l2cap_le_unregister_service(TEST_PSM);
2198edfa49cSMilanka Ringwald     l2cap_le_accept_connection(0X01, buffer, mtu, credits);
2208edfa49cSMilanka Ringwald     l2cap_le_decline_connection(0X01);
2218edfa49cSMilanka Ringwald     l2cap_le_create_channel(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, TEST_PSM, buffer, mtu, credits, LEVEL_2, &out_local_cid);
2228edfa49cSMilanka Ringwald     l2cap_le_provide_credits(0X01, credits);
2238edfa49cSMilanka Ringwald     l2cap_le_can_send_now(0X01);
2248edfa49cSMilanka Ringwald     l2cap_le_request_can_send_now_event(0X01);
2258edfa49cSMilanka Ringwald     l2cap_le_send_data(0X01, (const uint8_t * )buffer, sizeof(buffer));
2268edfa49cSMilanka Ringwald     l2cap_le_disconnect(0X01);
227af7c3ae6SMatthias Ringwald }
228af7c3ae6SMatthias Ringwald 
TEST(L2CAP_CHANNELS,outgoing_no_connection)229af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_no_connection){
230af7c3ae6SMatthias Ringwald     l2cap_cbm_create_channel(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, TEST_PSM, data_channel_buffer,
231af7c3ae6SMatthias Ringwald                             sizeof(data_channel_buffer), L2CAP_LE_AUTOMATIC_CREDITS, LEVEL_0, &l2cap_cid);
232af7c3ae6SMatthias Ringwald }
233af7c3ae6SMatthias Ringwald 
TEST(L2CAP_CHANNELS,outgoing_security_1)234af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_security_1){
235af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
236af7c3ae6SMatthias Ringwald     l2cap_cbm_create_channel(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, TEST_PSM, data_channel_buffer,
237af7c3ae6SMatthias Ringwald                             sizeof(data_channel_buffer), L2CAP_LE_AUTOMATIC_CREDITS, LEVEL_2, NULL);
238af7c3ae6SMatthias Ringwald     l2cap_cbm_create_channel(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, TEST_PSM, data_channel_buffer,
239af7c3ae6SMatthias Ringwald                             sizeof(data_channel_buffer), L2CAP_LE_AUTOMATIC_CREDITS, LEVEL_2, &l2cap_cid);
240af7c3ae6SMatthias Ringwald }
241af7c3ae6SMatthias Ringwald 
TEST(L2CAP_CHANNELS,outgoing_1)242af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, outgoing_1){
243af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
244af7c3ae6SMatthias Ringwald     l2cap_cbm_create_channel(&l2cap_channel_packet_handler, HCI_CON_HANDLE_TEST_LE, TEST_PSM, data_channel_buffer,
245af7c3ae6SMatthias Ringwald                             sizeof(data_channel_buffer), L2CAP_LE_AUTOMATIC_CREDITS, LEVEL_0, &l2cap_cid);
246af7c3ae6SMatthias Ringwald     // fix_boundary_flags(mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
247af7c3ae6SMatthias Ringwald     // print_acl("le_data_channel_conn_request_1", mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
248af7c3ae6SMatthias Ringwald     // simulate conn response
249af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, le_data_channel_conn_response_1, sizeof(le_data_channel_conn_response_1));
250af7c3ae6SMatthias Ringwald     CHECK(l2cap_channel_opened);
251af7c3ae6SMatthias Ringwald     CHECK(hci_number_free_acl_slots_for_handle(HCI_CON_HANDLE_TEST_LE) > 0);
252af7c3ae6SMatthias Ringwald     bool can_send_now = l2cap_can_send_packet_now(l2cap_cid);
253af7c3ae6SMatthias Ringwald     CHECK(can_send_now);
254af7c3ae6SMatthias Ringwald     l2cap_request_can_send_now_event(l2cap_cid);
255af7c3ae6SMatthias Ringwald     CHECK(hci_number_free_acl_slots_for_handle(HCI_CON_HANDLE_TEST_LE) > 0);
256af7c3ae6SMatthias Ringwald     l2cap_send(l2cap_cid, (uint8_t *) "hallo", 5);
257af7c3ae6SMatthias Ringwald     // CHECK(hci_number_free_acl_slots_for_handle(HCI_CON_HANDLE_TEST) > 0);
258af7c3ae6SMatthias Ringwald     // fix_boundary_flags(mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
259af7c3ae6SMatthias Ringwald     // print_acl("le_data_channel_data_1", mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
260af7c3ae6SMatthias Ringwald     // simulate data
261af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, le_data_channel_data_1, sizeof(le_data_channel_data_1));
262af7c3ae6SMatthias Ringwald     l2cap_disconnect(l2cap_cid);
263af7c3ae6SMatthias Ringwald }
264af7c3ae6SMatthias Ringwald 
TEST(L2CAP_CHANNELS,incoming_1)265af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, incoming_1){
266af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
267af7c3ae6SMatthias Ringwald     l2cap_cbm_register_service(&l2cap_channel_packet_handler, TEST_PSM, LEVEL_0);
268af7c3ae6SMatthias Ringwald     // simulate conn request
269af7c3ae6SMatthias Ringwald     l2cap_channel_accept_incoming = true;
270af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, le_data_channel_conn_request_1, sizeof(le_data_channel_conn_request_1));
271af7c3ae6SMatthias Ringwald     // fix_boundary_flags(mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
272af7c3ae6SMatthias Ringwald     // print_acl("le_data_channel_conn_response_1", mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
273af7c3ae6SMatthias Ringwald     // TODO: verify data
274af7c3ae6SMatthias Ringwald     l2cap_cbm_unregister_service(TEST_PSM);
275af7c3ae6SMatthias Ringwald }
276af7c3ae6SMatthias Ringwald 
TEST(L2CAP_CHANNELS,incoming_2)277af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, incoming_2){
278af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
279af7c3ae6SMatthias Ringwald     l2cap_cbm_register_service(&l2cap_channel_packet_handler, TEST_PSM, LEVEL_2);
280af7c3ae6SMatthias Ringwald     // simulate conn request
281af7c3ae6SMatthias Ringwald     l2cap_channel_accept_incoming = true;
282af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, le_data_channel_conn_request_1, sizeof(le_data_channel_conn_request_1));
283af7c3ae6SMatthias Ringwald }
284af7c3ae6SMatthias Ringwald 
TEST(L2CAP_CHANNELS,incoming_3)285af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, incoming_3){
286af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
287af7c3ae6SMatthias Ringwald     l2cap_cbm_register_service(&l2cap_channel_packet_handler, TEST_PSM, LEVEL_0);
288af7c3ae6SMatthias Ringwald     // simulate conn request
289af7c3ae6SMatthias Ringwald     l2cap_channel_accept_incoming = true;
290af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, le_data_channel_invalid_pdu, sizeof(le_data_channel_invalid_pdu));
291af7c3ae6SMatthias Ringwald }
292af7c3ae6SMatthias Ringwald 
TEST(L2CAP_CHANNELS,incoming_decline)293af7c3ae6SMatthias Ringwald TEST(L2CAP_CHANNELS, incoming_decline){
294af7c3ae6SMatthias Ringwald     hci_setup_test_connections_fuzz();
295af7c3ae6SMatthias Ringwald     l2cap_cbm_register_service(&l2cap_channel_packet_handler, TEST_PSM, LEVEL_0);
296af7c3ae6SMatthias Ringwald     // simulate conn request
297af7c3ae6SMatthias Ringwald     l2cap_channel_accept_incoming = false;
298af7c3ae6SMatthias Ringwald     mock_hci_transport_receive_packet(HCI_ACL_DATA_PACKET, le_data_channel_conn_request_1, sizeof(le_data_channel_conn_request_1));
299af7c3ae6SMatthias Ringwald     // fix_boundary_flags(mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
300af7c3ae6SMatthias Ringwald     // print_acl("le_data_channel_conn_response_1", mock_hci_transport_outgoing_packet_buffer, mock_hci_transport_outgoing_packet_size);
301af7c3ae6SMatthias Ringwald     // TODO: verify data
302af7c3ae6SMatthias Ringwald }
TEST(L2CAP_CHANNELS,fuzz)303*3b40ed98SMatthias Ringwald TEST(L2CAP_CHANNELS, fuzz) {
304*3b40ed98SMatthias Ringwald     l2cap_setup_test_channels_fuzz();
305*3b40ed98SMatthias Ringwald     l2cap_channel_t * channel = l2cap_get_dynamic_channel_fuzz();
306*3b40ed98SMatthias Ringwald     l2cap_free_channels_fuzz();
307*3b40ed98SMatthias Ringwald     l2cap_get_dynamic_channel_fuzz();
308*3b40ed98SMatthias Ringwald }
309af7c3ae6SMatthias Ringwald 
main(int argc,const char * argv[])310af7c3ae6SMatthias Ringwald int main (int argc, const char * argv[]){
311af7c3ae6SMatthias Ringwald     return CommandLineTestRunner::RunAllTests(argc, argv);
312af7c3ae6SMatthias Ringwald }
313