bluetooth: add cmocka unit tests for bluetooth service modules#549
bluetooth: add cmocka unit tests for bluetooth service modules#549huangyulong3 wants to merge 895 commits into
Conversation
bug: v/81589 remove global var g_sal_ag_sync_conn and sync call by addr Signed-off-by: YuhengLi <liyuheng@xiaomi.com>
bug: v/81504 Signed-off-by: YuhengLi <liyuheng@xiaomi.com>
bug: v/80268 Rootcause: Not unregister callbacks when cleanup. Signed-off-by: YuhengLi <liyuheng@xiaomi.com>
bug: v/81522 When event_id == BT_AVRCP_EVT_VOLUME_CHANGED, flag = true is set only if both CONFIG_BLUETOOTH_AVRCP_ABSOLUTE_VOLUME and CONFIG_BLUETOOTH_AVRCP_CONTROL are enabled. Otherwise in the else branch, flag = true is set only if CONFIG_BLUETOOTH_AVRCP_TARGET is enabled. All configurations are enabled, treating flag as always true, making the if (!flag) condition unreachable. Signed-off-by: YuhengLi <liyuheng@xiaomi.com>
bug: v/81702 Fixed memory leak caused by premature return. Signed-off-by: liuxiang18 <liuxiang18@xiaomi.com>
bug: v/80258 Rootcause: attributes in bt_sdp_discover_params may be modified by ZBlue SDP. Using const could cause a crash in some cases Signed-off-by: YuhengLi <liyuheng@xiaomi.com>
bug: v/81752 Rootcause: audio_connect should not be called in bluetoothd task Signed-off-by: YuhengLi <liyuheng@xiaomi.com>
bug: v/81968 Rootcause: disconnected_callback not called caused connect info not cleared in connection manager module Signed-off-by: YuhengLi <liyuheng@xiaomi.com>
… an asynchronous API. bug: v/81682 When priv dynamically allocates memory successfully but fails later due to other reasons before reaching the assignment ins->priv = priv;, the memory allocated to priv cannot be freed in bt_socket_async_client_deinit, leading to a resource leak. Signed-off-by: jialu <jialu@xiaomi.com>
…se functions bug: v/80811 Rootcause: In certain scenarios, users of `euv_pipe` must ensure all UV requests have completed execution before releasing resources. Consequently, it is necessary to notify users that `euv_pipe` has been fully released after its close operation is completed, thereby permitting subsequent operational procedures to proceed. Support for the close callback has therefore been added. Signed-off-by: chejinxian1 <chejinxian1@xiaomi.com>
…e callback bug: v/80808 Rootcause: In high-throughput reception scenarios, situations may arise where the `write_cb` for SPP data transmission to the application has not yet completed, yet the SPP device is released due to an abrupt disconnection, thereby preventing notification to the protocol stack that data reception has concluded. To circumvent this issue, it is imperative to ensure all write operations are finalised before releasing the SPP device. Consequently, an `euv_pipe` close callback implementation has been introduced to guarantee that all `write_cb` operations execute successfully prior to severing the data pathway. Signed-off-by: chejinxian1 <chejinxian1@xiaomi.com>
bug: v/74709
only open CONFIG_BLUETOOTH_AVRCP_CONTROL or CONFIG_BLUETOOTH_AVRCP_ABSOLUTE_VOLUME can build in bt_avrcp_control_notification_cb.
error: 'bt_avrcp_info_find_by_ct' undeclared (first use in this function); did you mean 'bt_avrcp_info_find_by_tg'?
1501 | avrcp_info = bt_list_find(bt_avrcp_conn, bt_avrcp_info_find_by_ct, ct);
| ^~~~~~~~~~~~~~~~~~~~~~~~
| bt_avrcp_info_find_by_tg
Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
bug: v/82095 The spp_connect_handler was attempting to look up the SPP connection by rfcomm_dlc before it was added to the connection list, causing "SPP connection not found for rfcomm_dlc" error. Root Cause: The connection object wasn't in the global connection list at the time of lookup, making spp_find_connection_by_dlc() always fail. Fix: Pass the spp_conn pointer directly as user_data to avoid the lookup, and add it to the connection list after successful initialization. Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
bug: v/81925 Add a generic descriptor allocation path, matching alloc_characteristic() style. Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
bug: v/81958 Some services are marked with GATT_PROP_EXPOSED_OVER_BREDR, but current not implement gatt over bredr. As a temporary workaround, clear this flag when calling add_service() so the service is exposed over BLE. Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
bug: v/82081 `bt_le_scan.h` is a globally exposed header file. Then, Zephyr's `#include <zephyr/bluetooth/bluetooth.h>` is declared as a private inclusion of Zephyr in CMake. However, the problem is that when third-party apps use my global `bt_le_scan.h`, the CMake system doesn't know where `zephyr/bluetooth/bluetooth.h` is and throws an error. One solution is for the third-party app to also declare a private inclusion of Zephyr in CMake, but this doesn't conform to design principles. The app only needs to be concerned with my framework layer. If the app also needs to include Zephyr's header files, then the framework layer is not properly configured. Therefore, `zephyr/bluetooth/bluetooth.h` must not be explicitly included in `bt_le_scan`. Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
bug: v/82104 The number of gatts sal DB attributes was insufficient for miwear's needs, so it was increased to a margin of 60. Future memory optimization projects will no longer maintain static arrays. Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
bug: v/81701 Rootcause: dereference null. Signed-off-by: Yuheng Li <liyuheng@xiaomi.com>
bug: v/81701 Rootcause: unnecessary malloc Signed-off-by: YuhengLi <liyuheng@xiaomi.com>
bug: v/81924 By default, just working (no I/O) should automatically accept user confirmation, but for compatibility with the watch app, app confirmation is required. Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
bug: v/59050 Rootcause:When reconnect to the headset during a call, the headset will obtain the call status through the cind command. Since Vela does not have modem, the status will be error. So, get cind from Android. Signed-off-by: zhangyuan20 <zhangyuan20@xiaomi.com>
bug: v/61170 Signed-off-by: zhangyuan20 <zhangyuan20@xiaomi.com>
bug: v/81520 In `spp_find_connection_by_sdp_param` and `spp_connect_with_uuid`, the pointer `spp_conn` was dereferenced before the NULL check. This patch ensures the pointer is validated before access to avoid potential crashes. Signed-off-by: v-yichenxi <v-yichenxi@xiaomi.com>
bug: v/82928 Remove the redundant stack variable bd_addr, which already exists in sal_conn. Signed-off-by: YuhengLi <liyuheng@xiaomi.com>
bug: v/82928 Rootcause: bt_sal_get_remote_address may fail Signed-off-by: YuhengLi <liyuheng@xiaomi.com>
Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
support. bug: v/65087 Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
…global database hash. bug :v/65095 The logic is implemented in gatts_service.c to trigger hash calculation and return the result via a registered callback, for trusted device sync and DB change tracking. Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
…h from adapter to profile. bug: v/65126 Root cause: Adapter had no generic message path to deliver events to profile services. GATTS could not receive a request to fetch the server database hash. Add adapter handler to forward profile_msg_t to service_manager. Add PROFILE_EVT_GATTS_REQUEST_DB_HASH and handle it in gatts_service via process_msg. Wire bt_sal_gatt_server_get_database_hash() call when the event is received. Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
bug: v/65129 Root cause: The stack did not compute and store the server GATT database hash on initial bonding. Peers could keep a stale GATT cache because no hash update was triggered. When a bonded LE device is connected, send PROFILE_EVT_GATTS_REQUEST_DB_HASH. Force hash update to start cache sync for the new bond. Signed-off-by: zhongzhijie1 <zhongzhijie1@xiaomi.com>
|
|
||
| config CM_SERVICE_PROFILES_TEST | ||
| tristate "Bluetooth service profiles unit test" | ||
| default y |
There was a problem hiding this comment.
suggest disable default
There was a problem hiding this comment.
Done. Changed to default n.
There was a problem hiding this comment.
Done. Changed to default n.
|
|
||
| config CM_SERVICE_MANAGER_TEST | ||
| tristate "vela auto tests service_manager" | ||
| default y |
There was a problem hiding this comment.
Done. Changed to default n. Also updated all other test Kconfig files to default n for consistency.
There was a problem hiding this comment.
Done. Changed to default n.
PR #549 Review — bluetooth: add cmocka unit tests规模:119 文件,17831+/0−,275 test cases,6 modules,6 commits 架构评估:与成熟社区对比1. 本 PR 大量使用
更严重的是,为了让 建议:对于需要测试 static 函数的模块,考虑将 static 函数改为 internal linkage( 2.
对比 AOSP 的做法:gmock 自动从接口定义生成 mock,API 签名变更时编译器直接报错在 mock 定义处。 建议:至少在 3. Kconfig 所有测试模块的 Kconfig 都设置了 成熟社区的做法是 4. 测试只验证 API 委托,不验证行为(设计层面)
这类测试的价值有限——它们验证的是"胶水代码是否正确转发",而不是"蓝牙功能是否正确工作"。对比 Zephyr 的蓝牙测试( 当前 PR 作为第一阶段是可以接受的(先有基础设施),但应在 PR 描述中明确这是 API 委托验证,后续需要补充行为测试。 5.
正面评价
结论测试基础设施搭建质量高,模块覆盖合理,文档完善。主要问题是
|
Regarding
|
bug: v/90257 Add log privacy as bitmask enum to support multiple privacy types. Currently supports address privacy (BIT 0), extensible for future types (e.g., name privacy). - bt_addr.c/h: bt_addr_set_privacy()/bt_addr_get_privacy() for address masking - log.h: enum bt_log_privacy_ bitmask, DEFAULT_BT_LOG_PRIVACY macro, bt_log_set_privacy() declaration - log_server.c: privacy_flags in g_logger, bt_log_set_privacy() dispatches by changed bits, init/monitor via kvdb - tools/log.c: bttool log privacy <bit> <0|1> - Kconfig: BLUETOOTH_LOG_PRIVACY (int, default 0) Signed-off-by: liuxiang18 <liuxiang18@xiaomi.com>
|
@huangyulong3 any update about disable default config ? |
|
Already done — all test module Kconfig entries now use |
bug: v/88468 Add cmocka unit tests for bluetooth framework API module, including test infrastructure (build files, Kconfig, user guide). Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
bug: v/88469 Add 146 cmocka unit tests for bluetooth framework common module, covering bt_addr, bt_uuid, bt_list, index_allocator, callbacks_list, scanner_filter, advertiser_data. Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
bug: v/88470 Add 48 cmocka unit tests for bluetooth service device module, covering device property get/set/delete operations. Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
bug: v/88471 Add 17 cmocka unit tests for bluetooth service manager module, covering register_service, init, startup, shutdown, get_uuid, processmsg, control, cleanup. Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
bug: v/88472 Add 50 cmocka unit tests for bluetooth service profiles module, covering a2dp codec SBC/AAC, a2dp_event, avrcp_msg, hfp_hf_event, hfp_ag_event, gatt_event (gatts + gattc). Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
bug: v/88473 Add 14 cmocka unit tests for bluetooth service utils module, covering btsnoop HCI packet filtering. Signed-off-by: huangyulong3 <huangyulong3@xiaomi.com>
71a8642 to
bf7366b
Compare
|
|
||
| config CM_FRAMEWORK_COMMON_TEST | ||
| tristate "vela auto tests framework_common" | ||
| default y |
There was a problem hiding this comment.
It is recommended to disable the configuration by default to avoid affecting the resource usage of existing products.
There was a problem hiding this comment.
Done. Changed to default n.
|
|
||
| config CM_SERVICE_DEVICE_TEST | ||
| tristate "vela auto tests service_device" | ||
| default y |
There was a problem hiding this comment.
It is recommended to disable the configuration by default to avoid affecting the resource usage of existing products.
There was a problem hiding this comment.
Done. Changed to default n.
I would suggest to do these "migration path" right from now. As this would become a template for all the other unit tests. |
|
|
||
| extern const void* service_manager_get_profile(enum profile_id id); | ||
|
|
||
| /* ===== A2DP Sink ===== */ |
There was a problem hiding this comment.
Should we move them into different files by profiles, rather than make all the profiles together here?
There was a problem hiding this comment.
Otherwise this file is way too long.
There was a problem hiding this comment.
This is a significant refactoring — splitting 912-line api_stubs.c into per-profile files (api_stubs_a2dp.c, api_stubs_hfp.c, etc.). Will address in a follow-up patch once the core test framework is merged, to avoid rebasing conflicts with the other review fixes.
There was a problem hiding this comment.
@huangyulong3 A heavy workload is not a valid excuse for the initial PR implementing the Cmoka functions.
On the contrary, it is even more important to fix these issues in this PR, as all subsequent PRs will follow the same style.
| * limitations under the License. | ||
| ***************************************************************************/ | ||
|
|
||
| /* clang-format off */ |
There was a problem hiding this comment.
Do not turn-off clang-format
If for any reason we need to include the headers not in alphabet order, just leave a empty line between them.
There was a problem hiding this comment.
Done. Removed all clang-format off/on directives from test files. Headers are now separated by empty lines instead.
| ${NUTTX_APPS_DIR}/frameworks/connectivity/bluetooth/service/profiles/hfp_ag | ||
| ${NUTTX_APPS_DIR}/frameworks/connectivity/bluetooth/service/profiles/include | ||
| ${NUTTX_APPS_DIR}/frameworks/connectivity/bluetooth/service/profiles/gatt | ||
| # Tests #include .c files directly, which causes: |
There was a problem hiding this comment.
Done. Removed all warning suppression flags from CMakeLists.txt.
|
@huangyulong3 it gerneral looks good to me, but suggest disable all config default, |
|
|
||
| /* Include the source under test */ | ||
|
|
||
| #include "../../../../service/profiles/a2dp/codec/a2dp_codec_aac.c" |
There was a problem hiding this comment.
Acknowledged. Removing the #include .c pattern requires restructuring the entire test architecture (adding proper mock/stub .c files, updating link dependencies). This is a substantial change that will be addressed in a follow-up patch to keep this PR reviewable.
There was a problem hiding this comment.
Done. Removed #include .c from 10 test files that only test public functions — source files are now compiled via Makefile CSRCS and linked normally.
6 files retain #include .c because they test static functions (e.g., test_btsnoop_filter tests 20+ static handler functions, test_scan_filter tests static match_uuid, test_device tests static device_create, etc.). Each has a comment explaining the reason. This matches Zephyr's approach where static-function tests include the source directly.
|
@gzh-terry Done. We've migrated away from Migrated (no more
The wrapper approach: a thin Remaining
These two use preprocessor function-name redirection to inject mocks, which fundamentally requires the |
|
@chengkai15 Thanks for the follow-up. All test module Kconfig entries already use |
| if(CONFIG_CM_SERVICE_UTILS_TEST) | ||
| set(SERVICE_UTILS_INCDIR ${CMAKE_CURRENT_LIST_DIR}/include | ||
| ${CMAKE_CURRENT_LIST_DIR}/src | ||
| ${CMAKE_CURRENT_LIST_DIR}/../../../../framework/include |
There was a problem hiding this comment.
Better to have a ${} for the Bluetooth service path so we won't have so many ../.. here.
| CFLAGS += -Wno-implicit-function-declaration | ||
| CFLAGS += -Wno-unused-variable | ||
| CFLAGS += -Wno-strict-prototypes |
There was a problem hiding this comment.
Are these -Wno necessary?
| include(${CMAKE_CURRENT_LIST_DIR}/framework_common/CMakeLists.txt) | ||
| include(${CMAKE_CURRENT_LIST_DIR}/framework_api/CMakeLists.txt) | ||
| include(${CMAKE_CURRENT_LIST_DIR}/service_device/CMakeLists.txt) | ||
| include(${CMAKE_CURRENT_LIST_DIR}/service_manager/CMakeLists.txt) | ||
| include(${CMAKE_CURRENT_LIST_DIR}/service_utils/CMakeLists.txt) | ||
| include(${CMAKE_CURRENT_LIST_DIR}/service_profiles/CMakeLists.txt) |
There was a problem hiding this comment.
file(GLOB test_modules ${CMAKE_CURRENT_LIST_DIR}/*/CMakeLists.txt)
foreach(module ${test_modules})
include(${module})
endforeach()
| source "$APPDIR/frameworks/connectivity/bluetooth/tests/unittest/framework_common/Kconfig" | ||
| source "$APPDIR/frameworks/connectivity/bluetooth/tests/unittest/framework_api/Kconfig" |
There was a problem hiding this comment.
| source "$APPDIR/frameworks/connectivity/bluetooth/tests/unittest/framework_common/Kconfig" | |
| source "$APPDIR/frameworks/connectivity/bluetooth/tests/unittest/framework_api/Kconfig" | |
| source "$APPDIR/frameworks/connectivity/bluetooth/tests/unittest/*/Kconfig" |
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/framework_common/Kconfig" | ||
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/framework_api/Kconfig" | ||
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/service_device/Kconfig" | ||
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/service_manager/Kconfig" | ||
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/service_utils/Kconfig" | ||
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/service_profiles/Kconfig" |
There was a problem hiding this comment.
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/framework_common/Kconfig" | |
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/framework_api/Kconfig" | |
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/service_device/Kconfig" | |
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/service_manager/Kconfig" | |
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/service_utils/Kconfig" | |
| source "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/service_profiles/Kconfig" | |
| osource "$APPSDIR/frameworks/connectivity/bluetooth/tests/unittest/*/Kconfig" |
Background
The Bluetooth stack currently lacks systematic unit test coverage, making it difficult to detect regression issues during code iteration. By adding cmocka-based unit tests, we can significantly improve automated test coverage, laying the foundation for future CI/CD pipeline integration and ensuring code quality and stability of core Bluetooth modules during continuous development.
Changes
Added cmocka unit tests for 6 core Bluetooth service modules, totaling 275 test cases. All tests have been verified on the goldfish-armeabi-v7a-ap emulator.
Module Coverage
New File Structure
Test Approach
#includethe source.cfiles under test to access static functions#definemacrosRelated JIRA
Verification
All 275 test cases PASSED on the goldfish-armeabi-v7a-ap emulator.