umockdev.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175
  1. /*
  2. * libusb umockdev based tests
  3. *
  4. * Copyright (C) 2022 Benjamin Berg <bberg@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "config.h"
  20. #include <glib.h>
  21. #include <glib/gstdio.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <linux/ioctl.h>
  26. #include <linux/usbdevice_fs.h>
  27. #include "libusb.h"
  28. #include "umockdev.h"
  29. #define UNUSED_DATA __attribute__ ((unused)) gconstpointer unused_data
  30. /* avoid leak reports inside assertions; leaking stuff on assertion failures does not matter in tests */
  31. #if !defined(__clang__)
  32. #pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
  33. #pragma GCC diagnostic ignored "-Wanalyzer-file-leak"
  34. #endif
  35. typedef struct {
  36. pid_t thread;
  37. libusb_context *ctx;
  38. enum libusb_log_level level;
  39. char *str;
  40. } LogMessage;
  41. static void
  42. log_message_free(LogMessage *msg)
  43. {
  44. g_free(msg->str);
  45. g_free(msg);
  46. }
  47. G_DEFINE_AUTOPTR_CLEANUP_FUNC(LogMessage, log_message_free)
  48. typedef struct _UsbChat UsbChat;
  49. struct _UsbChat {
  50. gboolean submit;
  51. gboolean reap;
  52. UsbChat *reaps;
  53. UsbChat *next;
  54. /* struct usbdevfs_urb */
  55. unsigned char type;
  56. unsigned char endpoint;
  57. int status;
  58. unsigned int flags;
  59. const unsigned char *buffer;
  60. int buffer_length;
  61. int actual_length;
  62. /* <submit urb> */
  63. UMockdevIoctlData *submit_urb;
  64. };
  65. typedef struct {
  66. UMockdevTestbed *testbed;
  67. UMockdevIoctlBase *handler;
  68. struct libusb_context *ctx;
  69. gchar *root_dir;
  70. gchar *sys_dir;
  71. gboolean libusb_log_silence;
  72. GList *libusb_log;
  73. UsbChat *chat;
  74. GList *flying_urbs;
  75. GList *discarded_urbs;
  76. /* GMutex confuses tsan unecessarily */
  77. pthread_mutex_t mutex;
  78. } UMockdevTestbedFixture;
  79. /* Global for log handler */
  80. static UMockdevTestbedFixture *cur_fixture = NULL;
  81. static void
  82. log_handler(libusb_context *ctx, enum libusb_log_level level, const char *str)
  83. {
  84. /* May be called from different threads without synchronization! */
  85. LogMessage *msg;
  86. pid_t tid = gettid();
  87. g_assert (cur_fixture != NULL);
  88. g_assert(pthread_mutex_lock(&cur_fixture->mutex) == 0);
  89. msg = g_new0(LogMessage, 1);
  90. msg->ctx = ctx;
  91. msg->level = level;
  92. msg->str = g_strchomp (g_strdup(str));
  93. msg->thread = tid;
  94. if (!cur_fixture->libusb_log_silence)
  95. g_printerr("%s\n", msg->str);
  96. cur_fixture->libusb_log = g_list_append(cur_fixture->libusb_log, msg);
  97. pthread_mutex_unlock(&cur_fixture->mutex);
  98. }
  99. static void
  100. log_handler_null(libusb_context *ctx, enum libusb_log_level level, const char *str)
  101. {
  102. (void) ctx;
  103. (void) level;
  104. (void) str;
  105. }
  106. static void
  107. clear_libusb_log(UMockdevTestbedFixture * fixture, enum libusb_log_level level)
  108. {
  109. g_assert(pthread_mutex_lock(&fixture->mutex) == 0);
  110. while (fixture->libusb_log) {
  111. LogMessage *msg = fixture->libusb_log->data;
  112. g_assert(msg->ctx == fixture->ctx);
  113. if (msg->level < level) {
  114. pthread_mutex_unlock(&fixture->mutex);
  115. return;
  116. }
  117. fixture->libusb_log = g_list_delete_link(fixture->libusb_log, fixture->libusb_log);
  118. log_message_free(msg);
  119. }
  120. pthread_mutex_unlock(&fixture->mutex);
  121. }
  122. static void
  123. assert_libusb_log_msg(UMockdevTestbedFixture * fixture, enum libusb_log_level level, const char *re)
  124. {
  125. g_assert(pthread_mutex_lock(&fixture->mutex) == 0);
  126. while (fixture->libusb_log) {
  127. g_autoptr(LogMessage) msg = NULL;
  128. if (fixture->libusb_log == NULL)
  129. g_error ("No level %d message found searching for %s", level, re);
  130. msg = fixture->libusb_log->data;
  131. fixture->libusb_log = g_list_delete_link(fixture->libusb_log, fixture->libusb_log);
  132. if (msg->ctx != fixture->ctx)
  133. g_error ("Saw unexpected message \"%s\" from context %p while %p was expected",
  134. msg->str, msg->ctx, fixture->ctx);
  135. if (msg->level == level && g_regex_match_simple(re, msg->str, 0, 0)) {
  136. pthread_mutex_unlock(&fixture->mutex);
  137. return;
  138. }
  139. /* Allow skipping INFO and DEBUG messages */
  140. if (msg->level >= LIBUSB_LOG_LEVEL_INFO)
  141. continue;
  142. g_error ("Searched for \"%s\" (%d) but found \"%s\" (%d)", re, level, msg->str, msg->level);
  143. }
  144. pthread_mutex_unlock(&fixture->mutex);
  145. g_error ("Searched for \"%s\" (%d) but no message matched", re, level);
  146. }
  147. static void
  148. assert_libusb_no_log_msg(UMockdevTestbedFixture * fixture, enum libusb_log_level level, const char *re)
  149. {
  150. g_assert(pthread_mutex_lock(&fixture->mutex) == 0);
  151. while (fixture->libusb_log) {
  152. g_autoptr(LogMessage) msg = NULL;
  153. gboolean matching;
  154. msg = fixture->libusb_log->data;
  155. fixture->libusb_log = g_list_delete_link(fixture->libusb_log, fixture->libusb_log);
  156. g_assert(msg->ctx == fixture->ctx);
  157. matching = (msg->level == level && g_regex_match_simple(re, msg->str, 0, 0));
  158. /* Allow skipping INFO and DEBUG messages */
  159. if (!matching && msg->level >= LIBUSB_LOG_LEVEL_INFO)
  160. continue;
  161. g_error ("Asserting \"%s\" (%d) not logged and found \"%s\" (%d)", re, level, msg->str, msg->level);
  162. }
  163. pthread_mutex_unlock(&fixture->mutex);
  164. }
  165. static void
  166. dump_buffer(const unsigned char *buffer, int len)
  167. {
  168. g_autoptr(GString) line = NULL;
  169. line = g_string_new ("");
  170. for (gint i = 0; i < len; i++) {
  171. g_string_append_printf(line, "%02x ", buffer[i]);
  172. if ((i + 1) % 16 == 0) {
  173. g_printerr(" %s\n", line->str);
  174. g_string_set_size(line, 0);
  175. }
  176. }
  177. if (line->len)
  178. g_printerr(" %s\n", line->str);
  179. }
  180. static gint
  181. cmp_ioctl_data_addr(const void *data, const void *addr)
  182. {
  183. return ((const UMockdevIoctlData*) data)->client_addr != (gulong) addr;
  184. }
  185. static gboolean
  186. handle_ioctl_cb (UMockdevIoctlBase *handler, UMockdevIoctlClient *client, UMockdevTestbedFixture *fixture)
  187. {
  188. UMockdevIoctlData *ioctl_arg;
  189. long int request;
  190. struct usbdevfs_urb *urb;
  191. (void) handler;
  192. request = umockdev_ioctl_client_get_request (client);
  193. ioctl_arg = umockdev_ioctl_client_get_arg (client);
  194. /* NOTE: We share the address space, dereferencing pointers *will* work.
  195. * However, to make tsan work, we still stick to the API that resolves
  196. * the data into a local copy! */
  197. switch (request) {
  198. case USBDEVFS_GET_CAPABILITIES: {
  199. g_autoptr(UMockdevIoctlData) d = NULL;
  200. d = umockdev_ioctl_data_resolve(ioctl_arg, 0, sizeof(guint32), NULL);
  201. *(guint32*) d->data = USBDEVFS_CAP_BULK_SCATTER_GATHER |
  202. USBDEVFS_CAP_BULK_CONTINUATION |
  203. USBDEVFS_CAP_NO_PACKET_SIZE_LIM |
  204. USBDEVFS_CAP_REAP_AFTER_DISCONNECT |
  205. USBDEVFS_CAP_ZERO_PACKET;
  206. umockdev_ioctl_client_complete(client, 0, 0);
  207. return TRUE;
  208. }
  209. case USBDEVFS_CLAIMINTERFACE:
  210. case USBDEVFS_RELEASEINTERFACE:
  211. case USBDEVFS_CLEAR_HALT:
  212. case USBDEVFS_RESET:
  213. case USBDEVFS_RESETEP:
  214. umockdev_ioctl_client_complete(client, 0, 0);
  215. return TRUE;
  216. case USBDEVFS_SUBMITURB: {
  217. g_autoptr(UMockdevIoctlData) urb_buffer = NULL;
  218. g_autoptr(UMockdevIoctlData) urb_data = NULL;
  219. gsize buflen;
  220. if (!fixture->chat || !fixture->chat->submit)
  221. return FALSE;
  222. buflen = fixture->chat->buffer_length;
  223. if (fixture->chat->type == USBDEVFS_URB_TYPE_CONTROL)
  224. buflen = 8;
  225. urb_data = umockdev_ioctl_data_resolve(ioctl_arg, 0, sizeof(struct usbdevfs_urb), NULL);
  226. urb = (struct usbdevfs_urb*) urb_data->data;
  227. urb_buffer = umockdev_ioctl_data_resolve(urb_data, G_STRUCT_OFFSET(struct usbdevfs_urb, buffer), urb->buffer_length, NULL);
  228. if (fixture->chat->type == urb->type &&
  229. fixture->chat->endpoint == urb->endpoint &&
  230. fixture->chat->buffer_length == urb->buffer_length &&
  231. (fixture->chat->buffer == NULL || memcmp (fixture->chat->buffer, urb_buffer->data, buflen) == 0)) {
  232. fixture->flying_urbs = g_list_append (fixture->flying_urbs, umockdev_ioctl_data_ref(urb_data));
  233. if (fixture->chat->reaps)
  234. fixture->chat->reaps->submit_urb = urb_data;
  235. if (fixture->chat->status)
  236. umockdev_ioctl_client_complete(client, -1, -fixture->chat->status);
  237. else
  238. umockdev_ioctl_client_complete(client, 0, 0);
  239. if (fixture->chat->next)
  240. fixture->chat = fixture->chat->next;
  241. else
  242. fixture->chat += 1;
  243. return TRUE;
  244. }
  245. /* chat message didn't match, don't accept it */
  246. g_printerr("Could not process submit urb:\n");
  247. g_printerr(" t: %d, ep: %d, actual_length: %d, buffer_length: %d\n",
  248. urb->type, urb->endpoint, urb->actual_length, urb->buffer_length);
  249. if (urb->type == USBDEVFS_URB_TYPE_CONTROL || urb->endpoint & LIBUSB_ENDPOINT_IN)
  250. dump_buffer(urb->buffer, urb->buffer_length);
  251. g_printerr("Looking for:\n");
  252. g_printerr(" t: %d, ep: %d, actual_length: %d, buffer_length: %d\n",
  253. fixture->chat->type, fixture->chat->endpoint,
  254. fixture->chat->actual_length, fixture->chat->buffer_length);
  255. if (fixture->chat->buffer)
  256. dump_buffer(fixture->chat->buffer, buflen);
  257. return FALSE;
  258. }
  259. case USBDEVFS_REAPURB:
  260. case USBDEVFS_REAPURBNDELAY: {
  261. g_autoptr(UMockdevIoctlData) urb_ptr = NULL;
  262. g_autoptr(UMockdevIoctlData) urb_data = NULL;
  263. if (fixture->discarded_urbs) {
  264. urb_data = fixture->discarded_urbs->data;
  265. urb = (struct usbdevfs_urb*) urb_data->data;
  266. fixture->discarded_urbs = g_list_delete_link(fixture->discarded_urbs, fixture->discarded_urbs);
  267. urb->status = -ENOENT;
  268. urb_ptr = umockdev_ioctl_data_resolve(ioctl_arg, 0, sizeof(gpointer), NULL);
  269. umockdev_ioctl_data_set_ptr(urb_ptr, 0, urb_data);
  270. umockdev_ioctl_client_complete(client, 0, 0);
  271. return TRUE;
  272. }
  273. if (fixture->chat && fixture->chat->reap) {
  274. GList *l = g_list_find(fixture->flying_urbs, fixture->chat->submit_urb);
  275. if (l) {
  276. fixture->flying_urbs = g_list_remove_link(fixture->flying_urbs, fixture->flying_urbs);
  277. urb_data = fixture->chat->submit_urb;
  278. urb = (struct usbdevfs_urb*) urb_data->data;
  279. urb->actual_length = fixture->chat->actual_length;
  280. if (urb->type == USBDEVFS_URB_TYPE_CONTROL && urb->actual_length)
  281. urb->actual_length -= 8;
  282. if (fixture->chat->buffer)
  283. memcpy(urb->buffer, fixture->chat->buffer, fixture->chat->actual_length);
  284. urb->status = fixture->chat->status;
  285. urb_ptr = umockdev_ioctl_data_resolve(ioctl_arg, 0, sizeof(gpointer), NULL);
  286. umockdev_ioctl_data_set_ptr(urb_ptr, 0, urb_data);
  287. if (fixture->chat->next)
  288. fixture->chat = fixture->chat->next;
  289. else
  290. fixture->chat += 1;
  291. umockdev_ioctl_client_complete(client, 0, 0);
  292. return TRUE;
  293. }
  294. }
  295. /* Nothing to reap */
  296. umockdev_ioctl_client_complete(client, -1, EAGAIN);
  297. return TRUE;
  298. }
  299. case USBDEVFS_DISCARDURB: {
  300. GList *l = g_list_find_custom(fixture->flying_urbs, *(void**) ioctl_arg->data, cmp_ioctl_data_addr);
  301. if (l) {
  302. fixture->discarded_urbs = g_list_append(fixture->discarded_urbs, l->data);
  303. fixture->flying_urbs = g_list_delete_link(fixture->flying_urbs, l);
  304. umockdev_ioctl_client_complete(client, 0, 0);
  305. } else {
  306. umockdev_ioctl_client_complete(client, -1, EINVAL);
  307. }
  308. return TRUE;
  309. }
  310. default:
  311. return FALSE;
  312. }
  313. }
  314. static void
  315. test_fixture_add_canon(UMockdevTestbedFixture * fixture)
  316. {
  317. /* Setup first, so we can be sure libusb_open works when the add uevent
  318. * happens.
  319. */
  320. g_assert_cmpint(umockdev_testbed_attach_ioctl(fixture->testbed, "/dev/bus/usb/001/001", fixture->handler, NULL), ==, 1);
  321. /* NOTE: add_device would not create a file, needed for device emulation */
  322. /* XXX: Racy, see https://github.com/martinpitt/umockdev/issues/173 */
  323. umockdev_testbed_add_from_string(fixture->testbed,
  324. "P: /devices/usb1\n"
  325. "N: bus/usb/001/001\n"
  326. "E: SUBSYSTEM=usb\n"
  327. "E: DRIVER=usb\n"
  328. "E: BUSNUM=001\n"
  329. "E: DEVNUM=001\n"
  330. "E: DEVNAME=/dev/bus/usb/001/001\n"
  331. "E: DEVTYPE=usb_device\n"
  332. "A: bConfigurationValue=1\\n\n"
  333. "A: busnum=1\\n\n"
  334. "A: devnum=1\\n\n"
  335. "A: bConfigurationValue=1\\n\n"
  336. "A: speed=480\\n\n"
  337. /* descriptor from a Canon PowerShot SX200; VID 04a9 PID 31c0 */
  338. "H: descriptors="
  339. "1201000200000040a904c03102000102"
  340. "030109022700010100c0010904000003"
  341. "06010100070581020002000705020200"
  342. "020007058303080009\n",
  343. NULL);
  344. }
  345. static void
  346. test_fixture_setup_libusb(UMockdevTestbedFixture * fixture, int devcount)
  347. {
  348. libusb_device **devs = NULL;
  349. libusb_init (&fixture->ctx);
  350. /* Supress global log messages completely
  351. * (though, in some tests it might be interesting to check there are no real ones).
  352. */
  353. libusb_set_log_cb (NULL, log_handler_null, LIBUSB_LOG_CB_GLOBAL);
  354. libusb_set_option (fixture->ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
  355. g_assert_cmpint(libusb_get_device_list(fixture->ctx, &devs), ==, devcount);
  356. libusb_free_device_list(devs, TRUE);
  357. libusb_set_log_cb (fixture->ctx, log_handler, LIBUSB_LOG_CB_CONTEXT);
  358. }
  359. static void
  360. test_fixture_setup_common(UMockdevTestbedFixture * fixture)
  361. {
  362. g_assert(cur_fixture == NULL);
  363. cur_fixture = fixture;
  364. pthread_mutex_init(&fixture->mutex, NULL);
  365. fixture->testbed = umockdev_testbed_new();
  366. g_assert(fixture->testbed != NULL);
  367. fixture->root_dir = umockdev_testbed_get_root_dir(fixture->testbed);
  368. fixture->sys_dir = umockdev_testbed_get_sys_dir(fixture->testbed);
  369. fixture->handler = umockdev_ioctl_base_new();
  370. g_object_connect(fixture->handler, "signal-after::handle-ioctl", handle_ioctl_cb, fixture, NULL);
  371. }
  372. static void
  373. test_fixture_setup_empty(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  374. {
  375. test_fixture_setup_common(fixture);
  376. test_fixture_setup_libusb(fixture, 0);
  377. }
  378. static void
  379. test_fixture_setup_with_canon(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  380. {
  381. test_fixture_setup_common(fixture);
  382. test_fixture_add_canon(fixture);
  383. test_fixture_setup_libusb(fixture, 1);
  384. }
  385. static void
  386. test_fixture_teardown(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  387. {
  388. g_assert(cur_fixture == fixture);
  389. /* Abort if there are any warnings/errors in the log */
  390. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_INFO);
  391. if (fixture->ctx) {
  392. libusb_device **devs = NULL;
  393. int count = libusb_get_device_list(fixture->ctx, &devs);
  394. libusb_free_device_list(devs, TRUE);
  395. libusb_exit (fixture->ctx);
  396. /* libusb_exit should result in the correct number of devices being destroyed */
  397. for (int i = 0; i < count; i++)
  398. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_DEBUG, "libusb_unref_device");
  399. assert_libusb_no_log_msg(fixture, LIBUSB_LOG_LEVEL_DEBUG, "libusb_unref_device");
  400. }
  401. libusb_set_log_cb (NULL, NULL, LIBUSB_LOG_CB_GLOBAL);
  402. cur_fixture = NULL;
  403. /* Abort if there are any warnings/errors in the log */
  404. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_INFO);
  405. fixture->ctx = NULL;
  406. g_assert_null(fixture->libusb_log);
  407. g_clear_object(&fixture->handler);
  408. g_clear_object(&fixture->testbed);
  409. /* verify that temp dir gets cleaned up properly */
  410. g_assert(!g_file_test(fixture->root_dir, G_FILE_TEST_EXISTS));
  411. g_free(fixture->root_dir);
  412. g_free(fixture->sys_dir);
  413. while (fixture->flying_urbs) {
  414. umockdev_ioctl_data_unref (fixture->flying_urbs->data);
  415. fixture->flying_urbs = g_list_delete_link (fixture->flying_urbs, fixture->flying_urbs);
  416. }
  417. pthread_mutex_destroy(&fixture->mutex);
  418. }
  419. static void
  420. test_open_close(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  421. {
  422. libusb_device **devs = NULL;
  423. struct libusb_device_descriptor desc;
  424. libusb_device_handle *handle = NULL;
  425. g_assert_cmpint(libusb_get_device_list(fixture->ctx, &devs), ==, 1);
  426. /* The linux_enumerate_device may happen from a different thread */
  427. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_DEBUG, "libusb_get_device_list");
  428. /* We have exactly one device */
  429. g_assert_cmpint(libusb_get_bus_number(devs[0]), ==, 1);
  430. g_assert_cmpint(libusb_get_device_address(devs[0]), ==, 1);
  431. /* Get/Check descriptor */
  432. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_INFO);
  433. libusb_get_device_descriptor (devs[0], &desc);
  434. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_DEBUG, "libusb_get_device_descriptor");
  435. g_assert_cmpint(desc.idVendor, ==, 0x04a9);
  436. g_assert_cmpint(desc.idProduct, ==, 0x31c0);
  437. /* Open and close */
  438. g_assert_cmpint(libusb_open(devs[0], &handle), ==, 0);
  439. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_DEBUG, "usbi_add_event_source");
  440. g_assert_nonnull(handle);
  441. libusb_close(handle);
  442. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_DEBUG, "usbi_remove_event_source");
  443. libusb_free_device_list(devs, TRUE);
  444. /* Open and close using vid/pid */
  445. handle = libusb_open_device_with_vid_pid(fixture->ctx, 0x04a9, 0x31c0);
  446. g_assert_nonnull(handle);
  447. libusb_close(handle);
  448. }
  449. static void
  450. test_implicit_default(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  451. {
  452. libusb_device **devs = NULL;
  453. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_INFO);
  454. g_assert_cmpint(libusb_get_device_list(NULL, &devs), ==, 1);
  455. libusb_free_device_list(devs, TRUE);
  456. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_ERROR, "\\[usbi_get_context\\].*implicit default");
  457. /* Only warns once */
  458. g_assert_cmpint(libusb_get_device_list(NULL, &devs), ==, 1);
  459. libusb_free_device_list(devs, TRUE);
  460. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_INFO);
  461. libusb_init(NULL);
  462. g_assert_cmpint(libusb_get_device_list(NULL, &devs), ==, 1);
  463. libusb_exit(NULL);
  464. /* We free late, causing a warning from libusb_exit. However,
  465. * we never see this warning (i.e. test success) because it is on a
  466. * different context.
  467. */
  468. libusb_free_device_list(devs, TRUE);
  469. }
  470. static void
  471. test_close_flying(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  472. {
  473. UsbChat chat[] = {
  474. {
  475. .submit = TRUE,
  476. .type = USBDEVFS_URB_TYPE_BULK,
  477. .endpoint = LIBUSB_ENDPOINT_OUT,
  478. .buffer = (unsigned char[]) { 0x01, 0x02, 0x03, 0x04 },
  479. .buffer_length = 4,
  480. },
  481. { .submit = FALSE }
  482. };
  483. libusb_device_handle *handle = NULL;
  484. struct libusb_transfer *transfer = NULL;
  485. fixture->chat = chat;
  486. /* Open */
  487. handle = libusb_open_device_with_vid_pid(fixture->ctx, 0x04a9, 0x31c0);
  488. g_assert_nonnull(handle);
  489. transfer = libusb_alloc_transfer(0);
  490. libusb_fill_bulk_transfer(transfer,
  491. handle,
  492. LIBUSB_ENDPOINT_OUT,
  493. (unsigned char*) chat[0].buffer,
  494. chat[0].buffer_length,
  495. NULL,
  496. NULL,
  497. 1);
  498. /* Submit */
  499. libusb_submit_transfer(transfer);
  500. /* Closing logs fat error (two lines) */
  501. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_DEBUG);
  502. libusb_close(handle);
  503. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_ERROR, "\\[do_close\\] .*connected as far as we know");
  504. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_ERROR, "\\[do_close\\] .*cancellation hasn't even been scheduled");
  505. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_DEBUG, "\\[do_close\\] Removed transfer");
  506. /* Free'ing the transfer works, and logs to the right context */
  507. libusb_free_transfer(transfer);
  508. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_DEBUG, "\\[libusb_free_transfer\\]");
  509. }
  510. static void
  511. test_close_cancelled(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  512. {
  513. UsbChat chat[] = {
  514. {
  515. .submit = TRUE,
  516. .type = USBDEVFS_URB_TYPE_BULK,
  517. .endpoint = LIBUSB_ENDPOINT_OUT,
  518. .buffer = (unsigned char[]) { 0x01, 0x02, 0x03, 0x04 },
  519. .buffer_length = 4,
  520. },
  521. { .submit = FALSE }
  522. };
  523. libusb_device_handle *handle = NULL;
  524. struct libusb_transfer *transfer = NULL;
  525. fixture->chat = chat;
  526. /* Open */
  527. handle = libusb_open_device_with_vid_pid(fixture->ctx, 0x04a9, 0x31c0);
  528. g_assert_nonnull(handle);
  529. transfer = libusb_alloc_transfer(0);
  530. libusb_fill_bulk_transfer(transfer,
  531. handle,
  532. LIBUSB_ENDPOINT_OUT,
  533. (unsigned char*) chat[0].buffer,
  534. chat[0].buffer_length,
  535. NULL,
  536. NULL,
  537. 1);
  538. /* Submit */
  539. libusb_submit_transfer(transfer);
  540. libusb_cancel_transfer(transfer);
  541. /* Closing logs fat error (two lines) */
  542. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_DEBUG);
  543. libusb_close(handle);
  544. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_ERROR, "\\[do_close\\] .*connected as far as we know");
  545. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_WARNING, "\\[do_close\\] .*cancellation.*hasn't completed");
  546. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_DEBUG, "\\[do_close\\] Removed transfer");
  547. libusb_free_transfer(transfer);
  548. }
  549. static void
  550. test_ctx_destroy(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  551. {
  552. UsbChat chat[] = {
  553. {
  554. .submit = TRUE,
  555. .type = USBDEVFS_URB_TYPE_BULK,
  556. .endpoint = LIBUSB_ENDPOINT_OUT,
  557. .buffer = (unsigned char[]) { 0x01, 0x02, 0x03, 0x04 },
  558. .buffer_length = 4,
  559. },
  560. { .submit = FALSE }
  561. };
  562. libusb_device_handle *handle = NULL;
  563. struct libusb_transfer *transfer = NULL;
  564. fixture->chat = chat;
  565. /* Open */
  566. handle = libusb_open_device_with_vid_pid(fixture->ctx, 0x04a9, 0x31c0);
  567. g_assert_nonnull(handle);
  568. transfer = libusb_alloc_transfer(0);
  569. libusb_fill_bulk_transfer(transfer,
  570. handle,
  571. LIBUSB_ENDPOINT_OUT,
  572. (unsigned char*) chat[0].buffer,
  573. chat[0].buffer_length,
  574. NULL,
  575. NULL,
  576. 1);
  577. /* Submit */
  578. libusb_submit_transfer(transfer);
  579. /* Now we are evil and destroy the ctx! */
  580. libusb_exit(fixture->ctx);
  581. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_WARNING, "\\[libusb_exit\\] device.*still referenced");
  582. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_WARNING, "\\[libusb_exit\\] application left some devices open");
  583. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_DEBUG);
  584. fixture->ctx = NULL;
  585. /* XXX: Closing crashes the application as it unref's the NULL pointer */
  586. /* libusb_close(handle); */
  587. libusb_free_transfer(transfer);
  588. }
  589. static void
  590. test_get_string_descriptor(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  591. {
  592. unsigned char data[255] = { 0, };
  593. libusb_device_handle *handle = NULL;
  594. UsbChat chat[] = {
  595. {
  596. .submit = TRUE,
  597. .reaps = &chat[1],
  598. .type = USBDEVFS_URB_TYPE_CONTROL,
  599. .buffer_length = 12, /* 8 byte out*/
  600. .buffer = (const unsigned char*) "\x80\x06\x00\x03\x00\x00\x04\x00",
  601. }, {
  602. /* String with content 0x0409 (en_US) */
  603. .reap = TRUE,
  604. .actual_length = 12,
  605. .buffer = (const unsigned char*) "\x80\x06\x00\x03\x00\x00\x04\x00\x04\x03\x09\x04",
  606. }, {
  607. .submit = TRUE,
  608. .reaps = &chat[3],
  609. .type = USBDEVFS_URB_TYPE_CONTROL,
  610. .buffer_length = 263, /* 8 byte out*/
  611. .buffer = (const unsigned char*) "\x80\x06\x01\x03\x09\x04\xff\x00",
  612. }, {
  613. /* 4 byte string, "ab" */
  614. .reap = TRUE,
  615. .actual_length = 14,
  616. .buffer = (const unsigned char*) "\x80\x06\x01\x03\x09\x04\xff\x00\x06\x03\x61\x00\x62\x00",
  617. }, {
  618. .submit = TRUE,
  619. .reaps = &chat[5],
  620. .type = USBDEVFS_URB_TYPE_CONTROL,
  621. .buffer_length = 12, /* 8 byte out*/
  622. .buffer = (const unsigned char*) "\x80\x06\x00\x03\x00\x00\x04\x00",
  623. }, {
  624. .reap = TRUE,
  625. .status = -ENOENT,
  626. }, {
  627. .submit = TRUE,
  628. .status = -ENOENT,
  629. .type = USBDEVFS_URB_TYPE_CONTROL,
  630. .buffer_length = 12, /* 8 byte out*/
  631. .buffer = (const unsigned char*) "\x80\x06\x00\x03\x00\x00\x04\x00",
  632. }, {
  633. .submit = FALSE,
  634. }
  635. };
  636. fixture->chat = chat;
  637. handle = libusb_open_device_with_vid_pid(fixture->ctx, 0x04a9, 0x31c0);
  638. g_assert_nonnull(handle);
  639. /* The chat allows us to fetch the descriptor */
  640. g_assert_cmpint(libusb_get_string_descriptor_ascii(handle, 1, data, sizeof(data)), ==, 2);
  641. g_assert_cmpint(memcmp(data, "ab", 2), ==, 0);
  642. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_DEBUG);
  643. /* Again, but the URB fails with ENOENT when reaping */
  644. g_assert_cmpint(libusb_get_string_descriptor_ascii(handle, 1, data, sizeof(data)), ==, -1);
  645. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_DEBUG);
  646. /* Again, but the URB fails to submit with ENOENT */
  647. g_assert_cmpint(libusb_get_string_descriptor_ascii(handle, 1, data, sizeof(data)), ==, -1);
  648. assert_libusb_log_msg(fixture, LIBUSB_LOG_LEVEL_ERROR, "\\[submit_control_transfer\\] submiturb failed, errno=2");
  649. clear_libusb_log(fixture, LIBUSB_LOG_LEVEL_DEBUG);
  650. libusb_close(handle);
  651. }
  652. static void
  653. transfer_cb_inc_user_data(struct libusb_transfer *transfer)
  654. {
  655. *(int*)transfer->user_data += 1;
  656. }
  657. static void
  658. test_timeout(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  659. {
  660. UsbChat chat[] = {
  661. {
  662. .submit = TRUE,
  663. .type = USBDEVFS_URB_TYPE_BULK,
  664. .endpoint = LIBUSB_ENDPOINT_OUT,
  665. .buffer = (unsigned char[]) { 0x01, 0x02, 0x03, 0x04 },
  666. .buffer_length = 4,
  667. },
  668. {
  669. .submit = FALSE,
  670. }
  671. };
  672. int completed = 0;
  673. libusb_device_handle *handle = NULL;
  674. struct libusb_transfer *transfer = NULL;
  675. fixture->chat = chat;
  676. handle = libusb_open_device_with_vid_pid(fixture->ctx, 0x04a9, 0x31c0);
  677. g_assert_nonnull(handle);
  678. transfer = libusb_alloc_transfer(0);
  679. libusb_fill_bulk_transfer(transfer,
  680. handle,
  681. LIBUSB_ENDPOINT_OUT,
  682. (unsigned char*) chat[0].buffer,
  683. chat[0].buffer_length,
  684. transfer_cb_inc_user_data,
  685. &completed,
  686. 10);
  687. libusb_submit_transfer(transfer);
  688. while (!completed) {
  689. g_assert_cmpint(libusb_handle_events_completed(fixture->ctx, &completed), ==, 0);
  690. /* Silence after one iteration. */
  691. fixture->libusb_log_silence = TRUE;
  692. }
  693. fixture->libusb_log_silence = FALSE;
  694. g_assert_cmpint(transfer->status, ==, LIBUSB_TRANSFER_TIMED_OUT);
  695. libusb_free_transfer(transfer);
  696. libusb_close(handle);
  697. }
  698. #define THREADED_SUBMIT_URB_SETS 64
  699. #define THREADED_SUBMIT_URB_IN_FLIGHT 64
  700. typedef struct {
  701. struct libusb_transfer *transfers[THREADED_SUBMIT_URB_IN_FLIGHT * THREADED_SUBMIT_URB_SETS];
  702. int submitted;
  703. int completed;
  704. int done;
  705. UMockdevTestbedFixture *fixture;
  706. } TestThreadedSubmit;
  707. static gpointer
  708. transfer_submit_all_retry(TestThreadedSubmit *data)
  709. {
  710. for (guint i = 0; i < G_N_ELEMENTS(data->transfers); i++) {
  711. while (libusb_submit_transfer(data->transfers[i]) < 0) {
  712. assert_libusb_log_msg(data->fixture, LIBUSB_LOG_LEVEL_ERROR, "submit_bulk_transfer");
  713. continue;
  714. }
  715. data->submitted += 1;
  716. }
  717. return NULL;
  718. }
  719. static void
  720. test_threaded_submit_transfer_cb(struct libusb_transfer *transfer)
  721. {
  722. TestThreadedSubmit *data = transfer->user_data;
  723. /* We should only be receiving packets in the main thread */
  724. g_assert_cmpint (getpid(), ==, gettid());
  725. /* Check that the transfer buffer has the expected value */
  726. g_assert_cmpint (*(int*)transfer->buffer, ==, data->completed);
  727. data->completed += 1;
  728. if (data->completed == G_N_ELEMENTS(data->transfers))
  729. data->done = TRUE;
  730. }
  731. static void
  732. test_threaded_submit(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  733. {
  734. GThread *thread = NULL;
  735. TestThreadedSubmit data = { .fixture = fixture };
  736. UsbChat out_msg = {
  737. .submit = TRUE,
  738. .type = USBDEVFS_URB_TYPE_BULK,
  739. .endpoint = LIBUSB_ENDPOINT_IN,
  740. .buffer_length = sizeof(int),
  741. };
  742. UsbChat in_msg = {
  743. .reap = TRUE,
  744. .actual_length = 4,
  745. };
  746. UsbChat *c;
  747. libusb_device_handle *handle = NULL;
  748. int urb;
  749. handle = libusb_open_device_with_vid_pid(fixture->ctx, 0x04a9, 0x31c0);
  750. g_assert_nonnull(handle);
  751. fixture->libusb_log_silence = TRUE;
  752. c = fixture->chat = g_new0(UsbChat, G_N_ELEMENTS(data.transfers) * 2 + 1);
  753. urb = 0;
  754. for (int i = 0; i < THREADED_SUBMIT_URB_SETS; i++) {
  755. for (int j = 0; j < THREADED_SUBMIT_URB_IN_FLIGHT; j++) {
  756. c[i*2*THREADED_SUBMIT_URB_IN_FLIGHT + j] = out_msg;
  757. c[i*2*THREADED_SUBMIT_URB_IN_FLIGHT + j].reaps = &c[(i*2+1)*THREADED_SUBMIT_URB_IN_FLIGHT + j];
  758. c[(i*2+1)*THREADED_SUBMIT_URB_IN_FLIGHT + j] = in_msg;
  759. c[(i*2+1)*THREADED_SUBMIT_URB_IN_FLIGHT + j].buffer = (unsigned char*) g_new0(int, 1);
  760. *(int*) c[(i*2+1)*THREADED_SUBMIT_URB_IN_FLIGHT + j].buffer = urb;
  761. data.transfers[urb] = libusb_alloc_transfer(0);
  762. libusb_fill_bulk_transfer(data.transfers[urb],
  763. handle,
  764. LIBUSB_ENDPOINT_IN,
  765. g_malloc(out_msg.buffer_length),
  766. out_msg.buffer_length,
  767. test_threaded_submit_transfer_cb,
  768. &data,
  769. G_MAXUINT);
  770. data.transfers[urb]->flags = LIBUSB_TRANSFER_FREE_BUFFER | LIBUSB_TRANSFER_FREE_TRANSFER;
  771. urb++;
  772. }
  773. }
  774. thread = g_thread_new("transfer all", (GThreadFunc) transfer_submit_all_retry, &data);
  775. while (!data.done)
  776. g_assert_cmpint(libusb_handle_events_completed(fixture->ctx, &data.done), ==, 0);
  777. g_thread_join(thread);
  778. fixture->libusb_log_silence = FALSE;
  779. libusb_close(handle);
  780. for (int i = 0; i < 2 * THREADED_SUBMIT_URB_SETS * THREADED_SUBMIT_URB_SETS; i++)
  781. g_clear_pointer ((void**) &c->buffer, g_free);
  782. g_free (c);
  783. }
  784. static int
  785. hotplug_count_arrival_cb(libusb_context *ctx,
  786. libusb_device *device,
  787. libusb_hotplug_event event,
  788. void *user_data)
  789. {
  790. g_assert_cmpint(event, ==, LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED);
  791. (void) ctx;
  792. (void) device;
  793. *(int*) user_data += 1;
  794. return 0;
  795. }
  796. #ifdef UMOCKDEV_HOTPLUG
  797. static int
  798. hotplug_count_removal_cb(libusb_context *ctx,
  799. libusb_device *device,
  800. libusb_hotplug_event event,
  801. void *user_data)
  802. {
  803. g_assert_cmpint(event, ==, LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT);
  804. (void) ctx;
  805. (void) device;
  806. *(int*) user_data += 1;
  807. return 0;
  808. }
  809. #endif
  810. static void
  811. test_hotplug_enumerate(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  812. {
  813. libusb_hotplug_callback_handle handle_enumerate;
  814. libusb_hotplug_callback_handle handle_no_enumerate;
  815. int event_count_enumerate = 0;
  816. int event_count_no_enumerate = 0;
  817. struct timeval zero_tv = { 0 };
  818. int r;
  819. r = libusb_hotplug_register_callback(fixture->ctx,
  820. LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
  821. LIBUSB_HOTPLUG_ENUMERATE,
  822. LIBUSB_HOTPLUG_MATCH_ANY,
  823. LIBUSB_HOTPLUG_MATCH_ANY,
  824. LIBUSB_HOTPLUG_MATCH_ANY,
  825. hotplug_count_arrival_cb,
  826. &event_count_enumerate,
  827. &handle_enumerate);
  828. g_assert_cmpint(r, ==, 0);
  829. r = libusb_hotplug_register_callback(fixture->ctx,
  830. LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED | LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
  831. 0,
  832. LIBUSB_HOTPLUG_MATCH_ANY,
  833. LIBUSB_HOTPLUG_MATCH_ANY,
  834. LIBUSB_HOTPLUG_MATCH_ANY,
  835. hotplug_count_arrival_cb,
  836. &event_count_no_enumerate,
  837. &handle_no_enumerate);
  838. g_assert_cmpint(r, ==, 0);
  839. g_assert_cmpint(event_count_enumerate, ==, 1);
  840. g_assert_cmpint(event_count_no_enumerate, ==, 0);
  841. libusb_handle_events_timeout(fixture->ctx, &zero_tv);
  842. g_assert_cmpint(event_count_enumerate, ==, 1);
  843. g_assert_cmpint(event_count_no_enumerate, ==, 0);
  844. libusb_hotplug_deregister_callback(fixture->ctx, handle_enumerate);
  845. libusb_hotplug_deregister_callback(fixture->ctx, handle_no_enumerate);
  846. libusb_handle_events_timeout(fixture->ctx, &zero_tv);
  847. g_assert_cmpint(event_count_enumerate, ==, 1);
  848. g_assert_cmpint(event_count_no_enumerate, ==, 0);
  849. }
  850. static void
  851. test_hotplug_add_remove(UMockdevTestbedFixture * fixture, UNUSED_DATA)
  852. {
  853. #ifdef UMOCKDEV_HOTPLUG
  854. libusb_device **devs = NULL;
  855. libusb_hotplug_callback_handle handle_add;
  856. libusb_hotplug_callback_handle handle_remove;
  857. int event_count_add = 0;
  858. int event_count_remove = 0;
  859. struct timeval zero_tv = { 0 };
  860. int r;
  861. r = libusb_hotplug_register_callback(fixture->ctx,
  862. LIBUSB_HOTPLUG_EVENT_DEVICE_ARRIVED,
  863. LIBUSB_HOTPLUG_ENUMERATE,
  864. LIBUSB_HOTPLUG_MATCH_ANY,
  865. LIBUSB_HOTPLUG_MATCH_ANY,
  866. LIBUSB_HOTPLUG_MATCH_ANY,
  867. hotplug_count_arrival_cb,
  868. &event_count_add,
  869. &handle_add);
  870. g_assert_cmpint(r, ==, 0);
  871. r = libusb_hotplug_register_callback(fixture->ctx,
  872. LIBUSB_HOTPLUG_EVENT_DEVICE_LEFT,
  873. LIBUSB_HOTPLUG_ENUMERATE,
  874. LIBUSB_HOTPLUG_MATCH_ANY,
  875. LIBUSB_HOTPLUG_MATCH_ANY,
  876. LIBUSB_HOTPLUG_MATCH_ANY,
  877. hotplug_count_removal_cb,
  878. &event_count_remove,
  879. &handle_remove);
  880. g_assert_cmpint(r, ==, 0);
  881. /* No device, even going into the mainloop will not call cb. */
  882. libusb_handle_events_timeout(fixture->ctx, &zero_tv);
  883. g_assert_cmpint(event_count_add, ==, 0);
  884. g_assert_cmpint(event_count_remove, ==, 0);
  885. /* Add a device */
  886. test_fixture_add_canon(fixture);
  887. /* Either the thread has picked it up already, or we do so now. */
  888. g_assert_cmpint(libusb_get_device_list(fixture->ctx, &devs), ==, 1);
  889. libusb_free_device_list(devs, TRUE);
  890. /* The hotplug event is pending now, but has not yet fired. */
  891. g_assert_cmpint(event_count_add, ==, 0);
  892. g_assert_cmpint(event_count_remove, ==, 0);
  893. /* Fire hotplug event. */
  894. libusb_handle_events_timeout(fixture->ctx, &zero_tv);
  895. g_assert_cmpint(event_count_add, ==, 1);
  896. g_assert_cmpint(event_count_remove, ==, 0);
  897. umockdev_testbed_uevent(fixture->testbed, "/sys/devices/usb1", "remove");
  898. //umockdev_testbed_remove_device(fixture->testbed, "/devices/usb1");
  899. /* Either the thread has picked it up already, or we do so now. */
  900. g_assert_cmpint(libusb_get_device_list(fixture->ctx, &devs), ==, 0);
  901. libusb_free_device_list(devs, TRUE);
  902. /* The hotplug event is pending now, but has not yet fired. */
  903. g_assert_cmpint(event_count_add, ==, 1);
  904. g_assert_cmpint(event_count_remove, ==, 0);
  905. /* Fire hotplug event. */
  906. libusb_handle_events_timeout(fixture->ctx, &zero_tv);
  907. g_assert_cmpint(event_count_add, ==, 1);
  908. g_assert_cmpint(event_count_remove, ==, 1);
  909. libusb_hotplug_deregister_callback(fixture->ctx, handle_add);
  910. libusb_hotplug_deregister_callback(fixture->ctx, handle_remove);
  911. #else
  912. (void) fixture;
  913. g_test_skip("UMockdev is too old to test hotplug");
  914. #endif
  915. }
  916. int
  917. main(int argc, char **argv)
  918. {
  919. g_test_init(&argc, &argv, NULL);
  920. g_test_add("/libusb/open-close", UMockdevTestbedFixture, NULL,
  921. test_fixture_setup_with_canon,
  922. test_open_close,
  923. test_fixture_teardown);
  924. g_test_add("/libusb/implicit-default", UMockdevTestbedFixture, NULL,
  925. test_fixture_setup_with_canon,
  926. test_implicit_default,
  927. test_fixture_teardown);
  928. g_test_add("/libusb/close-flying", UMockdevTestbedFixture, NULL,
  929. test_fixture_setup_with_canon,
  930. test_close_flying,
  931. test_fixture_teardown);
  932. g_test_add("/libusb/close-cancelled", UMockdevTestbedFixture, NULL,
  933. test_fixture_setup_with_canon,
  934. test_close_cancelled,
  935. test_fixture_teardown);
  936. g_test_add("/libusb/ctx-destroy", UMockdevTestbedFixture, NULL,
  937. test_fixture_setup_with_canon,
  938. test_ctx_destroy,
  939. test_fixture_teardown);
  940. g_test_add("/libusb/string-descriptor", UMockdevTestbedFixture, NULL,
  941. test_fixture_setup_with_canon,
  942. test_get_string_descriptor,
  943. test_fixture_teardown);
  944. g_test_add("/libusb/timeout", UMockdevTestbedFixture, NULL,
  945. test_fixture_setup_with_canon,
  946. test_timeout,
  947. test_fixture_teardown);
  948. g_test_add("/libusb/threaded-submit", UMockdevTestbedFixture, NULL,
  949. test_fixture_setup_with_canon,
  950. test_threaded_submit,
  951. test_fixture_teardown);
  952. g_test_add("/libusb/hotplug/enumerate", UMockdevTestbedFixture, NULL,
  953. test_fixture_setup_with_canon,
  954. test_hotplug_enumerate,
  955. test_fixture_teardown);
  956. g_test_add("/libusb/hotplug/add-remove", UMockdevTestbedFixture, NULL,
  957. test_fixture_setup_empty,
  958. test_hotplug_add_remove,
  959. test_fixture_teardown);
  960. return g_test_run();
  961. }