|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Nordic Semiconductor ASA |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "mocks/ecc_help_utils.h" |
| 8 | +#include "mocks/hci_core.h" |
| 9 | +#include "mocks/hci_core_expects.h" |
| 10 | + |
| 11 | +#include <zephyr/bluetooth/hci.h> |
| 12 | +#include <zephyr/fff.h> |
| 13 | +#include <zephyr/kernel.h> |
| 14 | + |
| 15 | +#include <host/ecc.h> |
| 16 | +#include <host/hci_core.h> |
| 17 | + |
| 18 | +DEFINE_FFF_GLOBALS; |
| 19 | + |
| 20 | +static void fff_reset_rule_before(const struct ztest_unit_test *test, void *fixture) |
| 21 | +{ |
| 22 | + sys_slist_t *pub_key_cb_slist = bt_ecc_get_pub_key_cb_slist(); |
| 23 | + |
| 24 | + memset(&bt_dev, 0x00, sizeof(struct bt_dev)); |
| 25 | + sys_slist_init(pub_key_cb_slist); |
| 26 | + |
| 27 | + HCI_CORE_FFF_FAKES_LIST(RESET_FAKE); |
| 28 | +} |
| 29 | + |
| 30 | +ZTEST_RULE(fff_reset_rule, fff_reset_rule_before, NULL); |
| 31 | + |
| 32 | +ZTEST_SUITE(bt_pub_key_gen, NULL, NULL, NULL, NULL, NULL); |
| 33 | + |
| 34 | +static void bt_pub_key_gen_debug_key_callback(const uint8_t key[BT_PUB_KEY_LEN]) |
| 35 | +{ |
| 36 | + uint8_t const *internal_dbg_key = bt_ecc_get_internal_debug_public_key(); |
| 37 | + const char *func_name = "bt_pub_key_gen_null_key_callback"; |
| 38 | + |
| 39 | + zassert_equal_ptr(key, internal_dbg_key, "'%s()' was called with incorrect '%s' value", |
| 40 | + func_name, "key"); |
| 41 | +} |
| 42 | + |
| 43 | +/* |
| 44 | + * Test using the internal debug public key |
| 45 | + * |
| 46 | + * Constraints: |
| 47 | + * - "LE Read Local P-256 Public Key" command is supported |
| 48 | + * - "LE Generate DH Key" command is supported |
| 49 | + * - "ECC Debug Keys" command is supported |
| 50 | + * - 'CONFIG_BT_USE_DEBUG_KEYS' is enabled |
| 51 | + * |
| 52 | + * Expected behaviour: |
| 53 | + * - bt_pub_key_gen() returns 0 (success) |
| 54 | + */ |
| 55 | +ZTEST(bt_pub_key_gen, test_using_internal_debug_public_key) |
| 56 | +{ |
| 57 | + int result; |
| 58 | + bool flags_check; |
| 59 | + struct bt_pub_key_cb new_cb = {0}; |
| 60 | + |
| 61 | + Z_TEST_SKIP_IFNDEF(CONFIG_BT_USE_DEBUG_KEYS); |
| 62 | + |
| 63 | + new_cb.func = bt_pub_key_gen_debug_key_callback; |
| 64 | + |
| 65 | + /* Set "LE Read Local P-256 Public Key" command support bit */ |
| 66 | + bt_dev.supported_commands[34] |= BIT(1); |
| 67 | + /* Set "LE Generate DH Key" command support bit */ |
| 68 | + bt_dev.supported_commands[34] |= BIT(2); |
| 69 | + /* Set "ECC Debug Keys" command support bit */ |
| 70 | + bt_dev.supported_commands[41] |= BIT(2); |
| 71 | + |
| 72 | + atomic_clear_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY); |
| 73 | + bt_hci_cmd_send_sync_fake.return_val = 0; |
| 74 | + |
| 75 | + result = bt_pub_key_gen(&new_cb); |
| 76 | + |
| 77 | + expect_not_called_bt_hci_cmd_send_sync(); |
| 78 | + |
| 79 | + zassert_ok(result, "Unexpected error code '%d' was returned", result); |
| 80 | + |
| 81 | + flags_check = atomic_test_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY); |
| 82 | + zassert_true(flags_check, "Flags were not correctly set"); |
| 83 | +} |
| 84 | + |
| 85 | +static void bt_pub_key_gen_callback(const uint8_t key[BT_PUB_KEY_LEN]) |
| 86 | +{ |
| 87 | + zassert_unreachable("Unexpected call to '%s()' occurred", __func__); |
| 88 | +} |
| 89 | + |
| 90 | +/* |
| 91 | + * Test generating a public key request |
| 92 | + * |
| 93 | + * Constraints: |
| 94 | + * - "LE Read Local P-256 Public Key" command is supported |
| 95 | + * - "LE Generate DH Key" command is supported |
| 96 | + * - bt_hci_cmd_send_sync() succeeds and returns 0 |
| 97 | + * |
| 98 | + * Expected behaviour: |
| 99 | + * - bt_pub_key_gen() returns 0 (success) |
| 100 | + */ |
| 101 | +ZTEST(bt_pub_key_gen, test_public_key_generation_request_passes) |
| 102 | +{ |
| 103 | + int result; |
| 104 | + bool flags_check; |
| 105 | + struct bt_pub_key_cb new_cb = {0}; |
| 106 | + |
| 107 | + new_cb.func = bt_pub_key_gen_callback; |
| 108 | + |
| 109 | + /* Set "LE Read Local P-256 Public Key" command support bit */ |
| 110 | + bt_dev.supported_commands[34] |= BIT(1); |
| 111 | + /* Set "LE Generate DH Key" command support bit */ |
| 112 | + bt_dev.supported_commands[34] |= BIT(2); |
| 113 | + |
| 114 | + atomic_set_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY); |
| 115 | + bt_hci_cmd_send_sync_fake.return_val = 0; |
| 116 | + |
| 117 | + result = bt_pub_key_gen(&new_cb); |
| 118 | + |
| 119 | + expect_single_call_bt_hci_cmd_send_sync(BT_HCI_OP_LE_P256_PUBLIC_KEY); |
| 120 | + |
| 121 | + zassert_ok(result, "Unexpected error code '%d' was returned", result); |
| 122 | + |
| 123 | + flags_check = atomic_test_bit(bt_dev.flags, BT_DEV_HAS_PUB_KEY); |
| 124 | + zassert_false(flags_check, "Flags were not correctly set"); |
| 125 | + |
| 126 | + flags_check = atomic_test_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY); |
| 127 | + zassert_true(flags_check, "Flags were not correctly set"); |
| 128 | +} |
| 129 | + |
| 130 | +/* |
| 131 | + * Test generating a public key request while 'BT_DEV_PUB_KEY_BUSY' flag is set |
| 132 | + * |
| 133 | + * Constraints: |
| 134 | + * - "LE Read Local P-256 Public Key" command is supported |
| 135 | + * - "LE Generate DH Key" command is supported |
| 136 | + * - bt_hci_cmd_send_sync() isn't called |
| 137 | + * |
| 138 | + * Expected behaviour: |
| 139 | + * - bt_pub_key_gen() returns 0 (success) |
| 140 | + */ |
| 141 | +ZTEST(bt_pub_key_gen, test_no_public_key_generation_request_duplication) |
| 142 | +{ |
| 143 | + int result; |
| 144 | + bool flags_check; |
| 145 | + struct bt_pub_key_cb new_cb = {0}; |
| 146 | + |
| 147 | + new_cb.func = bt_pub_key_gen_callback; |
| 148 | + |
| 149 | + /* Set "LE Read Local P-256 Public Key" command support bit */ |
| 150 | + bt_dev.supported_commands[34] |= BIT(1); |
| 151 | + /* Set "LE Generate DH Key" command support bit */ |
| 152 | + bt_dev.supported_commands[34] |= BIT(2); |
| 153 | + |
| 154 | + atomic_set_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY); |
| 155 | + |
| 156 | + result = bt_pub_key_gen(&new_cb); |
| 157 | + |
| 158 | + expect_not_called_bt_hci_cmd_send_sync(); |
| 159 | + |
| 160 | + zassert_ok(result, "Unexpected error code '%d' was returned", result); |
| 161 | + |
| 162 | + flags_check = atomic_test_bit(bt_dev.flags, BT_DEV_PUB_KEY_BUSY); |
| 163 | + zassert_true(flags_check, "Flags were not correctly set"); |
| 164 | +} |
0 commit comments