Misc: Simplify includes and misc. cleanup
[platform/upstream/libusb.git] / tests / stress.c
1 /*
2  * libusbx stress test program to perform simple stress tests
3  * 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>
21 #include <memory.h>
22
23 #include "libusb.h"
24 #include "libusbx_testlib.h"
25
26 /** Test that creates and destroys a single concurrent context
27  * 10000 times. */
28 static libusbx_testlib_result test_init_and_exit(libusbx_testlib_ctx * tctx)
29 {
30         libusb_context * ctx = NULL;
31         int i;
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",
37                                 i, r);
38                         return TEST_STATUS_FAILURE;
39                 }
40                 libusb_exit(ctx);
41                 ctx = NULL;
42         }
43
44         return TEST_STATUS_SUCCESS;
45 }
46
47 /** Tests that devices can be listed 1000 times. */
48 static libusbx_testlib_result test_get_device_list(libusbx_testlib_ctx * tctx)
49 {
50         libusb_context * ctx = NULL;
51         int r, i;
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;
56         }
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;
65                 }
66                 libusb_free_device_list(device_list, 1);
67         }
68         libusb_exit(ctx);
69         return TEST_STATUS_SUCCESS;
70 }
71
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)
74 {
75 #define LIST_COUNT 100
76         libusb_context * ctx = NULL;
77         libusb_device ** device_lists[LIST_COUNT];
78         int r, i;
79         memset(device_lists, 0, sizeof(device_lists));
80
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;
85         }
86
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;
95                 }
96         }
97
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;
103                 }
104         }
105
106         libusb_exit(ctx);
107         return TEST_STATUS_SUCCESS;
108 #undef LIST_COUNT
109 }
110
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)
115 {
116         libusb_context * ctx = NULL;
117         int r, i;
118
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;
125                 }
126
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);
130
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;
136                 }
137
138                 /* Destroy the first context */
139                 libusb_exit(ctx);
140                 /* Destroy the default context */
141                 libusb_exit(NULL);
142         }
143
144         return TEST_STATUS_SUCCESS;
145 }
146
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},
153         LIBUSBX_NULL_TEST
154 };
155
156 int main (int argc, char ** argv)
157 {
158         return libusbx_testlib_run_tests(argc, argv, tests);
159 }