|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "host_mocks/assert.h" |
| 8 | +#include "mocks/ecc_help_utils.h" |
| 9 | +#include "mocks/hci_core.h" |
| 10 | +#include "mocks/hci_core_expects.h" |
| 11 | +#include "mocks/net_buf.h" |
| 12 | +#include "mocks/net_buf_expects.h" |
| 13 | + |
| 14 | +#include <zephyr/bluetooth/hci.h> |
| 15 | +#include <zephyr/kernel.h> |
| 16 | + |
| 17 | +#include <host/ecc.h> |
| 18 | +#include <host/hci_core.h> |
| 19 | + |
| 20 | +ZTEST_SUITE(bt_dh_key_gen_invalid_cases, NULL, NULL, NULL, NULL, NULL); |
| 21 | + |
| 22 | +static void bt_dh_key_unreachable_cb(const uint8_t key[BT_DH_KEY_LEN]) |
| 23 | +{ |
| 24 | + zassert_unreachable("Unexpected call to '%s()' occurred", __func__); |
| 25 | +} |
| 26 | + |
| 27 | +/* |
| 28 | + * Test passing NULL reference for the 'remote_pk' argument |
| 29 | + * |
| 30 | + * Constraints: |
| 31 | + * - NULL reference is used for the 'remote_pk' argument |
| 32 | + * |
| 33 | + * Expected behaviour: |
| 34 | + * - An assertion is raised and execution stops |
| 35 | + */ |
| 36 | +ZTEST(bt_dh_key_gen_invalid_cases, test_null_remote_pk_reference) |
| 37 | +{ |
| 38 | + expect_assert(); |
| 39 | + bt_dh_key_gen(NULL, bt_dh_key_unreachable_cb); |
| 40 | +} |
| 41 | + |
| 42 | +/* |
| 43 | + * Test passing NULL reference for the 'dh_key_cb' argument |
| 44 | + * |
| 45 | + * Constraints: |
| 46 | + * - NULL reference is used for the 'dh_key_cb' argument |
| 47 | + * |
| 48 | + * Expected behaviour: |
| 49 | + * - An assertion is raised and execution stops |
| 50 | + */ |
| 51 | +ZTEST(bt_dh_key_gen_invalid_cases, test_null_dh_key_cb_reference) |
| 52 | +{ |
| 53 | + const uint8_t remote_pk[BT_PUB_KEY_LEN] = {0}; |
| 54 | + |
| 55 | + expect_assert(); |
| 56 | + bt_dh_key_gen(remote_pk, NULL); |
| 57 | +} |
| 58 | + |
| 59 | +/* |
| 60 | + * Test DH key generation fails if the callback is already registered |
| 61 | + * |
| 62 | + * Constraints: |
| 63 | + * - 'BT_DEV_HAS_PUB_KEY' flag is set |
| 64 | + * - 'BT_DEV_PUB_KEY_BUSY' flag isn't set |
| 65 | + * |
| 66 | + * Expected behaviour: |
| 67 | + * - bt_dh_key_gen() returns a negative error code (-EALREADY) |
| 68 | + */ |
| 69 | +ZTEST(bt_dh_key_gen_invalid_cases, test_callback_already_registered) |
| 70 | +{ |
| 71 | + int result; |
| 72 | + struct net_buf net_buff = {0}; |
| 73 | + struct bt_hci_cp_le_generate_dhkey cp = {0}; |
| 74 | + const uint8_t remote_pk[BT_PUB_KEY_LEN] = {0}; |
| 75 | + |
| 76 | + atomic_set_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY); |
| 77 | + atomic_clear_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY); |
| 78 | + |
| 79 | + /* This makes hci_generate_dhkey_v1() succeeds and returns 0 */ |
| 80 | + net_buf_simple_add_fake.return_val = &cp; |
| 81 | + bt_hci_cmd_create_fake.return_val = &net_buff; |
| 82 | + bt_hci_cmd_send_sync_fake.return_val = 0; |
| 83 | + |
| 84 | + /* Pass first call that will set dh_key_cb = cb*/ |
| 85 | + bt_dh_key_gen(remote_pk, bt_dh_key_unreachable_cb); |
| 86 | + result = bt_dh_key_gen(remote_pk, bt_dh_key_unreachable_cb); |
| 87 | + |
| 88 | + zassert_true(result == -EALREADY, "Unexpected error code '%d' was returned", result); |
| 89 | +} |
| 90 | + |
| 91 | +static void bt_dh_key_unreachable_2nd_trial_cb(const uint8_t key[BT_DH_KEY_LEN]) |
| 92 | +{ |
| 93 | + zassert_unreachable("Unexpected call to '%s()' occurred", __func__); |
| 94 | +} |
| 95 | + |
| 96 | +/* |
| 97 | + * Test DH key generation fails if a current key generation cycle hasn't been finished yet and |
| 98 | + * 'dh_key_cb' isn't NULL. |
| 99 | + * |
| 100 | + * Constraints: |
| 101 | + * - 'BT_DEV_HAS_PUB_KEY' flag is set |
| 102 | + * - 'BT_DEV_PUB_KEY_BUSY' flag isn't set |
| 103 | + * |
| 104 | + * Expected behaviour: |
| 105 | + * - bt_dh_key_gen() returns a negative error code (-EBUSY) |
| 106 | + */ |
| 107 | +ZTEST(bt_dh_key_gen_invalid_cases, test_generate_key_parallel_with_running_one) |
| 108 | +{ |
| 109 | + int result; |
| 110 | + struct net_buf net_buff = {0}; |
| 111 | + struct bt_hci_cp_le_generate_dhkey cp = {0}; |
| 112 | + const uint8_t remote_pk[BT_PUB_KEY_LEN] = {0}; |
| 113 | + |
| 114 | + atomic_set_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY); |
| 115 | + atomic_clear_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY); |
| 116 | + |
| 117 | + /* This makes hci_generate_dhkey_v1() succeeds and returns 0 */ |
| 118 | + net_buf_simple_add_fake.return_val = &cp; |
| 119 | + bt_hci_cmd_create_fake.return_val = &net_buff; |
| 120 | + bt_hci_cmd_send_sync_fake.return_val = 0; |
| 121 | + |
| 122 | + /* Pass first call that will set dh_key_cb = cb*/ |
| 123 | + bt_dh_key_gen(remote_pk, bt_dh_key_unreachable_cb); |
| 124 | + result = bt_dh_key_gen(remote_pk, bt_dh_key_unreachable_2nd_trial_cb); |
| 125 | + |
| 126 | + zassert_true(result == -EBUSY, "Unexpected error code '%d' was returned", result); |
| 127 | +} |
| 128 | + |
| 129 | +/* |
| 130 | + * Test DH key generation fails if a current key generation cycle hasn't been finished yet and |
| 131 | + * 'dh_key_cb' isn't NULL. |
| 132 | + * |
| 133 | + * Constraints: |
| 134 | + * - 'BT_DEV_PUB_KEY_BUSY' flag is set |
| 135 | + * |
| 136 | + * Expected behaviour: |
| 137 | + * - bt_dh_key_gen() returns a negative error code (-EBUSY) |
| 138 | + */ |
| 139 | +ZTEST(bt_dh_key_gen_invalid_cases, test_bt_dev_pub_key_busy_set) |
| 140 | +{ |
| 141 | + int result; |
| 142 | + const uint8_t remote_pk[BT_PUB_KEY_LEN] = {0}; |
| 143 | + |
| 144 | + atomic_set_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY); |
| 145 | + |
| 146 | + result = bt_dh_key_gen(remote_pk, bt_dh_key_unreachable_cb); |
| 147 | + |
| 148 | + zassert_true(result == -EBUSY, "Unexpected error code '%d' was returned", result); |
| 149 | +} |
| 150 | + |
| 151 | +/* |
| 152 | + * Test DH key generation fails if the 'BT_DEV_HAS_PUB_KEY' flag isn't set |
| 153 | + * |
| 154 | + * Constraints: |
| 155 | + * - 'BT_DEV_HAS_PUB_KEY' flag isn't set |
| 156 | + * - 'BT_DEV_PUB_KEY_BUSY' flag isn't set |
| 157 | + * |
| 158 | + * Expected behaviour: |
| 159 | + * - bt_dh_key_gen() returns a negative error code (-EADDRNOTAVAIL) |
| 160 | + */ |
| 161 | +ZTEST(bt_dh_key_gen_invalid_cases, test_device_has_no_pub_key) |
| 162 | +{ |
| 163 | + int result; |
| 164 | + const uint8_t remote_pk[BT_PUB_KEY_LEN] = {0}; |
| 165 | + |
| 166 | + atomic_clear_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY); |
| 167 | + atomic_clear_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY); |
| 168 | + |
| 169 | + result = bt_dh_key_gen(remote_pk, bt_dh_key_unreachable_cb); |
| 170 | + |
| 171 | + zassert_true(result == -EADDRNOTAVAIL, "Unexpected error code '%d' was returned", result); |
| 172 | +} |
| 173 | + |
| 174 | +/* |
| 175 | + * Test DH key generation fails when hci_generate_dhkey_v1/2() fails |
| 176 | + * |
| 177 | + * Constraints: |
| 178 | + * - 'BT_DEV_HAS_PUB_KEY' flag is set |
| 179 | + * - 'BT_DEV_PUB_KEY_BUSY' flag isn't set |
| 180 | + * - 'hci_generate_dhkey_v1/2()' fails and returns a negative error code (-ENOBUFS) |
| 181 | + * |
| 182 | + * Expected behaviour: |
| 183 | + * - bt_dh_key_gen() returns a negative error code (-ENOBUFS) |
| 184 | + */ |
| 185 | +ZTEST(bt_dh_key_gen_invalid_cases, test_hci_generate_dhkey_vx_fails) |
| 186 | +{ |
| 187 | + int result; |
| 188 | + bt_dh_key_cb_t *dh_key_cb; |
| 189 | + const uint8_t remote_pk[BT_PUB_KEY_LEN] = {0}; |
| 190 | + |
| 191 | + atomic_set_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY); |
| 192 | + atomic_clear_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY); |
| 193 | + |
| 194 | + if (IS_ENABLED(CONFIG_BT_USE_DEBUG_KEYS)) { |
| 195 | + /* Set "ECC Debug Keys" command support bit */ |
| 196 | + bt_dev.supported_commands[41] |= BIT(2); |
| 197 | + } |
| 198 | + |
| 199 | + /* This makes hci_generate_dhkey_vx() fails and returns (-ENOBUFS) */ |
| 200 | + bt_hci_cmd_create_fake.return_val = NULL; |
| 201 | + |
| 202 | + result = bt_dh_key_gen(remote_pk, bt_dh_key_unreachable_cb); |
| 203 | + |
| 204 | + zassert_true(result == -ENOBUFS, "Unexpected error code '%d' was returned", result); |
| 205 | + |
| 206 | + dh_key_cb = bt_ecc_get_dh_key_cb(); |
| 207 | + zassert_is_null(*dh_key_cb, "Unexpected callback reference was set"); |
| 208 | +} |
0 commit comments