@@ -87,26 +87,30 @@ def mdns_server(esp_host, events):
87
87
continue
88
88
data , addr = sock .recvfrom (1024 )
89
89
dns = dpkt .dns .DNS (data )
90
- if len (dns .qd ) > 0 and dns .qd [0 ].type == dpkt .dns .DNS_A :
91
- if dns .qd [0 ].name == TESTER_NAME :
92
- print ('Received query: {} ' .format (dns .__repr__ ()))
93
- sock .sendto (get_dns_answer_to_mdns (TESTER_NAME ),
94
- (MCAST_GRP , UDP_PORT ))
95
- elif dns .qd [0 ].name == TESTER_NAME_LWIP :
96
- print ('Received query: {} ' .format (dns .__repr__ ()))
97
- sock .sendto (
98
- get_dns_answer_to_mdns_lwip (TESTER_NAME_LWIP , dns .id ),
99
- addr )
100
- if len (dns .an ) > 0 and dns .an [0 ].type == dpkt .dns .DNS_A :
101
- print ('Received answer from {}' .format (dns .an [0 ].name ))
102
- if dns .an [0 ].name == esp_host + u'.local' :
103
- print ('Received answer to esp32-mdns query: {}' .format (
104
- dns .__repr__ ()))
105
- events ['esp_answered' ].set ()
106
- if dns .an [0 ].name == esp_host + u'-delegated.local' :
107
- print ('Received answer to esp32-mdns-delegate query: {}' .
108
- format (dns .__repr__ ()))
109
- events ['esp_delegated_answered' ].set ()
90
+ if len (dns .qd ) > 0 :
91
+ for dns_query in dns .qd :
92
+ if dns_query .type == dpkt .dns .DNS_A :
93
+ if dns_query .name == TESTER_NAME :
94
+ print ('Received query: {} ' .format (dns .__repr__ ()))
95
+ sock .sendto (get_dns_answer_to_mdns (TESTER_NAME ),
96
+ (MCAST_GRP , UDP_PORT ))
97
+ elif dns_query .name == TESTER_NAME_LWIP :
98
+ print ('Received query: {} ' .format (dns .__repr__ ()))
99
+ sock .sendto (
100
+ get_dns_answer_to_mdns_lwip (TESTER_NAME_LWIP , dns .id ),
101
+ addr )
102
+ if len (dns .an ) > 0 :
103
+ for dns_answer in dns .an :
104
+ if dns_answer .type == dpkt .dns .DNS_A :
105
+ print ('Received answer from {}' .format (dns_answer .name ))
106
+ if dns_answer .name == esp_host + u'.local' :
107
+ print ('Received answer to esp32-mdns query: {}' .format (
108
+ dns .__repr__ ()))
109
+ events ['esp_answered' ].set ()
110
+ if dns_answer .name == esp_host + u'-delegated.local' :
111
+ print ('Received answer to esp32-mdns-delegate query: {}' .format (
112
+ dns .__repr__ ()))
113
+ events ['esp_delegated_answered' ].set ()
110
114
except socket .timeout :
111
115
break
112
116
except dpkt .UnpackError :
@@ -133,62 +137,66 @@ def test_examples_protocol_mdns(dut):
133
137
}
134
138
mdns_responder = Thread (target = mdns_server ,
135
139
args = (str (specific_host ), mdns_server_events ))
136
- ipv4 = dut .expect (r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]' ,
137
- timeout = 30 )[1 ].decode ()
138
- ip_addresses = [ipv4 ]
140
+ ip_addresses = []
141
+ if dut .app .sdkconfig .get ('LWIP_IPV4' ) is True :
142
+ ipv4 = dut .expect (r'IPv4 address: (\d+\.\d+\.\d+\.\d+)[^\d]' ,
143
+ timeout = 30 )[1 ].decode ()
144
+ ip_addresses .append (ipv4 )
139
145
if dut .app .sdkconfig .get ('LWIP_IPV6' ) is True :
140
146
ipv6_r = r':' .join ((r'[0-9a-fA-F]{4}' , ) * 8 )
141
147
ipv6 = dut .expect (ipv6_r , timeout = 30 )[0 ].decode ()
142
148
ip_addresses .append (ipv6 )
143
149
print ('Connected with IP addresses: {}' .format (',' .join (ip_addresses )))
144
150
try :
145
- # 3. check the mdns name is accessible.
151
+ # TODO: Add test for example disabling IPV4
146
152
mdns_responder .start ()
147
- if not mdns_server_events ['esp_answered' ].wait (timeout = 30 ):
148
- raise ValueError (
149
- 'Test has failed: did not receive mdns answer within timeout' )
150
- if not mdns_server_events ['esp_delegated_answered' ].wait (timeout = 30 ):
151
- raise ValueError (
152
- 'Test has failed: did not receive mdns answer for delegated host within timeout'
153
+ if dut .app .sdkconfig .get ('LWIP_IPV4' ) is True :
154
+ # 3. check the mdns name is accessible.
155
+ if not mdns_server_events ['esp_answered' ].wait (timeout = 30 ):
156
+ raise ValueError (
157
+ 'Test has failed: did not receive mdns answer within timeout' )
158
+ if not mdns_server_events ['esp_delegated_answered' ].wait (timeout = 30 ):
159
+ raise ValueError (
160
+ 'Test has failed: did not receive mdns answer for delegated host within timeout'
161
+ )
162
+ # 4. check DUT output if mdns advertized host is resolved
163
+ dut .expect (
164
+ re .compile (
165
+ b'mdns-test: Query A: tinytester.local resolved to: 127.0.0.1' )
153
166
)
154
- # 4. check DUT output if mdns advertized host is resolved
155
- dut .expect (
156
- re .compile (
157
- b'mdns-test: Query A: tinytester.local resolved to: 127.0.0.1' )
158
- )
159
- dut .expect (
160
- re .compile (
161
- b'mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1'
162
- ))
163
- dut .expect (
164
- re .compile (
165
- b'mdns-test: getaddrinfo: tinytester-lwip.local resolved to: 127.0.0.1'
166
- ))
167
- # 5. check the DUT answers to `dig` command
168
- dig_output = subprocess .check_output ([
169
- 'dig' , '+short' , '-p' , '5353' , '@224.0.0.251' ,
170
- '{}.local' .format (specific_host )
171
- ])
172
- print ('Resolving {} using "dig" succeeded with:\n {}' .format (
173
- specific_host , dig_output ))
174
- if not ipv4 .encode ('utf-8' ) in dig_output :
175
- raise ValueError (
176
- 'Test has failed: Incorrectly resolved DUT hostname using dig'
177
- "Output should've contained DUT's IP address:{}" .format (ipv4 ))
178
- # 6. check the DUT reverse lookup
179
- if dut .app .sdkconfig .get ('MDNS_RESPOND_REVERSE_QUERIES' ) is True :
180
- for ip_address in ip_addresses :
181
- dig_output = subprocess .check_output ([
182
- 'dig' , '+short' , '-p' , '5353' , '@224.0.0.251' , '-x' ,
183
- '{}' .format (ip_address )
184
- ])
185
- print ('Reverse lookup for {} using "dig" succeeded with:\n {}' .
186
- format (ip_address , dig_output ))
187
- if specific_host not in dig_output .decode ():
188
- raise ValueError (
189
- 'Test has failed: Incorrectly resolved DUT IP address using dig'
190
- "Output should've contained DUT's name:{}" .format (
191
- specific_host ))
167
+ dut .expect (
168
+ re .compile (
169
+ b'mdns-test: gethostbyname: tinytester-lwip.local resolved to: 127.0.0.1'
170
+ ))
171
+ dut .expect (
172
+ re .compile (
173
+ b'mdns-test: getaddrinfo: tinytester-lwip.local resolved to: 127.0.0.1'
174
+ ))
175
+ # 5. check the DUT answers to `dig` command
176
+ dig_output = subprocess .check_output ([
177
+ 'dig' , '+short' , '-p' , '5353' , '@224.0.0.251' ,
178
+ '{}.local' .format (specific_host )
179
+ ])
180
+ print ('Resolving {} using "dig" succeeded with:\n {}' .format (
181
+ specific_host , dig_output ))
182
+ if not ipv4 .encode ('utf-8' ) in dig_output :
183
+ raise ValueError (
184
+ 'Test has failed: Incorrectly resolved DUT hostname using dig'
185
+ "Output should've contained DUT's IP address:{}" .format (ipv4 ))
186
+ # 6. check the DUT reverse lookup
187
+ if dut .app .sdkconfig .get ('MDNS_RESPOND_REVERSE_QUERIES' ) is True :
188
+ for ip_address in ip_addresses :
189
+ dig_output = subprocess .check_output ([
190
+ 'dig' , '+short' , '-p' , '5353' , '@224.0.0.251' , '-x' ,
191
+ '{}' .format (ip_address )
192
+ ])
193
+ print ('Reverse lookup for {} using "dig" succeeded with:\n {}' .
194
+ format (ip_address , dig_output ))
195
+ if specific_host not in dig_output .decode ():
196
+ raise ValueError (
197
+ 'Test has failed: Incorrectly resolved DUT IP address using dig'
198
+ "Output should've contained DUT's name:{}" .format (
199
+ specific_host ))
192
200
193
201
finally :
194
202
mdns_server_events ['stop' ].set ()
0 commit comments