-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathtest-umockdev-vala.vala
747 lines (635 loc) · 22.3 KB
/
test-umockdev-vala.vala
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
* test-umockdev.vala
*
* Copyright (C) 2012 Canonical Ltd.
* Author: Martin Pitt <martin.pitt@ubuntu.com>
*
* umockdev is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* umockdev is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; If not, see <http://www.gnu.org/licenses/>.
*/
using Assertions;
static void
tb_add_from_string (UMockdev.Testbed tb, string s)
{
try {
assert (tb.add_from_string (s));
} catch (Error e) {
stderr.printf ("Failed to call Testbed.add_from_string(): %s\n", e.message);
Process.abort ();
}
}
void
t_testbed_empty ()
{
var tb = new UMockdev.Testbed ();
assert (tb != null);
var enumerator = new GUdev.Enumerator (new GUdev.Client(null));
var devices = enumerator.execute ();
assert_cmpuint (devices.length(), Op.EQ, 0);
}
void
t_testbed_add_device ()
{
var tb = new UMockdev.Testbed ();
string syspath = tb.add_devicev ("usb",
"extkeyboard1",
null,
{ "idVendor", "0815", "idProduct", "AFFE" },
{ "ID_INPUT", "1", "ID_INPUT_KEYBOARD", "1" });
assert_cmpstr (syspath, Op.EQ, "/sys/devices/extkeyboard1");
var enumerator = new GUdev.Enumerator (new GUdev.Client(null));
var devices = enumerator.execute ();
assert_cmpuint (devices.length(), Op.EQ, 1);
GUdev.Device device = devices.nth_data(0);
assert_cmpstr (device.get_name (), Op.EQ, "extkeyboard1");
assert_cmpstr (device.get_sysfs_path (), Op.EQ, "/sys/devices/extkeyboard1");
assert_cmpstr (device.get_subsystem (), Op.EQ, "usb");
assert_cmpstr (device.get_sysfs_attr ("idVendor"), Op.EQ, "0815");
assert_cmpstr (device.get_sysfs_attr ("idProduct"), Op.EQ, "AFFE");
assert_cmpstr (device.get_sysfs_attr ("noSuchAttr"), Op.EQ, null);
assert_cmpstr (device.get_property ("DEVPATH"), Op.EQ, "/devices/extkeyboard1");
assert_cmpstr (device.get_property ("SUBSYSTEM"), Op.EQ, "usb");
assert_cmpstr (device.get_property ("ID_INPUT"), Op.EQ, "1");
assert_cmpstr (device.get_property ("ID_INPUT_KEYBOARD"), Op.EQ, "1");
assert_cmpstr (device.get_property ("NO_SUCH_PROP"), Op.EQ, null);
}
void
t_testbed_gudev_query_list ()
{
var tb = new UMockdev.Testbed ();
tb_add_from_string (tb, """P: /devices/myusbhub/cam
N: bus/usb/001/002
E: SUBSYSTEM=usb
E: DEVTYPE=usb_device
E: DEVNAME=/dev/bus/usb/001/002
P: /devices/myusbhub
N: bus/usb/001/001
E: SUBSYSTEM=usb
E: DEVTYPE=usb_device
E: DEVNAME=/dev/bus/usb/001/001
""");
var client = new GUdev.Client (null);
var devices = client.query_by_subsystem (null);
assert_cmpuint (devices.length (), Op.EQ, 2);
foreach (var dev in devices) {
assert_cmpstr (dev.get_subsystem(), Op.EQ, "usb");
if (dev.get_sysfs_path () == "/sys/devices/myusbhub") {
assert_cmpstr (dev.get_name(), Op.EQ, "myusbhub");
assert_cmpstr (dev.get_device_file(), Op.EQ, "/dev/bus/usb/001/001");
} else {
assert_cmpstr (dev.get_sysfs_path (), Op.EQ, "/sys/devices/myusbhub/cam");
assert_cmpstr (dev.get_name(), Op.EQ, "cam");
assert_cmpstr (dev.get_device_file(), Op.EQ, "/dev/bus/usb/001/002");
}
}
}
void
assert_listdir (string path, string[] entries)
{
var dir = Dir.open(path);
var files = new List<string>();
string? entry;
while ((entry = dir.read_name()) != null)
files.append(entry);
files.sort(strcmp);
assert_cmpuint (files.length(), Op.EQ, entries.length);
uint i = 0;
foreach (var n in files)
assert_cmpstr (n, Op.EQ, entries[i++]);
}
void
t_testbed_fs_ops ()
{
var tb = new UMockdev.Testbed ();
var orig_cwd = Environment.get_current_dir ();
var syspath = tb.add_devicev ("pci", "dev1", null, {"a", "1"}, {"DEVTYPE", "fancy"});
assert_cmpstr (syspath, Op.EQ, "/sys/devices/dev1");
// absolute paths
assert_listdir ("/sys", {"bus", "devices"});
assert_listdir ("/sys/devices", {"dev1"});
assert_listdir ("/sys/bus", {"pci"});
assert_listdir ("/sys/devices/dev1", {"a", "subsystem", "uevent"});
// change directory into trapped /sys
assert_cmpint (Posix.chdir ("/sys"), Op.EQ, 0);
assert_listdir (".", {"bus", "devices"});
assert_listdir ("bus", {"pci"});
assert_cmpstr (Environment.get_current_dir (), Op.EQ, "/sys");
assert_cmpint (Posix.chdir ("/sys/devices/dev1"), Op.EQ, 0);
assert_listdir (".", {"a", "subsystem", "uevent"});
assert_cmpstr (Environment.get_current_dir (), Op.EQ, "/sys/devices/dev1");
assert_cmpint (Posix.chdir ("/sys/class"), Op.EQ, -1);
assert_cmpint (Posix.errno, Op.EQ, Posix.ENOENT);
// relative paths into trapped /sys
assert_cmpint (Posix.chdir ("/"), Op.EQ, 0);
assert_listdir ("sys", {"bus", "devices"});
assert_listdir ("sys/devices", {"dev1"});
assert_listdir ("sys/bus", {"pci"});
assert_cmpint (Posix.chdir ("/etc"), Op.EQ, 0);
assert_listdir ("../sys", {"bus", "devices"});
assert_listdir ("../sys/devices", {"dev1"});
assert_listdir ("../sys/bus", {"pci"});
assert_cmpint (Posix.chdir (orig_cwd), Op.EQ, 0);
}
void
t_usbfs_ioctl_static ()
{
var tb = new UMockdev.Testbed ();
tb_add_from_string (tb, """P: /devices/mycam
N: 001
E: SUBSYSTEM=usb
""");
int fd = Posix.open ("/dev/001", Posix.O_RDWR, 0);
assert_cmpint (fd, Op.GE, 0);
int i = 1;
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_CLAIMINTERFACE, ref i), Op.EQ, 0);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_GETDRIVER, ref i), Op.EQ, -1);
assert_cmpint (Posix.errno, Op.EQ, Posix.ENODATA);
Posix.errno = 0;
/* no ioctl tree loaded */
var ci = Ioctl.usbdevfs_connectinfo();
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_CONNECTINFO, ref ci), Op.EQ, -1);
// usually ENOTTY, but seem to be EINVAL
assert_cmpint (Posix.errno, Op.GE, 22);
errno = 0;
// unknown ioctls don't work on an emulated device
assert_cmpint (Posix.ioctl (fd, Ioctl.TIOCSBRK, 0), Op.EQ, -1);
assert_cmpint (Posix.errno, Op.EQ, Posix.ENOTTY);
Posix.errno = 0;
// unknown ioctls do work on non-emulated devices
int fd2 = Posix.open ("/dev/tty", Posix.O_RDWR, 0);
if (fd2 > 0) {
assert_cmpint (Posix.ioctl (fd2, Ioctl.TIOCSBRK, 0), Op.EQ, 0);
assert_cmpint (Posix.errno, Op.EQ, 0);
Posix.close (fd2);
}
Posix.close (fd);
}
void
t_usbfs_ioctl_tree ()
{
var tb = new UMockdev.Testbed ();
tb_add_from_string (tb, """P: /devices/mycam
N: 001
E: SUBSYSTEM=usb
""");
// add simple ioctl tree
string test_tree;
if (BYTE_ORDER == ByteOrder.LITTLE_ENDIAN)
test_tree = """# little-endian test ioctls
USBDEVFS_CONNECTINFO 0 0B00000000000000
USBDEVFS_REAPURB 0 1 129 -1 0 4 4 0 9902AAFF
# another connect info
USBDEVFS_CONNECTINFO 42 0C00000001000000
""";
else
test_tree = """# big-endian test ioctls
USBDEVFS_CONNECTINFO 0 0000000B00000000
USBDEVFS_REAPURB 0 1 129 -1 0 4 4 0 9902AAFF
# another connect info
USBDEVFS_CONNECTINFO 42 0000000C01000000
""";
string tmppath;
int fd;
try {
fd = FileUtils.open_tmp ("test_ioctl_tree.XXXXXX", out tmppath);
} catch (Error e) { Process.abort (); }
assert_cmpint ((int) Posix.write (fd, test_tree, test_tree.length), Op.GT, 20);
// ioctl emulation does not get in the way of non-/dev fds
int i = 1;
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_CLAIMINTERFACE, ref i), Op.EQ, -1);
// usually ENOTTY, but seem to be EINVAL
assert_cmpint (Posix.errno, Op.GE, 22);
Posix.close (fd);
try {
tb.load_ioctl ("/dev/001", tmppath);
} catch (Error e) {
stderr.printf ("Cannot load ioctls: %s\n", e.message);
Process.abort ();
}
FileUtils.unlink (tmppath);
fd = Posix.open ("/dev/001", Posix.O_RDWR, 0);
assert_cmpint (fd, Op.GE, 0);
// static ioctl
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_CLAIMINTERFACE, ref i), Op.EQ, 0);
assert_cmpint (Posix.errno, Op.EQ, 0);
// loaded ioctl
var ci = Ioctl.usbdevfs_connectinfo();
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_CONNECTINFO, ref ci), Op.EQ, 0);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert_cmpuint (ci.devnum, Op.EQ, 11);
assert_cmpuint (ci.slow, Op.EQ, 0);
/* loaded ioctl: URB */
var urb_buffer = new uint8[4];
Ioctl.usbdevfs_urb urb = {1, 129, 0, 0, urb_buffer, 4, 0};
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_SUBMITURB, ref urb), Op.EQ, 0);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert_cmpuint (urb.status, Op.EQ, 0);
assert_cmpint (urb_buffer[0], Op.EQ, 0);
Ioctl.usbdevfs_urb* urb_reap = null;
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_REAPURB, ref urb_reap), Op.EQ, 0);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert (urb_reap == &urb);
assert_cmpint (urb.status, Op.EQ, -1);
assert_cmpuint (urb.buffer[0], Op.EQ, 0x99);
assert_cmpuint (urb.buffer[1], Op.EQ, 0x02);
assert_cmpuint (urb.buffer[2], Op.EQ, 0xAA);
assert_cmpuint (urb.buffer[3], Op.EQ, 0xFF);
// open the device a second time
int fd2 = Posix.open ("/dev/001", Posix.O_RDWR, 0);
assert_cmpint (fd2, Op.GE, 0);
// exercise ioctl on fd2, should iterate from beginning
ci.devnum = 99;
ci.slow = 99;
assert_cmpint (Posix.ioctl (fd2, Ioctl.USBDEVFS_CONNECTINFO, ref ci), Op.EQ, 0);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert_cmpuint (ci.devnum, Op.EQ, 11);
assert_cmpuint (ci.slow, Op.EQ, 0);
// should still work on first fd, and continue with original tree state
ci.devnum = 99;
ci.slow = 99;
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_CONNECTINFO, ref ci), Op.EQ, 42);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert_cmpuint (ci.devnum, Op.EQ, 12);
assert_cmpuint (ci.slow, Op.EQ, 1);
// should work after closing first fd, advancing position
Posix.close (fd);
ci.devnum = 99;
ci.slow = 99;
assert_cmpint (Posix.ioctl (fd2, Ioctl.USBDEVFS_CONNECTINFO, ref ci), Op.EQ, 42);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert_cmpuint (ci.devnum, Op.EQ, 12);
assert_cmpuint (ci.slow, Op.EQ, 1);
Posix.close (fd2);
}
void
t_usbfs_ioctl_tree_with_default_device ()
{
var tb = new UMockdev.Testbed ();
tb_add_from_string (tb, """P: /devices/mycam
N: 001
E: SUBSYSTEM=usb
""");
// add simple ioctl tree
string test_tree;
if (BYTE_ORDER == ByteOrder.LITTLE_ENDIAN)
test_tree = """# little-endian test ioctls
@DEV /dev/001
USBDEVFS_CONNECTINFO 0 0B00000000000000
""";
else
test_tree = """# big-endian test ioctls
@DEV /dev/001
USBDEVFS_CONNECTINFO 0 0000000B00000000
""";
string tmppath;
int fd;
try {
fd = FileUtils.open_tmp ("test_ioctl_tree.XXXXXX", out tmppath);
} catch (Error e) { Process.abort (); }
assert_cmpint ((int) Posix.write (fd, test_tree, test_tree.length), Op.GT, 20);
Posix.close (fd);
try {
tb.load_ioctl (null, tmppath);
} catch (Error e) {
stderr.printf ("Cannot load ioctls: %s\n", e.message);
Process.abort ();
}
FileUtils.unlink (tmppath);
fd = Posix.open ("/dev/001", Posix.O_RDWR, 0);
assert_cmpint (fd, Op.GE, 0);
// loaded ioctl
var ci = Ioctl.usbdevfs_connectinfo();
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_CONNECTINFO, ref ci), Op.EQ, 0);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert_cmpuint (ci.devnum, Op.EQ, 11);
assert_cmpuint (ci.slow, Op.EQ, 0);
}
void
t_usbfs_ioctl_tree_override_default_device ()
{
var tb = new UMockdev.Testbed ();
tb_add_from_string (tb, """P: /devices/mycam
N: 002
E: SUBSYSTEM=usb
""");
// add simple ioctl tree
string test_tree;
if (BYTE_ORDER == ByteOrder.LITTLE_ENDIAN)
test_tree = """# little-endian test ioctls
@DEV /dev/001
USBDEVFS_CONNECTINFO 0 0B00000000000000
""";
else
test_tree = """# big-endian test ioctls
@DEV /dev/001
USBDEVFS_CONNECTINFO 0 0000000B00000000
""";
string tmppath;
int fd;
try {
fd = FileUtils.open_tmp ("test_ioctl_tree.XXXXXX", out tmppath);
} catch (Error e) { Process.abort (); }
assert_cmpint ((int) Posix.write (fd, test_tree, test_tree.length), Op.GT, 20);
Posix.close (fd);
try {
tb.load_ioctl ("/dev/002", tmppath);
} catch (Error e) {
stderr.printf ("Cannot load ioctls: %s\n", e.message);
Process.abort ();
}
FileUtils.unlink (tmppath);
fd = Posix.open ("/dev/002", Posix.O_RDWR, 0);
assert_cmpint (fd, Op.GE, 0);
// loaded ioctl
var ci = Ioctl.usbdevfs_connectinfo();
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_CONNECTINFO, ref ci), Op.EQ, 0);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert_cmpuint (ci.devnum, Op.EQ, 11);
assert_cmpuint (ci.slow, Op.EQ, 0);
}
void
t_usbfs_ioctl_tree_xz ()
{
var tb = new UMockdev.Testbed ();
tb_add_from_string (tb, """P: /devices/mycam
N: 001
E: SUBSYSTEM=usb
""");
// add simple ioctl tree
string test_tree;
if (BYTE_ORDER == ByteOrder.LITTLE_ENDIAN)
test_tree = """USBDEVFS_CONNECTINFO 0 0B00000000000000
USBDEVFS_REAPURB 0 1 129 -1 0 4 4 0 9902AAFF
USBDEVFS_CONNECTINFO 42 0C00000001000000
""";
else
test_tree = """USBDEVFS_CONNECTINFO 0 0000000B00000000
USBDEVFS_REAPURB 0 1 129 -1 0 4 4 0 9902AAFF
USBDEVFS_CONNECTINFO 42 0000000C01000000
""";
string tmppath;
try {
Posix.close (FileUtils.open_tmp ("test_ioctl_tree.XXXXXX.xz", out tmppath));
} catch (Error e) { Process.abort (); }
int exit;
try {
Process.spawn_command_line_sync (
"sh -c 'echo \"" + test_tree + "\" | xz -9c > " + tmppath + "; sync'",
null, null, out exit);
} catch (SpawnError e) {
stderr.printf ("Cannot call xz: %s\n", e.message);
Process.abort ();
}
assert_cmpint (exit, Op.EQ, 0);
try {
tb.load_ioctl ("/dev/001", tmppath);
} catch (Error e) {
stderr.printf ("Cannot load ioctls: %s\n", e.message);
Process.abort ();
}
FileUtils.unlink (tmppath);
int fd = Posix.open ("/dev/001", Posix.O_RDWR, 0);
assert_cmpint (fd, Op.GE, 0);
var ci = Ioctl.usbdevfs_connectinfo();
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_CONNECTINFO, ref ci), Op.EQ, 0);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert_cmpuint (ci.devnum, Op.EQ, 11);
assert_cmpuint (ci.slow, Op.EQ, 0);
assert_cmpint (Posix.ioctl (fd, Ioctl.USBDEVFS_CONNECTINFO, ref ci), Op.EQ, 42);
assert_cmpint (Posix.errno, Op.EQ, 0);
assert_cmpuint (ci.devnum, Op.EQ, 12);
assert_cmpuint (ci.slow, Op.EQ, 1);
Posix.close (fd);
}
void
t_tty_stty ()
{
var tb = new UMockdev.Testbed ();
tb_add_from_string (tb, """P: /devices/usb/tty/ttyUSB1
N: ttyUSB1
E: DEVNAME=/dev/ttyUSB1
E: SUBSYSTEM=tty
A: dev=188:1
""");
// appears as a proper char device
Posix.Stat st;
assert_cmpint (Posix.lstat ("/dev/ttyUSB1", out st), Op.EQ, 0);
assert (Posix.S_ISCHR (st.st_mode));
assert_cmpuint (Posix.hack_major (st.st_rdev), Op.EQ, 188);
assert_cmpuint (Posix.minor (st.st_rdev), Op.EQ, 1);
// stty issues an ioctl; verify that it recognizes the fake device as a real tty
string pout, perr;
int pexit;
try {
Process.spawn_command_line_sync ("stty -F /dev/ttyUSB1", out pout, out perr, out pexit);
} catch (SpawnError e) {
stderr.printf ("Cannot call stty: %s\n", e.message);
Process.abort ();
}
assert_cmpstr (perr, Op.EQ, "");
assert_cmpint (pexit, Op.EQ, 0);
assert (pout.contains ("speed 38400 baud"));
}
void
t_tty_data ()
{
var tb = new UMockdev.Testbed ();
tb_add_from_string (tb, """P: /devices/serial/ttyS10
N: ttyS10
E: DEVNAME=/dev/ttyS10
E: SUBSYSTEM=tty
A: dev=4:74
""");
var client_fd = Posix.open ("/dev/ttyS10", Posix.O_RDWR, 0);
assert_cmpint (client_fd, Op.GE, 0);
var master_fd = tb.get_dev_fd ("/dev/ttyS10");
assert_cmpint (master_fd, Op.GE, 0);
char[] buf = new char[100];
/* client -> master */
assert_cmpint ((int) Posix.write (client_fd, "hello\n", 6), Op.EQ, 6);
assert_cmpint ((int) Posix.read (master_fd, buf, 100), Op.EQ, 6);
assert_cmpstr ((string) buf, Op.EQ, "hello\n");
/* master -> client */
buf = new char[100];
assert_cmpint ((int) Posix.write (master_fd, "world\n", 6), Op.EQ, 6);
assert_cmpint ((int) Posix.read (client_fd, buf, 100), Op.EQ, 6);
assert_cmpstr ((string) buf, Op.EQ, "world\n");
Posix.close (client_fd);
}
void
t_detects_running_in_testbed ()
{
assert (UMockdev.in_mock_environment());
}
void
t_detects_not_running_in_testbed ()
{
int pipefds[2];
assert_cmpint (Posix.pipe(pipefds), Op.EQ, 0);
Posix.pid_t pid = Posix.fork();
assert_cmpint (pid, Op.NE, -1);
if (pid == 0) {
Posix.close(pipefds[0]);
GLib.Environment.unset_variable("LD_PRELOAD");
string[] argv = { "--test-outside-testbed", pipefds[1].to_string() };
Posix.execv("/proc/self/exe", argv);
}
Posix.close(pipefds[1]);
char buf = 'x';
assert_cmpint ((int) Posix.read(pipefds[0], &buf, 1), Op.EQ, 1);
assert_cmpint (buf, Op.EQ, '0');
Posix.close(pipefds[0]);
}
int
is_test_inside_testbed (int pipefd)
{
char buf[1];
buf[0] = '0';
if (UMockdev.in_mock_environment())
buf[0] = '1';
Posix.write(pipefd, buf, 1);
return int.parse((string) buf);
}
class AttributeCounterThread {
public AttributeCounterThread (UMockdev.Testbed tb, string syspath, string name, uint max) {
this.tb = tb;
this.syspath = syspath;
this.name = name;
this.count = max;
}
public void* run () {
string attr_path = Path.build_filename (this.syspath, this.name);
for (; this.count > 0; --this.count) {
string cur_value;
try {
FileUtils.get_contents (attr_path, out cur_value);
} catch (FileError e) {
error ("(count %u) failed to read %s: %s", this.count, attr_path, e.message);
}
tb.set_attribute_int (this.syspath, name, int.parse(cur_value) + 1);
}
return null;
}
private UMockdev.Testbed tb;
private string name;
private string syspath;
private uint count;
}
/* set attributes in parallel; every thread handles a different
* attribute, so no locking required */
void
t_mt_parallel_attr_distinct ()
{
var tb = new UMockdev.Testbed ();
string syspath = tb.add_devicev ("changelings", "rapid", null,
{ "c1", "0", "c2", "0", "c3", "0"},
{});
var t1d = new AttributeCounterThread (tb, syspath, "c1", 100);
var t2d = new AttributeCounterThread (tb, syspath, "c2", 100);
var t3d = new AttributeCounterThread (tb, syspath, "c3", 100);
var t1 = new Thread<void*> ("t_c1", t1d.run);
var t2 = new Thread<void*> ("t_c2", t2d.run);
var t3 = new Thread<void*> ("t_c3", t3d.run);
t1.join();
t2.join();
t3.join();
string val;
try {
FileUtils.get_contents(Path.build_filename(syspath, "c1"), out val);
assert_cmpstr (val, Op.EQ, "100");
FileUtils.get_contents(Path.build_filename(syspath, "c2"), out val);
assert_cmpstr (val, Op.EQ, "100");
FileUtils.get_contents(Path.build_filename(syspath, "c3"), out val);
assert_cmpstr (val, Op.EQ, "100");
} catch (FileError e) {
error ("failed to read attribute: %s", e.message);
}
}
void
t_mt_uevent ()
{
var tb = new UMockdev.Testbed ();
var gudev = new GUdev.Client ({"pci"});
var ml = new MainLoop ();
uint add_count = 0;
uint change_count = 0;
uint num_changes = 10;
gudev.uevent.connect((client, action, device) => {
if (action == "add")
add_count++;
else {
if (++change_count == num_changes)
ml.quit ();
}
});
// causes one "add" event
var syspath = tb.add_devicev ("pci", "dev1", null, {"a", "1"}, {"DEVTYPE", "fancy"});
// this thread is purely to create noise in the preload lib
var t_noise = new Thread<void*> ("noise", () => {
string contents;
while (ml.is_running ()) {
try {
FileUtils.get_contents ("/sys/devices/dev1/a", out contents);
} catch (FileError e) {
error ("(#changes: %u) Error opening attribute file: %s", change_count, e.message);
}
assert_cmpstr (contents, Op.EQ, "1");
tb.set_property (syspath, "ID_FOO", "1");
}
return null;
});
// synthesize num_changes "change" events
var t_emitter = new Thread<void*> ("emitter", () => {
for (uint i = 0; i < num_changes; ++i)
tb.uevent (syspath, "change");
return null;
});
// fallback timeout
Timeout.add(3000, () => { ml.quit(); return false; });
ml.run ();
t_emitter.join ();
t_noise.join ();
assert_cmpuint (add_count, Op.EQ, 1);
assert_cmpuint (change_count, Op.EQ, num_changes);
}
int
main (string[] args)
{
for (int i = 0; i < args.length; i++) {
if (args[i] == "--test-outside-testbed")
return is_test_inside_testbed(int.parse(args[i+1]));
}
Test.init (ref args);
/* tests for mocking /sys */
Test.add_func ("/umockdev-testbed-vala/empty", t_testbed_empty);
Test.add_func ("/umockdev-testbed-vala/add_devicev", t_testbed_add_device);
Test.add_func ("/umockdev-testbed-vala/gudev-query-list", t_testbed_gudev_query_list);
Test.add_func ("/umockdev-testbed-vala/fs_ops", t_testbed_fs_ops);
/* tests for mocking ioctls */
Test.add_func ("/umockdev-testbed-vala/usbfs_ioctl_static", t_usbfs_ioctl_static);
Test.add_func ("/umockdev-testbed-vala/usbfs_ioctl_tree", t_usbfs_ioctl_tree);
Test.add_func ("/umockdev-testbed-vala/usbfs_ioctl_tree_with_default_device", t_usbfs_ioctl_tree_with_default_device);
Test.add_func ("/umockdev-testbed-vala/usbfs_ioctl_tree_override_default_device", t_usbfs_ioctl_tree_override_default_device);
Test.add_func ("/umockdev-testbed-vala/usbfs_ioctl_tree_xz", t_usbfs_ioctl_tree_xz);
/* tests for mocking TTYs */
Test.add_func ("/umockdev-testbed-vala/tty_stty", t_tty_stty);
Test.add_func ("/umockdev-testbed-vala/tty_data", t_tty_data);
/* test for umockdev-preload detection */
Test.add_func ("/umockdev-testbed-vala/detects_running_in_testbed", t_detects_running_in_testbed);
Test.add_func ("/umockdev-testbed-vala/detects_running_outside_testbed", t_detects_not_running_in_testbed);
/* tests for multi-thread safety */
Test.add_func ("/umockdev-testbed-vala/mt_parallel_attr_distinct", t_mt_parallel_attr_distinct);
Test.add_func ("/umockdev-testbed-vala/mt_uevent", t_mt_uevent);
return Test.run();
}