@@ -538,6 +538,94 @@ def test_incoming_ipv6(self):
538
538
pass
539
539
540
540
541
+ class TestRegistrar (unittest .TestCase ):
542
+
543
+ def test_ttl (self ):
544
+
545
+ # instantiate a zeroconf instance
546
+ zc = Zeroconf (interfaces = ['127.0.0.1' ])
547
+
548
+ # service definition
549
+ type_ = "_test-srvc-type._tcp.local."
550
+ name = "xxxyyy"
551
+ registration_name = "%s.%s" % (name , type_ )
552
+
553
+ desc = {'path' : '/~paulsm/' }
554
+ info = ServiceInfo (
555
+ type_ , registration_name ,
556
+ socket .inet_aton ("10.0.1.2" ), 80 , 0 , 0 ,
557
+ desc , "ash-2.local." )
558
+
559
+ # we are going to monkey patch the zeroconf send to check packet sizes
560
+ old_send = zc .send
561
+
562
+ # needs to be a list so that we can modify it in our phony send
563
+ nbr_answers = [0 , None ]
564
+ nbr_additionals = [0 , None ]
565
+ nbr_authorities = [0 , None ]
566
+
567
+ def send (out , addr = r ._MDNS_ADDR , port = r ._MDNS_PORT ):
568
+ """Sends an outgoing packet."""
569
+ for answer , time_ in out .answers :
570
+ nbr_answers [0 ] += 1
571
+ assert answer .ttl == expected_ttl
572
+ for answer in out .additionals :
573
+ nbr_additionals [0 ] += 1
574
+ assert answer .ttl == expected_ttl
575
+ for answer in out .authorities :
576
+ nbr_authorities [0 ] += 1
577
+ assert answer .ttl == expected_ttl
578
+ old_send (out , addr = addr , port = port )
579
+
580
+ # monkey patch the zeroconf send
581
+ zc .send = send
582
+
583
+ # register service with default TTL
584
+ expected_ttl = r ._DNS_TTL
585
+ zc .register_service (info )
586
+ assert nbr_answers [0 ] == 12 and nbr_additionals [0 ] == 0 and nbr_authorities [0 ] == 3
587
+ nbr_answers [0 ] = nbr_additionals [0 ] = nbr_authorities [0 ] = 0
588
+
589
+ # query
590
+ query = r .DNSOutgoing (r ._FLAGS_QR_QUERY | r ._FLAGS_AA )
591
+ query .add_question (r .DNSQuestion (info .type , r ._TYPE_PTR , r ._CLASS_IN ))
592
+ query .add_question (r .DNSQuestion (info .name , r ._TYPE_SRV , r ._CLASS_IN ))
593
+ query .add_question (r .DNSQuestion (info .name , r ._TYPE_TXT , r ._CLASS_IN ))
594
+ query .add_question (r .DNSQuestion (info .server , r ._TYPE_A , r ._CLASS_IN ))
595
+ zc .handle_query (query , r ._MDNS_ADDR , r ._MDNS_PORT )
596
+ assert nbr_answers [0 ] == 4 and nbr_additionals [0 ] == 1 and nbr_authorities [0 ] == 0
597
+ nbr_answers [0 ] = nbr_additionals [0 ] = nbr_authorities [0 ] = 0
598
+
599
+ # unregister
600
+ expected_ttl = 0
601
+ zc .unregister_service (info )
602
+ assert nbr_answers [0 ] == 12 and nbr_additionals [0 ] == 0 and nbr_authorities [0 ] == 0
603
+ nbr_answers [0 ] = nbr_additionals [0 ] = nbr_authorities [0 ] = 0
604
+
605
+ # register service with custom TTL
606
+ expected_ttl = r ._DNS_TTL * 2
607
+ assert expected_ttl != r ._DNS_TTL
608
+ zc .register_service (info , ttl = expected_ttl )
609
+ assert nbr_answers [0 ] == 12 and nbr_additionals [0 ] == 0 and nbr_authorities [0 ] == 3
610
+ nbr_answers [0 ] = nbr_additionals [0 ] = nbr_authorities [0 ] = 0
611
+
612
+ # query
613
+ query = r .DNSOutgoing (r ._FLAGS_QR_QUERY | r ._FLAGS_AA )
614
+ query .add_question (r .DNSQuestion (info .type , r ._TYPE_PTR , r ._CLASS_IN ))
615
+ query .add_question (r .DNSQuestion (info .name , r ._TYPE_SRV , r ._CLASS_IN ))
616
+ query .add_question (r .DNSQuestion (info .name , r ._TYPE_TXT , r ._CLASS_IN ))
617
+ query .add_question (r .DNSQuestion (info .server , r ._TYPE_A , r ._CLASS_IN ))
618
+ zc .handle_query (query , r ._MDNS_ADDR , r ._MDNS_PORT )
619
+ assert nbr_answers [0 ] == 4 and nbr_additionals [0 ] == 1 and nbr_authorities [0 ] == 0
620
+ nbr_answers [0 ] = nbr_additionals [0 ] = nbr_authorities [0 ] = 0
621
+
622
+ # unregister
623
+ expected_ttl = 0
624
+ zc .unregister_service (info )
625
+ assert nbr_answers [0 ] == 12 and nbr_additionals [0 ] == 0 and nbr_authorities [0 ] == 0
626
+ nbr_answers [0 ] = nbr_additionals [0 ] = nbr_authorities [0 ] = 0
627
+
628
+
541
629
class TestDNSCache (unittest .TestCase ):
542
630
543
631
def test_order (self ):
0 commit comments