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
22 #if !defined(_WIN32_WCE)
23 #include <sys/types.h>
28 #include "libusbx_testlib.h"
30 /** Test that creates and destroys a single concurrent context
32 static libusbx_testlib_result test_init_and_exit(libusbx_testlib_ctx * tctx)
34 libusb_context * ctx = NULL;
36 for (i = 0; i < 10000; ++i) {
37 int r = libusb_init(&ctx);
38 if (r != LIBUSB_SUCCESS) {
39 libusbx_testlib_logf(tctx,
40 "Failed to init libusb on iteration %d: %d",
42 return TEST_STATUS_FAILURE;
48 return TEST_STATUS_SUCCESS;
51 /** Tests that devices can be listed 1000 times. */
52 static libusbx_testlib_result test_get_device_list(libusbx_testlib_ctx * tctx)
54 libusb_context * ctx = NULL;
56 r = libusb_init(&ctx);
57 if (r != LIBUSB_SUCCESS) {
58 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
59 return TEST_STATUS_FAILURE;
61 for (i = 0; i < 1000; ++i) {
62 libusb_device ** device_list;
63 ssize_t list_size = libusb_get_device_list(ctx, &device_list);
64 if (list_size < 0 || device_list == NULL) {
65 libusbx_testlib_logf(tctx,
66 "Failed to get device list on iteration %d: %d (%p)",
67 i, -list_size, device_list);
68 return TEST_STATUS_FAILURE;
70 libusb_free_device_list(device_list, 1);
73 return TEST_STATUS_SUCCESS;
76 /** Tests that 100 concurrent device lists can be open at a time. */
77 static libusbx_testlib_result test_many_device_lists(libusbx_testlib_ctx * tctx)
79 #define LIST_COUNT 100
80 libusb_context * ctx = NULL;
81 libusb_device ** device_lists[LIST_COUNT];
83 memset(device_lists, 0, sizeof(device_lists));
85 r = libusb_init(&ctx);
86 if (r != LIBUSB_SUCCESS) {
87 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
88 return TEST_STATUS_FAILURE;
91 /* Create the 100 device lists. */
92 for (i = 0; i < LIST_COUNT; ++i) {
93 ssize_t list_size = libusb_get_device_list(ctx, &(device_lists[i]));
94 if (list_size < 0 || device_lists[i] == NULL) {
95 libusbx_testlib_logf(tctx,
96 "Failed to get device list on iteration %d: %d (%p)",
97 i, -list_size, device_lists[i]);
98 return TEST_STATUS_FAILURE;
102 /* Destroy the 100 device lists. */
103 for (i = 0; i < LIST_COUNT; ++i) {
104 if (device_lists[i]) {
105 libusb_free_device_list(device_lists[i], 1);
106 device_lists[i] = NULL;
111 return TEST_STATUS_SUCCESS;
115 /** Tests that the default context (used for various things including
116 * logging) works correctly when the first context created in a
117 * process is destroyed. */
118 static libusbx_testlib_result test_default_context_change(libusbx_testlib_ctx * tctx)
120 libusb_context * ctx = NULL;
123 for (i = 0; i < 100; ++i) {
124 /* First create a new context */
125 r = libusb_init(&ctx);
126 if (r != LIBUSB_SUCCESS) {
127 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
128 return TEST_STATUS_FAILURE;
131 /* Enable debug output, to be sure to use the context */
132 libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_DEBUG);
133 libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_DEBUG);
135 /* Now create a reference to the default context */
136 r = libusb_init(NULL);
137 if (r != LIBUSB_SUCCESS) {
138 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
139 return TEST_STATUS_FAILURE;
142 /* Destroy the first context */
144 /* Destroy the default context */
148 return TEST_STATUS_SUCCESS;
151 /* Fill in the list of tests. */
152 static const libusbx_testlib_test tests[] = {
153 {"init_and_exit", &test_init_and_exit},
154 {"get_device_list", &test_get_device_list},
155 {"many_device_lists", &test_many_device_lists},
156 {"default_context_change", &test_default_context_change},
160 int main (int argc, char ** argv)
162 return libusbx_testlib_run_tests(argc, argv, tests);