FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-zeroconf/test_zeroconf.py at master · jacobo3d/python-zeroconf · GitHub
jacobo3d
python-zeroconf
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-zeroconf
/
test_zeroconf.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
865 lines (695 loc) · 30.2 KB
Breadcrumbs
python-zeroconf
/
test_zeroconf.py
Copy path
File metadata and controls
865 lines (695 loc) · 30.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" Unit tests for zeroconf.py """
import
copy
import
logging
import
socket
import
struct
import
time
import
unittest
from
threading
import
Event
import
zeroconf
as
r
from
zeroconf
import
(
DNSHinfo
,
DNSText
,
ServiceBrowser
,
ServiceInfo
,
ServiceStateChange
,
Zeroconf
,
ZeroconfServiceTypes
,
)
log
=
logging
.
getLogger
(
'zeroconf'
)
original_logging_level
=
[
None
]
def
setup_module
():
original_logging_level
[
0
]
=
log
.
level
log
.
setLevel
(
logging
.
DEBUG
)
def
teardown_module
():
log
.
setLevel
(
original_logging_level
[
0
])
class
TestDunder
(
unittest
.
TestCase
):
def
test_dns_text_repr
(
self
):
# There was an issue on Python 3 that prevented DNSText's repr
# from working when the text was longer than 10 bytes
text
=
DNSText
(
'irrelevant'
,
None
,
0
,
0
,
b'12345678901'
)
repr
(
text
)
text
=
DNSText
(
'irrelevant'
,
None
,
0
,
0
,
b'123'
)
repr
(
text
)
def
test_dns_hinfo_repr_eq
(
self
):
hinfo
=
DNSHinfo
(
'irrelevant'
,
r
.
_TYPE_HINFO
,
0
,
0
,
'cpu'
,
'os'
)
assert
hinfo
==
hinfo
repr
(
hinfo
)
def
test_dns_pointer_repr
(
self
):
pointer
=
r
.
DNSPointer
(
'irrelevant'
,
r
.
_TYPE_PTR
,
r
.
_CLASS_IN
,
r
.
_DNS_TTL
,
'123'
)
repr
(
pointer
)
def
test_dns_address_repr
(
self
):
address
=
r
.
DNSAddress
(
'irrelevant'
,
r
.
_TYPE_SOA
,
r
.
_CLASS_IN
,
1
,
b'a'
)
repr
(
address
)
def
test_dns_question_repr
(
self
):
question
=
r
.
DNSQuestion
(
'irrelevant'
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
|
r
.
_CLASS_UNIQUE
)
repr
(
question
)
assert
not
question
!=
question
def
test_dns_service_repr
(
self
):
service
=
r
.
DNSService
(
'irrelevant'
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
,
r
.
_DNS_TTL
,
0
,
0
,
80
,
b'a'
)
repr
(
service
)
def
test_dns_record_abc
(
self
):
record
=
r
.
DNSRecord
(
'irrelevant'
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
,
r
.
_DNS_TTL
)
self
.
assertRaises
(
r
.
AbstractMethodException
,
record
.
__eq__
,
record
)
self
.
assertRaises
(
r
.
AbstractMethodException
,
record
.
write
,
None
)
def
test_service_info_dunder
(
self
):
type_
=
"_test-srvc-type._tcp.local."
name
=
"xxxyyy"
registration_name
=
"%s.%s"
%
(
name
,
type_
)
info
=
ServiceInfo
(
type_
,
registration_name
,
socket
.
inet_aton
(
"10.0.1.2"
),
80
,
0
,
0
,
None
,
"ash-2.local."
)
assert
not
info
!=
info
repr
(
info
)
def
test_dns_outgoing_repr
(
self
):
dns_outgoing
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_QUERY
)
repr
(
dns_outgoing
)
class
PacketGeneration
(
unittest
.
TestCase
):
def
test_parse_own_packet_simple
(
self
):
generated
=
r
.
DNSOutgoing
(
0
)
r
.
DNSIncoming
(
generated
.
packet
())
def
test_parse_own_packet_simple_unicast
(
self
):
generated
=
r
.
DNSOutgoing
(
0
,
0
)
r
.
DNSIncoming
(
generated
.
packet
())
def
test_parse_own_packet_flags
(
self
):
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_QUERY
)
r
.
DNSIncoming
(
generated
.
packet
())
def
test_parse_own_packet_question
(
self
):
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_QUERY
)
generated
.
add_question
(
r
.
DNSQuestion
(
"testname.local."
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
))
r
.
DNSIncoming
(
generated
.
packet
())
def
test_parse_own_packet_response
(
self
):
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_RESPONSE
)
generated
.
add_answer_at_time
(
r
.
DNSService
(
"æøå.local."
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
,
r
.
_DNS_TTL
,
0
,
0
,
80
,
"foo.local."
),
0
)
parsed
=
r
.
DNSIncoming
(
generated
.
packet
())
self
.
assertEqual
(
len
(
generated
.
answers
),
1
)
self
.
assertEqual
(
len
(
generated
.
answers
),
len
(
parsed
.
answers
))
def
test_match_question
(
self
):
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_QUERY
)
question
=
r
.
DNSQuestion
(
"testname.local."
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
)
generated
.
add_question
(
question
)
parsed
=
r
.
DNSIncoming
(
generated
.
packet
())
self
.
assertEqual
(
len
(
generated
.
questions
),
1
)
self
.
assertEqual
(
len
(
generated
.
questions
),
len
(
parsed
.
questions
))
self
.
assertEqual
(
question
,
parsed
.
questions
[
0
])
def
test_suppress_answer
(
self
):
query_generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_QUERY
)
question
=
r
.
DNSQuestion
(
"testname.local."
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
)
query_generated
.
add_question
(
question
)
answer1
=
r
.
DNSService
(
"testname1.local."
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
,
r
.
_DNS_TTL
,
0
,
0
,
80
,
"foo.local."
)
staleanswer2
=
r
.
DNSService
(
"testname2.local."
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
,
r
.
_DNS_TTL
/
2
,
0
,
0
,
80
,
"foo.local."
)
answer2
=
r
.
DNSService
(
"testname2.local."
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
,
r
.
_DNS_TTL
,
0
,
0
,
80
,
"foo.local."
)
query_generated
.
add_answer_at_time
(
answer1
,
0
)
query_generated
.
add_answer_at_time
(
staleanswer2
,
0
)
query
=
r
.
DNSIncoming
(
query_generated
.
packet
())
# Should be suppressed
response
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_RESPONSE
)
response
.
add_answer
(
query
,
answer1
)
assert
len
(
response
.
answers
)
==
0
# Should not be suppressed, TTL in query is too short
response
.
add_answer
(
query
,
answer2
)
assert
len
(
response
.
answers
)
==
1
# Should not be suppressed, name is different
tmp
=
copy
.
copy
(
answer1
)
tmp
.
name
=
"testname3.local."
response
.
add_answer
(
query
,
tmp
)
assert
len
(
response
.
answers
)
==
2
# Should not be suppressed, type is different
tmp
=
copy
.
copy
(
answer1
)
tmp
.
type
=
r
.
_TYPE_A
response
.
add_answer
(
query
,
tmp
)
assert
len
(
response
.
answers
)
==
3
# Should not be suppressed, class is different
tmp
=
copy
.
copy
(
answer1
)
tmp
.
class_
=
r
.
_CLASS_NONE
response
.
add_answer
(
query
,
tmp
)
assert
len
(
response
.
answers
)
==
4
# ::TODO:: could add additional tests for DNSAddress, DNSHinfo, DNSPointer, DNSText, DNSService
def
test_dns_hinfo
(
self
):
generated
=
r
.
DNSOutgoing
(
0
)
generated
.
add_additional_answer
(
DNSHinfo
(
'irrelevant'
,
r
.
_TYPE_HINFO
,
0
,
0
,
'cpu'
,
'os'
))
parsed
=
r
.
DNSIncoming
(
generated
.
packet
())
self
.
assertEqual
(
parsed
.
answers
[
0
].
cpu
,
u'cpu'
)
self
.
assertEqual
(
parsed
.
answers
[
0
].
os
,
u'os'
)
generated
=
r
.
DNSOutgoing
(
0
)
generated
.
add_additional_answer
(
DNSHinfo
(
'irrelevant'
,
r
.
_TYPE_HINFO
,
0
,
0
,
'cpu'
,
'x'
*
257
))
self
.
assertRaises
(
r
.
NamePartTooLongException
,
generated
.
packet
)
class
PacketForm
(
unittest
.
TestCase
):
def
test_transaction_id
(
self
):
"""ID must be zero in a DNS-SD packet"""
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_QUERY
)
bytes
=
generated
.
packet
()
id
=
bytes
[
0
]
<<
8
|
bytes
[
1
]
self
.
assertEqual
(
id
,
0
)
def
test_query_header_bits
(
self
):
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_QUERY
)
bytes
=
generated
.
packet
()
flags
=
bytes
[
2
]
<<
8
|
bytes
[
3
]
self
.
assertEqual
(
flags
,
0x0
)
def
test_response_header_bits
(
self
):
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_RESPONSE
)
bytes
=
generated
.
packet
()
flags
=
bytes
[
2
]
<<
8
|
bytes
[
3
]
self
.
assertEqual
(
flags
,
0x8000
)
def
test_numbers
(
self
):
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_RESPONSE
)
bytes
=
generated
.
packet
()
(
num_questions
,
num_answers
,
num_authorities
,
num_additionals
)
=
struct
.
unpack
(
'!4H'
,
bytes
[
4
:
12
])
self
.
assertEqual
(
num_questions
,
0
)
self
.
assertEqual
(
num_answers
,
0
)
self
.
assertEqual
(
num_authorities
,
0
)
self
.
assertEqual
(
num_additionals
,
0
)
def
test_numbers_questions
(
self
):
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_RESPONSE
)
question
=
r
.
DNSQuestion
(
"testname.local."
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
)
for
i
in
range
(
10
):
generated
.
add_question
(
question
)
bytes
=
generated
.
packet
()
(
num_questions
,
num_answers
,
num_authorities
,
num_additionals
)
=
struct
.
unpack
(
'!4H'
,
bytes
[
4
:
12
])
self
.
assertEqual
(
num_questions
,
10
)
self
.
assertEqual
(
num_answers
,
0
)
self
.
assertEqual
(
num_authorities
,
0
)
self
.
assertEqual
(
num_additionals
,
0
)
class
Names
(
unittest
.
TestCase
):
def
test_long_name
(
self
):
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_RESPONSE
)
question
=
r
.
DNSQuestion
(
"this.is.a.very.long.name.with.lots.of.parts.in.it.local."
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
)
generated
.
add_question
(
question
)
r
.
DNSIncoming
(
generated
.
packet
())
def
test_exceedingly_long_name
(
self
):
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_RESPONSE
)
name
=
"%slocal."
%
(
"part."
*
1000
)
question
=
r
.
DNSQuestion
(
name
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
)
generated
.
add_question
(
question
)
r
.
DNSIncoming
(
generated
.
packet
())
def
test_exceedingly_long_name_part
(
self
):
name
=
"%s.local."
%
(
"a"
*
1000
)
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_RESPONSE
)
question
=
r
.
DNSQuestion
(
name
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
)
generated
.
add_question
(
question
)
self
.
assertRaises
(
r
.
NamePartTooLongException
,
generated
.
packet
)
def
test_same_name
(
self
):
name
=
"paired.local."
generated
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_RESPONSE
)
question
=
r
.
DNSQuestion
(
name
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
)
generated
.
add_question
(
question
)
generated
.
add_question
(
question
)
r
.
DNSIncoming
(
generated
.
packet
())
def
test_lots_of_names
(
self
):
# instantiate a zeroconf instance
zc
=
Zeroconf
(
interfaces
=
[
'127.0.0.1'
])
# create a bunch of servers
type_
=
"_my-service._tcp.local."
name
=
'a wonderful service'
server_count
=
300
self
.
generate_many_hosts
(
zc
,
type_
,
name
,
server_count
)
# verify that name changing works
self
.
verify_name_change
(
zc
,
type_
,
name
,
server_count
)
# we are going to monkey patch the zeroconf send to check packet sizes
old_send
=
zc
.
send
# needs to be a list so that we can modify it in our phony send
longest_packet
=
[
0
,
None
]
def
send
(
out
,
addr
=
r
.
_MDNS_ADDR
,
port
=
r
.
_MDNS_PORT
):
"""Sends an outgoing packet."""
packet
=
out
.
packet
()
if
longest_packet
[
0
]
<
len
(
packet
):
longest_packet
[
0
]
=
len
(
packet
)
longest_packet
[
1
]
=
out
old_send
(
out
,
addr
=
addr
,
port
=
port
)
# monkey patch the zeroconf send
zc
.
send
=
send
# dummy service callback
def
on_service_state_change
(
zeroconf
,
service_type
,
state_change
,
name
):
pass
# start a browser
browser
=
ServiceBrowser
(
zc
,
type_
, [
on_service_state_change
])
# wait until the browse request packet has maxed out in size
sleep_count
=
0
while
sleep_count
<
100
and
\
longest_packet
[
0
]
<
r
.
_MAX_MSG_ABSOLUTE
-
100
:
sleep_count
+=
1
time
.
sleep
(
0.1
)
browser
.
cancel
()
time
.
sleep
(
0.5
)
import
zeroconf
zeroconf
.
log
.
debug
(
'sleep_count %d, sized %d'
,
sleep_count
,
longest_packet
[
0
])
# now the browser has sent at least one request, verify the size
assert
longest_packet
[
0
]
<=
r
.
_MAX_MSG_ABSOLUTE
assert
longest_packet
[
0
]
>=
r
.
_MAX_MSG_ABSOLUTE
-
100
# mock zeroconf's logger warning() and debug()
from
unittest
.
mock
import
patch
patch_warn
=
patch
(
'zeroconf.log.warning'
)
patch_debug
=
patch
(
'zeroconf.log.debug'
)
mocked_log_warn
=
patch_warn
.
start
()
mocked_log_debug
=
patch_debug
.
start
()
# now that we have a long packet in our possession, let's verify the
# exception handling.
out
=
longest_packet
[
1
]
out
.
data
.
append
(
b'
\0
'
*
1000
)
# mock the zeroconf logger and check for the correct logging backoff
call_counts
=
mocked_log_warn
.
call_count
,
mocked_log_debug
.
call_count
# try to send an oversized packet
zc
.
send
(
out
)
assert
mocked_log_warn
.
call_count
==
call_counts
[
0
]
+
1
assert
mocked_log_debug
.
call_count
==
call_counts
[
0
]
zc
.
send
(
out
)
assert
mocked_log_warn
.
call_count
==
call_counts
[
0
]
+
1
assert
mocked_log_debug
.
call_count
==
call_counts
[
0
]
+
1
# force a receive of an oversized packet
packet
=
out
.
packet
()
s
=
zc
.
_respond_sockets
[
0
]
# mock the zeroconf logger and check for the correct logging backoff
call_counts
=
mocked_log_warn
.
call_count
,
mocked_log_debug
.
call_count
# force receive on oversized packet
s
.
sendto
(
packet
,
0
, (
r
.
_MDNS_ADDR
,
r
.
_MDNS_PORT
))
s
.
sendto
(
packet
,
0
, (
r
.
_MDNS_ADDR
,
r
.
_MDNS_PORT
))
time
.
sleep
(
2.0
)
zeroconf
.
log
.
debug
(
'warn %d debug %d was %s'
,
mocked_log_warn
.
call_count
,
mocked_log_debug
.
call_count
,
call_counts
)
assert
mocked_log_debug
.
call_count
>
call_counts
[
0
]
# close our zeroconf which will close the sockets
zc
.
close
()
# pop the big chunk off the end of the data and send on a closed socket
out
.
data
.
pop
()
zc
.
_GLOBAL_DONE
=
False
# mock the zeroconf logger and check for the correct logging backoff
call_counts
=
mocked_log_warn
.
call_count
,
mocked_log_debug
.
call_count
# send on a closed socket (force a socket error)
zc
.
send
(
out
)
zeroconf
.
log
.
debug
(
'warn %d debug %d was %s'
,
mocked_log_warn
.
call_count
,
mocked_log_debug
.
call_count
,
call_counts
)
assert
mocked_log_warn
.
call_count
>
call_counts
[
0
]
assert
mocked_log_debug
.
call_count
>
call_counts
[
0
]
zc
.
send
(
out
)
zeroconf
.
log
.
debug
(
'warn %d debug %d was %s'
,
mocked_log_warn
.
call_count
,
mocked_log_debug
.
call_count
,
call_counts
)
assert
mocked_log_debug
.
call_count
>
call_counts
[
0
]
+
2
mocked_log_warn
.
stop
()
mocked_log_debug
.
stop
()
def
verify_name_change
(
self
,
zc
,
type_
,
name
,
number_hosts
):
desc
=
{
'path'
:
'/~paulsm/'
}
info_service
=
ServiceInfo
(
type_
,
'%s.%s'
%
(
name
,
type_
),
socket
.
inet_aton
(
"10.0.1.2"
),
80
,
0
,
0
,
desc
,
"ash-2.local."
)
# verify name conflict
self
.
assertRaises
(
r
.
NonUniqueNameException
,
zc
.
register_service
,
info_service
)
zc
.
register_service
(
info_service
,
allow_name_change
=
True
)
assert
info_service
.
name
.
split
(
'.'
)[
0
]
==
'%s-%d'
%
(
name
,
number_hosts
+
1
)
def
generate_many_hosts
(
self
,
zc
,
type_
,
name
,
number_hosts
):
records_per_server
=
2
block_size
=
25
number_hosts
=
int
(((
number_hosts
-
1
)
/
block_size
+
1
))
*
block_size
for
i
in
range
(
1
,
number_hosts
+
1
):
next_name
=
name
if
i
==
1
else
'%s-%d'
%
(
name
,
i
)
self
.
generate_host
(
zc
,
next_name
,
type_
)
if
i
%
block_size
==
0
:
sleep_count
=
0
while
sleep_count
<
40
and
\
i
*
records_per_server
>
len
(
zc
.
cache
.
entries_with_name
(
type_
)):
sleep_count
+=
1
time
.
sleep
(
0.05
)
@
staticmethod
def
generate_host
(
zc
,
host_name
,
type_
):
name
=
'.'
.
join
((
host_name
,
type_
))
out
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_RESPONSE
|
r
.
_FLAGS_AA
)
out
.
add_answer_at_time
(
r
.
DNSPointer
(
type_
,
r
.
_TYPE_PTR
,
r
.
_CLASS_IN
,
r
.
_DNS_TTL
,
name
),
0
)
out
.
add_answer_at_time
(
r
.
DNSService
(
type_
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
,
r
.
_DNS_TTL
,
0
,
0
,
80
,
name
),
0
)
zc
.
send
(
out
)
class
Framework
(
unittest
.
TestCase
):
def
test_launch_and_close
(
self
):
rv
=
r
.
Zeroconf
(
interfaces
=
r
.
InterfaceChoice
.
All
)
rv
.
close
()
rv
=
r
.
Zeroconf
(
interfaces
=
r
.
InterfaceChoice
.
Default
)
rv
.
close
()
class
Exceptions
(
unittest
.
TestCase
):
browser
=
None
@
classmethod
def
setUpClass
(
cls
):
cls
.
browser
=
Zeroconf
(
interfaces
=
[
'127.0.0.1'
])
@
classmethod
def
tearDownClass
(
cls
):
cls
.
browser
.
close
()
cls
.
browser
=
None
def
test_bad_service_info_name
(
self
):
self
.
assertRaises
(
r
.
BadTypeInNameException
,
self
.
browser
.
get_service_info
,
"type"
,
"type_not"
)
def
test_bad_service_names
(
self
):
bad_names_to_try
=
(
''
,
'local'
,
'_tcp.local.'
,
'_udp.local.'
,
'._udp.local.'
,
'_@._tcp.local.'
,
'_A@._tcp.local.'
,
'_x--x._tcp.local.'
,
'_-x._udp.local.'
,
'_x-._tcp.local.'
,
'_22._udp.local.'
,
'_2-2._tcp.local.'
,
'_1234567890-abcde._udp.local.'
,
'
\x00
._x._udp.local.'
,
)
for
name
in
bad_names_to_try
:
self
.
assertRaises
(
r
.
BadTypeInNameException
,
self
.
browser
.
get_service_info
,
name
,
'x.'
+
name
)
def
test_good_instance_names
(
self
):
good_names_to_try
=
(
'.._x._tcp.local.'
,
'x.sub._http._tcp.local.'
,
'6d86f882b90facee9170ad3439d72a4d6ee9f511._zget._http._tcp.local.'
)
for
name
in
good_names_to_try
:
r
.
service_type_name
(
name
)
def
test_bad_types
(
self
):
bad_names_to_try
=
(
'._x._tcp.local.'
,
'a'
*
64
+
'._sub._http._tcp.local.'
,
'a'
*
62
+
u'â._sub._http._tcp.local.'
,
)
for
name
in
bad_names_to_try
:
self
.
assertRaises
(
r
.
BadTypeInNameException
,
r
.
service_type_name
,
name
)
def
test_bad_sub_types
(
self
):
bad_names_to_try
=
(
'_sub._http._tcp.local.'
,
'._sub._http._tcp.local.'
,
'
\x7f
._sub._http._tcp.local.'
,
'
\x1f
._sub._http._tcp.local.'
,
)
for
name
in
bad_names_to_try
:
self
.
assertRaises
(
r
.
BadTypeInNameException
,
r
.
service_type_name
,
name
)
def
test_good_service_names
(
self
):
good_names_to_try
=
(
'_x._tcp.local.'
,
'_x._udp.local.'
,
'_12345-67890-abc._udp.local.'
,
'x._sub._http._tcp.local.'
,
'a'
*
63
+
'._sub._http._tcp.local.'
,
'a'
*
61
+
u'â._sub._http._tcp.local.'
,
)
for
name
in
good_names_to_try
:
r
.
service_type_name
(
name
)
r
.
service_type_name
(
'_one_two._tcp.local.'
,
allow_underscores
=
True
)
class
TestDnsIncoming
(
unittest
.
TestCase
):
def
test_incoming_exception_handling
(
self
):
generated
=
r
.
DNSOutgoing
(
0
)
packet
=
generated
.
packet
()
packet
=
packet
[:
8
]
+
b'deadbeef'
+
packet
[
8
:]
parsed
=
r
.
DNSIncoming
(
packet
)
parsed
=
r
.
DNSIncoming
(
packet
)
assert
parsed
.
valid
is
False
def
test_incoming_unknown_type
(
self
):
generated
=
r
.
DNSOutgoing
(
0
)
answer
=
r
.
DNSAddress
(
'a'
,
r
.
_TYPE_SOA
,
r
.
_CLASS_IN
,
1
,
b'a'
)
generated
.
add_additional_answer
(
answer
)
packet
=
generated
.
packet
()
parsed
=
r
.
DNSIncoming
(
packet
)
assert
len
(
parsed
.
answers
)
==
0
assert
parsed
.
is_query
()
!=
parsed
.
is_response
()
def
test_incoming_ipv6
(
self
):
# ::TODO:: could use a test here if we add IPV6 record handling
# ie: _TYPE_AAAA
pass
class
TestRegistrar
(
unittest
.
TestCase
):
def
test_ttl
(
self
):
# instantiate a zeroconf instance
zc
=
Zeroconf
(
interfaces
=
[
'127.0.0.1'
])
# service definition
type_
=
"_test-srvc-type._tcp.local."
name
=
"xxxyyy"
registration_name
=
"%s.%s"
%
(
name
,
type_
)
desc
=
{
'path'
:
'/~paulsm/'
}
info
=
ServiceInfo
(
type_
,
registration_name
,
socket
.
inet_aton
(
"10.0.1.2"
),
80
,
0
,
0
,
desc
,
"ash-2.local."
)
# we are going to monkey patch the zeroconf send to check packet sizes
old_send
=
zc
.
send
# needs to be a list so that we can modify it in our phony send
nbr_answers
=
[
0
,
None
]
nbr_additionals
=
[
0
,
None
]
nbr_authorities
=
[
0
,
None
]
def
send
(
out
,
addr
=
r
.
_MDNS_ADDR
,
port
=
r
.
_MDNS_PORT
):
"""Sends an outgoing packet."""
for
answer
,
time_
in
out
.
answers
:
nbr_answers
[
0
]
+=
1
assert
answer
.
ttl
==
expected_ttl
for
answer
in
out
.
additionals
:
nbr_additionals
[
0
]
+=
1
assert
answer
.
ttl
==
expected_ttl
for
answer
in
out
.
authorities
:
nbr_authorities
[
0
]
+=
1
assert
answer
.
ttl
==
expected_ttl
old_send
(
out
,
addr
=
addr
,
port
=
port
)
# monkey patch the zeroconf send
zc
.
send
=
send
# register service with default TTL
expected_ttl
=
r
.
_DNS_TTL
zc
.
register_service
(
info
)
assert
nbr_answers
[
0
]
==
12
and
nbr_additionals
[
0
]
==
0
and
nbr_authorities
[
0
]
==
3
nbr_answers
[
0
]
=
nbr_additionals
[
0
]
=
nbr_authorities
[
0
]
=
0
# query
query
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_QUERY
|
r
.
_FLAGS_AA
)
query
.
add_question
(
r
.
DNSQuestion
(
info
.
type
,
r
.
_TYPE_PTR
,
r
.
_CLASS_IN
))
query
.
add_question
(
r
.
DNSQuestion
(
info
.
name
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
))
query
.
add_question
(
r
.
DNSQuestion
(
info
.
name
,
r
.
_TYPE_TXT
,
r
.
_CLASS_IN
))
query
.
add_question
(
r
.
DNSQuestion
(
info
.
server
,
r
.
_TYPE_A
,
r
.
_CLASS_IN
))
zc
.
handle_query
(
query
,
r
.
_MDNS_ADDR
,
r
.
_MDNS_PORT
)
assert
nbr_answers
[
0
]
==
4
and
nbr_additionals
[
0
]
==
1
and
nbr_authorities
[
0
]
==
0
nbr_answers
[
0
]
=
nbr_additionals
[
0
]
=
nbr_authorities
[
0
]
=
0
# unregister
expected_ttl
=
0
zc
.
unregister_service
(
info
)
assert
nbr_answers
[
0
]
==
12
and
nbr_additionals
[
0
]
==
0
and
nbr_authorities
[
0
]
==
0
nbr_answers
[
0
]
=
nbr_additionals
[
0
]
=
nbr_authorities
[
0
]
=
0
# register service with custom TTL
expected_ttl
=
r
.
_DNS_TTL
*
2
assert
expected_ttl
!=
r
.
_DNS_TTL
zc
.
register_service
(
info
,
ttl
=
expected_ttl
)
assert
nbr_answers
[
0
]
==
12
and
nbr_additionals
[
0
]
==
0
and
nbr_authorities
[
0
]
==
3
nbr_answers
[
0
]
=
nbr_additionals
[
0
]
=
nbr_authorities
[
0
]
=
0
# query
query
=
r
.
DNSOutgoing
(
r
.
_FLAGS_QR_QUERY
|
r
.
_FLAGS_AA
)
query
.
add_question
(
r
.
DNSQuestion
(
info
.
type
,
r
.
_TYPE_PTR
,
r
.
_CLASS_IN
))
query
.
add_question
(
r
.
DNSQuestion
(
info
.
name
,
r
.
_TYPE_SRV
,
r
.
_CLASS_IN
))
query
.
add_question
(
r
.
DNSQuestion
(
info
.
name
,
r
.
_TYPE_TXT
,
r
.
_CLASS_IN
))
query
.
add_question
(
r
.
DNSQuestion
(
info
.
server
,
r
.
_TYPE_A
,
r
.
_CLASS_IN
))
zc
.
handle_query
(
query
,
r
.
_MDNS_ADDR
,
r
.
_MDNS_PORT
)
assert
nbr_answers
[
0
]
==
4
and
nbr_additionals
[
0
]
==
1
and
nbr_authorities
[
0
]
==
0
nbr_answers
[
0
]
=
nbr_additionals
[
0
]
=
nbr_authorities
[
0
]
=
0
# unregister
expected_ttl
=
0
zc
.
unregister_service
(
info
)
assert
nbr_answers
[
0
]
==
12
and
nbr_additionals
[
0
]
==
0
and
nbr_authorities
[
0
]
==
0
nbr_answers
[
0
]
=
nbr_additionals
[
0
]
=
nbr_authorities
[
0
]
=
0
class
TestDNSCache
(
unittest
.
TestCase
):
def
test_order
(
self
):
record1
=
r
.
DNSAddress
(
'a'
,
r
.
_TYPE_SOA
,
r
.
_CLASS_IN
,
1
,
b'a'
)
record2
=
r
.
DNSAddress
(
'a'
,
r
.
_TYPE_SOA
,
r
.
_CLASS_IN
,
1
,
b'b'
)
cache
=
r
.
DNSCache
()
cache
.
add
(
record1
)
cache
.
add
(
record2
)
entry
=
r
.
DNSEntry
(
'a'
,
r
.
_TYPE_SOA
,
r
.
_CLASS_IN
)
cached_record
=
cache
.
get
(
entry
)
self
.
assertEqual
(
cached_record
,
record2
)
class
ServiceTypesQuery
(
unittest
.
TestCase
):
def
test_integration_with_listener
(
self
):
type_
=
"_test-srvc-type._tcp.local."
name
=
"xxxyyy"
registration_name
=
"%s.%s"
%
(
name
,
type_
)
zeroconf_registrar
=
Zeroconf
(
interfaces
=
[
'127.0.0.1'
])
desc
=
{
'path'
:
'/~paulsm/'
}
info
=
ServiceInfo
(
type_
,
registration_name
,
socket
.
inet_aton
(
"10.0.1.2"
),
80
,
0
,
0
,
desc
,
"ash-2.local."
)
zeroconf_registrar
.
register_service
(
info
)
try
:
service_types
=
ZeroconfServiceTypes
.
find
(
interfaces
=
[
'127.0.0.1'
],
timeout
=
0.5
)
assert
type_
in
service_types
service_types
=
ZeroconfServiceTypes
.
find
(
zc
=
zeroconf_registrar
,
timeout
=
0.5
)
assert
type_
in
service_types
finally
:
zeroconf_registrar
.
close
()
def
test_integration_with_subtype_and_listener
(
self
):
subtype_
=
"_subtype._sub"
type_
=
"_type._tcp.local."
name
=
"xxxyyy"
# Note: discovery returns only DNS-SD type not subtype
discovery_type
=
"%s.%s"
%
(
subtype_
,
type_
)
registration_name
=
"%s.%s"
%
(
name
,
type_
)
zeroconf_registrar
=
Zeroconf
(
interfaces
=
[
'127.0.0.1'
])
desc
=
{
'path'
:
'/~paulsm/'
}
info
=
ServiceInfo
(
discovery_type
,
registration_name
,
socket
.
inet_aton
(
"10.0.1.2"
),
80
,
0
,
0
,
desc
,
"ash-2.local."
)
zeroconf_registrar
.
register_service
(
info
)
try
:
service_types
=
ZeroconfServiceTypes
.
find
(
interfaces
=
[
'127.0.0.1'
],
timeout
=
0.5
)
assert
discovery_type
in
service_types
service_types
=
ZeroconfServiceTypes
.
find
(
zc
=
zeroconf_registrar
,
timeout
=
0.5
)
assert
discovery_type
in
service_types
finally
:
zeroconf_registrar
.
close
()
class
ListenerTest
(
unittest
.
TestCase
):
def
test_integration_with_listener_class
(
self
):
service_added
=
Event
()
service_removed
=
Event
()
subtype_name
=
"My special Subtype"
type_
=
"_http._tcp.local."
subtype
=
subtype_name
+
"._sub."
+
type_
name
=
"xxxyyyæøå"
registration_name
=
"%s.%s"
%
(
name
,
type_
)
class
MyListener
:
def
add_service
(
self
,
zeroconf
,
type
,
name
):
zeroconf
.
get_service_info
(
type
,
name
)
service_added
.
set
()
def
remove_service
(
self
,
zeroconf
,
type
,
name
):
service_removed
.
set
()
listener
=
MyListener
()
zeroconf_browser
=
Zeroconf
(
interfaces
=
[
'127.0.0.1'
])
zeroconf_browser
.
add_service_listener
(
subtype
,
listener
)
properties
=
dict
(
prop_none
=
None
,
prop_string
=
b'a_prop'
,
prop_float
=
1.0
,
prop_blank
=
b'a blanked string'
,
prop_true
=
1
,
prop_false
=
0
,
)
zeroconf_registrar
=
Zeroconf
(
interfaces
=
[
'127.0.0.1'
])
desc
=
{
'path'
:
'/~paulsm/'
}
desc
.
update
(
properties
)
info_service
=
ServiceInfo
(
subtype
,
registration_name
,
socket
.
inet_aton
(
"10.0.1.2"
),
80
,
0
,
0
,
desc
,
"ash-2.local."
)
zeroconf_registrar
.
register_service
(
info_service
)
try
:
service_added
.
wait
(
1
)
assert
service_added
.
is_set
()
# short pause to allow multicast timers to expire
time
.
sleep
(
2
)
# clear the answer cache to force query
for
record
in
zeroconf_browser
.
cache
.
entries
():
zeroconf_browser
.
cache
.
remove
(
record
)
# get service info without answer cache
info
=
zeroconf_browser
.
get_service_info
(
type_
,
registration_name
)
assert
info
.
properties
[
b'prop_none'
]
is
False
assert
info
.
properties
[
b'prop_string'
]
==
properties
[
'prop_string'
]
assert
info
.
properties
[
b'prop_float'
]
is
False
assert
info
.
properties
[
b'prop_blank'
]
==
properties
[
'prop_blank'
]
assert
info
.
properties
[
b'prop_true'
]
is
True
assert
info
.
properties
[
b'prop_false'
]
is
False
info
=
zeroconf_browser
.
get_service_info
(
subtype
,
registration_name
)
assert
info
.
properties
[
b'prop_none'
]
is
False
zeroconf_registrar
.
unregister_service
(
info_service
)
service_removed
.
wait
(
1
)
assert
service_removed
.
is_set
()
finally
:
zeroconf_registrar
.
close
()
zeroconf_browser
.
remove_service_listener
(
listener
)
zeroconf_browser
.
close
()
def
test_integration
():
service_added
=
Event
()
service_removed
=
Event
()
unexpected_ttl
=
Event
()
got_query
=
Event
()
type_
=
"_http._tcp.local."
registration_name
=
"xxxyyy.%s"
%
type_
def
on_service_state_change
(
zeroconf
,
service_type
,
state_change
,
name
):
if
name
==
registration_name
:
if
state_change
is
ServiceStateChange
.
Added
:
service_added
.
set
()
elif
state_change
is
ServiceStateChange
.
Removed
:
service_removed
.
set
()
zeroconf_browser
=
Zeroconf
(
interfaces
=
[
'127.0.0.1'
])
# we are going to monkey patch the zeroconf send to check packet sizes
old_send
=
zeroconf_browser
.
send
time_offset
=
0
def
current_time_millis
():
"""Current system time in milliseconds"""
return
time
.
time
()
*
1000
+
time_offset
*
1000
expected_ttl
=
r
.
_DNS_TTL
# needs to be a list so that we can modify it in our phony send
nbr_queries
=
[
0
,
None
]
def
send
(
out
,
addr
=
r
.
_MDNS_ADDR
,
port
=
r
.
_MDNS_PORT
):
"""Sends an outgoing packet."""
pout
=
r
.
DNSIncoming
(
out
.
packet
())
for
answer
in
pout
.
answers
:
nbr_queries
[
0
]
+=
1
if
not
answer
.
ttl
>
expected_ttl
/
2
:
unexpected_ttl
.
set
()
got_query
.
set
()
old_send
(
out
,
addr
=
addr
,
port
=
port
)
# monkey patch the zeroconf send
zeroconf_browser
.
send
=
send
# monkey patch the zeroconf current_time_millis
r
.
current_time_millis
=
current_time_millis
service_added
=
Event
()
service_removed
=
Event
()
browser
=
ServiceBrowser
(
zeroconf_browser
,
type_
, [
on_service_state_change
])
zeroconf_registrar
=
Zeroconf
(
interfaces
=
[
'127.0.0.1'
])
desc
=
{
'path'
:
'/~paulsm/'
}
info
=
ServiceInfo
(
type_
,
registration_name
,
socket
.
inet_aton
(
"10.0.1.2"
),
80
,
0
,
0
,
desc
,
"ash-2.local."
)
zeroconf_registrar
.
register_service
(
info
)
try
:
service_added
.
wait
(
1
)
assert
service_added
.
is_set
()
sleep_count
=
0
while
nbr_queries
[
0
]
<
50
:
time_offset
+=
expected_ttl
/
4
zeroconf_browser
.
notify_all
()
sleep_count
+=
1
got_query
.
wait
(
1
)
got_query
.
clear
()
assert
not
unexpected_ttl
.
is_set
()
# Don't remove service, allow close() to cleanup
finally
:
zeroconf_registrar
.
close
()
browser
.
cancel
()
zeroconf_browser
.
close
()
Back
|
FazBrowse Home
|
New Git URL