117 lines
4.7 KiB
C
117 lines
4.7 KiB
C
/* test_bt_dev.c — 蓝牙 DEV 自识别宿主验证(纯逻辑,bt_dev.c)。
|
||
*
|
||
* 覆盖矩阵(对齐任务B 宿主测试要求):
|
||
* D1 bt_dev_detect_mac:a2dpsrc/<MAC> 端点 → 提取 MAC(含 a2dp_source、多空行、多设备取第一个、
|
||
* 无 a2dpsrc / 空输出 / NULL → -1)。
|
||
* D2 bt_dev_rewrite:真实 alsa.conf 文本 → DEV=、device 两处 MAC 换成目标,保留 codec/其余 token;
|
||
* 同值 → 1(无需改动);无 pcm.bluealsa / 无 DEV= → -1;带引号风格保留。
|
||
*
|
||
* 编译(在 patch_v2/meow_audiod 下):
|
||
* cc -Wall -Wextra -o test_bt_dev test_bt_dev.c bt_dev.c
|
||
* 运行:./test_bt_dev
|
||
*/
|
||
#include "bt_dev.h"
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
|
||
static int g_pass = 0, g_fail = 0;
|
||
static void check(int cond, const char *name)
|
||
{
|
||
if (cond) { printf(" PASS %s\n", name); g_pass++; }
|
||
else { printf(" FAIL %s\n", name); g_fail++; }
|
||
}
|
||
|
||
int main(void)
|
||
{
|
||
setbuf(stdout, NULL);
|
||
|
||
/* ---- D1 设备列表 → 选中 a2dpsrc 连接设备 ---- */
|
||
{
|
||
char mac[32] = "";
|
||
int rc = bt_dev_detect_mac(
|
||
"a2dpsrc/E0:42:6D:79:C5:98\na2dpsnk/E0:42:6D:79:C5:98\n", mac, sizeof mac);
|
||
check(rc == 0 && strcmp(mac, "E0:42:6D:79:C5:98") == 0, "D1a a2dpsrc/<MAC> -> extract MAC");
|
||
}
|
||
{
|
||
char mac[32] = "";
|
||
int rc = bt_dev_detect_mac(
|
||
"profile/aptx\n\na2dp_source/33:DE:2E:21:AD:30\n", mac, sizeof mac);
|
||
check(rc == 0 && strcmp(mac, "33:DE:2E:21:AD:30") == 0, "D1b a2dp_source/<MAC> -> extract MAC");
|
||
}
|
||
{
|
||
char mac[32] = "";
|
||
int rc = bt_dev_detect_mac(
|
||
"a2dpsrc/AA:BB:CC:DD:EE:FF 0\na2dpsrc/11:22:33:44:55:66 1\n", mac, sizeof mac);
|
||
check(rc == 0 && strcmp(mac, "AA:BB:CC:DD:EE:FF") == 0, "D1c multi-device -> first a2dpsrc");
|
||
}
|
||
{
|
||
char mac[32] = "";
|
||
int rc = bt_dev_detect_mac("\n", mac, sizeof mac);
|
||
check(rc == -1, "D1d empty output -> -1");
|
||
}
|
||
{
|
||
char mac[32] = "";
|
||
int rc = bt_dev_detect_mac("a2dpsnk/11:22:33:44:55:66\n", mac, sizeof mac);
|
||
check(rc == -1, "D1e only sink (no a2dpsrc) -> -1");
|
||
}
|
||
{
|
||
char mac[32] = "";
|
||
int rc = bt_dev_detect_mac(NULL, mac, sizeof mac);
|
||
check(rc == -1, "D1f NULL input -> -1");
|
||
}
|
||
|
||
/* ---- D2 DEV 串入 / 保留其它 token ---- */
|
||
{
|
||
const char *conf =
|
||
"pcm.bluealsa:DEV=E0:42:6D:79:C5:98 {\n"
|
||
" type plug\n"
|
||
" slave {\n"
|
||
" pcm {\n"
|
||
" type bluealsa\n"
|
||
" device E0:42:6D:79:C5:98\n"
|
||
" profile \"a2dp\"\n"
|
||
" codec \"LDAC\"\n"
|
||
" ldac_eqmid \"LDAC_ABR\"\n"
|
||
" }\n"
|
||
" }\n"
|
||
"}\n";
|
||
char *out = NULL; size_t olen = 0;
|
||
int rc = bt_dev_rewrite(conf, strlen(conf), "33:DE:2E:21:AD:30", &out, &olen);
|
||
check(rc == 0 && out != NULL, "D2a rewrite returns changed");
|
||
check(out && strstr(out, ":DEV=33:DE:2E:21:AD:30 {"), "D2b DEV= updated");
|
||
check(out && strstr(out, "device 33:DE:2E:21:AD:30\n"), "D2c device updated");
|
||
check(out && strstr(out, "codec \"LDAC\"") && strstr(out, "ldac_eqmid \"LDAC_ABR\""),
|
||
"D2d codec/other tokens preserved");
|
||
/* 旧 MAC 不再出现 */
|
||
check(out && !strstr(out, "E0:42:6D:79:C5:98"), "D2e old MAC gone");
|
||
free(out);
|
||
}
|
||
{
|
||
const char *conf =
|
||
"pcm.bluealsa:DEV=33:DE:2E:21:AD:30 {\n"
|
||
" type plug\n slave {\n pcm {\n"
|
||
" type bluealsa\n device 33:DE:2E:21:AD:30\n"
|
||
" profile \"a2dp\"\n codec \"LDAC\"\n }\n }\n}\n";
|
||
char *out = NULL; size_t olen = 0;
|
||
int rc = bt_dev_rewrite(conf, strlen(conf), "33:DE:2E:21:AD:30", &out, &olen);
|
||
check(rc == 1 && out == NULL, "D2f same MAC -> unchanged (idempotent)");
|
||
}
|
||
{
|
||
const char *conf = "pcm.bluealsa {\n type bluealsa\n device \"E0:42:6D:79:C5:98\"\n}\n";
|
||
char *out = NULL; size_t olen = 0;
|
||
int rc = bt_dev_rewrite(conf, strlen(conf), "33:DE:2E:21:AD:30", &out, &olen);
|
||
check(rc == 0 && out && strstr(out, "device \"33:DE:2E:21:AD:30\""),
|
||
"D2g quoted device preserved (no DEV= token)");
|
||
free(out);
|
||
}
|
||
{
|
||
char *out = NULL; size_t olen = 0;
|
||
int rc = bt_dev_rewrite("pcm.hw {\n type hw\n}\n", 21, "33:DE:2E:21:AD:30", &out, &olen);
|
||
check(rc == -1 && out == NULL, "D2h no pcm.bluealsa -> -1");
|
||
}
|
||
|
||
printf(g_fail ? "\nRESULT: %d FAIL(S)\n" : "\nRESULT: ALL PASS\n", g_fail);
|
||
return g_fail;
|
||
}
|