23
23
#
24
24
import unittest
25
25
from unittest import mock
26
- from unittest .mock import Mock
26
+ from unittest .mock import Mock , AsyncMock
27
27
28
28
import jinja2
29
29
40
40
try :
41
41
from qubes .device_protocol import DeviceAssignment , VirtualDevice , Port
42
42
43
- def make_assignment (backend , ident , required = False ):
43
+ def make_assignment (backend , ident , auto_attach = False ):
44
44
return DeviceAssignment (VirtualDevice (Port (
45
45
backend , ident , 'usb' )),
46
- mode = 'required ' if required else 'manual' )
46
+ mode = 'auto-attach ' if auto_attach else 'manual' )
47
47
48
48
def assign (test , collection , assignment ):
49
49
test .loop .run_until_complete (collection .assign (assignment ))
@@ -401,7 +401,8 @@ def setUp(self):
401
401
self .qrexec_policy ('qubes.USB' , self .frontend .name , self .backend .name )
402
402
self .usbdev_ident = create_usb_gadget (self .backend ).decode ()
403
403
self .usbdev_name = '{}:{}:{}' .format (
404
- self .backend .name , self .usbdev_ident , "0000:0000::?******" )
404
+ self .backend .name , self .usbdev_ident ,
405
+ "1234:1234:0123456789:u080650" )
405
406
406
407
def tearDown (self ):
407
408
# remove vms in this specific order, otherwise there may remain stray
@@ -416,9 +417,7 @@ def test_000_list(self):
416
417
417
418
def test_010_assign (self ):
418
419
usb_dev = self .backend .devices ['usb' ][self .usbdev_ident ]
419
- ass = DeviceAssignment (VirtualDevice (Port (
420
- self .backend , self .usbdev_ident , 'usb' )),
421
- mode = 'ask-to-attach' )
420
+ ass = make_assignment (self .backend , self .usbdev_ident , auto_attach = True )
422
421
assign (self , self .frontend .devices ['usb' ], ass )
423
422
self .assertIsNone (usb_dev .attachment )
424
423
try :
@@ -432,10 +431,14 @@ def test_010_assign(self):
432
431
433
432
self .assertEqual (usb_dev .attachment , self .frontend )
434
433
434
+ @unittest .mock .patch ('qubes.ext.utils.confirm_device_attachment' )
435
435
@unittest .skipIf (LEGACY , "new feature" )
436
- def test_011_assign_ask (self ):
436
+ def test_011_assign_ask (self , confirm ):
437
+ confirm .return_value = self .frontend .name
437
438
usb_dev = self .backend .devices ['usb' ][self .usbdev_ident ]
438
- ass = make_assignment (self .backend , self .usbdev_ident , required = True )
439
+ ass = DeviceAssignment (VirtualDevice (Port (
440
+ self .backend , self .usbdev_ident , 'usb' )),
441
+ mode = 'ask-to-attach' )
439
442
assign (self , self .frontend .devices ['usb' ], ass )
440
443
self .assertIsNone (usb_dev .attachment )
441
444
try :
@@ -488,7 +491,7 @@ def test_030_detach(self):
488
491
489
492
def test_040_unassign (self ):
490
493
usb_dev = self .backend .devices ['usb' ][self .usbdev_ident ]
491
- ass = make_assignment (self .backend , self .usbdev_ident , required = True )
494
+ ass = make_assignment (self .backend , self .usbdev_ident , auto_attach = True )
492
495
assign (self , self .frontend .devices ['usb' ], ass )
493
496
self .assertIsNone (usb_dev .attachment )
494
497
unassign (self , self .frontend .devices ['usb' ], ass )
@@ -540,7 +543,7 @@ def test_060_auto_detach_on_remove(self):
540
543
def test_061_auto_attach_on_reconnect (self ):
541
544
self .frontend .start ()
542
545
usb_list = self .backend .devices ['usb' ]
543
- ass = make_assignment (self .backend , self .usbdev_ident , required = True )
546
+ ass = make_assignment (self .backend , self .usbdev_ident , auto_attach = True )
544
547
try :
545
548
assign (self , self .frontend .devices ['usb' ], ass )
546
549
except qubesusbproxy .core3ext .USBProxyNotInstalled as e :
@@ -568,7 +571,7 @@ def test_061_auto_attach_on_reconnect(self):
568
571
def test_062_ask_to_attach_on_start (self ):
569
572
self .frontend .start ()
570
573
usb_list = self .backend .devices ['usb' ]
571
- ass = make_assignment (self .backend , self .usbdev_ident , required = True )
574
+ ass = make_assignment (self .backend , self .usbdev_ident , auto_attach = True )
572
575
try :
573
576
assign (self , self .frontend .devices ['usb' ], ass )
574
577
except qubesusbproxy .core3ext .USBProxyNotInstalled as e :
@@ -855,8 +858,10 @@ def test_012_on_qdb_change_multiple_assignments_dev(self):
855
858
self .assertEqual (self .ext .attach_and_notify .call_args [0 ][1 ].options ,
856
859
{'any' : 'did' })
857
860
858
- @unittest .mock .patch ('subprocess.Popen' )
859
- def test_013_on_qdb_change_two_fronts_failed (self , mock_confirm ):
861
+ # replace async function with sync one
862
+ @unittest .mock .patch ('qubes.ext.utils.confirm_device_attachment' ,
863
+ new_callable = Mock )
864
+ def test_013_on_qdb_change_two_fronts_failed (self , _confirm ):
860
865
back , front = self .added_assign_setup ()
861
866
862
867
exp_dev = qubesusbproxy .core3ext .USBDevice (back , '1-1' )
@@ -866,18 +871,22 @@ def test_013_on_qdb_change_two_fronts_failed(self, mock_confirm):
866
871
back .devices ['usb' ]._assigned .append (assign )
867
872
back .devices ['usb' ]._exposed .append (exp_dev )
868
873
869
- proc = Mock ()
870
- proc .communicate = Mock ()
871
- proc .communicate .return_value = (b'nonsense' , b'' )
872
- mock_confirm .return_value = proc
874
+ class quick_mock :
875
+ @staticmethod
876
+ def result ():
877
+ return "nonsense"
878
+
873
879
self .ext .attach_and_notify = Mock ()
874
- with mock .patch ('asyncio.ensure_future' ):
880
+ with mock .patch ('asyncio.ensure_future' ) as future :
881
+ future .return_value = quick_mock
875
882
self .ext .on_qdb_change (back , None , None )
876
- proc . communicate . assert_called_once ()
883
+
877
884
self .ext .attach_and_notify .assert_not_called ()
878
885
879
- @unittest .mock .patch ('subprocess.Popen' )
880
- def test_014_on_qdb_change_two_fronts (self , mock_confirm ):
886
+ # replace async function with sync one
887
+ @unittest .mock .patch ('qubes.ext.utils.confirm_device_attachment' ,
888
+ new_callable = Mock )
889
+ def test_014_on_qdb_change_two_fronts (self , _confirm ):
881
890
back , front = self .added_assign_setup ()
882
891
883
892
exp_dev = qubesusbproxy .core3ext .USBDevice (back , '1-1' )
@@ -887,16 +896,17 @@ def test_014_on_qdb_change_two_fronts(self, mock_confirm):
887
896
back .devices ['usb' ]._assigned .append (assign )
888
897
back .devices ['usb' ]._exposed .append (exp_dev )
889
898
890
- proc = Mock ()
891
- proc .communicate = Mock ()
892
- proc .communicate .return_value = (b'front-vm' , b'' )
893
- mock_confirm .return_value = proc
899
+ class quick_mock :
900
+ @staticmethod
901
+ def result ():
902
+ return "front-vm"
903
+
894
904
self .ext .attach_and_notify = Mock ()
895
- with mock .patch ('asyncio.ensure_future' ):
905
+ with mock .patch ('asyncio.ensure_future' ) as future :
906
+ future .return_value = quick_mock
896
907
self .ext .on_qdb_change (back , None , None )
897
- proc .communicate .assert_called_once ()
898
- self .ext .attach_and_notify .assert_called_once_with (
899
- front , assign )
908
+
909
+ self .ext .attach_and_notify .assert_called_once_with (front , assign )
900
910
# don't ask again
901
911
self .assertEqual (self .ext .attach_and_notify .call_args [0 ][1 ].mode .value ,
902
912
'auto-attach' )
@@ -915,6 +925,7 @@ def test_015_on_qdb_change_ask(self):
915
925
self .ext .on_qdb_change (back , None , None )
916
926
self .assertEqual (self .ext .attach_and_notify .call_args [0 ][1 ].mode .value ,
917
927
'ask-to-attach' )
928
+
918
929
def test_020_on_startup_multiple_assignments_including_full (self ):
919
930
back , front = self .added_assign_setup ()
920
931
@@ -936,10 +947,9 @@ def test_020_on_startup_multiple_assignments_including_full(self):
936
947
back .devices ['usb' ]._exposed .append (
937
948
qubesusbproxy .core3ext .USBDevice (back , '1-1' ))
938
949
939
- self .ext .attach_and_notify = Mock ()
950
+ self .ext .attach_and_notify = AsyncMock ()
940
951
loop = asyncio .get_event_loop ()
941
- with mock .patch ('asyncio.ensure_future' ):
942
- loop .run_until_complete (self .ext .on_domain_start (front , None ))
952
+ loop .run_until_complete (self .ext .on_domain_start (front , None ))
943
953
self .assertEqual (self .ext .attach_and_notify .call_args [0 ][1 ].options ,
944
954
{'pid' : 'did' })
945
955
@@ -961,9 +971,8 @@ def test_021_on_startup_multiple_assignments_port_vs_dev(self):
961
971
qubesusbproxy .core3ext .USBDevice (back , '1-1' ))
962
972
963
973
loop = asyncio .get_event_loop ()
964
- self .ext .attach_and_notify = Mock ()
965
- with mock .patch ('asyncio.ensure_future' ):
966
- loop .run_until_complete (self .ext .on_domain_start (front , None ))
974
+ self .ext .attach_and_notify = AsyncMock ()
975
+ loop .run_until_complete (self .ext .on_domain_start (front , None ))
967
976
self .assertEqual (self .ext .attach_and_notify .call_args [0 ][1 ].options ,
968
977
{'pid' : 'any' })
969
978
@@ -986,10 +995,9 @@ def test_022_on_startup_multiple_assignments_dev(self):
986
995
back .devices ['usb' ]._exposed .append (
987
996
qubesusbproxy .core3ext .USBDevice (back , '1-2' ))
988
997
989
- self .ext .attach_and_notify = Mock ()
998
+ self .ext .attach_and_notify = AsyncMock ()
990
999
loop = asyncio .get_event_loop ()
991
- with mock .patch ('asyncio.ensure_future' ):
992
- loop .run_until_complete (self .ext .on_domain_start (front , None ))
1000
+ loop .run_until_complete (self .ext .on_domain_start (front , None ))
993
1001
self .assertEqual (self .ext .attach_and_notify .call_args [0 ][1 ].options ,
994
1002
{'any' : 'did' })
995
1003
@@ -1001,7 +1009,7 @@ def test_023_on_startup_already_attached(self):
1001
1009
exp_dev .port , exp_dev .device_id ), mode = 'auto-attach' )
1002
1010
1003
1011
front .devices ['usb' ]._assigned .append (assign )
1004
- attached_device = back .devices ['usb' ]._exposed .append (exp_dev )
1012
+ back .devices ['usb' ]._exposed .append (exp_dev )
1005
1013
1006
1014
self .ext .attach_and_notify = Mock ()
1007
1015
loop = asyncio .get_event_loop ()
0 commit comments