|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <zephyr/types.h> |
| 8 | +#include <ztest.h> |
| 9 | + |
| 10 | +#include <bluetooth/hci.h> |
| 11 | +#include <sys/byteorder.h> |
| 12 | +#include <sys/slist.h> |
| 13 | +#include <sys/util.h> |
| 14 | +#include "hal/ccm.h" |
| 15 | + |
| 16 | +#include "util/util.h" |
| 17 | +#include "util/mem.h" |
| 18 | +#include "util/memq.h" |
| 19 | +#include "util/dbuf.h" |
| 20 | + |
| 21 | +#include "pdu.h" |
| 22 | +#include "ll.h" |
| 23 | +#include "ll_settings.h" |
| 24 | +#include "ll_feat.h" |
| 25 | + |
| 26 | +#include "lll.h" |
| 27 | +#include "lll_df_types.h" |
| 28 | +#include "lll_conn.h" |
| 29 | +#include "ull_tx_queue.h" |
| 30 | + |
| 31 | +#include "ull_conn_types.h" |
| 32 | +#include "ull_llcp.h" |
| 33 | +#include "ull_llcp_internal.h" |
| 34 | + |
| 35 | +#include "helper_pdu.h" |
| 36 | +#include "helper_util.h" |
| 37 | + |
| 38 | +struct ll_conn test_conn; |
| 39 | + |
| 40 | +static void setup(void) |
| 41 | +{ |
| 42 | + test_setup(&test_conn); |
| 43 | +} |
| 44 | + |
| 45 | +#define LLCTRL_PDU_SIZE (offsetof(struct pdu_data, llctrl) + sizeof(struct pdu_data_llctrl)) |
| 46 | + |
| 47 | +/* +-----+ +-------+ +-----+ |
| 48 | + * | UT | | LL_A | | LT | |
| 49 | + * +-----+ +-------+ +-----+ |
| 50 | + * | | | |
| 51 | + * | | <PDU> | |
| 52 | + * | |<------------------| |
| 53 | + * | | | |
| 54 | + */ |
| 55 | + |
| 56 | +static void lt_tx_invalid_pdu_size(enum helper_pdu_opcode opcode, int adj_size) |
| 57 | +{ |
| 58 | + struct pdu_data_llctrl_unknown_rsp unknown_rsp; |
| 59 | + struct pdu_data pdu; |
| 60 | + struct node_tx *tx; |
| 61 | + /* PDU contents does not matter when testing for invalid PDU size */ |
| 62 | + uint8_t data[LLCTRL_PDU_SIZE] = { 0 }; |
| 63 | + |
| 64 | + /* Encode a PDU for the opcode */ |
| 65 | + encode_pdu(opcode, &pdu, &data); |
| 66 | + |
| 67 | + /* Setup the LL_UNKNOWN_RSP expected for the PDU */ |
| 68 | + if (opcode == LL_ZERO) { |
| 69 | + /* we use 0xff in response if length was 0 */ |
| 70 | + unknown_rsp.type = PDU_DATA_LLCTRL_TYPE_UNUSED; |
| 71 | + } else { |
| 72 | + unknown_rsp.type = pdu.llctrl.opcode; |
| 73 | + } |
| 74 | + |
| 75 | + /* adjust PDU len */ |
| 76 | + pdu.len += adj_size; |
| 77 | + |
| 78 | + /* Connect */ |
| 79 | + ull_cp_state_set(&test_conn, ULL_CP_CONNECTED); |
| 80 | + |
| 81 | + /* Prepare */ |
| 82 | + event_prepare(&test_conn); |
| 83 | + |
| 84 | + /* Rx */ |
| 85 | + lt_tx_no_encode(&pdu, &test_conn, NULL); |
| 86 | + |
| 87 | + /* Done */ |
| 88 | + event_done(&test_conn); |
| 89 | + |
| 90 | + /* Prepare */ |
| 91 | + event_prepare(&test_conn); |
| 92 | + |
| 93 | + /* Tx Queue should have one LL Control PDU */ |
| 94 | + lt_rx(LL_UNKNOWN_RSP, &test_conn, &tx, &unknown_rsp); |
| 95 | + lt_rx_q_is_empty(&test_conn); |
| 96 | + |
| 97 | + /* Done */ |
| 98 | + event_done(&test_conn); |
| 99 | + |
| 100 | + /* Release Tx */ |
| 101 | + ull_cp_release_tx(&test_conn, tx); |
| 102 | + |
| 103 | + /* There should not be a host notifications */ |
| 104 | + ut_rx_q_is_empty(); |
| 105 | + |
| 106 | + zassert_equal(ctx_buffers_free(), test_ctx_buffers_cnt(), |
| 107 | + "Free CTX buffers %d", ctx_buffers_free()); |
| 108 | +} |
| 109 | + |
| 110 | +void test_invalid_pdu_ignore_rx(void) |
| 111 | +{ |
| 112 | + /* Role */ |
| 113 | + test_set_role(&test_conn, BT_HCI_ROLE_PERIPHERAL); |
| 114 | + |
| 115 | + /* Test too small PDUs */ |
| 116 | + lt_tx_invalid_pdu_size(LL_ZERO, 0); /* 0 length PDU */ |
| 117 | + lt_tx_invalid_pdu_size(LL_VERSION_IND, -1); |
| 118 | +/* lt_tx_invalid_pdu_size(LL_LE_PING_REQ, -1); */ |
| 119 | +/* lt_tx_invalid_pdu_size(LL_LE_PING_RSP, -1); */ |
| 120 | + lt_tx_invalid_pdu_size(LL_FEATURE_REQ, -1); |
| 121 | + lt_tx_invalid_pdu_size(LL_PERIPH_FEAT_XCHG, -1); |
| 122 | + lt_tx_invalid_pdu_size(LL_FEATURE_RSP, -1); |
| 123 | + lt_tx_invalid_pdu_size(LL_MIN_USED_CHANS_IND, -1); |
| 124 | + lt_tx_invalid_pdu_size(LL_REJECT_IND, -1); |
| 125 | + lt_tx_invalid_pdu_size(LL_REJECT_EXT_IND, -1); |
| 126 | + lt_tx_invalid_pdu_size(LL_ENC_REQ, -1); |
| 127 | + lt_tx_invalid_pdu_size(LL_ENC_RSP, -1); |
| 128 | +/* lt_tx_invalid_pdu_size(LL_START_ENC_REQ, -1); 0 length */ |
| 129 | +/* lt_tx_invalid_pdu_size(LL_START_ENC_RSP, -1); 0 length */ |
| 130 | +/* lt_tx_invalid_pdu_size(LL_PAUSE_ENC_REQ, -1); 0 length */ |
| 131 | +/* lt_tx_invalid_pdu_size(LL_PAUSE_ENC_RSP, -1); 0 length */ |
| 132 | + lt_tx_invalid_pdu_size(LL_PHY_REQ, -1); |
| 133 | + lt_tx_invalid_pdu_size(LL_PHY_RSP, -1); |
| 134 | + lt_tx_invalid_pdu_size(LL_PHY_UPDATE_IND, -1); |
| 135 | + lt_tx_invalid_pdu_size(LL_UNKNOWN_RSP, -1); |
| 136 | + lt_tx_invalid_pdu_size(LL_CONNECTION_UPDATE_IND, -1); |
| 137 | + lt_tx_invalid_pdu_size(LL_CONNECTION_PARAM_REQ, -1); |
| 138 | + lt_tx_invalid_pdu_size(LL_CONNECTION_PARAM_RSP, -1); |
| 139 | + lt_tx_invalid_pdu_size(LL_TERMINATE_IND, -1); |
| 140 | + lt_tx_invalid_pdu_size(LL_CHAN_MAP_UPDATE_IND, -1); |
| 141 | + lt_tx_invalid_pdu_size(LL_LENGTH_REQ, -1); |
| 142 | + lt_tx_invalid_pdu_size(LL_LENGTH_RSP, -1); |
| 143 | + lt_tx_invalid_pdu_size(LL_CTE_REQ, -1); |
| 144 | +/* lt_tx_invalid_pdu_size(LL_CTE_RSP, -1); 0 length */ |
| 145 | + |
| 146 | + /* Test too big PDUs */ |
| 147 | + lt_tx_invalid_pdu_size(LL_VERSION_IND, 1); |
| 148 | + lt_tx_invalid_pdu_size(LL_LE_PING_REQ, 1); |
| 149 | + lt_tx_invalid_pdu_size(LL_LE_PING_RSP, 1); |
| 150 | + lt_tx_invalid_pdu_size(LL_FEATURE_REQ, 1); |
| 151 | + lt_tx_invalid_pdu_size(LL_PERIPH_FEAT_XCHG, 1); |
| 152 | + lt_tx_invalid_pdu_size(LL_FEATURE_RSP, 1); |
| 153 | + lt_tx_invalid_pdu_size(LL_MIN_USED_CHANS_IND, 1); |
| 154 | + lt_tx_invalid_pdu_size(LL_REJECT_IND, 1); |
| 155 | + lt_tx_invalid_pdu_size(LL_REJECT_EXT_IND, 1); |
| 156 | + lt_tx_invalid_pdu_size(LL_ENC_REQ, 1); |
| 157 | + lt_tx_invalid_pdu_size(LL_ENC_RSP, 1); |
| 158 | + lt_tx_invalid_pdu_size(LL_START_ENC_REQ, 1); |
| 159 | + lt_tx_invalid_pdu_size(LL_START_ENC_RSP, 1); |
| 160 | + lt_tx_invalid_pdu_size(LL_PAUSE_ENC_REQ, 1); |
| 161 | + lt_tx_invalid_pdu_size(LL_PAUSE_ENC_RSP, 1); |
| 162 | + lt_tx_invalid_pdu_size(LL_PHY_REQ, 1); |
| 163 | + lt_tx_invalid_pdu_size(LL_PHY_RSP, 1); |
| 164 | + lt_tx_invalid_pdu_size(LL_PHY_UPDATE_IND, 1); |
| 165 | + lt_tx_invalid_pdu_size(LL_UNKNOWN_RSP, 1); |
| 166 | + lt_tx_invalid_pdu_size(LL_CONNECTION_UPDATE_IND, 1); |
| 167 | + lt_tx_invalid_pdu_size(LL_CONNECTION_PARAM_REQ, 1); |
| 168 | + lt_tx_invalid_pdu_size(LL_CONNECTION_PARAM_RSP, 1); |
| 169 | + lt_tx_invalid_pdu_size(LL_TERMINATE_IND, 1); |
| 170 | + lt_tx_invalid_pdu_size(LL_CHAN_MAP_UPDATE_IND, 1); |
| 171 | + lt_tx_invalid_pdu_size(LL_LENGTH_REQ, 1); |
| 172 | + lt_tx_invalid_pdu_size(LL_LENGTH_RSP, 1); |
| 173 | + lt_tx_invalid_pdu_size(LL_CTE_REQ, 1); |
| 174 | + lt_tx_invalid_pdu_size(LL_CTE_RSP, 1); |
| 175 | +} |
| 176 | + |
| 177 | +void test_main(void) |
| 178 | +{ |
| 179 | + ztest_test_suite(invalid, |
| 180 | + ztest_unit_test_setup_teardown(test_invalid_pdu_ignore_rx, setup, |
| 181 | + unit_test_noop)); |
| 182 | + |
| 183 | + ztest_run_test_suite(invalid); |
| 184 | +} |
0 commit comments