WinCE: Add support for WinCE (sources)
[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 #if !defined(_WIN32_WCE)
23 #include <sys/types.h>
24 #endif
25
26 #include "libusb.h"
27
28 #include "libusbx_testlib.h"
29
30 /** Test that creates and destroys a single concurrent context
31  * 10000 times. */
32 static libusbx_testlib_result test_init_and_exit(libusbx_testlib_ctx * tctx)
33 {
34         libusb_context * ctx = NULL;
35         int i;
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",
41                                 i, r);
42                         return TEST_STATUS_FAILURE;
43                 }
44                 libusb_exit(ctx);
45                 ctx = NULL;
46         }
47
48         return TEST_STATUS_SUCCESS;
49 }
50
51 /** Tests that devices can be listed 1000 times. */
52 static libusbx_testlib_result test_get_device_list(libusbx_testlib_ctx * tctx)
53 {
54         libusb_context * ctx = NULL;
55         int r, i;
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;
60         }
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;
69                 }
70                 libusb_free_device_list(device_list, 1);
71         }
72         libusb_exit(ctx);
73         return TEST_STATUS_SUCCESS;
74 }
75
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)
78 {
79 #define LIST_COUNT 100
80         libusb_context * ctx = NULL;
81         libusb_device ** device_lists[LIST_COUNT];
82         int r, i;
83         memset(device_lists, 0, sizeof(device_lists));
84
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;
89         }
90
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;
99                 }
100         }
101
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;
107                 }
108         }
109
110         libusb_exit(ctx);
111         return TEST_STATUS_SUCCESS;
112 #undef LIST_COUNT
113 }
114
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)
119 {
120         libusb_context * ctx = NULL;
121         int r, i;
122
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;
129                 }
130
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);
134
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;
140                 }
141
142                 /* Destroy the first context */
143                 libusb_exit(ctx);
144                 /* Destroy the default context */
145                 libusb_exit(NULL);
146         }
147
148         return TEST_STATUS_SUCCESS;
149 }
150
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},
157         LIBUSBX_NULL_TEST
158 };
159
160 int main (int argc, char ** argv)
161 {
162         return libusbx_testlib_run_tests(argc, argv, tests);
163 }