forked from paulsm/pyzeroconf
-
Notifications
You must be signed in to change notification settings - Fork 13
/
zunittest.py
executable file
·120 lines (98 loc) · 4.36 KB
/
zunittest.py
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
#!/usr/bin/env python
""" Unit tests for zeroconf.py """
import zeroconf as r
import struct
import unittest
class PacketGeneration(unittest.TestCase):
def testParseOwnPacketSimple(self):
generated = r.DNSOutgoing(0)
parsed = r.DNSIncoming(generated.packet())
def testParseOwnPacketSimpleUnicast(self):
generated = r.DNSOutgoing(0, 0)
parsed = r.DNSIncoming(generated.packet())
def testParseOwnPacketFlags(self):
generated = r.DNSOutgoing(r._FLAGS_QR_QUERY)
parsed = r.DNSIncoming(generated.packet())
def testParseOwnPacketQuestion(self):
generated = r.DNSOutgoing(r._FLAGS_QR_QUERY)
generated.addQuestion(r.DNSQuestion("testname.local.", r._TYPE_SRV,
r._CLASS_IN))
parsed = r.DNSIncoming(generated.packet())
def testMatchQuestion(self):
generated = r.DNSOutgoing(r._FLAGS_QR_QUERY)
question = r.DNSQuestion("testname.local.", r._TYPE_SRV, r._CLASS_IN)
generated.addQuestion(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])
class PacketForm(unittest.TestCase):
def testTransactionID(self):
"""ID must be zero in a DNS-SD packet"""
generated = r.DNSOutgoing(r._FLAGS_QR_QUERY)
bytes = generated.packet()
id = r.getByte(bytes[0]) << 8 | r.getByte(bytes[1])
self.assertEqual(id, 0)
def testQueryHeaderBits(self):
generated = r.DNSOutgoing(r._FLAGS_QR_QUERY)
bytes = generated.packet()
flags = r.getByte(bytes[2]) << 8 | r.getByte(bytes[3])
self.assertEqual(flags, 0x0)
def testResponseHeaderBits(self):
generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE)
bytes = generated.packet()
flags = r.getByte(bytes[2]) << 8 | r.getByte(bytes[3])
self.assertEqual(flags, 0x8000)
def testNumbers(self):
generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE)
bytes = generated.packet()
(numQuestions, numAnswers, numAuthorities,
numAdditionals) = struct.unpack('!4H', bytes[4:12])
self.assertEqual(numQuestions, 0)
self.assertEqual(numAnswers, 0)
self.assertEqual(numAuthorities, 0)
self.assertEqual(numAdditionals, 0)
def testNumbersQuestions(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.addQuestion(question)
bytes = generated.packet()
(numQuestions, numAnswers, numAuthorities,
numAdditionals) = struct.unpack('!4H', bytes[4:12])
self.assertEqual(numQuestions, 10)
self.assertEqual(numAnswers, 0)
self.assertEqual(numAuthorities, 0)
self.assertEqual(numAdditionals, 0)
class Names(unittest.TestCase):
def testLongName(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.addQuestion(question)
parsed = r.DNSIncoming(generated.packet())
def testExceedinglyLongName(self):
generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE)
name = "%slocal." % ("part." * 1000)
question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN)
generated.addQuestion(question)
parsed = r.DNSIncoming(generated.packet())
def testExceedinglyLongNamePart(self):
name = "%s.local." % ("a" * 1000)
generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE)
question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN)
generated.addQuestion(question)
self.assertRaises(r.NamePartTooLongException, generated.packet)
def testSameName(self):
name = "paired.local."
generated = r.DNSOutgoing(r._FLAGS_QR_RESPONSE)
question = r.DNSQuestion(name, r._TYPE_SRV, r._CLASS_IN)
generated.addQuestion(question)
generated.addQuestion(question)
parsed = r.DNSIncoming(generated.packet())
class Framework(unittest.TestCase):
def testLaunchAndClose(self):
rv = r.Zeroconf()
rv.close()
if __name__ == '__main__':
unittest.main()