blob: 1602ee93ff04243e8f5b02685490704859ba36bc [file] [log] [blame]
Toby Gray21cf6e42012-11-21 14:00:31 +00001/*
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +00002 * libusb stress test program to perform simple stress tests
Toby Gray21cf6e42012-11-21 14:00:31 +00003 * Copyright © 2012 Toby Gray <toby.gray@realvnc.com>
4 *
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.
9 *
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.
14 *
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
18 */
19
20#include <stdio.h>
Toby Grayc6321012013-08-08 18:22:21 +010021#include <string.h>
Toby Gray21cf6e42012-11-21 14:00:31 +000022#include <memory.h>
Toby Gray21cf6e42012-11-21 14:00:31 +000023
24#include "libusb.h"
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +000025#include "libusb_testlib.h"
Toby Gray21cf6e42012-11-21 14:00:31 +000026
27/** Test that creates and destroys a single concurrent context
28 * 10000 times. */
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +000029static libusb_testlib_result test_init_and_exit(libusb_testlib_ctx * tctx)
Toby Gray21cf6e42012-11-21 14:00:31 +000030{
31 libusb_context * ctx = NULL;
32 int i;
33 for (i = 0; i < 10000; ++i) {
34 int r = libusb_init(&ctx);
35 if (r != LIBUSB_SUCCESS) {
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +000036 libusb_testlib_logf(tctx,
Toby Gray21cf6e42012-11-21 14:00:31 +000037 "Failed to init libusb on iteration %d: %d",
38 i, r);
39 return TEST_STATUS_FAILURE;
40 }
41 libusb_exit(ctx);
42 ctx = NULL;
43 }
44
45 return TEST_STATUS_SUCCESS;
46}
47
48/** Tests that devices can be listed 1000 times. */
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +000049static libusb_testlib_result test_get_device_list(libusb_testlib_ctx * tctx)
Toby Gray21cf6e42012-11-21 14:00:31 +000050{
51 libusb_context * ctx = NULL;
52 int r, i;
53 r = libusb_init(&ctx);
54 if (r != LIBUSB_SUCCESS) {
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +000055 libusb_testlib_logf(tctx, "Failed to init libusb: %d", r);
Toby Gray21cf6e42012-11-21 14:00:31 +000056 return TEST_STATUS_FAILURE;
57 }
58 for (i = 0; i < 1000; ++i) {
59 libusb_device ** device_list;
60 ssize_t list_size = libusb_get_device_list(ctx, &device_list);
61 if (list_size < 0 || device_list == NULL) {
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +000062 libusb_testlib_logf(tctx,
Uri Lublinab68ed42020-07-05 16:29:57 +030063 "Failed to get device list on iteration %d: %ld (%p)",
64 i, (long)-list_size, device_list);
Toby Gray21cf6e42012-11-21 14:00:31 +000065 return TEST_STATUS_FAILURE;
66 }
67 libusb_free_device_list(device_list, 1);
68 }
69 libusb_exit(ctx);
70 return TEST_STATUS_SUCCESS;
71}
72
73/** Tests that 100 concurrent device lists can be open at a time. */
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +000074static libusb_testlib_result test_many_device_lists(libusb_testlib_ctx * tctx)
Toby Gray21cf6e42012-11-21 14:00:31 +000075{
76#define LIST_COUNT 100
77 libusb_context * ctx = NULL;
78 libusb_device ** device_lists[LIST_COUNT];
79 int r, i;
80 memset(device_lists, 0, sizeof(device_lists));
81
82 r = libusb_init(&ctx);
83 if (r != LIBUSB_SUCCESS) {
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +000084 libusb_testlib_logf(tctx, "Failed to init libusb: %d", r);
Toby Gray21cf6e42012-11-21 14:00:31 +000085 return TEST_STATUS_FAILURE;
86 }
87
88 /* Create the 100 device lists. */
89 for (i = 0; i < LIST_COUNT; ++i) {
90 ssize_t list_size = libusb_get_device_list(ctx, &(device_lists[i]));
91 if (list_size < 0 || device_lists[i] == NULL) {
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +000092 libusb_testlib_logf(tctx,
Toby Gray21cf6e42012-11-21 14:00:31 +000093 "Failed to get device list on iteration %d: %d (%p)",
94 i, -list_size, device_lists[i]);
95 return TEST_STATUS_FAILURE;
96 }
97 }
98
99 /* Destroy the 100 device lists. */
100 for (i = 0; i < LIST_COUNT; ++i) {
101 if (device_lists[i]) {
102 libusb_free_device_list(device_lists[i], 1);
103 device_lists[i] = NULL;
104 }
105 }
106
107 libusb_exit(ctx);
108 return TEST_STATUS_SUCCESS;
109#undef LIST_COUNT
110}
111
112/** Tests that the default context (used for various things including
113 * logging) works correctly when the first context created in a
114 * process is destroyed. */
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +0000115static libusb_testlib_result test_default_context_change(libusb_testlib_ctx * tctx)
Toby Gray21cf6e42012-11-21 14:00:31 +0000116{
117 libusb_context * ctx = NULL;
118 int r, i;
119
120 for (i = 0; i < 100; ++i) {
121 /* First create a new context */
122 r = libusb_init(&ctx);
123 if (r != LIBUSB_SUCCESS) {
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +0000124 libusb_testlib_logf(tctx, "Failed to init libusb: %d", r);
Toby Gray21cf6e42012-11-21 14:00:31 +0000125 return TEST_STATUS_FAILURE;
126 }
127
128 /* Enable debug output, to be sure to use the context */
Chris Dickens539f22e2017-07-10 22:37:13 -0700129 libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
130 libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_DEBUG);
Toby Gray21cf6e42012-11-21 14:00:31 +0000131
132 /* Now create a reference to the default context */
133 r = libusb_init(NULL);
134 if (r != LIBUSB_SUCCESS) {
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +0000135 libusb_testlib_logf(tctx, "Failed to init libusb: %d", r);
Toby Gray21cf6e42012-11-21 14:00:31 +0000136 return TEST_STATUS_FAILURE;
137 }
138
139 /* Destroy the first context */
140 libusb_exit(ctx);
141 /* Destroy the default context */
142 libusb_exit(NULL);
143 }
144
145 return TEST_STATUS_SUCCESS;
146}
147
148/* Fill in the list of tests. */
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +0000149static const libusb_testlib_test tests[] = {
Toby Gray21cf6e42012-11-21 14:00:31 +0000150 {"init_and_exit", &test_init_and_exit},
151 {"get_device_list", &test_get_device_list},
152 {"many_device_lists", &test_many_device_lists},
153 {"default_context_change", &test_default_context_change},
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +0000154 LIBUSB_NULL_TEST
Toby Gray21cf6e42012-11-21 14:00:31 +0000155};
156
157int main (int argc, char ** argv)
158{
hjelmn@cs.unm.edu1eff2202014-01-08 23:50:34 +0000159 return libusb_testlib_run_tests(argc, argv, tests);
Toby Gray21cf6e42012-11-21 14:00:31 +0000160}