Repository navigation
Expand file tree
/
Copy pathtest_PhysicalDevice.py
More file actions
832 lines (696 loc) · 32.9 KB
/
Copy pathtest_PhysicalDevice.py
File metadata and controls
832 lines (696 loc) · 32.9 KB
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
#!/usr/bin/python3
## Copyright (C) 2015 Red Hat, Inc.
## Authors:
## Tim Waugh <twaugh@redhat.com>
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
## This program 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 General Public License for more details.
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import pytest
try:
import cups
from PhysicalDevice import PhysicalDevice
from cupshelpers import cupshelpers
except ImportError:
cups = None
def _make_device(uri, device_class, serial, make_and_model='Xerox B235 MFP'):
return cupshelpers.Device(
uri,
**{'device-class': device_class,
'device-make-and-model': make_and_model,
'device-id': 'MFG:Xerox;MDL:B235 MFP;SN:%s;' % serial})
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_ordering():
# See https://bugzilla.redhat.com/show_bug.cgi?id=1154686
device = cupshelpers.Device("dnssd://Abc%20Def%20%5BABCDEF%5D._ipp._tcp.local/",
**{'device-class': "network",
'device-make-and-model': "Abc Def",
'device-id': "MFG:Abc;MDL:Def;"})
phys = PhysicalDevice (device)
device = cupshelpers.Device("hp:/net/Abc_Def?hostname=ABCDEF",
**{'device-class': "network",
'device-make-and-model': "Abc Def",
'device-id': "MFG:Abc;MDL:Def;"})
phys.add_device (device)
devices = phys.get_devices ()
assert devices[0].uri.startswith ("hp:")
device = cupshelpers.Device("usb://Abc/Def",
**{'device-class': "direct",
'device-make-and-model': "Abc Def",
'device-id': "MFG:Abc;MDL:Def;"})
phys = PhysicalDevice (device)
device = cupshelpers.Device("hp://Abc/Def",
**{'device-class': "direct",
'device-make-and-model': "Abc Def",
'device-id': "MFG:Abc;MDL:Def;"})
phys.add_device (device)
devices = phys.get_devices ()
assert devices[0].uri.startswith ("hp")
dev1 = cupshelpers.Device("hp:/usb/HP_Color_LaserJet_CP3525?serial=CNCTC8G0QX",
**{'device-id':'MFG:Hewlett-Packard;CMD:PJL,MLC,BIDI-ECP,PJL,PCLXL,PCL,POSTSCRIPT,PDF;MDL:HP Color LaserJet CP3525;CLS:PRINTER;DES:Hewlett-Packard Color LaserJet CP3525;',
'device-make-and-model':'HP Color LaserJet CP3525',
'device-class':'direct'})
phys = PhysicalDevice (dev1)
dev2 = cupshelpers.Device('usb://HP/Color%20LaserJet%20CP3525?serial=CNCTC8G0QX',
**{'device-id':'MFG:Hewlett-Packard;CMD:PJL,MLC,BIDI-ECP,PJL,PCLXL,PCL,POSTSCRIPT,PDF;MDL:HP Color LaserJet CP3525;CLS:PRINTER;DES:Hewlett-Packard Color LaserJet CP3525;',
'device-make-and-model':'HP Color LaserJet CP3525',
'device-class':'direct'})
# hp device should sort < usb device
assert dev1 < dev2
phys.add_device (dev2)
devices = phys.get_devices ()
assert devices[0] < devices[1]
assert devices[0].uri.startswith ("hp")
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_usb_serial_drops_interface_suffix():
device = cupshelpers.Device(
"usb://HP/Color%20LaserJet%20CP3525?serial=34004H030206H&interface=1",
**{'device-id':'MFG:Hewlett-Packard;CMD:PJL,MLC,BIDI-ECP,PJL,PCLXL,PCL,POSTSCRIPT,PDF;MDL:HP Color LaserJet CP3525;CLS:PRINTER;DES:Hewlett-Packard Color LaserJet CP3525;',
'device-make-and-model':'HP Color LaserJet CP3525',
'device-class':'direct'})
assert device.id_dict['SN'] == '34004H030206H'
def _make_dnssd_device(uri, device_class='network',
make_and_model='Xerox B235 MFP',
device_id='MFG:Xerox;MDL:B235 MFP;'):
"""Helper to build a cupshelpers.Device for DNS-SD test URIs."""
return cupshelpers.Device(
uri,
**{'device-class': device_class,
'device-make-and-model': make_and_model,
'device-id': device_id})
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_multiprotocol_same_printer_grouped():
"""Case A: Two protocols for same printer must become one PhysicalDevice."""
dev_ipps = _make_dnssd_device('ipps://Office._ipps._tcp.local/')
dev_lpd = _make_dnssd_device('lpd://Office._printer._tcp.local/')
phys1 = PhysicalDevice(dev_ipps)
phys2 = PhysicalDevice(dev_lpd)
assert phys1 == phys2
phys1.add_device(dev_lpd)
assert len(phys1.get_devices()) == 2
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_same_instance_different_serial_not_merged():
"""Case C: Same instance name but different serial numbers must NOT merge."""
dev1 = _make_dnssd_device(
'ipps://Office._ipps._tcp.local/',
device_id='MFG:Xerox;MDL:B235 MFP;SN:SER001;')
dev2 = _make_dnssd_device(
'lpd://Office._printer._tcp.local/',
device_id='MFG:Xerox;MDL:B235 MFP;SN:SER002;')
assert PhysicalDevice(dev1) != PhysicalDevice(dev2)
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_normal_hostnames_not_misinterpreted():
"""Cases D & E: Regular hostnames or hostnames with underscores must remain network hosts."""
for uri, expected_host in [
('ipp://printer.office.local/ipp/print', 'printer.office.local'),
('ipp://my_printer.local/ipp/print', 'my_printer.local'),
]:
dev = cupshelpers.Device(uri, **{'device-class': 'network',
'device-make-and-model': 'Generic Printer',
'device-id': 'MFG:Generic;MDL:Printer;'})
phys = PhysicalDevice(dev)
assert phys._network_host == expected_host
assert phys.dnssd_hostname is None
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_instance_name_with_dots():
"""Case J: DNS-SD instance names containing dots must extract instance correctly."""
dev1 = _make_dnssd_device('ipps://Office.Floor2._ipps._tcp.local/')
dev2 = _make_dnssd_device('lpd://Office.Floor2._printer._tcp.local/')
p1 = PhysicalDevice(dev1)
p2 = PhysicalDevice(dev2)
assert p1.dnssd_hostname == 'Office.Floor2'
assert p1 == p2
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_different_printers_same_model_not_merged():
"""Two printers with the same model but different instance names must
remain separate, even if their make/model are identical."""
dev_a = _make_dnssd_device(
'ipps://Printer%20A._ipps._tcp.local/')
dev_b = _make_dnssd_device(
'ipps://Printer%20B._ipps._tcp.local/')
phys_a = PhysicalDevice(dev_a)
phys_b = PhysicalDevice(dev_b)
assert phys_a != phys_b, (
"Different DNS-SD instance names must produce different PhysicalDevices")
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_single_protocol_unchanged():
"""A printer discovered via a single protocol must behave as before."""
dev = _make_dnssd_device(
'ipps://Solo%20Printer._ipps._tcp.local/')
phys = PhysicalDevice(dev)
assert len(phys.get_devices()) == 1
assert phys.get_devices()[0].uri == \
'ipps://Solo%20Printer._ipps._tcp.local/'
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_usb_device_not_affected():
"""USB devices must not be affected by DNS-SD hostname extraction."""
dev = cupshelpers.Device(
'usb://HP/LaserJet?serial=XYZ',
**{'device-class': 'direct',
'device-make-and-model': 'HP LaserJet',
'device-id': 'MFG:HP;MDL:LaserJet;SN:XYZ;'})
phys = PhysicalDevice(dev)
assert phys._network_host is None
assert phys.dnssd_hostname is None
assert phys.sn == 'XYZ'
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_regular_ip_printer_not_affected():
"""Network printers discovered by IP (not DNS-SD names) must behave
as before."""
dev = cupshelpers.Device(
'ipp://192.168.1.100/ipp/print',
**{'device-class': 'network',
'device-make-and-model': 'HP LaserJet',
'device-id': 'MFG:HP;MDL:LaserJet;'})
phys = PhysicalDevice(dev)
assert phys._network_host == '192.168.1.100'
assert phys.dnssd_hostname is None
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_socket_printer_not_affected():
"""Socket printers discovered by IP must behave as before."""
dev = cupshelpers.Device(
'socket://10.0.0.5',
**{'device-class': 'network',
'device-make-and-model': 'Brother MFC-L2700DW',
'device-id': 'MFG:Brother;MDL:MFC-L2700DW;'})
phys = PhysicalDevice(dev)
assert phys._network_host == '10.0.0.5'
assert phys.dnssd_hostname is None
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_dnssd_scheme_device_unchanged():
"""Devices with dnssd:// URIs (classic CUPS dnssd backend) must still
work correctly — _get_host_from_uri returns (None, None) for them,
and hostname/address are set via Avahi resolution."""
dev = cupshelpers.Device(
'dnssd://Printer%20Name._ipp._tcp.local/',
**{'device-class': 'network',
'device-make-and-model': 'HP LaserJet',
'device-id': 'MFG:HP;MDL:LaserJet;'})
dev.address = '192.168.1.50'
dev.hostname = 'printer-name'
phys = PhysicalDevice(dev)
assert phys._network_host == '192.168.1.50'
assert phys.dnssd_hostname == 'printer-name.local'
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_multiprotocol_grouping_loop():
"""End-to-end grouping loop as performed by newprinter.add_devices()."""
devices = [
_make_dnssd_device(
'ipps://Xerox%20B235._ipps._tcp.local/',
device_id='MFG:Xerox;MDL:B235 MFP;CMD:PCLM,PS;'),
_make_dnssd_device(
'lpd://Xerox%20B235._printer._tcp.local/',
device_id='MFG:Xerox;MDL:B235 MFP;CMD:PS;'),
_make_dnssd_device(
'ipp://Xerox%20B235._ipp._tcp.local/',
device_id='MFG:Xerox;MDL:B235 MFP;CMD:PCLM;'),
# Unrelated USB printer
cupshelpers.Device(
'usb://HP/LaserJet?serial=AAA',
**{'device-class': 'direct',
'device-make-and-model': 'HP LaserJet',
'device-id': 'MFG:HP;MDL:LaserJet;SN:AAA;'}),
]
physical_devices = []
for device in devices:
pd = PhysicalDevice(device)
try:
i = physical_devices.index(pd)
physical_devices[i].add_device(device)
except ValueError:
physical_devices.append(pd)
assert len(physical_devices) == 2
xerox = [p for p in physical_devices if p.mdl == 'B235 MFP']
hp = [p for p in physical_devices if p.mdl == 'LaserJet']
assert len(xerox) == 1
assert len(hp) == 1
assert len(xerox[0].get_devices()) == 3
assert len(hp[0].get_devices()) == 1
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_extract_dnssd_instance():
"""Verify _extract_dnssd_instance accurately parses DNS-SD service hostnames
and ignores normal hostnames and IP addresses."""
phys = PhysicalDevice(cupshelpers.Device('ipp://localhost'))
assert phys._extract_dnssd_instance('Broken%20Xerox%20B235%20MFP._ipps._tcp.local') == 'Broken Xerox B235 MFP'
assert phys._extract_dnssd_instance('Office._printer._tcp.local.') == 'Office'
assert phys._extract_dnssd_instance('Floor.2.Printer._ipp._tcp.local') == 'Floor.2.Printer'
assert phys._extract_dnssd_instance('Device._service._udp.local') == 'Device'
assert phys._extract_dnssd_instance('B%C3%BCro%20Drucker._ipps._tcp.local') == 'Büro Drucker'
assert phys._extract_dnssd_instance('printer.example.com') is None
assert phys._extract_dnssd_instance('printer.local') is None
assert phys._extract_dnssd_instance('192.168.1.100') is None
assert phys._extract_dnssd_instance('server.sub.example.local') is None
assert phys._extract_dnssd_instance('') is None
assert phys._extract_dnssd_instance(None) is None
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_get_info_clean_printer_name():
"""Verify get_info() displays a clean printer name without cryptic .local hostnames."""
dnssd_dev = cupshelpers.Device(
'ipps://Broken%20Xerox%20B235%20MFP._ipps._tcp.local/',
**{'device-class': 'network',
'device-make-and-model': 'Broken Xerox B235 MFP',
'device-id': 'MFG:Xerox;MDL:B235 MFP;'})
phys_dnssd = PhysicalDevice(dnssd_dev)
assert phys_dnssd.get_info() == 'Xerox B235 MFP'
assert '.local' not in phys_dnssd.get_info()
ip_dev = cupshelpers.Device(
'socket://192.168.1.100',
**{'device-class': 'network',
'device-make-and-model': 'HP LaserJet 4000',
'device-id': 'MFG:HP;MDL:LaserJet 4000;'})
phys_ip = PhysicalDevice(ip_dev)
assert phys_ip.get_info() == 'HP LaserJet 4000 (192.168.1.100)'
local_dev = cupshelpers.Device(
'ipp://printer.local/ipp/print',
**{'device-class': 'network',
'device-make-and-model': 'HP LaserJet 4000',
'device-id': 'MFG:HP;MDL:LaserJet 4000;'})
phys_local = PhysicalDevice(local_dev)
assert phys_local.get_info() == 'HP LaserJet 4000 (printer.local)'
fqdn_dev = cupshelpers.Device(
'ipp://printer.example.com/ipp/print',
**{'device-class': 'network',
'device-make-and-model': 'HP LaserJet 4000',
'device-id': 'MFG:HP;MDL:LaserJet 4000;'})
phys_fqdn = PhysicalDevice(fqdn_dev)
assert phys_fqdn.get_info() == 'HP LaserJet 4000 (printer.example.com)'
usb_dev = cupshelpers.Device(
'usb://HP/LaserJet?serial=XYZ123',
**{'device-class': 'direct',
'device-make-and-model': 'HP LaserJet',
'device-id': 'MFG:HP;MDL:LaserJet;SN:XYZ123;'})
phys_usb = PhysicalDevice(usb_dev)
assert phys_usb.get_info() == 'HP LaserJet (XYZ123)'
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_multiprotocol_encoded_unicode_instance_grouped():
"""Verify that multi-protocol printers with percent-encoded Unicode instance names
(e.g., spaces and non-ASCII chars) are correctly unquoted and grouped."""
dev_ipps = _make_dnssd_device('ipps://B%C3%BCro%20Drucker._ipps._tcp.local/')
dev_lpd = _make_dnssd_device('lpd://B%C3%BCro%20Drucker._printer._tcp.local/')
p_ipps = PhysicalDevice(dev_ipps)
p_lpd = PhysicalDevice(dev_lpd)
assert p_ipps.dnssd_hostname == 'Büro Drucker.local'
assert p_lpd.dnssd_hostname == 'Büro Drucker.local'
assert p_ipps == p_lpd
def test_canonical_id_fallback_when_mfg_mdl_empty():
"""Test A: A device with empty MFG/MDL in id_dict must fall back to
make_and_model instead of returning ('', '')."""
dev = cupshelpers.Device(
'ipp://Xerox(R)%20B235%20MFP%20(USB)._ipp._tcp.local/',
**{'device-class': 'network',
'device-info': 'Xerox(R) B235 MFP (USB)',
'device-make-and-model': 'Xerox(R) B235 MFP (USB)',
'device-id': 'MFG:;MDL:;SN:34004H030206H;'})
phys = PhysicalDevice(dev)
assert phys.mfg != ''
assert phys.mdl != ''
assert (phys.mfg, phys.mdl) == ('Xerox(R)', 'B235 MFP')
def test_get_info_falls_back_to_device_info_when_translation_absent():
"""Test B: A device whose info string is not present in _ppdippstr
must have get_info() return the original device.info rather than None."""
dev = cupshelpers.Device(
'ipp://Xerox(R)%20B235%20MFP%20(USB)._ipp._tcp.local/',
**{'device-class': 'network',
'device-info': 'Xerox(R) B235 MFP (USB)',
'device-make-and-model': 'Xerox(R) B235 MFP (USB)',
'device-id': 'MFG:;MDL:;SN:34004H030206H;'})
phys = PhysicalDevice(dev)
# Manually clear mfg to trigger the backend translation lookup branch
phys.mfg = ''
info = phys.get_info()
assert info is not None
assert info == 'Xerox(R) B235 MFP (USB)'
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_lpd_dnssd_and_lpd_schemes_merge():
"""Alex regression test: dnssd://..._printer._tcp.local/?uuid=UUID and
lpd://..._printer._tcp.local/ must resolve to the SAME PhysicalDevice,
canonicalize to the appropriate LPD connection representation, and
UUID differences must not create another PhysicalDevice.
Also tests discovery order both ways."""
uuid1 = "cbca9fde-d208-4156-9a2f-82857dd0be5f"
uuid2 = "12345678-abcd-ef01-2345-6789abcdef01"
# 1. Forward order: dnssd first, then lpd
dev_dnssd = _make_dnssd_device(
f'dnssd://OpenPrinting%20LPD%20Printer._printer._tcp.local/?uuid={uuid1}',
make_and_model='OpenPrinting LPD Printer',
device_id='MFG:OpenPrinting;MDL:LPD Printer;')
dev_lpd = _make_dnssd_device(
'lpd://OpenPrinting%20LPD%20Printer._printer._tcp.local/',
make_and_model='OpenPrinting LPD Printer',
device_id='MFG:OpenPrinting;MDL:LPD Printer;')
phys_dnssd = PhysicalDevice(dev_dnssd)
phys_lpd = PhysicalDevice(dev_lpd)
assert phys_dnssd == phys_lpd
# Merging via grouping loop
pds = []
for d in [dev_dnssd, dev_lpd]:
pd = PhysicalDevice(d)
try:
i = pds.index(pd)
pds[i].add_device(d)
except ValueError:
pds.append(pd)
assert len(pds) == 1
devices = pds[0].get_devices()
assert len(devices) == 1
assert devices[0].uri == 'lpd://OpenPrinting%20LPD%20Printer._printer._tcp.local/'
assert devices[0].type == 'lpd'
# 2. Reverse order: lpd first, then dnssd
dev_dnssd_rev = _make_dnssd_device(
f'dnssd://OpenPrinting%20LPD%20Printer._printer._tcp.local/?uuid={uuid1}',
make_and_model='OpenPrinting LPD Printer',
device_id='MFG:OpenPrinting;MDL:LPD Printer;')
dev_lpd_rev = _make_dnssd_device(
'lpd://OpenPrinting%20LPD%20Printer._printer._tcp.local/',
make_and_model='OpenPrinting LPD Printer',
device_id='MFG:OpenPrinting;MDL:LPD Printer;')
pds_rev = []
for d in [dev_lpd_rev, dev_dnssd_rev]:
pd = PhysicalDevice(d)
try:
i = pds_rev.index(pd)
pds_rev[i].add_device(d)
except ValueError:
pds_rev.append(pd)
assert len(pds_rev) == 1
devices_rev = pds_rev[0].get_devices()
assert len(devices_rev) == 1
assert devices_rev[0].uri == 'lpd://OpenPrinting%20LPD%20Printer._printer._tcp.local/'
assert devices_rev[0].type == 'lpd'
# 3. UUID differences do not create another PhysicalDevice
dev_dnssd_diff_uuid = _make_dnssd_device(
f'dnssd://OpenPrinting%20LPD%20Printer._printer._tcp.local/?uuid={uuid2}',
make_and_model='OpenPrinting LPD Printer',
device_id='MFG:OpenPrinting;MDL:LPD Printer;')
phys_diff_uuid = PhysicalDevice(dev_dnssd_diff_uuid)
assert phys_dnssd == phys_diff_uuid
pds[0].add_device(dev_dnssd_diff_uuid)
assert len(pds[0].get_devices()) == 1
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_appsocket_dnssd_and_socket_schemes_merge():
"""Alex regression test: dnssd://..._pdl-datastream._tcp.local/?uuid=UUID and
socket://..._pdl-datastream._tcp.local/ must resolve to the SAME PhysicalDevice,
canonicalize to the appropriate AppSocket connection representation.
Also tests discovery order both ways."""
uuid = "cbca9fde-d208-4156-9a2f-82857dd0be5f"
# Forward order
dev_dnssd = _make_dnssd_device(
f'dnssd://OpenPrinting%20AppSocket%20Printer._pdl-datastream._tcp.local/?uuid={uuid}',
make_and_model='OpenPrinting AppSocket Printer',
device_id='MFG:OpenPrinting;MDL:AppSocket Printer;')
dev_socket = _make_dnssd_device(
'socket://OpenPrinting%20AppSocket%20Printer._pdl-datastream._tcp.local/',
make_and_model='OpenPrinting AppSocket Printer',
device_id='MFG:OpenPrinting;MDL:AppSocket Printer;')
phys_dnssd = PhysicalDevice(dev_dnssd)
phys_socket = PhysicalDevice(dev_socket)
assert phys_dnssd == phys_socket
pds = []
for d in [dev_dnssd, dev_socket]:
pd = PhysicalDevice(d)
try:
i = pds.index(pd)
pds[i].add_device(d)
except ValueError:
pds.append(pd)
assert len(pds) == 1
devices = pds[0].get_devices()
assert len(devices) == 1
assert devices[0].uri == 'socket://OpenPrinting%20AppSocket%20Printer._pdl-datastream._tcp.local/'
assert devices[0].type == 'socket'
# Reverse order
dev_dnssd_rev = _make_dnssd_device(
f'dnssd://OpenPrinting%20AppSocket%20Printer._pdl-datastream._tcp.local/?uuid={uuid}',
make_and_model='OpenPrinting AppSocket Printer',
device_id='MFG:OpenPrinting;MDL:AppSocket Printer;')
dev_socket_rev = _make_dnssd_device(
'socket://OpenPrinting%20AppSocket%20Printer._pdl-datastream._tcp.local/',
make_and_model='OpenPrinting AppSocket Printer',
device_id='MFG:OpenPrinting;MDL:AppSocket Printer;')
pds_rev = []
for d in [dev_socket_rev, dev_dnssd_rev]:
pd = PhysicalDevice(d)
try:
i = pds_rev.index(pd)
pds_rev[i].add_device(d)
except ValueError:
pds_rev.append(pd)
assert len(pds_rev) == 1
devices_rev = pds_rev[0].get_devices()
assert len(devices_rev) == 1
assert devices_rev[0].uri == 'socket://OpenPrinting%20AppSocket%20Printer._pdl-datastream._tcp.local/'
assert devices_rev[0].type == 'socket'
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_network_printer_protocols_separation():
"""Verify that OpenPrinting LPD, OpenPrinting AppSocket, and two different
AppSocket printer names remain separate PhysicalDevices."""
dev_lpd = _make_dnssd_device(
'dnssd://OpenPrinting%20LPD%20Printer._printer._tcp.local/?uuid=111',
make_and_model='OpenPrinting LPD Printer',
device_id='MFG:OpenPrinting;MDL:LPD Printer;')
dev_appsocket = _make_dnssd_device(
'dnssd://OpenPrinting%20AppSocket%20Printer._pdl-datastream._tcp.local/?uuid=222',
make_and_model='OpenPrinting AppSocket Printer',
device_id='MFG:OpenPrinting;MDL:AppSocket Printer;')
dev_appsocket2 = _make_dnssd_device(
'socket://Another%20AppSocket%20Printer._pdl-datastream._tcp.local/',
make_and_model='Another AppSocket Printer',
device_id='MFG:Another;MDL:AppSocket Printer;')
p_lpd = PhysicalDevice(dev_lpd)
p_appsocket = PhysicalDevice(dev_appsocket)
p_appsocket2 = PhysicalDevice(dev_appsocket2)
assert p_lpd != p_appsocket
assert p_lpd != p_appsocket2
assert p_appsocket != p_appsocket2
pds = []
for d in [dev_lpd, dev_appsocket, dev_appsocket2]:
pd = PhysicalDevice(d)
try:
i = pds.index(pd)
pds[i].add_device(d)
except ValueError:
pds.append(pd)
assert len(pds) == 3
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_kyocera_ipp_dnssd_and_ipps_schemes_merge():
uuid1 = "4509a320-00a0-008f-00b6-002507510eca"
uuid2 = "98765432-1111-2222-3333-444455556666"
dev_dnssd = _make_dnssd_device(
f"dnssd://Kyocera%20ECOSYS%20M2040dn._ipp._tcp.local/?uuid={uuid1}",
make_and_model="Kyocera ECOSYS M2040dn",
device_id="MFG:Kyocera;MDL:ECOSYS M2040dn;")
dev_ipps = _make_dnssd_device(
"ipps://Kyocera%20ECOSYS%20M2040dn._ipps._tcp.local/",
make_and_model="Kyocera ECOSYS M2040dn",
device_id="MFG:Kyocera;MDL:ECOSYS M2040dn;")
phys_dnssd = PhysicalDevice(dev_dnssd)
phys_ipps = PhysicalDevice(dev_ipps)
assert phys_dnssd == phys_ipps
pds = []
for d in [dev_dnssd, dev_ipps]:
pd = PhysicalDevice(d)
try:
i = pds.index(pd)
pds[i].add_device(d)
except ValueError:
pds.append(pd)
assert len(pds) == 1
devices = pds[0].get_devices()
assert len(devices) == 2
uris = [d.uri for d in devices]
assert any("._ipp._tcp.local" in u for u in uris)
assert any("._ipps._tcp.local" in u for u in uris)
dev_dnssd_rev = _make_dnssd_device(
f"dnssd://Kyocera%20ECOSYS%20M2040dn._ipp._tcp.local/?uuid={uuid1}",
make_and_model="Kyocera ECOSYS M2040dn",
device_id="MFG:Kyocera;MDL:ECOSYS M2040dn;")
dev_ipps_rev = _make_dnssd_device(
"ipps://Kyocera%20ECOSYS%20M2040dn._ipps._tcp.local/",
make_and_model="Kyocera ECOSYS M2040dn",
device_id="MFG:Kyocera;MDL:ECOSYS M2040dn;")
pds_rev = []
for d in [dev_ipps_rev, dev_dnssd_rev]:
pd = PhysicalDevice(d)
try:
i = pds_rev.index(pd)
pds_rev[i].add_device(d)
except ValueError:
pds_rev.append(pd)
assert len(pds_rev) == 1
devices_rev = pds_rev[0].get_devices()
assert len(devices_rev) == 2
uris_rev = [d.uri for d in devices_rev]
assert any("._ipp._tcp.local" in u for u in uris_rev)
assert any("._ipps._tcp.local" in u for u in uris_rev)
dev_dnssd_diff_uuid = _make_dnssd_device(
f"dnssd://Kyocera%20ECOSYS%20M2040dn._ipp._tcp.local/?uuid={uuid2}",
make_and_model="Kyocera ECOSYS M2040dn",
device_id="MFG:Kyocera;MDL:ECOSYS M2040dn;")
phys_diff_uuid = PhysicalDevice(dev_dnssd_diff_uuid)
assert phys_dnssd == phys_diff_uuid
pds_uuid = []
for d in [dev_dnssd, dev_ipps, dev_dnssd_diff_uuid]:
pd = PhysicalDevice(d)
try:
i = pds_uuid.index(pd)
pds_uuid[i].add_device(d)
except ValueError:
pds_uuid.append(pd)
assert len(pds_uuid) == 1
assert len(pds_uuid[0].get_devices()) == 2
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_cross_namespace_network_printers_never_merge():
dev_ip = cupshelpers.Device(
"socket://192.168.1.102",
**{"device-class": "network",
"device-make-and-model": "Kyocera ECOSYS M2040dn",
"device-id": "MFG:Kyocera;MDL:ECOSYS M2040dn;"})
dev_symbolic = cupshelpers.Device(
"socket://printer.local",
**{"device-class": "network",
"device-make-and-model": "Kyocera ECOSYS M2040dn",
"device-id": "MFG:Kyocera;MDL:ECOSYS M2040dn;"})
phys_ip = PhysicalDevice(dev_ip)
phys_symbolic = PhysicalDevice(dev_symbolic)
assert phys_ip != phys_symbolic
pds = []
for d in [dev_ip, dev_symbolic]:
pd = PhysicalDevice(d)
try:
i = pds.index(pd)
pds[i].add_device(d)
except ValueError:
pds.append(pd)
assert len(pds) == 2
with pytest.raises(ValueError):
phys_ip.add_device(dev_symbolic)
with pytest.raises(ValueError):
phys_symbolic.add_device(dev_ip)
dev_dnssd = cupshelpers.Device(
"dnssd://Printer._ipp._tcp.local/",
**{"device-class": "network",
"device-make-and-model": "Printer",
"device-id": "MFG:Generic;MDL:Printer;"})
dev_ipps_ip = cupshelpers.Device(
"ipps://192.168.1.102/",
**{"device-class": "network",
"device-make-and-model": "Printer",
"device-id": "MFG:Generic;MDL:Printer;"})
phys_dnssd = PhysicalDevice(dev_dnssd)
phys_ipps_ip = PhysicalDevice(dev_ipps_ip)
assert phys_dnssd != phys_ipps_ip
pds_ipp = []
for d in [dev_dnssd, dev_ipps_ip]:
pd = PhysicalDevice(d)
try:
i = pds_ipp.index(pd)
pds_ipp[i].add_device(d)
except ValueError:
pds_ipp.append(pd)
assert len(pds_ipp) == 2
with pytest.raises(ValueError):
phys_dnssd.add_device(dev_ipps_ip)
with pytest.raises(ValueError):
phys_ipps_ip.add_device(dev_dnssd)
dev_kyo_dnssd = cupshelpers.Device(
"dnssd://Kyocera%20ECOSYS%20M2040dn._ipp._tcp.local/?uuid=4509a320-00a0-008f-00b6-002507510eca",
**{"device-class": "network",
"device-make-and-model": "Kyocera ECOSYS M2040dn",
"device-id": "MFG:Kyocera;MDL:ECOSYS M2040dn;"})
dev_kyo_ipps = cupshelpers.Device(
"ipps://Kyocera%20ECOSYS%20M2040dn._ipps._tcp.local/",
**{"device-class": "network",
"device-make-and-model": "Kyocera ECOSYS M2040dn",
"device-id": "MFG:Kyocera;MDL:ECOSYS M2040dn;"})
dev_kyo_ip = cupshelpers.Device(
"socket://192.168.1.102",
**{"device-class": "network",
"device-make-and-model": "Kyocera ECOSYS M2040dn",
"device-id": "MFG:Kyocera;MDL:ECOSYS M2040dn;"})
pds_kyo = []
for d in [dev_kyo_dnssd, dev_kyo_ipps, dev_kyo_ip]:
pd = PhysicalDevice(d)
try:
i = pds_kyo.index(pd)
pds_kyo[i].add_device(d)
except ValueError:
pds_kyo.append(pd)
assert len(pds_kyo) == 2
dnssd_pd = [p for p in pds_kyo if not p.is_literal_ip()][0]
legacy_pd = [p for p in pds_kyo if p.is_literal_ip()][0]
assert len(dnssd_pd.get_devices()) == 2
assert len(legacy_pd.get_devices()) == 1
@pytest.mark.skipif(cups is None, reason="cups module not available")
def test_kyocera_resolved_dnssd_and_ipps_merge():
dev_resolved = cupshelpers.Device(
"dnssd://Kyocera%20ECOSYS%20M2040dn._ipp._tcp.local/?uuid=4509a320-00a0-008f-00b6-002507510eca",
**{"device-class": "network",
"device-make-and-model": "Kyocera ECOSYS M2040dn",
"device-id": "MFG:Kyocera;MDL:ECOSYS M2040dn;"})
dev_resolved.hostname = "KM7B6A91"
dev_resolved.address = "192.168.1.102"
dev_ipps = cupshelpers.Device(
"ipps://Kyocera%20ECOSYS%20M2040dn._ipps._tcp.local/",
**{"device-class": "network",
"device-make-and-model": "Kyocera ECOSYS M2040dn",
"device-id": "MFG:Kyocera;MDL:ECOSYS M2040dn;"})
dev_socket = cupshelpers.Device(
"socket://Kyocera%20ECOSYS%20M2040dn._pdl-datastream._tcp.local/",
**{"device-class": "network",
"device-make-and-model": "Kyocera ECOSYS M2040dn",
"device-id": "MFG:Kyocera;MDL:ECOSYS M2040dn;"})
dev_ip = cupshelpers.Device(
"socket://192.168.1.102",
**{"device-class": "network",
"device-make-and-model": "Kyocera ECOSYS M2040dn",
"device-id": "MFG:Kyocera;MDL:ECOSYS M2040dn;"})
p_resolved = PhysicalDevice(dev_resolved)
p_ipps = PhysicalDevice(dev_ipps)
p_socket = PhysicalDevice(dev_socket)
p_ip = PhysicalDevice(dev_ip)
assert p_resolved == p_ipps
assert p_resolved == p_socket
assert p_ipps == p_socket
assert p_resolved != p_ip
assert p_ipps != p_ip
assert p_socket != p_ip
pds = []
for d in [dev_resolved, dev_ipps, dev_socket, dev_ip]:
pd = PhysicalDevice(d)
try:
i = pds.index(pd)
pds[i].add_device(d)
except ValueError:
pds.append(pd)
assert len(pds) == 2
dnssd_pd = [p for p in pds if not p.is_literal_ip()][0]
legacy_pd = [p for p in pds if p.is_literal_ip()][0]
assert len(dnssd_pd.get_devices()) == 3
assert len(legacy_pd.get_devices()) == 1
pds_rev = []
for d in [dev_ipps, dev_resolved, dev_socket, dev_ip]:
pd = PhysicalDevice(d)
try:
i = pds_rev.index(pd)
pds_rev[i].add_device(d)
except ValueError:
pds_rev.append(pd)
assert len(pds_rev) == 2
dnssd_pd_rev = [p for p in pds_rev if not p.is_literal_ip()][0]
assert len(dnssd_pd_rev.get_devices()) == 3
def test_canonical_uri_and_protocol_detection():
assert PhysicalDevice._protocol_from_uri("dnssd://Printer._ipp._tcp.local/?uuid=123") == "ipp"
assert PhysicalDevice._protocol_from_uri("dnssd://Printer._ipps._tcp.local/?uuid=123") == "ipps"
assert PhysicalDevice._protocol_from_uri("dnssd://Printer._printer._tcp.local/?uuid=123") == "lpd"
assert PhysicalDevice._protocol_from_uri("dnssd://Printer._pdl-datastream._tcp.local/?uuid=123") == "socket"
assert PhysicalDevice._canonical_uri("dnssd://Printer._ipp._tcp.local/?uuid=123") == "ipp://Printer._ipp._tcp.local/"
assert PhysicalDevice._canonical_uri("dnssd://Printer._ipps._tcp.local/?uuid=123") == "ipps://Printer._ipps._tcp.local/"
assert PhysicalDevice._canonical_uri("dnssd://Printer._printer._tcp.local/?uuid=123") == "lpd://Printer._printer._tcp.local/"
assert PhysicalDevice._canonical_uri("dnssd://Printer._pdl-datastream._tcp.local/?uuid=123") == "socket://Printer._pdl-datastream._tcp.local/"