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
21 #include <sys/types.h>
26 #include "libusbx_testlib.h"
28 /** Test that creates and destroys a single concurrent context
30 static libusbx_testlib_result test_init_and_exit(libusbx_testlib_ctx * tctx)
32 libusb_context * ctx = NULL;
34 for (i = 0; i < 10000; ++i) {
35 int r = libusb_init(&ctx);
36 if (r != LIBUSB_SUCCESS) {
37 libusbx_testlib_logf(tctx,
38 "Failed to init libusb on iteration %d: %d",
40 return TEST_STATUS_FAILURE;
46 return TEST_STATUS_SUCCESS;
49 /** Tests that devices can be listed 1000 times. */
50 static libusbx_testlib_result test_get_device_list(libusbx_testlib_ctx * tctx)
52 libusb_context * ctx = NULL;
54 r = libusb_init(&ctx);
55 if (r != LIBUSB_SUCCESS) {
56 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
57 return TEST_STATUS_FAILURE;
59 for (i = 0; i < 1000; ++i) {
60 libusb_device ** device_list;
61 ssize_t list_size = libusb_get_device_list(ctx, &device_list);
62 if (list_size < 0 || device_list == NULL) {
63 libusbx_testlib_logf(tctx,
64 "Failed to get device list on iteration %d: %d (%p)",
65 i, -list_size, device_list);
66 return TEST_STATUS_FAILURE;
68 libusb_free_device_list(device_list, 1);
71 return TEST_STATUS_SUCCESS;
74 /** Tests that 100 concurrent device lists can be open at a time. */
75 static libusbx_testlib_result test_many_device_lists(libusbx_testlib_ctx * tctx)
77 #define LIST_COUNT 100
78 libusb_context * ctx = NULL;
79 libusb_device ** device_lists[LIST_COUNT];
81 memset(device_lists, 0, sizeof(device_lists));
83 r = libusb_init(&ctx);
84 if (r != LIBUSB_SUCCESS) {
85 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
86 return TEST_STATUS_FAILURE;
89 /* Create the 100 device lists. */
90 for (i = 0; i < LIST_COUNT; ++i) {
91 ssize_t list_size = libusb_get_device_list(ctx, &(device_lists[i]));
92 if (list_size < 0 || device_lists[i] == NULL) {
93 libusbx_testlib_logf(tctx,
94 "Failed to get device list on iteration %d: %d (%p)",
95 i, -list_size, device_lists[i]);
96 return TEST_STATUS_FAILURE;
100 /* Destroy the 100 device lists. */
101 for (i = 0; i < LIST_COUNT; ++i) {
102 if (device_lists[i]) {
103 libusb_free_device_list(device_lists[i], 1);
104 device_lists[i] = NULL;
109 return TEST_STATUS_SUCCESS;
113 /** Tests that the default context (used for various things including
114 * logging) works correctly when the first context created in a
115 * process is destroyed. */
116 static libusbx_testlib_result test_default_context_change(libusbx_testlib_ctx * tctx)
118 libusb_context * ctx = NULL;
121 for (i = 0; i < 100; ++i) {
122 /* First create a new context */
123 r = libusb_init(&ctx);
124 if (r != LIBUSB_SUCCESS) {
125 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
126 return TEST_STATUS_FAILURE;
129 /* Enable debug output, to be sure to use the context */
130 libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_DEBUG);
131 libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_DEBUG);
133 /* Now create a reference to the default context */
134 r = libusb_init(NULL);
135 if (r != LIBUSB_SUCCESS) {
136 libusbx_testlib_logf(tctx, "Failed to init libusb: %d", r);
137 return TEST_STATUS_FAILURE;
140 /* Destroy the first context */
142 /* Destroy the default context */
146 return TEST_STATUS_SUCCESS;
149 /* Fill in the list of tests. */
150 static const libusbx_testlib_test tests[] = {
151 {"init_and_exit", &test_init_and_exit},
152 {"get_device_list", &test_get_device_list},
153 {"many_device_lists", &test_many_device_lists},
154 {"default_context_change", &test_default_context_change},
158 int main (int argc, char ** argv)
160 return libusbx_testlib_run_tests(argc, argv, tests);