File: fu-test-plugin.c

package info (click to toggle)
fwupd 2.0.19-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 32,340 kB
  • sloc: ansic: 274,440; python: 11,468; xml: 9,432; sh: 1,625; makefile: 167; cpp: 19; asm: 11; javascript: 9
file content (434 lines) | stat: -rw-r--r-- 14,491 bytes parent folder | download
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * Copyright 2016 Richard Hughes <richard@hughsie.com>
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "config.h"

#include "fu-test-device.h"
#include "fu-test-plugin.h"

struct _FuTestPlugin {
	FuPlugin parent_instance;
};

G_DEFINE_TYPE(FuTestPlugin, fu_test_plugin, FU_TYPE_PLUGIN)

static gboolean
fu_test_plugin_coldplug(FuPlugin *plugin, FuProgress *progress, GError **error)
{
	FuContext *ctx = fu_plugin_get_context(plugin);
	g_autoptr(FuDevice) device = fu_test_device_new(ctx);

	if (!fu_device_setup(device, error))
		return FALSE;
	if (fu_plugin_get_config_value_boolean(plugin, "RegistrationSupported")) {
		fu_plugin_device_register(plugin, device);
		if (fu_device_get_metadata(device, "BestDevice") == NULL) {
			g_set_error_literal(error,
					    FWUPD_ERROR,
					    FWUPD_ERROR_NOT_FOUND,
					    "Device not set by another plugin");
			return FALSE;
		}
	}
	fu_plugin_device_add(plugin, device);

	if (fu_plugin_get_config_value_boolean(plugin, "CompositeChild")) {
		g_autoptr(FuDevice) child1 = NULL;
		g_autoptr(FuDevice) child2 = NULL;

		child1 = fu_device_new(ctx);
		fu_device_build_vendor_id_u16(child1, "USB", 0xFFFF);
		fu_device_add_protocol(child1, "com.acme");
		fu_device_set_physical_id(child1, "fake");
		fu_device_set_logical_id(child1, "child1");
		fu_device_add_instance_id(child1, "7fddead7-12b5-4fb9-9fa0-6d30305df755");
		fu_device_set_name(child1, "Module1");
		fu_device_set_version_format(child1, FWUPD_VERSION_FORMAT_PLAIN);
		fu_device_set_version(child1, "1");
		fu_device_add_parent_guid(child1, "b585990a-003e-5270-89d5-3705a17f9a43");
		fu_device_add_flag(child1, FWUPD_DEVICE_FLAG_UPDATABLE);
		fu_device_add_flag(child1, FWUPD_DEVICE_FLAG_UNSIGNED_PAYLOAD);
		fu_device_add_private_flag(child1, FU_DEVICE_PRIVATE_FLAG_INSTALL_PARENT_FIRST);
		fu_device_add_private_flag(child1, FU_DEVICE_PRIVATE_FLAG_PARENT_NAME_PREFIX);
		fu_plugin_device_add(plugin, child1);

		child2 = fu_device_new(ctx);
		fu_device_build_vendor_id_u16(child2, "USB", 0xFFFF);
		fu_device_add_protocol(child2, "com.acme");
		fu_device_set_physical_id(child2, "fake");
		fu_device_set_logical_id(child2, "child2");
		fu_device_add_instance_id(child2, "b8fe6b45-8702-4bcd-8120-ef236caac76f");
		fu_device_set_name(child2, "Module2");
		fu_device_set_version_format(child2, FWUPD_VERSION_FORMAT_PLAIN);
		fu_device_set_version(child2, "10");
		fu_device_add_parent_guid(child2, "b585990a-003e-5270-89d5-3705a17f9a43");
		fu_device_add_flag(child2, FWUPD_DEVICE_FLAG_UPDATABLE);
		fu_device_add_flag(child2, FWUPD_DEVICE_FLAG_UNSIGNED_PAYLOAD);
		fu_device_add_private_flag(child2, FU_DEVICE_PRIVATE_FLAG_INSTALL_PARENT_FIRST);
		fu_device_add_private_flag(child2, FU_DEVICE_PRIVATE_FLAG_PARENT_NAME_PREFIX);
		fu_plugin_device_add(plugin, child2);
	}

	return TRUE;
}

static gboolean
fu_test_plugin_modify_config(FuPlugin *plugin, const gchar *key, const gchar *value, GError **error)
{
	const gchar *keys[] = {"AnotherWriteRequired",
			       "CompositeChild",
			       "DecompressDelay",
			       "NeedsActivation",
			       "NeedsReboot",
			       "RegistrationSupported",
			       "RequestDelay",
			       "RequestSupported",
			       "VerifyDelay",
			       "WriteDelay",
			       "WriteSupported",
			       NULL};
	if (!g_strv_contains(keys, key)) {
		g_set_error(error,
			    FWUPD_ERROR,
			    FWUPD_ERROR_NOT_SUPPORTED,
			    "config key %s not supported",
			    key);
		return FALSE;
	}
	return fu_plugin_set_config_value(plugin, key, value, error);
}

static void
fu_test_plugin_device_registered(FuPlugin *plugin, FuDevice *device)
{
	fu_device_set_metadata(device, "BestDevice", "/dev/urandom");
}

static gboolean
fu_test_plugin_verify(FuPlugin *plugin,
		      FuDevice *device,
		      FuProgress *progress,
		      FuPluginVerifyFlags flags,
		      GError **error)
{
	if (g_strcmp0(fu_device_get_version(device), "1.2.2") == 0) {
		fu_device_add_checksum(device, "90d0ad436d21e0687998cd2127b2411135e1f730");
		fu_device_add_checksum(
		    device,
		    "921631916a60b295605dbae6a0309f9b64e2401b3de8e8506e109fc82c586e3a");
		return TRUE;
	}
	if (g_strcmp0(fu_device_get_version(device), "1.2.3") == 0) {
		fu_device_add_checksum(device, "7998cd212721e068b2411135e1f90d0ad436d730");
		fu_device_add_checksum(
		    device,
		    "dbae6a0309b3de8e850921631916a60b2956056e109fc82c586e3f9b64e2401a");
		return TRUE;
	}
	if (g_strcmp0(fu_device_get_version(device), "1.2.4") == 0) {
		fu_device_add_checksum(device, "2b8546ba805ad10bf8a2e5ad539d53f303812ba5");
		fu_device_add_checksum(
		    device,
		    "b546c241029ce4e16c99eb6bfd77b86e4490aa3826ba71b8a4114e96a2d69bcd");
		return TRUE;
	}
	g_set_error(error,
		    FWUPD_ERROR,
		    FWUPD_ERROR_NOT_SUPPORTED,
		    "no checksum for %s",
		    fu_device_get_version(device));
	return FALSE;
}

static gchar *
fu_test_plugin_get_version(GBytes *blob_fw)
{
	guint64 val = 0;
	g_autoptr(GError) error_local = NULL;
	g_autofree gchar *str_safe = fu_strsafe_bytes(blob_fw, G_MAXSIZE);

	if (str_safe == NULL)
		return NULL;
	if (!fu_strtoull(str_safe, &val, 0, G_MAXSIZE, FU_INTEGER_BASE_AUTO, &error_local)) {
		g_debug("invalid version specified: %s", error_local->message);
		return NULL;
	}
	if (val == 0x0)
		return NULL;
	return fu_version_from_uint32(val, FWUPD_VERSION_FORMAT_TRIPLET);
}

static gboolean
fu_test_plugin_write_firmware(FuPlugin *plugin,
			      FuDevice *device,
			      FuFirmware *firmware,
			      FuProgress *progress,
			      FwupdInstallFlags flags,
			      GError **error)
{
	g_autofree gchar *decompress_delay_str = NULL;
	g_autofree gchar *write_delay_str = NULL;
	g_autofree gchar *verify_delay_str = NULL;
	guint64 delay_decompress_ms = 0;
	guint64 delay_write_ms = 0;
	guint64 delay_verify_ms = 0;
	guint64 delay_request_ms = 0;

	if (!fu_plugin_get_config_value_boolean(plugin, "WriteSupported")) {
		g_set_error_literal(error,
				    FWUPD_ERROR,
				    FWUPD_ERROR_NOT_SUPPORTED,
				    "device was not in supported mode");
		return FALSE;
	}

	fu_progress_set_status(progress, FWUPD_STATUS_DECOMPRESSING);
	decompress_delay_str = fu_plugin_get_config_value(plugin, "DecompressDelay");
	if (decompress_delay_str != NULL) {
		if (!fu_strtoull(decompress_delay_str,
				 &delay_decompress_ms,
				 0,
				 10000,
				 FU_INTEGER_BASE_AUTO,
				 error)) {
			g_prefix_error_literal(error, "failed to parse DecompressDelay: ");
			return FALSE;
		}
	}
	for (guint i = 0; i <= delay_decompress_ms; i++) {
		fu_device_sleep(device, 1);
		fu_progress_set_percentage_full(progress, i, delay_decompress_ms);
	}

	/* send an interactive request, and wait some time */
	if (fu_plugin_get_config_value_boolean(plugin, "RequestSupported")) {
		g_autofree gchar *request_delay_str = NULL;
		g_autoptr(FwupdRequest) request = fwupd_request_new();

		fwupd_request_set_kind(request, FWUPD_REQUEST_KIND_IMMEDIATE);
		fwupd_request_set_id(request, FWUPD_REQUEST_ID_REMOVE_REPLUG);
		fwupd_request_add_flag(request, FWUPD_REQUEST_FLAG_ALLOW_GENERIC_MESSAGE);
		fwupd_request_set_message(request,
					  "Please pretend to remove the device you cannot see or "
					  "touch and please re-insert it.");
		if (!fu_device_emit_request(device, request, progress, error))
			return FALSE;
		request_delay_str = fu_plugin_get_config_value(plugin, "RequestDelay");
		if (request_delay_str != NULL) {
			if (!fu_strtoull(request_delay_str,
					 &delay_request_ms,
					 0,
					 10000,
					 FU_INTEGER_BASE_AUTO,
					 error)) {
				g_prefix_error_literal(error, "failed to parse RequestDelay: ");
				return FALSE;
			}
		}
		fu_device_sleep(device, delay_request_ms);
	}

	fu_progress_set_status(progress, FWUPD_STATUS_DEVICE_WRITE);
	write_delay_str = fu_plugin_get_config_value(plugin, "WriteDelay");
	if (write_delay_str != NULL) {
		if (!fu_strtoull(write_delay_str,
				 &delay_write_ms,
				 0,
				 10000,
				 FU_INTEGER_BASE_AUTO,
				 error)) {
			g_prefix_error_literal(error, "failed to parse WriteDelay: ");
			return FALSE;
		}
	}
	for (guint i = 0; i <= delay_write_ms; i++) {
		fu_device_sleep(device, 1);
		fu_progress_set_percentage_full(progress, i, delay_write_ms);
	}
	fu_progress_set_status(progress, FWUPD_STATUS_DEVICE_VERIFY);
	verify_delay_str = fu_plugin_get_config_value(plugin, "VerifyDelay");
	if (verify_delay_str != NULL) {
		if (!fu_strtoull(verify_delay_str,
				 &delay_verify_ms,
				 0,
				 10000,
				 FU_INTEGER_BASE_AUTO,
				 error)) {
			g_prefix_error_literal(error, "failed to parse VerifyDelay: ");
			return FALSE;
		}
	}
	for (guint i = 0; i <= delay_verify_ms; i++) {
		fu_device_sleep(device, 1);
		fu_progress_set_percentage_full(progress, i, delay_verify_ms);
	}

	/* composite test, upgrade composite devices */
	if (fu_plugin_get_config_value_boolean(plugin, "CompositeChild")) {
		fu_device_set_version_format(device, FWUPD_VERSION_FORMAT_PLAIN);
		if (g_strcmp0(fu_device_get_logical_id(device), "child1") == 0) {
			fu_device_set_version(device, "2");
			return TRUE;
		}
		if (g_strcmp0(fu_device_get_logical_id(device), "child2") == 0) {
			fu_device_set_version(device, "11");
			return TRUE;
		}
	}

	/* upgrade, or downgrade */
	if (fu_plugin_get_config_value_boolean(plugin, "NeedsActivation")) {
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION);
	} else if (fu_plugin_get_config_value_boolean(plugin, "NeedsReboot")) {
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT);
	} else {
		g_autofree gchar *ver = NULL;
		g_autoptr(GBytes) blob_fw = NULL;
		g_autoptr(GInputStream) stream = NULL;

		/* live update */
		fu_device_remove_flag(device, FWUPD_DEVICE_FLAG_NEEDS_ACTIVATION);
		fu_device_remove_flag(device, FWUPD_DEVICE_FLAG_NEEDS_REBOOT);

		stream = fu_firmware_get_stream(firmware, error);
		if (stream == NULL)
			return FALSE;
		blob_fw = fu_input_stream_read_bytes(stream, 0x0, 9, NULL, error);
		if (blob_fw == NULL)
			return FALSE;
		ver = fu_test_plugin_get_version(blob_fw);
		fu_device_set_version_format(device, FWUPD_VERSION_FORMAT_TRIPLET);
		if (ver != NULL) {
			fu_device_set_version(device, ver);
		} else {
			if (flags & FWUPD_INSTALL_FLAG_ALLOW_OLDER) {
				fu_device_set_version(device, "1.2.2");
			} else {
				fu_device_set_version(device, "1.2.3");
			}
		}
	}

	/* do this all over again */
	if (fu_plugin_get_config_value_boolean(plugin, "AnotherWriteRequired") &&
	    !fu_device_get_metadata_boolean(device, "DoneAnotherWriteRequired")) {
		fu_device_add_flag(device, FWUPD_DEVICE_FLAG_ANOTHER_WRITE_REQUIRED);
		fu_device_set_metadata_boolean(device, "DoneAnotherWriteRequired", TRUE);
	}

	/* do this all over again */
	if (fu_plugin_get_config_value_boolean(plugin, "InstallLoopRestart") &&
	    !fu_device_get_metadata_boolean(device, "DoneInstallLoopRestart")) {
		fu_device_add_private_flag(device, FU_DEVICE_PRIVATE_FLAG_INSTALL_LOOP_RESTART);
		fu_device_set_metadata_boolean(device, "DoneInstallLoopRestart", TRUE);
	}

	/* for the self tests only */
	fu_device_set_metadata_integer(device,
				       "nr-update",
				       fu_device_get_metadata_integer(device, "nr-update") + 1);

	return TRUE;
}

static gboolean
fu_test_plugin_activate(FuPlugin *plugin, FuDevice *device, FuProgress *process, GError **error)
{
	fu_device_set_version_format(device, FWUPD_VERSION_FORMAT_TRIPLET);
	fu_device_set_version(device, "1.2.3");
	return TRUE;
}

static gboolean
fu_test_plugin_get_results(FuPlugin *plugin, FuDevice *device, GError **error)
{
	fu_device_set_update_state(device, FWUPD_UPDATE_STATE_SUCCESS);
	return TRUE;
}

static gboolean
fu_test_plugin_composite_prepare(FuPlugin *plugin, GPtrArray *devices, GError **error)
{
	if (fu_plugin_get_config_value_boolean(plugin, "CompositeChild")) {
		for (guint i = 0; i < devices->len; i++) {
			FuDevice *device = g_ptr_array_index(devices, i);
			fu_device_set_metadata(device, "frimbulator", "1");
		}
	}
	return TRUE;
}

static gboolean
fu_test_plugin_composite_cleanup(FuPlugin *plugin, GPtrArray *devices, GError **error)
{
	if (fu_plugin_get_config_value_boolean(plugin, "CompositeChild")) {
		for (guint i = 0; i < devices->len; i++) {
			FuDevice *device = g_ptr_array_index(devices, i);
			fu_device_set_metadata(device, "frombulator", "1");
		}
	}
	return TRUE;
}

static gboolean
fu_test_plugin_attach(FuPlugin *plugin, FuDevice *device, FuProgress *progress, GError **error)
{
	fu_device_set_metadata_integer(device,
				       "nr-attach",
				       fu_device_get_metadata_integer(device, "nr-attach") + 1);
	return TRUE;
}

static void
fu_test_plugin_init(FuTestPlugin *self)
{
	fu_plugin_add_flag(FU_PLUGIN(self), FWUPD_PLUGIN_FLAG_TEST_ONLY);
}

static void
fu_test_plugin_constructed(GObject *obj)
{
	FuPlugin *plugin = FU_PLUGIN(obj);
	fu_plugin_set_config_default(plugin, "AnotherWriteRequired", "false");
	fu_plugin_set_config_default(plugin, "InstallLoopRestart", "false");
	fu_plugin_set_config_default(plugin, "CompositeChild", "false");
	fu_plugin_set_config_default(plugin, "DecompressDelay", "0");
	fu_plugin_set_config_default(plugin, "NeedsActivation", "false");
	fu_plugin_set_config_default(plugin, "NeedsReboot", "false");
	fu_plugin_set_config_default(plugin, "RegistrationSupported", "false");
	fu_plugin_set_config_default(plugin, "RequestDelay", "10"); /* ms */
	fu_plugin_set_config_default(plugin, "RequestSupported", "false");
	fu_plugin_set_config_default(plugin, "VerifyDelay", "0");
	fu_plugin_set_config_default(plugin, "WriteDelay", "0");
	fu_plugin_set_config_default(plugin, "WriteSupported", "true");
}

static void
fu_test_plugin_finalize(GObject *obj)
{
	g_debug("destroy");
	G_OBJECT_CLASS(fu_test_plugin_parent_class)->finalize(obj);
}

static void
fu_test_plugin_class_init(FuTestPluginClass *klass)
{
	FuPluginClass *plugin_class = FU_PLUGIN_CLASS(klass);
	GObjectClass *object_class = G_OBJECT_CLASS(klass);

	object_class->finalize = fu_test_plugin_finalize;
	plugin_class->constructed = fu_test_plugin_constructed;
	plugin_class->composite_cleanup = fu_test_plugin_composite_cleanup;
	plugin_class->composite_prepare = fu_test_plugin_composite_prepare;
	plugin_class->get_results = fu_test_plugin_get_results;
	plugin_class->activate = fu_test_plugin_activate;
	plugin_class->write_firmware = fu_test_plugin_write_firmware;
	plugin_class->verify = fu_test_plugin_verify;
	plugin_class->attach = fu_test_plugin_attach;
	plugin_class->coldplug = fu_test_plugin_coldplug;
	plugin_class->device_registered = fu_test_plugin_device_registered;
	plugin_class->modify_config = fu_test_plugin_modify_config;
}