mesh-cfgtest.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2021 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <assert.h>
  15. #include <ctype.h>
  16. #include <dbus/dbus.h>
  17. #include <dirent.h>
  18. #include <errno.h>
  19. #include <ftw.h>
  20. #include <getopt.h>
  21. #include <libgen.h>
  22. #include <limits.h>
  23. #include <signal.h>
  24. #include <stdio.h>
  25. #include <time.h>
  26. #include <unistd.h>
  27. #include <sys/stat.h>
  28. #include <ell/ell.h>
  29. #include "src/shared/util.h"
  30. #include "mesh/mesh-defs.h"
  31. #include "mesh/mesh.h"
  32. #define MAX_CRPL_SIZE 0x7fff
  33. #define CFG_SRV_MODEL 0x0000
  34. #define CFG_CLI_MODEL 0x0001
  35. #define DEFAULT_IV_INDEX 0x0000
  36. #define IS_CONFIG_MODEL(x) ((x) == CFG_SRV_MODEL || (x) == CFG_CLI_MODEL)
  37. struct meshcfg_el {
  38. const char *path;
  39. uint8_t index;
  40. uint16_t mods[2];
  41. uint32_t vmods[2];
  42. };
  43. struct meshcfg_app {
  44. const char *path;
  45. const char *agent_path;
  46. struct meshcfg_node *node;
  47. uint8_t num_ele;
  48. struct meshcfg_el ele[2];
  49. uint16_t cid;
  50. uint16_t pid;
  51. uint16_t vid;
  52. uint16_t crpl;
  53. uint8_t uuid[16];
  54. };
  55. struct meshcfg_node {
  56. const char *path;
  57. struct l_dbus_proxy *proxy;
  58. struct l_dbus_proxy *mgmt_proxy;
  59. union {
  60. uint64_t u64;
  61. uint8_t u8[8];
  62. } token;
  63. };
  64. struct msg_data {
  65. uint16_t len;
  66. uint8_t data[MAX_MSG_LEN];
  67. };
  68. struct key_data {
  69. uint16_t idx;
  70. bool update;
  71. };
  72. typedef void (*startup_func_t)(const void *data);
  73. struct startup_entry {
  74. startup_func_t func;
  75. void *data;
  76. };
  77. struct test_data {
  78. const char *ele_path;
  79. uint16_t dst;
  80. uint16_t subnet;
  81. void *req;
  82. };
  83. static struct l_tester *tester;
  84. static bool option_list;
  85. static const char *option_prefix;
  86. static const char *option_string;
  87. static char *tester_name;
  88. static bool init_done;
  89. static bool init_failed;
  90. static bool terminated;
  91. static struct l_queue *startup_chain;
  92. static struct l_dbus *dbus;
  93. struct l_dbus_client *client;
  94. static struct l_queue *node_proxies;
  95. static struct l_dbus_proxy *net_proxy;
  96. static char *test_dir;
  97. static char *io;
  98. static char *exe;
  99. static uint32_t iv_index = DEFAULT_IV_INDEX;
  100. static const char *dbus_err_args = "org.freedesktop.DBus.Error.InvalidArgs";
  101. static const char *const cli_app_path = "/mesh/cfgtest/client";
  102. static const char *const cli_agent_path = "/mesh/cfgtest/client/agent";
  103. static const char *const cli_ele_path_00 = "/mesh/cfgtest/client/ele0";
  104. static const char *const srv_app_path = "/mesh/cfgtest/server";
  105. static const char *const srv_agent_path = "/mesh/cfgtest/server/agent";
  106. static const char *const srv_ele_path_00 = "/mesh/cfgtest/server/ele0";
  107. static const char *const srv_ele_path_01 = "/mesh/cfgtest/server/ele1";
  108. static struct meshcfg_app client_app = {
  109. .path = cli_app_path,
  110. .agent_path = cli_agent_path,
  111. .cid = 0x05f1,
  112. .pid = 0x0002,
  113. .vid = 0x0001,
  114. .crpl = MAX_CRPL_SIZE,
  115. .num_ele = 1,
  116. .ele = {
  117. {
  118. .path = cli_ele_path_00,
  119. .index = PRIMARY_ELE_IDX,
  120. .mods = {CFG_SRV_MODEL, CFG_CLI_MODEL},
  121. .vmods = {0xffffffff, 0xffffffff}
  122. }
  123. }
  124. };
  125. static struct meshcfg_app server_app = {
  126. .path = srv_app_path,
  127. .agent_path = srv_agent_path,
  128. .cid = 0x05f1,
  129. .pid = 0x0002,
  130. .vid = 0x0001,
  131. .crpl = MAX_CRPL_SIZE,
  132. .num_ele = 2,
  133. .ele = {
  134. {
  135. .path = srv_ele_path_00,
  136. .index = PRIMARY_ELE_IDX,
  137. .mods = {CFG_SRV_MODEL, 0xffff},
  138. .vmods = {0xffffffff, 0xffffffff}
  139. },
  140. {
  141. .path = srv_ele_path_01,
  142. .index = PRIMARY_ELE_IDX + 1,
  143. .mods = {0x1000, 0xffff},
  144. .vmods = {0x5F10001, 0xffffffff}
  145. }
  146. }
  147. };
  148. static uint8_t import_devkey[16];
  149. static uint8_t import_netkey[16];
  150. static const uint16_t import_netkey_idx = 0x001;
  151. static const uint16_t import_node_unicast = 0xbcd;
  152. static void create_network(const void *data);
  153. static struct startup_entry init_create_client = {
  154. .func = create_network,
  155. .data = NULL,
  156. };
  157. static void import_node(const void *data);
  158. static struct startup_entry init_import_server = {
  159. .func = import_node,
  160. .data = NULL,
  161. };
  162. static void attach_node(const void *data);
  163. static struct startup_entry init_attach_client = {
  164. .func = attach_node,
  165. .data = NULL,
  166. };
  167. static void import_subnet(const void *data);
  168. static struct startup_entry init_import_subnet = {
  169. .func = import_subnet,
  170. .data = NULL,
  171. };
  172. static void import_remote(const void *data);
  173. static struct startup_entry init_import_remote = {
  174. .func = import_remote,
  175. .data = NULL,
  176. };
  177. static struct msg_data init_add_netkey_rsp = {
  178. .len = 5,
  179. .data = {0x80, 0x44, 0x00, 0x01, 0x00}
  180. };
  181. static struct key_data init_add_netkey_req = {
  182. .idx = import_netkey_idx,
  183. .update = false
  184. };
  185. static struct test_data init_add_netkey_data = {
  186. .ele_path = cli_ele_path_00,
  187. .dst = 0x0001,
  188. .subnet = 0x0000,
  189. .req = &init_add_netkey_req
  190. };
  191. static void add_netkey(const void *data);
  192. static struct startup_entry init_add_netkey = {
  193. .func = add_netkey,
  194. .data = &init_add_netkey_data
  195. };
  196. static struct msg_data init_add_appkey_rsp = {
  197. .len = 6,
  198. .data = {0x80, 0x03, 0x00, 0x01, 0x10, 0x00}
  199. };
  200. static struct key_data init_add_appkey_req = {
  201. .idx = 0x001,
  202. .update = false
  203. };
  204. static struct test_data init_add_appkey_data = {
  205. .ele_path = cli_ele_path_00,
  206. .dst = import_node_unicast,
  207. .subnet = import_netkey_idx,
  208. .req = &init_add_appkey_req,
  209. };
  210. static void create_appkey(const void *data);
  211. static struct startup_entry init_create_appkey = {
  212. .func = create_appkey,
  213. .data = &init_add_appkey_data
  214. };
  215. static void add_appkey(const void *data);
  216. static struct startup_entry init_add_appkey = {
  217. .func = add_appkey,
  218. .data = &init_add_appkey_data
  219. };
  220. static struct msg_data test_add_appkey_rsp = {
  221. .len = 6,
  222. .data = {0x80, 0x03, 0x00, 0x01, 0x20, 0x00}
  223. };
  224. static struct key_data test_add_appkey_req = {
  225. .idx = 0x002,
  226. .update = false
  227. };
  228. static struct test_data test_add_appkey = {
  229. .ele_path = cli_ele_path_00,
  230. .dst = import_node_unicast,
  231. .subnet = import_netkey_idx,
  232. .req = &test_add_appkey_req,
  233. };
  234. static struct test_data common_route = {
  235. .ele_path = cli_ele_path_00,
  236. .dst = import_node_unicast,
  237. .subnet = import_netkey_idx,
  238. };
  239. static struct msg_data test_set_ttl_rsp = {
  240. .len = 3,
  241. .data = { 0x80, 0x0E, 0x7}
  242. };
  243. static struct msg_data test_set_ttl_req = {
  244. .len = 3,
  245. .data = { 0x80, 0x0D, 0x7}
  246. };
  247. static struct msg_data test_bind_rsp = {
  248. .len = 9,
  249. .data = { 0x80, 0x3E, 0x00, 0xCE, 0x0B, 0x01, 0x00, 0x00, 0x10},
  250. };
  251. static struct msg_data test_bind_req = {
  252. .len = 8,
  253. .data = { 0x80, 0x3D, 0xCE, 0x0B, 0x01, 0x00, 0x00, 0x10}
  254. };
  255. static struct msg_data test_bind_inv_mod_rsp = {
  256. .len = 9,
  257. .data = { 0x80, 0x3E, 0x02, 0xCE, 0x0B, 0x01, 0x00, 0x00, 0x11},
  258. };
  259. static struct msg_data test_bind_inv_mod_req = {
  260. .len = 8,
  261. .data = { 0x80, 0x3D, 0xCE, 0x0B, 0x01, 0x00, 0x00, 0x11}
  262. };
  263. static struct msg_data test_dev_comp_rsp = {
  264. .len = 28,
  265. .data = { 0x02, 0x00, 0xf1, 0x05, 0x02, 0x00, 0x01, 0x00, 0xff, 0x7f,
  266. 0x05, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  267. 0x00, 0x01, 0x01, 0x00, 0x10, 0xf1, 0x05, 0x01, 0x00}
  268. };
  269. static struct msg_data test_dev_comp_req = {
  270. .len = 3,
  271. .data = {0x80, 0x08, 0x00}
  272. };
  273. static void append_byte_array(struct l_dbus_message_builder *builder,
  274. unsigned char *data, unsigned int len)
  275. {
  276. unsigned int i;
  277. l_dbus_message_builder_enter_array(builder, "y");
  278. for (i = 0; i < len; i++)
  279. l_dbus_message_builder_append_basic(builder, 'y', &(data[i]));
  280. l_dbus_message_builder_leave_array(builder);
  281. }
  282. static void append_dict_entry_basic(struct l_dbus_message_builder *builder,
  283. const char *key, const char *signature,
  284. const void *data)
  285. {
  286. if (!builder)
  287. return;
  288. l_dbus_message_builder_enter_dict(builder, "sv");
  289. l_dbus_message_builder_append_basic(builder, 's', key);
  290. l_dbus_message_builder_enter_variant(builder, signature);
  291. l_dbus_message_builder_append_basic(builder, signature[0], data);
  292. l_dbus_message_builder_leave_variant(builder);
  293. l_dbus_message_builder_leave_dict(builder);
  294. }
  295. static void init_continue(void *data)
  296. {
  297. struct startup_entry *next_step;
  298. enum l_tester_stage stage = l_tester_get_stage(tester);
  299. if (l_queue_isempty(startup_chain) &&
  300. stage == L_TESTER_STAGE_PRE_SETUP) {
  301. l_tester_pre_setup_complete(tester);
  302. init_done = true;
  303. return;
  304. }
  305. next_step = l_queue_pop_head(startup_chain);
  306. next_step->func(next_step->data);
  307. }
  308. static void test_success(void *user_data)
  309. {
  310. l_tester_test_passed(tester);
  311. }
  312. static void test_fail(void *user_data)
  313. {
  314. l_tester_test_failed(tester);
  315. }
  316. static void try_set_node_proxy(void *a, void *b)
  317. {
  318. struct l_dbus_proxy *proxy = a;
  319. struct meshcfg_node *node = b;
  320. const char *interface = l_dbus_proxy_get_interface(proxy);
  321. const char *path = l_dbus_proxy_get_path(proxy);
  322. if (strcmp(node->path, path))
  323. return;
  324. if (!strcmp(interface, MESH_MANAGEMENT_INTERFACE))
  325. node->mgmt_proxy = proxy;
  326. else if (!strcmp(interface, MESH_NODE_INTERFACE))
  327. node->proxy = proxy;
  328. }
  329. static void generic_reply(struct l_dbus_proxy *proxy,
  330. struct l_dbus_message *msg, void *user_data)
  331. {
  332. if (l_dbus_message_is_error(msg)) {
  333. const char *name;
  334. l_dbus_message_get_error(msg, &name, NULL);
  335. l_error("D-Bus call failed: %s", name);
  336. l_idle_oneshot(test_fail, NULL, NULL);
  337. }
  338. }
  339. static void send_cfg_msg_setup(struct l_dbus_message *msg, void *user_data)
  340. {
  341. struct msg_data *req = user_data;
  342. struct l_dbus_message_builder *builder;
  343. bool remote = true;
  344. builder = l_dbus_message_builder_new(msg);
  345. l_dbus_message_builder_append_basic(builder, 'o',
  346. common_route.ele_path);
  347. l_dbus_message_builder_append_basic(builder, 'q', &common_route.dst);
  348. l_dbus_message_builder_append_basic(builder, 'b', &remote);
  349. l_dbus_message_builder_append_basic(builder, 'q', &common_route.subnet);
  350. /* Options */
  351. l_dbus_message_builder_enter_array(builder, "{sv}");
  352. l_dbus_message_builder_enter_dict(builder, "sv");
  353. l_dbus_message_builder_leave_dict(builder);
  354. l_dbus_message_builder_leave_array(builder);
  355. /* Data */
  356. append_byte_array(builder, req->data, req->len);
  357. l_dbus_message_builder_finalize(builder);
  358. l_dbus_message_builder_destroy(builder);
  359. }
  360. static void send_cfg_msg(const void *data)
  361. {
  362. struct meshcfg_node *node = client_app.node;
  363. l_dbus_proxy_method_call(node->proxy, "DevKeySend",
  364. send_cfg_msg_setup, generic_reply,
  365. (void *) data, NULL);
  366. }
  367. static void add_key_setup(struct l_dbus_message *msg, void *user_data)
  368. {
  369. struct test_data *tst = user_data;
  370. struct key_data *req = tst->req;
  371. struct l_dbus_message_builder *builder;
  372. builder = l_dbus_message_builder_new(msg);
  373. l_dbus_message_builder_append_basic(builder, 'o', tst->ele_path);
  374. l_dbus_message_builder_append_basic(builder, 'q', &tst->dst);
  375. l_dbus_message_builder_append_basic(builder, 'q', &req->idx);
  376. l_dbus_message_builder_append_basic(builder, 'q', &tst->subnet);
  377. l_dbus_message_builder_append_basic(builder, 'b', &req->update);
  378. l_dbus_message_builder_finalize(builder);
  379. l_dbus_message_builder_destroy(builder);
  380. }
  381. static void add_appkey(const void *data)
  382. {
  383. struct meshcfg_node *node = client_app.node;
  384. l_dbus_proxy_method_call(node->proxy, "AddAppKey", add_key_setup,
  385. generic_reply, (void *) data, NULL);
  386. }
  387. static void add_netkey(const void *data)
  388. {
  389. struct meshcfg_node *node = client_app.node;
  390. l_dbus_proxy_method_call(node->proxy, "AddNetKey", add_key_setup,
  391. generic_reply, (void *) data, NULL);
  392. }
  393. static void create_appkey_reply(struct l_dbus_proxy *proxy,
  394. struct l_dbus_message *msg, void *user_data)
  395. {
  396. enum l_tester_stage stage = l_tester_get_stage(tester);
  397. if (l_dbus_message_is_error(msg)) {
  398. const char *name;
  399. l_dbus_message_get_error(msg, &name, NULL);
  400. l_info("Add key failed: %s", name);
  401. if (stage == L_TESTER_STAGE_PRE_SETUP) {
  402. init_failed = true;
  403. l_tester_pre_setup_failed(tester);
  404. } else
  405. l_tester_setup_failed(tester);
  406. } else {
  407. if (stage == L_TESTER_STAGE_PRE_SETUP)
  408. l_idle_oneshot(init_continue, NULL, NULL);
  409. else
  410. l_tester_setup_complete(tester);
  411. }
  412. }
  413. static void create_appkey_setup(struct l_dbus_message *msg, void *user_data)
  414. {
  415. struct test_data *tst = user_data;
  416. struct key_data *req = tst->req;
  417. l_dbus_message_set_arguments(msg, "qq", tst->subnet, req->idx);
  418. }
  419. static void create_appkey(const void *data)
  420. {
  421. struct meshcfg_node *node = client_app.node;
  422. if (!node || !node->proxy || !node->mgmt_proxy) {
  423. l_info("Node is not attached\n");
  424. l_tester_setup_failed(tester);
  425. return;
  426. }
  427. l_dbus_proxy_method_call(node->mgmt_proxy, "CreateAppKey",
  428. create_appkey_setup, create_appkey_reply,
  429. (void *) data, NULL);
  430. }
  431. static void import_remote_reply(struct l_dbus_proxy *proxy,
  432. struct l_dbus_message *msg, void *user_data)
  433. {
  434. enum l_tester_stage stage = l_tester_get_stage(tester);
  435. if (l_dbus_message_is_error(msg)) {
  436. const char *name;
  437. l_dbus_message_get_error(msg, &name, NULL);
  438. l_info("Import remote call failed: %s", name);
  439. if (stage == L_TESTER_STAGE_PRE_SETUP) {
  440. init_failed = true;
  441. l_tester_pre_setup_failed(tester);
  442. }
  443. return;
  444. }
  445. if (stage == L_TESTER_STAGE_PRE_SETUP)
  446. l_idle_oneshot(init_continue, NULL, NULL);
  447. else
  448. l_tester_test_abort(tester);
  449. }
  450. static void import_remote_setup(struct l_dbus_message *msg, void *user_data)
  451. {
  452. struct l_dbus_message_builder *builder;
  453. builder = l_dbus_message_builder_new(msg);
  454. l_dbus_message_builder_append_basic(builder, 'q', &import_node_unicast);
  455. l_dbus_message_builder_append_basic(builder, 'y', &server_app.num_ele);
  456. append_byte_array(builder, import_devkey, 16);
  457. l_dbus_message_builder_finalize(builder);
  458. l_dbus_message_builder_destroy(builder);
  459. }
  460. static void import_remote(const void *data)
  461. {
  462. struct meshcfg_node *node = client_app.node;
  463. if (!node || !node->mgmt_proxy) {
  464. l_tester_test_abort(tester);
  465. return;
  466. }
  467. l_dbus_proxy_method_call(node->mgmt_proxy, "ImportRemoteNode",
  468. import_remote_setup, import_remote_reply,
  469. NULL, NULL);
  470. }
  471. static void import_subnet_reply(struct l_dbus_proxy *proxy,
  472. struct l_dbus_message *msg, void *user_data)
  473. {
  474. enum l_tester_stage stage = l_tester_get_stage(tester);
  475. if (stage != L_TESTER_STAGE_PRE_SETUP)
  476. return;
  477. if (l_dbus_message_is_error(msg)) {
  478. const char *name;
  479. l_dbus_message_get_error(msg, &name, NULL);
  480. l_info("Import subnet failed: %s", name);
  481. init_failed = true;
  482. return l_tester_pre_setup_failed(tester);
  483. }
  484. l_idle_oneshot(init_continue, NULL, NULL);
  485. }
  486. static void import_subnet_setup(struct l_dbus_message *msg, void *user_data)
  487. {
  488. struct l_dbus_message_builder *builder;
  489. builder = l_dbus_message_builder_new(msg);
  490. l_dbus_message_builder_append_basic(builder, 'q', &import_netkey_idx);
  491. append_byte_array(builder, import_netkey, 16);
  492. l_dbus_message_builder_finalize(builder);
  493. l_dbus_message_builder_destroy(builder);
  494. }
  495. static void import_subnet(const void *data)
  496. {
  497. struct meshcfg_node *node = client_app.node;
  498. if (!node || !node->mgmt_proxy) {
  499. l_tester_test_abort(tester);
  500. return;
  501. }
  502. l_dbus_proxy_method_call(node->mgmt_proxy, "ImportSubnet",
  503. import_subnet_setup, import_subnet_reply,
  504. NULL, NULL);
  505. }
  506. static void attach_node_reply(struct l_dbus_proxy *proxy,
  507. struct l_dbus_message *msg, void *user_data)
  508. {
  509. struct meshcfg_app *app = user_data;
  510. struct meshcfg_node *node = app->node;
  511. struct l_dbus_message_iter iter_cfg;
  512. uint32_t ivi;
  513. enum l_tester_stage stage = l_tester_get_stage(tester);
  514. if (l_dbus_message_is_error(msg)) {
  515. const char *name;
  516. l_dbus_message_get_error(msg, &name, NULL);
  517. l_error("Failed to attach node: %s", name);
  518. goto fail;
  519. }
  520. if (!l_dbus_message_get_arguments(msg, "oa(ya(qa{sv}))",
  521. &node->path, &iter_cfg))
  522. goto fail;
  523. l_info("Attached with path %s\n", node->path);
  524. /* Populate node's proxies */
  525. l_queue_foreach(node_proxies, try_set_node_proxy, node);
  526. /* Remove from orphaned proxies list */
  527. if (node->proxy)
  528. l_queue_remove(node_proxies, node->proxy);
  529. if (node->mgmt_proxy)
  530. l_queue_remove(node_proxies, node->mgmt_proxy);
  531. if (l_dbus_proxy_get_property(node->proxy, "IvIndex", "u", &ivi) &&
  532. ivi != iv_index)
  533. iv_index = ivi;
  534. if (stage == L_TESTER_STAGE_PRE_SETUP)
  535. l_idle_oneshot(init_continue, NULL, NULL);
  536. return;
  537. fail:
  538. l_free(node);
  539. app->node = NULL;
  540. if (stage == L_TESTER_STAGE_PRE_SETUP) {
  541. init_failed = true;
  542. l_tester_pre_setup_failed(tester);
  543. }
  544. }
  545. static void attach_node_setup(struct l_dbus_message *msg, void *user_data)
  546. {
  547. struct meshcfg_app *app = user_data;
  548. l_dbus_message_set_arguments(msg, "ot", app->path,
  549. l_get_be64(app->node->token.u8));
  550. }
  551. static void attach_node(const void *data)
  552. {
  553. struct meshcfg_node *node = client_app.node;
  554. if (!node) {
  555. l_tester_test_abort(tester);
  556. return;
  557. }
  558. l_dbus_proxy_method_call(net_proxy, "Attach",
  559. attach_node_setup, attach_node_reply,
  560. &client_app, NULL);
  561. }
  562. static struct l_dbus_message *join_complete(struct l_dbus *dbus,
  563. struct l_dbus_message *message,
  564. void *user_data)
  565. {
  566. struct meshcfg_app *app = user_data;
  567. uint64_t tmp;
  568. enum l_tester_stage stage = l_tester_get_stage(tester);
  569. if (!l_dbus_message_get_arguments(message, "t", &tmp)) {
  570. if (stage == L_TESTER_STAGE_PRE_SETUP) {
  571. init_failed = true;
  572. l_tester_pre_setup_failed(tester);
  573. } else
  574. l_idle_oneshot(test_fail, NULL, NULL);
  575. return l_dbus_message_new_error(message, dbus_err_args, NULL);
  576. }
  577. app->node = l_new(struct meshcfg_node, 1);
  578. app->node->token.u64 = l_get_be64(&tmp);
  579. if (stage == L_TESTER_STAGE_PRE_SETUP)
  580. l_idle_oneshot(init_continue, NULL, NULL);
  581. return l_dbus_message_new_method_return(message);
  582. }
  583. static void create_net_reply(struct l_dbus_proxy *proxy,
  584. struct l_dbus_message *msg, void *user_data)
  585. {
  586. if (l_dbus_message_is_error(msg)) {
  587. const char *name;
  588. l_dbus_message_get_error(msg, &name, NULL);
  589. l_error("Failed to create network: %s", name);
  590. l_tester_setup_failed(tester);
  591. }
  592. }
  593. static void create_net_setup(struct l_dbus_message *msg, void *user_data)
  594. {
  595. struct l_dbus_message_builder *builder;
  596. /* Generate random UUID */
  597. l_uuid_v4(client_app.uuid);
  598. builder = l_dbus_message_builder_new(msg);
  599. l_dbus_message_builder_append_basic(builder, 'o', client_app.path);
  600. append_byte_array(builder, client_app.uuid, 16);
  601. l_dbus_message_builder_finalize(builder);
  602. l_dbus_message_builder_destroy(builder);
  603. }
  604. static void create_network(const void *data)
  605. {
  606. l_dbus_proxy_method_call(net_proxy, "CreateNetwork", create_net_setup,
  607. create_net_reply, &client_app,
  608. NULL);
  609. }
  610. static void import_node_reply(struct l_dbus_proxy *proxy,
  611. struct l_dbus_message *msg, void *user_data)
  612. {
  613. if (l_dbus_message_is_error(msg)) {
  614. const char *name;
  615. l_dbus_message_get_error(msg, &name, NULL);
  616. l_error("Failed to import local node: %s", name);
  617. l_idle_oneshot(test_fail, NULL, NULL);
  618. return;
  619. }
  620. }
  621. static void import_node_setup(struct l_dbus_message *msg, void *user_data)
  622. {
  623. struct l_dbus_message_builder *builder;
  624. bool iv_update = false;
  625. bool key_refresh = false;
  626. /* Generate random UUID, DevKey, NetKey */
  627. l_uuid_v4(server_app.uuid);
  628. l_getrandom(import_netkey, sizeof(import_netkey));
  629. l_getrandom(import_devkey, sizeof(import_devkey));
  630. builder = l_dbus_message_builder_new(msg);
  631. l_dbus_message_builder_append_basic(builder, 'o', server_app.path);
  632. append_byte_array(builder, server_app.uuid, 16);
  633. append_byte_array(builder, import_devkey, 16);
  634. append_byte_array(builder, import_netkey, 16);
  635. l_dbus_message_builder_append_basic(builder, 'q', &import_netkey_idx);
  636. l_dbus_message_builder_enter_array(builder, "{sv}");
  637. append_dict_entry_basic(builder, "IvUpdate", "b", &iv_update);
  638. append_dict_entry_basic(builder, "KeyRefresh", "b", &key_refresh);
  639. l_dbus_message_builder_leave_array(builder);
  640. l_dbus_message_builder_append_basic(builder, 'u', &iv_index);
  641. l_dbus_message_builder_append_basic(builder, 'q', &import_node_unicast);
  642. l_dbus_message_builder_finalize(builder);
  643. l_dbus_message_builder_destroy(builder);
  644. }
  645. static void import_node(const void *data)
  646. {
  647. l_dbus_proxy_method_call(net_proxy, "Import", import_node_setup,
  648. import_node_reply, &server_app,
  649. NULL);
  650. }
  651. static void proxy_added(struct l_dbus_proxy *proxy, void *user_data)
  652. {
  653. const char *interface = l_dbus_proxy_get_interface(proxy);
  654. const char *path = l_dbus_proxy_get_path(proxy);
  655. l_info("Proxy added: %s (%s)\n", interface, path);
  656. if (!strcmp(interface, MESH_NETWORK_INTERFACE)) {
  657. net_proxy = proxy;
  658. return;
  659. }
  660. if (!strcmp(interface, MESH_MANAGEMENT_INTERFACE)) {
  661. if (client_app.node && client_app.node->path) {
  662. if (!strcmp(client_app.node->path, path)) {
  663. client_app.node->mgmt_proxy = proxy;
  664. return;
  665. }
  666. }
  667. if (server_app.node && server_app.node->path) {
  668. if (!strcmp(server_app.node->path, path)) {
  669. server_app.node->mgmt_proxy = proxy;
  670. return;
  671. }
  672. }
  673. l_queue_push_tail(node_proxies, proxy);
  674. return;
  675. }
  676. if (!strcmp(interface, MESH_NODE_INTERFACE)) {
  677. if (client_app.node && client_app.node->path) {
  678. if (!strcmp(client_app.node->path, path)) {
  679. client_app.node->proxy = proxy;
  680. return;
  681. }
  682. }
  683. if (server_app.node && server_app.node->path) {
  684. if (!strcmp(server_app.node->path, path)) {
  685. server_app.node->proxy = proxy;
  686. return;
  687. }
  688. }
  689. l_queue_push_tail(node_proxies, proxy);
  690. }
  691. }
  692. static void proxy_removed(struct l_dbus_proxy *proxy, void *user_data)
  693. {
  694. const char *interface = l_dbus_proxy_get_interface(proxy);
  695. const char *path = l_dbus_proxy_get_path(proxy);
  696. l_info("Proxy removed: %s (%s)\n", interface, path);
  697. if (!strcmp(interface, MESH_NETWORK_INTERFACE)) {
  698. l_info("Mesh removed, terminating.\n");
  699. l_main_quit();
  700. return;
  701. }
  702. l_queue_remove(node_proxies, proxy);
  703. }
  704. static void build_model(struct l_dbus_message_builder *builder, uint16_t mod_id,
  705. bool pub_enable, bool sub_enable)
  706. {
  707. l_dbus_message_builder_enter_struct(builder, "qa{sv}");
  708. l_dbus_message_builder_append_basic(builder, 'q', &mod_id);
  709. l_dbus_message_builder_enter_array(builder, "{sv}");
  710. append_dict_entry_basic(builder, "Subscribe", "b", &sub_enable);
  711. append_dict_entry_basic(builder, "Publish", "b", &pub_enable);
  712. l_dbus_message_builder_leave_array(builder);
  713. l_dbus_message_builder_leave_struct(builder);
  714. }
  715. static bool mod_getter(struct l_dbus *dbus,
  716. struct l_dbus_message *message,
  717. struct l_dbus_message_builder *builder,
  718. void *user_data)
  719. {
  720. struct meshcfg_el *ele = user_data;
  721. uint32_t i;
  722. l_dbus_message_builder_enter_array(builder, "(qa{sv})");
  723. for (i = 0; i < L_ARRAY_SIZE(ele->mods); i++) {
  724. bool is_cfg = IS_CONFIG_MODEL(ele->mods[i]);
  725. if (ele->mods[i] == 0xffff)
  726. continue;
  727. build_model(builder, ele->mods[i], !is_cfg, !is_cfg);
  728. }
  729. l_dbus_message_builder_leave_array(builder);
  730. return true;
  731. }
  732. static void build_vmodel(struct l_dbus_message_builder *builder, uint16_t vid,
  733. uint16_t mod, bool pub_enable, bool sub_enable)
  734. {
  735. l_dbus_message_builder_enter_struct(builder, "qqa{sv}");
  736. l_dbus_message_builder_append_basic(builder, 'q', &vid);
  737. l_dbus_message_builder_append_basic(builder, 'q', &mod);
  738. l_dbus_message_builder_enter_array(builder, "{sv}");
  739. append_dict_entry_basic(builder, "Subscribe", "b", &sub_enable);
  740. append_dict_entry_basic(builder, "Publish", "b", &pub_enable);
  741. l_dbus_message_builder_leave_array(builder);
  742. l_dbus_message_builder_leave_struct(builder);
  743. }
  744. static bool vmod_getter(struct l_dbus *dbus,
  745. struct l_dbus_message *message,
  746. struct l_dbus_message_builder *builder,
  747. void *user_data)
  748. {
  749. struct meshcfg_el *ele = user_data;
  750. uint32_t i;
  751. l_dbus_message_builder_enter_array(builder, "(qqa{sv})");
  752. for (i = 0; i < L_ARRAY_SIZE(ele->vmods); i++) {
  753. if (ele->vmods[i] == 0xffffffff)
  754. continue;
  755. build_vmodel(builder, ele->vmods[i] >> 16,
  756. ele->vmods[i] & 0xffff, true, true);
  757. }
  758. l_dbus_message_builder_leave_array(builder);
  759. return true;
  760. }
  761. static bool ele_idx_getter(struct l_dbus *dbus,
  762. struct l_dbus_message *message,
  763. struct l_dbus_message_builder *builder,
  764. void *user_data)
  765. {
  766. struct meshcfg_el *ele = user_data;
  767. l_dbus_message_builder_append_basic(builder, 'y', &ele->index);
  768. return true;
  769. }
  770. static struct l_dbus_message *dev_msg_recv_call(struct l_dbus *dbus,
  771. struct l_dbus_message *msg,
  772. void *user_data)
  773. {
  774. struct msg_data *rsp;
  775. struct l_dbus_message_iter iter;
  776. uint16_t src, idx;
  777. uint8_t *data;
  778. uint32_t n;
  779. bool rmt;
  780. enum l_tester_stage stage = l_tester_get_stage(tester);
  781. if (!l_dbus_message_get_arguments(msg, "qbqay", &src, &rmt, &idx,
  782. &iter)) {
  783. l_error("Cannot parse received message");
  784. return l_dbus_message_new_error(msg, dbus_err_args, NULL);
  785. }
  786. if (!l_dbus_message_iter_get_fixed_array(&iter, &data, &n)) {
  787. l_error("Cannot parse received message: data");
  788. return l_dbus_message_new_error(msg, dbus_err_args, NULL);
  789. }
  790. printf("Received dev key message (len %u):", n);
  791. {
  792. uint32_t i;
  793. for (i = 0; i < n; i++)
  794. printf("%x ", data[i]);
  795. printf("\n");
  796. }
  797. if (stage == L_TESTER_STAGE_PRE_SETUP) {
  798. if (n == init_add_netkey_rsp.len &&
  799. !memcmp(data, init_add_netkey_rsp.data, n))
  800. l_idle_oneshot(init_continue, NULL, NULL);
  801. else if (n == init_add_appkey_rsp.len &&
  802. !memcmp(data, init_add_appkey_rsp.data, n))
  803. l_idle_oneshot(init_continue, NULL, NULL);
  804. else {
  805. init_failed = true;
  806. l_tester_pre_setup_failed(tester);
  807. }
  808. } else {
  809. rsp = l_tester_get_data(tester);
  810. if (rsp && rsp->len == n && !memcmp(data, rsp->data, n))
  811. l_idle_oneshot(test_success, NULL, NULL);
  812. else
  813. l_idle_oneshot(test_fail, NULL, NULL);
  814. }
  815. return l_dbus_message_new_method_return(msg);
  816. }
  817. static void setup_ele_iface(struct l_dbus_interface *iface)
  818. {
  819. /* Properties */
  820. l_dbus_interface_property(iface, "Index", 0, "y", ele_idx_getter,
  821. NULL);
  822. l_dbus_interface_property(iface, "VendorModels", 0, "a(qqa{sv})",
  823. vmod_getter, NULL);
  824. l_dbus_interface_property(iface, "Models", 0, "a(qa{sv})", mod_getter,
  825. NULL);
  826. /* Methods */
  827. l_dbus_interface_method(iface, "DevKeyMessageReceived", 0,
  828. dev_msg_recv_call, "", "qbqay", "source",
  829. "remote", "net_index", "data");
  830. /* TODO: Other methods? */
  831. }
  832. static bool cid_getter(struct l_dbus *dbus,
  833. struct l_dbus_message *message,
  834. struct l_dbus_message_builder *builder,
  835. void *user_data)
  836. {
  837. struct meshcfg_app *app = user_data;
  838. l_dbus_message_builder_append_basic(builder, 'q', &app->cid);
  839. return true;
  840. }
  841. static bool pid_getter(struct l_dbus *dbus,
  842. struct l_dbus_message *message,
  843. struct l_dbus_message_builder *builder,
  844. void *user_data)
  845. {
  846. struct meshcfg_app *app = user_data;
  847. l_dbus_message_builder_append_basic(builder, 'q', &app->pid);
  848. return true;
  849. }
  850. static bool vid_getter(struct l_dbus *dbus,
  851. struct l_dbus_message *message,
  852. struct l_dbus_message_builder *builder,
  853. void *user_data)
  854. {
  855. struct meshcfg_app *app = user_data;
  856. l_dbus_message_builder_append_basic(builder, 'q', &app->vid);
  857. return true;
  858. }
  859. static bool crpl_getter(struct l_dbus *dbus,
  860. struct l_dbus_message *message,
  861. struct l_dbus_message_builder *builder,
  862. void *user_data)
  863. {
  864. struct meshcfg_app *app = user_data;
  865. l_dbus_message_builder_append_basic(builder, 'q', &app->crpl);
  866. return true;
  867. }
  868. static void property_changed(struct l_dbus_proxy *proxy, const char *name,
  869. struct l_dbus_message *msg, void *user_data)
  870. {
  871. struct meshcfg_app *app = user_data;
  872. struct meshcfg_node *node = app->node;
  873. const char *interface = l_dbus_proxy_get_interface(proxy);
  874. const char *path = l_dbus_proxy_get_path(proxy);
  875. if (strcmp(path, node->path))
  876. return;
  877. printf("Property changed: %s %s %s\n", name, path, interface);
  878. if (!strcmp(interface, "org.bluez.mesh.Node1")) {
  879. if (!strcmp(name, "IvIndex")) {
  880. uint32_t ivi;
  881. if (!l_dbus_message_get_arguments(msg, "u", &ivi))
  882. return;
  883. printf("New IV Index: %u\n", ivi);
  884. }
  885. }
  886. }
  887. static void setup_app_iface(struct l_dbus_interface *iface)
  888. {
  889. l_dbus_interface_property(iface, "CompanyID", 0, "q", cid_getter,
  890. NULL);
  891. l_dbus_interface_property(iface, "VersionID", 0, "q", vid_getter,
  892. NULL);
  893. l_dbus_interface_property(iface, "ProductID", 0, "q", pid_getter,
  894. NULL);
  895. l_dbus_interface_property(iface, "CRPL", 0, "q", crpl_getter, NULL);
  896. l_dbus_interface_method(iface, "JoinComplete", 0, join_complete,
  897. "", "t", "token");
  898. /* TODO: Other methods? */
  899. }
  900. static bool register_app_iface(void)
  901. {
  902. if (!l_dbus_register_interface(dbus, MESH_APPLICATION_INTERFACE,
  903. setup_app_iface, NULL, false)) {
  904. l_error("Failed to register interface %s",
  905. MESH_APPLICATION_INTERFACE);
  906. return false;
  907. }
  908. if (!l_dbus_register_interface(dbus, MESH_ELEMENT_INTERFACE,
  909. setup_ele_iface, NULL, false)) {
  910. l_error("Failed to register interface %s",
  911. MESH_ELEMENT_INTERFACE);
  912. return false;
  913. }
  914. return true;
  915. }
  916. static bool register_app(struct meshcfg_app *app)
  917. {
  918. uint32_t i;
  919. if (!l_dbus_register_object(dbus, app->path, NULL, NULL,
  920. MESH_APPLICATION_INTERFACE, app,
  921. NULL)) {
  922. l_error("Failed to register object %s", app->path);
  923. return false;
  924. }
  925. for (i = 0; i < L_ARRAY_SIZE(app->ele) && i < app->num_ele; i++) {
  926. if (!l_dbus_register_object(dbus, app->ele[i].path, NULL, NULL,
  927. MESH_ELEMENT_INTERFACE, &app->ele[i], NULL)) {
  928. l_error("Failed to register obj %s", app->ele[i].path);
  929. l_dbus_unregister_interface(dbus,
  930. MESH_ELEMENT_INTERFACE);
  931. return false;
  932. }
  933. }
  934. if (!l_dbus_object_add_interface(dbus, app->path,
  935. L_DBUS_INTERFACE_OBJECT_MANAGER, NULL)) {
  936. l_error("Failed to add interface %s",
  937. L_DBUS_INTERFACE_OBJECT_MANAGER);
  938. return false;
  939. }
  940. return true;
  941. }
  942. static void client_ready(struct l_dbus_client *client, void *user_data)
  943. {
  944. enum l_tester_stage stage = l_tester_get_stage(tester);
  945. printf("D-Bus client ready\n");
  946. if (!register_app_iface() || !register_app(&client_app) ||
  947. !register_app(&server_app))
  948. return;
  949. if (stage == L_TESTER_STAGE_PRE_SETUP)
  950. init_continue(NULL);
  951. }
  952. static void client_connected(struct l_dbus *dbus, void *user_data)
  953. {
  954. printf("D-Bus client connected\n");
  955. }
  956. static void client_disconnected(struct l_dbus *dbus, void *user_data)
  957. {
  958. printf("D-Bus client disconnected, exit\n");
  959. l_main_exit();
  960. }
  961. static void ready_callback(void *user_data)
  962. {
  963. printf("Connected to D-Bus\n");
  964. if (l_dbus_object_manager_enable(dbus, "/"))
  965. return;
  966. printf("Failed to register the ObjectManager\n");
  967. init_failed = true;
  968. l_tester_pre_setup_failed(tester);
  969. }
  970. static void init_test(const void *data)
  971. {
  972. if (init_failed) {
  973. l_tester_pre_setup_failed(tester);
  974. return;
  975. }
  976. if (init_done) {
  977. l_tester_pre_setup_complete(tester);
  978. return;
  979. }
  980. node_proxies = l_queue_new();
  981. dbus = l_dbus_new_default(L_DBUS_SESSION_BUS);
  982. l_dbus_set_ready_handler(dbus, ready_callback, NULL, NULL);
  983. client = l_dbus_client_new(dbus, BLUEZ_MESH_NAME, "/org/bluez/mesh");
  984. l_dbus_client_set_connect_handler(client, client_connected, NULL, NULL);
  985. l_dbus_client_set_disconnect_handler(client, client_disconnected, NULL,
  986. NULL);
  987. l_dbus_client_set_proxy_handlers(client, proxy_added, proxy_removed,
  988. property_changed, NULL, NULL);
  989. l_dbus_client_set_ready_handler(client, client_ready, NULL, NULL);
  990. }
  991. static void init_startup_chain(void)
  992. {
  993. startup_chain = l_queue_new();
  994. l_queue_push_tail(startup_chain, &init_create_client);
  995. l_queue_push_tail(startup_chain, &init_import_server);
  996. l_queue_push_tail(startup_chain, &init_attach_client);
  997. l_queue_push_tail(startup_chain, &init_import_subnet);
  998. l_queue_push_tail(startup_chain, &init_import_remote);
  999. l_queue_push_tail(startup_chain, &init_add_netkey);
  1000. l_queue_push_tail(startup_chain, &init_create_appkey);
  1001. l_queue_push_tail(startup_chain, &init_add_appkey);
  1002. }
  1003. static int del_fobject(const char *fpath, const struct stat *sb, int typeflag,
  1004. struct FTW *ftwbuf)
  1005. {
  1006. switch (typeflag) {
  1007. case FTW_DP:
  1008. rmdir(fpath);
  1009. break;
  1010. case FTW_SL:
  1011. default:
  1012. remove(fpath);
  1013. break;
  1014. }
  1015. return 0;
  1016. }
  1017. #define tester_add_with_response(name, test_data, test_func, rsp_data) \
  1018. l_tester_add_full(tester, name, test_data, init_test, NULL, \
  1019. test_func, NULL, NULL, 2, rsp_data, NULL)
  1020. static void signal_callback(unsigned int signum, void *user_data)
  1021. {
  1022. switch (signum) {
  1023. case SIGINT:
  1024. case SIGTERM:
  1025. if (!terminated)
  1026. l_main_quit();
  1027. terminated = true;
  1028. break;
  1029. }
  1030. }
  1031. static void done_callback(struct l_tester *tester)
  1032. {
  1033. if (terminated)
  1034. return;
  1035. l_main_quit();
  1036. terminated = true;
  1037. }
  1038. static const struct option options[] = {
  1039. { "version", no_argument, NULL, 'v' },
  1040. { "list", no_argument, NULL, 'l' },
  1041. { "prefix", required_argument, NULL, 'p' },
  1042. { "string", required_argument, NULL, 's' },
  1043. { }
  1044. };
  1045. static void usage(void)
  1046. {
  1047. fprintf(stderr,
  1048. "Usage:\n"
  1049. "\%s [options]\n", tester_name);
  1050. fprintf(stderr,
  1051. "Options:\n"
  1052. "\t-v, --version Show version information and exit\n"
  1053. "\t-l, --list Only list the tests to be run\n"
  1054. "\t-p, --prefix Run tests matching the provided prefix\n"
  1055. "\t-s, --string Run tests matching the provided string\n");
  1056. }
  1057. static void parse_options(int *argc, char ***argv)
  1058. {
  1059. tester_name = strrchr(*argv[0], '/');
  1060. for (;;) {
  1061. int opt;
  1062. opt = getopt_long(*argc, *argv, "s:p:vl", options, NULL);
  1063. if (opt < 0)
  1064. break;
  1065. switch (opt) {
  1066. case 'v':
  1067. printf("%s\n", VERSION);
  1068. exit(EXIT_SUCCESS);
  1069. case 'l':
  1070. option_list = true;
  1071. break;
  1072. case 'p':
  1073. option_prefix = optarg;
  1074. break;
  1075. case 's':
  1076. option_string = optarg;
  1077. break;
  1078. default:
  1079. usage();
  1080. exit(0);
  1081. }
  1082. }
  1083. }
  1084. static bool setup_test_dir(void)
  1085. {
  1086. char *bluez_dir;
  1087. char buf[PATH_MAX];
  1088. ssize_t len;
  1089. len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
  1090. if (len == -1)
  1091. return false;
  1092. buf[len] = '\0';
  1093. test_dir = l_strdup_printf("/tmp/mesh");
  1094. nftw(test_dir, del_fobject, 5, FTW_DEPTH | FTW_PHYS);
  1095. if (mkdir(test_dir, 0700) != 0) {
  1096. printf("Failed to create dir %s\n", test_dir);
  1097. l_free(test_dir);
  1098. return false;
  1099. }
  1100. bluez_dir = dirname(dirname(buf));
  1101. exe = l_strdup_printf("%s/mesh/bluetooth-meshd", bluez_dir);
  1102. io = l_strdup_printf("unit:%s/%s", test_dir, "test_sk");
  1103. return true;
  1104. }
  1105. int main(int argc, char *argv[])
  1106. {
  1107. int status = EXIT_SUCCESS, pid = -1;
  1108. if (!setup_test_dir())
  1109. return EXIT_FAILURE;
  1110. l_log_set_stderr();
  1111. l_main_init();
  1112. parse_options(&argc, &argv);
  1113. if (!option_list) {
  1114. pid = fork();
  1115. if (pid < 0)
  1116. return EXIT_FAILURE;
  1117. if (pid == 0) {
  1118. char *const dargs[] = {
  1119. exe,
  1120. "--io",
  1121. io,
  1122. "-s",
  1123. test_dir,
  1124. NULL
  1125. };
  1126. printf("spawning %s --io %s -s %s", exe, io, test_dir);
  1127. execv(exe, dargs);
  1128. return EXIT_SUCCESS;
  1129. }
  1130. }
  1131. tester = l_tester_new(option_prefix, option_string, option_list);
  1132. init_startup_chain();
  1133. l_tester_add_full(tester, "Config AppKey Add: Success",
  1134. &test_add_appkey, init_test, create_appkey, add_appkey,
  1135. NULL, NULL, 2, &test_add_appkey_rsp, NULL);
  1136. tester_add_with_response("Config Default TTL Set: Success",
  1137. &test_set_ttl_req, send_cfg_msg,
  1138. &test_set_ttl_rsp);
  1139. tester_add_with_response("Config Get Device Composition: Success",
  1140. &test_dev_comp_req, send_cfg_msg,
  1141. &test_dev_comp_rsp);
  1142. tester_add_with_response("Config Bind: Success",
  1143. &test_bind_req, send_cfg_msg,
  1144. &test_bind_rsp);
  1145. tester_add_with_response("Config Bind: Error Invalid Model",
  1146. &test_bind_inv_mod_req, send_cfg_msg,
  1147. &test_bind_inv_mod_rsp);
  1148. l_tester_start(tester, done_callback);
  1149. if (!option_list && !terminated) {
  1150. l_main_run_with_signal(signal_callback, NULL);
  1151. if (!l_tester_summarize(tester))
  1152. status = EXIT_FAILURE;
  1153. }
  1154. l_queue_destroy(startup_chain, NULL);
  1155. l_queue_destroy(node_proxies, NULL);
  1156. l_free(client_app.node);
  1157. l_free(server_app.node);
  1158. l_dbus_client_destroy(client);
  1159. l_dbus_destroy(dbus);
  1160. l_tester_destroy(tester);
  1161. if (pid > 0)
  1162. kill(pid, SIGTERM);
  1163. l_free(test_dir);
  1164. l_free(io);
  1165. l_free(exe);
  1166. return status;
  1167. }