summaryrefslogtreecommitdiff
path: root/gusb/gusb-umockdev-test.c
blob: e1b7714911e4aea98126611637ef84513dc34964 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * libusb umockdev based tests
 *
 * Copyright (C) 2022 Benjamin Berg <bberg@redhat.com>
 *
 * SPDX-License-Identifier: LGPL-2.1+
 */

#include <glib.h>
#include <string.h>
#include <unistd.h>

#include "gusb.h"
#include "umockdev.h"

#define UNUSED_DATA __attribute__((unused)) gconstpointer unused_data

/* avoid leak reports inside assertions; leaking stuff on assertion failures does not matter in
 * tests */
#if !defined(__clang__)
#pragma GCC diagnostic ignored "-Wanalyzer-malloc-leak"
#pragma GCC diagnostic ignored "-Wanalyzer-file-leak"
#endif

typedef struct {
	UMockdevTestbed *testbed;
	GUsbContext *ctx;
} UMockdevTestbedFixture;

static void
test_fixture_setup(UMockdevTestbedFixture *fixture, UNUSED_DATA)
{
	fixture->testbed = umockdev_testbed_new();
	g_assert(fixture->testbed != NULL);
}

static void
test_fixture_setup_empty(UMockdevTestbedFixture *fixture, UNUSED_DATA)
{
	test_fixture_setup(fixture, NULL);
	fixture->ctx = g_usb_context_new(NULL);
}

static void
test_fixture_teardown(UMockdevTestbedFixture *fixture, UNUSED_DATA)
{
	/* break context -> device -> context cycle */
	if (fixture->ctx)
		g_object_run_dispose(G_OBJECT(fixture->ctx));
	g_clear_object(&fixture->ctx);
	g_clear_object(&fixture->testbed);

	/* running the mainloop is needed to ensure everything is cleaned up */
	while (g_main_context_iteration(NULL, FALSE)) {
	}
}

static void
test_fixture_add_canon(UMockdevTestbedFixture *fixture)
{
	/* NOTE: there is no device file, so cannot be opened */

	/* NOTE: add_device would not create a file, needed for device emulation */
	/* XXX: racy, see https://github.com/martinpitt/umockdev/issues/173 */
	umockdev_testbed_add_from_string(
	    fixture->testbed,
	    "P: /devices/usb1\n"
	    "N: bus/usb/001/001\n"
	    "E: SUBSYSTEM=usb\n"
	    "E: DRIVER=usb\n"
	    "E: BUSNUM=001\n"
	    "E: DEVNUM=001\n"
	    "E: DEVNAME=/dev/bus/usb/001/001\n"
	    "E: DEVTYPE=usb_device\n"
	    "A: bConfigurationValue=1\\n\n"
	    "A: busnum=1\\n\n"
	    "A: devnum=1\\n\n"
	    "A: bConfigurationValue=1\\n\n"
	    "A: speed=480\\n\n"
	    /* descriptor from a Canon PowerShot SX200; VID 04a9 PID 31c0 */
	    "H: descriptors="
	    "1201000200000040a904c03102000102"
	    "030109022700010100c0010904000003"
	    "06010100070581020002000705020200"
	    "020007058303080009\n",
	    NULL);
}

static void
test_ctx_enumerate(UMockdevTestbedFixture *fixture, UNUSED_DATA)
{
	g_autoptr(GPtrArray) devices = NULL;

	test_fixture_add_canon(fixture);

	g_usb_context_enumerate(fixture->ctx);

	devices = g_usb_context_get_devices(fixture->ctx);
	g_assert_cmpint(devices->len, ==, 1);
}

static void
count_hotplug_event_cb(GUsbContext *context, GUsbDevice *device, gpointer user_data)
{
	int *counter = user_data;

	*counter += 1;
}

static void
test_ctx_hotplug(UMockdevTestbedFixture *fixture, UNUSED_DATA)
{
	g_autoptr(GPtrArray) devices = NULL;
	gint events = 0;

	g_signal_connect(fixture->ctx, "device-added", G_CALLBACK(count_hotplug_event_cb), &events);

	g_usb_context_enumerate(fixture->ctx);

	devices = g_usb_context_get_devices(fixture->ctx);
	g_assert_cmpint(devices->len, ==, 0);
	g_assert_cmpint(events, ==, 0);
	g_clear_pointer(&devices, g_ptr_array_unref);

	test_fixture_add_canon(fixture);
	/* ensure the event was processed by helper thread */
	g_usleep(G_USEC_PER_SEC / 2);

	/* still not returned (and no event fired) */
	devices = g_usb_context_get_devices(fixture->ctx);
	g_assert_cmpint(devices->len, ==, 0);
	g_assert_cmpint(events, ==, 0);
	g_clear_pointer(&devices, g_ptr_array_unref);

	/* run mainloop, which causes the event to be processed */
	while (g_main_context_iteration(NULL, FALSE)) {
	}

	devices = g_usb_context_get_devices(fixture->ctx);
	g_assert_cmpint(events, ==, 1);
	g_assert_cmpint(devices->len, ==, 1);
	g_clear_pointer(&devices, g_ptr_array_unref);
}

static void
test_ctx_hotplug_dispose(UMockdevTestbedFixture *fixture, UNUSED_DATA)
{
	g_autoptr(GPtrArray) devices = NULL;
	gint events = 0;

	g_signal_connect(fixture->ctx, "device-added", G_CALLBACK(count_hotplug_event_cb), &events);

	g_usb_context_enumerate(fixture->ctx);
	devices = g_usb_context_get_devices(fixture->ctx);
	g_assert_cmpint(devices->len, ==, 0);
	g_assert_cmpint(events, ==, 0);
	g_clear_pointer(&devices, g_ptr_array_unref);

	test_fixture_add_canon(fixture);
	/* ensure the event was processed by helper thread */
	g_usleep(G_USEC_PER_SEC / 2);

	/* still not returned (and no event fired) */
	g_usb_context_enumerate(fixture->ctx);
	devices = g_usb_context_get_devices(fixture->ctx);
	g_assert_cmpint(devices->len, ==, 0);
	g_assert_cmpint(events, ==, 0);
	g_clear_pointer(&devices, g_ptr_array_unref);

	/* idle handler is pending, we dispose our context reference */
	g_object_run_dispose(G_OBJECT(fixture->ctx));

	/* run mainloop, which causes the event to be processed */
	while (g_main_context_iteration(NULL, FALSE)) {
	}

	/* but no signal is fired */
	g_assert_cmpint(events, ==, 0);

	g_clear_object(&fixture->ctx);
}

int
main(int argc, char **argv)
{
	g_test_init(&argc, &argv, NULL);

	g_test_add("/gusb/ctx/enumerate",
		   UMockdevTestbedFixture,
		   NULL,
		   test_fixture_setup_empty,
		   test_ctx_enumerate,
		   test_fixture_teardown);

	g_test_add("/gusb/ctx/hotplug",
		   UMockdevTestbedFixture,
		   NULL,
		   test_fixture_setup_empty,
		   test_ctx_hotplug,
		   test_fixture_teardown);

	g_test_add("/gusb/ctx/hotplug-dispose",
		   UMockdevTestbedFixture,
		   NULL,
		   test_fixture_setup_empty,
		   test_ctx_hotplug_dispose,
		   test_fixture_teardown);

	return g_test_run();
}