1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
|
/*
*
* BlueZ - Bluetooth protocol stack for Linux
*
* Copyright (C) 2014 Intel Corporation. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <sys/socket.h>
#include <unistd.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <glib.h>
#include "src/shared/hfp.h"
struct context {
GMainLoop *main_loop;
guint watch_id;
int fd_server;
int fd_client;
struct hfp_gw *hfp;
const struct test_data *data;
unsigned int pdu_offset;
};
struct test_pdu {
bool valid;
const uint8_t *data;
size_t size;
enum hfp_gw_cmd_type type;
bool fragmented;
};
struct test_data {
char *test_name;
struct test_pdu *pdu_list;
hfp_result_func_t result_func;
};
#define data(args...) ((const unsigned char[]) { args })
#define raw_pdu(args...) \
{ \
.valid = true, \
.data = data(args), \
.size = sizeof(data(args)), \
}
#define data_end() \
{ \
.valid = false, \
}
#define type_pdu(cmd_type, args...) \
{ \
.valid = true, \
.data = data(args), \
.size = sizeof(data(args)), \
.type = cmd_type, \
}
#define frg_pdu(args...) \
{ \
.valid = true, \
.data = data(args), \
.size = sizeof(data(args)), \
.fragmented = true, \
}
#define define_test(name, function, result_function, args...) \
do { \
const struct test_pdu pdus[] = { \
args, { } \
}; \
static struct test_data data; \
data.test_name = g_strdup(name); \
data.pdu_list = g_malloc(sizeof(pdus)); \
data.result_func = result_function; \
memcpy(data.pdu_list, pdus, sizeof(pdus)); \
g_test_add_data_func(name, &data, function); \
} while (0)
static void context_quit(struct context *context)
{
g_main_loop_quit(context->main_loop);
}
static void test_free(gconstpointer user_data)
{
const struct test_data *data = user_data;
g_free(data->test_name);
g_free(data->pdu_list);
}
static gboolean test_handler(GIOChannel *channel, GIOCondition cond,
gpointer user_data)
{
struct context *context = user_data;
const struct test_pdu *pdu;
pdu = &context->data->pdu_list[context->pdu_offset++];
g_assert(!pdu->valid);
context_quit(context);
context->watch_id = 0;
return FALSE;
}
static void cmd_handler(const char *command, void *user_data)
{
struct context *context = user_data;
const struct test_pdu *pdu;
unsigned int cmd_len = strlen(command);
pdu = &context->data->pdu_list[context->pdu_offset++];
g_assert(cmd_len == pdu->size);
g_assert(!memcmp(command, pdu->data, cmd_len));
hfp_gw_send_result(context->hfp, HFP_RESULT_ERROR);
}
static void prefix_handler(struct hfp_gw_result *result,
enum hfp_gw_cmd_type type, void *user_data)
{
struct context *context = user_data;
const struct test_pdu *pdu;
pdu = &context->data->pdu_list[context->pdu_offset++];
g_assert(type == pdu->type);
hfp_gw_send_result(context->hfp, HFP_RESULT_ERROR);
}
static struct context *create_context(gconstpointer data)
{
struct context *context = g_new0(struct context, 1);
GIOChannel *channel;
int err, sv[2];
context->main_loop = g_main_loop_new(NULL, FALSE);
g_assert(context->main_loop);
err = socketpair(AF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC, 0, sv);
g_assert(err == 0);
channel = g_io_channel_unix_new(sv[1]);
g_io_channel_set_close_on_unref(channel, TRUE);
g_io_channel_set_encoding(channel, NULL, NULL);
g_io_channel_set_buffered(channel, FALSE);
context->watch_id = g_io_add_watch(channel,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
test_handler, context);
g_assert(context->watch_id > 0);
g_io_channel_unref(channel);
context->fd_server = sv[1];
context->fd_client = sv[0];
context->data = data;
return context;
}
static void execute_context(struct context *context)
{
g_main_loop_run(context->main_loop);
if (context->watch_id)
g_source_remove(context->watch_id);
g_main_loop_unref(context->main_loop);
test_free(context->data);
if (context->hfp)
hfp_gw_unref(context->hfp);
g_free(context);
}
static void test_init(gconstpointer data)
{
struct context *context = create_context(data);
context->hfp = hfp_gw_new(context->fd_client);
g_assert(context->hfp);
g_assert(hfp_gw_set_close_on_unref(context->hfp, true));
hfp_gw_unref(context->hfp);
context->hfp = NULL;
execute_context(context);
}
static void test_command_handler(gconstpointer data)
{
struct context *context = create_context(data);
const struct test_pdu *pdu;
ssize_t len;
bool ret;
context->hfp = hfp_gw_new(context->fd_client);
g_assert(context->hfp);
pdu = &context->data->pdu_list[context->pdu_offset++];
ret = hfp_gw_set_close_on_unref(context->hfp, true);
g_assert(ret);
ret = hfp_gw_set_command_handler(context->hfp, cmd_handler,
context, NULL);
g_assert(ret);
len = write(context->fd_server, pdu->data, pdu->size);
g_assert_cmpint(len, ==, pdu->size);
execute_context(context);
}
static void test_register(gconstpointer data)
{
struct context *context = create_context(data);
const struct test_pdu *pdu;
ssize_t len;
bool ret;
context->hfp = hfp_gw_new(context->fd_client);
g_assert(context->hfp);
pdu = &context->data->pdu_list[context->pdu_offset++];
ret = hfp_gw_set_close_on_unref(context->hfp, true);
g_assert(ret);
if (context->data->result_func) {
ret = hfp_gw_register(context->hfp, context->data->result_func,
(char *)pdu->data, context, NULL);
g_assert(ret);
}
pdu = &context->data->pdu_list[context->pdu_offset++];
len = write(context->fd_server, pdu->data, pdu->size);
g_assert_cmpint(len, ==, pdu->size);
execute_context(context);
}
static gboolean send_pdu(gpointer user_data)
{
struct context *context = user_data;
const struct test_pdu *pdu;
ssize_t len;
pdu = &context->data->pdu_list[context->pdu_offset++];
len = write(context->fd_server, pdu->data, pdu->size);
g_assert_cmpint(len, ==, pdu->size);
pdu = &context->data->pdu_list[context->pdu_offset];
if (pdu->fragmented)
g_idle_add(send_pdu, context);
return FALSE;
}
static void test_fragmented(gconstpointer data)
{
struct context *context = create_context(data);
bool ret;
context->hfp = hfp_gw_new(context->fd_client);
g_assert(context->hfp);
ret = hfp_gw_set_close_on_unref(context->hfp, true);
g_assert(ret);
g_idle_add(send_pdu, context);
execute_context(context);
}
static void check_ustring_1(struct hfp_gw_result *result,
enum hfp_gw_cmd_type type, void *user_data)
{
struct context *context = user_data;
const struct test_pdu *pdu;
unsigned int i = 3, j = 0;
char str[10];
pdu = &context->data->pdu_list[context->pdu_offset++];
g_assert(type == pdu->type);
g_assert(hfp_gw_result_get_unquoted_string(result, str, sizeof(str)));
while (context->data->pdu_list[1].data[i] != '\r') {
g_assert(j < sizeof(str));
g_assert(str[j] == context->data->pdu_list[1].data[i]);
i++;
j++;
}
g_assert(str[j] == '\0');
hfp_gw_send_result(context->hfp, HFP_RESULT_ERROR);
}
static void check_ustring_2(struct hfp_gw_result *result,
enum hfp_gw_cmd_type type, void *user_data)
{
struct context *context = user_data;
const struct test_pdu *pdu;
char str[10];
memset(str, 'X', sizeof(str));
pdu = &context->data->pdu_list[context->pdu_offset++];
g_assert(type == pdu->type);
g_assert(!hfp_gw_result_get_unquoted_string(result, str, 3));
g_assert(str[3] == 'X');
hfp_gw_send_result(context->hfp, HFP_RESULT_ERROR);
}
static void check_string_1(struct hfp_gw_result *result,
enum hfp_gw_cmd_type type, void *user_data)
{
struct context *context = user_data;
const struct test_pdu *pdu;
unsigned int i = 4, j = 0;
char str[10];
pdu = &context->data->pdu_list[context->pdu_offset++];
g_assert(type == pdu->type);
g_assert(hfp_gw_result_get_string(result, str, sizeof(str)));
while (context->data->pdu_list[1].data[i] != '\"') {
g_assert(j < sizeof(str));
g_assert(str[j] == context->data->pdu_list[1].data[i]);
i++;
j++;
}
g_assert(context->data->pdu_list[1].data[i] == '\"');
g_assert(str[j] == '\0');
hfp_gw_send_result(context->hfp, HFP_RESULT_ERROR);
}
static void check_string_2(struct hfp_gw_result *result,
enum hfp_gw_cmd_type type, void *user_data)
{
struct context *context = user_data;
const struct test_pdu *pdu;
char str[10];
memset(str, 'X', sizeof(str));
pdu = &context->data->pdu_list[context->pdu_offset++];
g_assert(type == pdu->type);
g_assert(!hfp_gw_result_get_string(result, str, 3));
g_assert(str[3] == 'X');
hfp_gw_send_result(context->hfp, HFP_RESULT_ERROR);
}
int main(int argc, char *argv[])
{
g_test_init(&argc, &argv, NULL);
define_test("/hfp/test_init", test_init, NULL, data_end());
define_test("/hfp/test_cmd_handler_1", test_command_handler, NULL,
raw_pdu('A', 'T', '+', 'B', 'R', 'S', 'F', '\r'),
raw_pdu('A', 'T', '+', 'B', 'R', 'S', 'F'),
data_end());
define_test("/hfp/test_cmd_handler_2", test_command_handler, NULL,
raw_pdu('A', 'T', 'D', '1', '2', '3', '4', '\r'),
raw_pdu('A', 'T', 'D', '1', '2', '3', '4'),
data_end());
define_test("/hfp/test_register_1", test_register, prefix_handler,
raw_pdu('+', 'B', 'R', 'S', 'F', '\0'),
raw_pdu('A', 'T', '+', 'B', 'R', 'S', 'F', '\r'),
type_pdu(HFP_GW_CMD_TYPE_COMMAND, 0),
data_end());
define_test("/hfp/test_register_2", test_register, prefix_handler,
raw_pdu('+', 'B', 'R', 'S', 'F', '\0'),
raw_pdu('A', 'T', '+', 'B', 'R', 'S', 'F', '=', '\r'),
type_pdu(HFP_GW_CMD_TYPE_SET, 0),
data_end());
define_test("/hfp/test_register_3", test_register, prefix_handler,
raw_pdu('+', 'B', 'R', 'S', 'F', '\0'),
raw_pdu('A', 'T', '+', 'B', 'R', 'S', 'F', '?', '\r'),
type_pdu(HFP_GW_CMD_TYPE_READ, 0),
data_end());
define_test("/hfp/test_register_4", test_register, prefix_handler,
raw_pdu('+', 'B', 'R', 'S', 'F', '\0'),
raw_pdu('A', 'T', '+', 'B', 'R', 'S', 'F', '=', '?',
'\r'),
type_pdu(HFP_GW_CMD_TYPE_TEST, 0),
data_end());
define_test("/hfp/test_register_5", test_register, prefix_handler,
raw_pdu('D', '\0'),
raw_pdu('A', 'T', 'D', '1', '2', '3', '4', '5', '\r'),
type_pdu(HFP_GW_CMD_TYPE_SET, 0),
data_end());
define_test("/hfp/test_fragmented_1", test_fragmented, NULL,
frg_pdu('A'), frg_pdu('T'), frg_pdu('+'), frg_pdu('B'),
frg_pdu('R'), frg_pdu('S'), frg_pdu('F'), frg_pdu('\r'),
data_end());
define_test("/hfp/test_ustring_1", test_register, check_ustring_1,
raw_pdu('D', '\0'),
raw_pdu('A', 'T', 'D', '0', '1', '2', '3', '\r'),
type_pdu(HFP_GW_CMD_TYPE_SET, 0),
data_end());
define_test("/hfp/test_ustring_2", test_register, check_ustring_2,
raw_pdu('D', '\0'),
raw_pdu('A', 'T', 'D', '0', '1', '2', '3', '\r'),
type_pdu(HFP_GW_CMD_TYPE_SET, 0),
data_end());
define_test("/hfp/test_string_1", test_register, check_string_1,
raw_pdu('D', '\0'),
raw_pdu('A', 'T', 'D', '\"', '0', '1', '2', '3', '\"',
'\r'),
type_pdu(HFP_GW_CMD_TYPE_SET, 0),
data_end());
define_test("/hfp/test_string_2", test_register, check_string_2,
raw_pdu('D', '\0'),
raw_pdu('A', 'T', 'D', '\"', '0', '1', '2', '3', '\"',
'\r'),
type_pdu(HFP_GW_CMD_TYPE_SET, 0),
data_end());
define_test("/hfp/test_empty", test_fragmented, NULL,
raw_pdu('\r'),
data_end());
return g_test_run();
}
|