2 * libusbx stress test program to perform simple stress tests
3 * Copyright © 2012 Toby Gray <toby.gray@realvnc.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libusbx_testlib.h"
26 /** Test that creates and destroys a single concurrent context
28 static libusbx_testlib_result test_init_and_exit(libusbx_testlib_ctx * tctx)
30 libusb_context * ctx = NULL;
32 for (i = 0; i < 10000; ++i) {
33 int r = libusb_init(&ctx);
34 if (r != LIBUSB_SUCCESS) {
35 libusbx_testlib_logf(tctx,
36 "Failed to init libusb on iteration %d: %d",
38 return TEST_STATUS_FAILURE;
44 return TEST_STATUS_SUCCESS;
47 /** Tests that devices can be listed 1000 times. */
48 static libusbx_testlib_result test_get_device_list(libusbx_testlib_ctx * tctx)
50 libusb_context * ctx = NULL;
52 r = libusb_init(&ctx);
53 if (r != LIBUSB_SUCCESS) {
54 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
55 return TEST_STATUS_FAILURE;
57 for (i = 0; i < 1000; ++i) {
58 libusb_device ** device_list;
59 ssize_t list_size = libusb_get_device_list(ctx, &device_list);
60 if (list_size < 0 || device_list == NULL) {
61 libusbx_testlib_logf(tctx,
62 "Failed to get device list on iteration %d: %d (%p)",
63 i, -list_size, device_list);
64 return TEST_STATUS_FAILURE;
66 libusb_free_device_list(device_list, 1);
69 return TEST_STATUS_SUCCESS;
72 /** Tests that 100 concurrent device lists can be open at a time. */
73 static libusbx_testlib_result test_many_device_lists(libusbx_testlib_ctx * tctx)
75 #define LIST_COUNT 100
76 libusb_context * ctx = NULL;
77 libusb_device ** device_lists[LIST_COUNT];
79 memset(device_lists, 0, sizeof(device_lists));
81 r = libusb_init(&ctx);
82 if (r != LIBUSB_SUCCESS) {
83 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
84 return TEST_STATUS_FAILURE;
87 /* Create the 100 device lists. */
88 for (i = 0; i < LIST_COUNT; ++i) {
89 ssize_t list_size = libusb_get_device_list(ctx, &(device_lists[i]));
90 if (list_size < 0 || device_lists[i] == NULL) {
91 libusbx_testlib_logf(tctx,
92 "Failed to get device list on iteration %d: %d (%p)",
93 i, -list_size, device_lists[i]);
94 return TEST_STATUS_FAILURE;
98 /* Destroy the 100 device lists. */
99 for (i = 0; i < LIST_COUNT; ++i) {
100 if (device_lists[i]) {
101 libusb_free_device_list(device_lists[i], 1);
102 device_lists[i] = NULL;
107 return TEST_STATUS_SUCCESS;
111 /** Tests that the default context (used for various things including
112 * logging) works correctly when the first context created in a
113 * process is destroyed. */
114 static libusbx_testlib_result test_default_context_change(libusbx_testlib_ctx * tctx)
116 libusb_context * ctx = NULL;
119 for (i = 0; i < 100; ++i) {
120 /* First create a new context */
121 r = libusb_init(&ctx);
122 if (r != LIBUSB_SUCCESS) {
123 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
124 return TEST_STATUS_FAILURE;
127 /* Enable debug output, to be sure to use the context */
128 libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_DEBUG);
129 libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_DEBUG);
131 /* Now create a reference to the default context */
132 r = libusb_init(NULL);
133 if (r != LIBUSB_SUCCESS) {
134 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
135 return TEST_STATUS_FAILURE;
138 /* Destroy the first context */
140 /* Destroy the default context */
144 return TEST_STATUS_SUCCESS;
147 /* Fill in the list of tests. */
148 static const libusbx_testlib_test tests[] = {
149 {"init_and_exit", &test_init_and_exit},
150 {"get_device_list", &test_get_device_list},
151 {"many_device_lists", &test_many_device_lists},
152 {"default_context_change", &test_default_context_change},
156 int main (int argc, char ** argv)
158 return libusbx_testlib_run_tests(argc, argv, tests);