1#!/usr/bin/env python3.4
2#
3#   Copyright 2018 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the 'License');
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an 'AS IS' BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17import time
18from acts.test_decorators import test_tracker_info
19from acts.test_utils.power import PowerWiFiBaseTest as PWBT
20from acts.test_utils.wifi import wifi_power_test_utils as wputils
21from acts.controllers import packet_sender as pkt_utils
22
23RA_SHORT_LIFETIME = 3
24RA_LONG_LIFETIME = 1000
25DNS_LONG_LIFETIME = 300
26DNS_SHORT_LIFETIME = 3
27
28
29class PowerWiFimulticastTest(PWBT.PowerWiFiBaseTest):
30    def setup_class(self):
31        super().setup_class()
32        self.unpack_userparams(sub_mask="255.255.255.0",
33                               mac_dst="get_from_dut",
34                               mac_src="get_local",
35                               ipv4_dst="get_from_dut",
36                               ipv4_src="get_local",
37                               ipv6_dst="get_from_dut",
38                               ipv6_src="get_local",
39                               ipv6_src_type="LINK_LOCAL",
40                               ipv4_gwt="192.168.1.1",
41                               mac_dst_fake="40:90:28:EF:4B:20",
42                               ipv4_dst_fake="192.168.1.60",
43                               ipv6_dst_fake="fe80::300f:40ee:ee0a:5000",
44                               interval=1)
45
46    def set_connection(self):
47        """Setup connection between AP and client.
48
49        Setup connection between AP and phone, change DTIMx1 and get information
50        such as IP addresses to prepare packet construction.
51
52        """
53        attrs = ['screen_status', 'wifi_band']
54        indices = [2, 4]
55        self.decode_test_configs(attrs, indices)
56        # Change DTIMx1 on the phone to receive all Multicast packets
57        rebooted = wputils.change_dtim(self.dut,
58                                       gEnableModulatedDTIM=1,
59                                       gMaxLIModulatedDTIM=10)
60        self.dut.log.info('DTIM value of the phone is now DTIMx1')
61        if rebooted:
62            self.dut_rockbottom()
63
64        self.setup_ap_connection(
65            self.main_network[self.test_configs.wifi_band])
66        # Wait for DHCP with timeout of 60 seconds
67        wputils.wait_for_dhcp(self.pkt_sender.interface)
68
69        # Set the desired screen status
70        if self.test_configs.screen_status == 'OFF':
71            self.dut.droid.goToSleepNow()
72            self.dut.log.info('Screen is OFF')
73        time.sleep(5)
74
75    def sendPacketAndMeasure(self, packet):
76        """Packet injection template function
77
78        Args:
79            packet: packet to be sent/inject
80        """
81        # Start sending packets and measure power
82        self.pkt_sender.start_sending(packet, self.interval)
83        self.measure_power_and_validate()
84
85    # Test cases - screen OFF
86    @test_tracker_info(uuid='b5378aaf-7949-48ac-95fb-ee94c85d49c3')
87    def test_screen_OFF_band_5g_directed_arp(self):
88        self.set_connection()
89        self.pkt_gen_config = wputils.create_pkt_config(self)
90        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
91        packet = pkt_gen.generate()
92        self.sendPacketAndMeasure(packet)
93
94    @test_tracker_info(uuid='3b5d348d-70bf-483d-8736-13da569473aa')
95    def test_screen_OFF_band_5g_misdirected_arp(self):
96        self.set_connection()
97        self.pkt_gen_config = wputils.create_pkt_config(self)
98        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
99        packet = pkt_gen.generate(ip_dst=self.ipv4_dst_fake)
100        self.sendPacketAndMeasure(packet)
101
102    @test_tracker_info(uuid='dd3ff80d-97ce-4408-92f8-f2c72ce8d79c')
103    def test_screen_OFF_band_5g_unicast_invalid_ip_arp_request(self):
104        self.set_connection()
105        self.pkt_gen_config = wputils.create_pkt_config(self)
106        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
107        packet = pkt_gen.generate(ip_dst='0.0.0.0',
108                                  eth_dst=self.pkt_gen_config['dst_mac'])
109        self.sendPacketAndMeasure(packet)
110
111    @test_tracker_info(uuid='5dcb16f1-725c-45de-8103-340104d60a22')
112    def test_screen_OFF_band_5g_unicast_misdirected_arp_request(self):
113        self.set_connection()
114        self.pkt_gen_config = wputils.create_pkt_config(self)
115        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
116        packet = pkt_gen.generate(ip_dst=self.ipv4_dst_fake,
117                                  eth_dst=self.pkt_gen_config['dst_mac'])
118        self.sendPacketAndMeasure(packet)
119
120    @test_tracker_info(uuid='5ec4800f-a82e-4462-8b65-4fcd0b1940a2')
121    def test_screen_OFF_band_5g_unicast_invalid_src_arp_reply(self):
122        self.set_connection()
123        self.pkt_gen_config = wputils.create_pkt_config(self)
124        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
125        packet = pkt_gen.generate(op='is-at',
126                                  ip_src='0.0.0.0',
127                                  ip_dst=self.ipv4_dst_fake,
128                                  hwdst=self.mac_dst_fake,
129                                  eth_dst=self.pkt_gen_config['dst_mac'])
130        self.sendPacketAndMeasure(packet)
131
132    @test_tracker_info(uuid='6c5c0e9e-7a00-43d0-a6e8-355141467703')
133    def test_screen_OFF_band_5g_unicast_misdirected_arp_reply(self):
134        self.set_connection()
135        self.pkt_gen_config = wputils.create_pkt_config(self)
136        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
137        packet = pkt_gen.generate(op='is-at',
138                                  ip_dst=self.ipv4_dst_fake,
139                                  hwdst=self.mac_dst_fake,
140                                  eth_dst=self.pkt_gen_config['dst_mac'])
141        self.sendPacketAndMeasure(packet)
142
143    @test_tracker_info(uuid='8e534d3b-5a25-429a-a1bb-8119d7d28b5a')
144    def test_screen_OFF_band_5g_directed_ns(self):
145        self.set_connection()
146        self.pkt_gen_config = wputils.create_pkt_config(self)
147        pkt_gen = pkt_utils.NsGenerator(**self.pkt_gen_config)
148        packet = pkt_gen.generate()
149        self.sendPacketAndMeasure(packet)
150
151    @test_tracker_info(uuid='536d716d-f30b-4d20-9976-e2cbc36c3415')
152    def test_screen_OFF_band_5g_misdirected_ns(self):
153        self.set_connection()
154        self.pkt_gen_config = wputils.create_pkt_config(self)
155        pkt_gen = pkt_utils.NsGenerator(**self.pkt_gen_config)
156        packet = pkt_gen.generate(ip_dst=self.ipv6_dst_fake)
157        self.sendPacketAndMeasure(packet)
158
159    @test_tracker_info(uuid='5eed3174-8e94-428e-8527-19a9b5a90322')
160    def test_screen_OFF_band_5g_ra_short(self):
161        self.set_connection()
162        self.pkt_gen_config = wputils.create_pkt_config(self)
163        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
164        packet = pkt_gen.generate(RA_SHORT_LIFETIME)
165        self.sendPacketAndMeasure(packet)
166
167    @test_tracker_info(uuid='67867bae-f1c5-44a4-9bd0-2b832ac8059c')
168    def test_screen_OFF_band_5g_ra_long(self):
169        self.set_connection()
170        self.pkt_gen_config = wputils.create_pkt_config(self)
171        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
172        packet = pkt_gen.generate(RA_LONG_LIFETIME)
173        self.sendPacketAndMeasure(packet)
174
175    @test_tracker_info(uuid='db19bc94-3513-45c4-b3a5-d6219649d0bb')
176    def test_screen_OFF_band_5g_directed_dhcp_offer(self):
177        self.set_connection()
178        self.pkt_gen_config = wputils.create_pkt_config(self)
179        pkt_gen = pkt_utils.DhcpOfferGenerator(**self.pkt_gen_config)
180        packet = pkt_gen.generate()
181        self.sendPacketAndMeasure(packet)
182
183    @test_tracker_info(uuid='a8059869-40ee-4cf3-a957-4b7aed03fcf9')
184    def test_screen_OFF_band_5g_misdirected_dhcp_offer(self):
185        self.set_connection()
186        self.pkt_gen_config = wputils.create_pkt_config(self)
187        pkt_gen = pkt_utils.DhcpOfferGenerator(**self.pkt_gen_config)
188        packet = pkt_gen.generate(self.mac_dst_fake, self.ipv4_dst_fake)
189        self.sendPacketAndMeasure(packet)
190
191    @test_tracker_info(uuid='6e663f0a-3eb5-46f6-a79e-311baebd5d2a')
192    def test_screen_OFF_band_5g_ra_rnds_short(self):
193        self.set_connection()
194        self.pkt_gen_config = wputils.create_pkt_config(self)
195        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
196        packet = pkt_gen.generate(RA_LONG_LIFETIME,
197                                  enableDNS=True,
198                                  dns_lifetime=DNS_SHORT_LIFETIME)
199        self.sendPacketAndMeasure(packet)
200
201    @test_tracker_info(uuid='84d2f1ff-bd4f-46c6-9b06-826d9b14909c')
202    def test_screen_OFF_band_5g_ra_rnds_long(self):
203        self.set_connection()
204        self.pkt_gen_config = wputils.create_pkt_config(self)
205        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
206        packet = pkt_gen.generate(RA_LONG_LIFETIME,
207                                  enableDNS=True,
208                                  dns_lifetime=DNS_LONG_LIFETIME)
209        self.sendPacketAndMeasure(packet)
210
211    @test_tracker_info(uuid='4a17e74f-3e7f-4e90-ac9e-884a7c13cede')
212    def test_screen_OFF_band_5g_directed_ping6(self):
213        self.set_connection()
214        self.pkt_gen_config = wputils.create_pkt_config(self)
215        pkt_gen = pkt_utils.Ping6Generator(**self.pkt_gen_config)
216        packet = pkt_gen.generate()
217        self.sendPacketAndMeasure(packet)
218
219    @test_tracker_info(uuid='ab249e0d-58ba-4b55-8a81-e1e4fb04780a')
220    def test_screen_OFF_band_5g_misdirected_ping6(self):
221        self.set_connection()
222        self.pkt_gen_config = wputils.create_pkt_config(self)
223        pkt_gen = pkt_utils.Ping6Generator(**self.pkt_gen_config)
224        packet = pkt_gen.generate(self.ipv6_dst_fake, pkt_utils.MAC_BROADCAST)
225        self.sendPacketAndMeasure(packet)
226
227    @test_tracker_info(uuid='e37112e6-5c35-4c89-8d15-f5a44e69be0b')
228    def test_screen_OFF_band_5g_directed_ping4(self):
229        self.set_connection()
230        self.pkt_gen_config = wputils.create_pkt_config(self)
231        pkt_gen = pkt_utils.Ping4Generator(**self.pkt_gen_config)
232        packet = pkt_gen.generate()
233        self.sendPacketAndMeasure(packet)
234
235    @test_tracker_info(uuid='afd4a011-63a9-46c3-8a75-13f515ba8475')
236    def test_screen_OFF_band_5g_misdirected_ping4(self):
237        self.set_connection()
238        self.pkt_gen_config = wputils.create_pkt_config(self)
239        pkt_gen = pkt_utils.Ping4Generator(**self.pkt_gen_config)
240        packet = pkt_gen.generate(self.ipv4_dst_fake, pkt_utils.MAC_BROADCAST)
241        self.sendPacketAndMeasure(packet)
242
243    @test_tracker_info(uuid='03f0e845-fd66-4120-a79d-5eb64d49b6cd')
244    def test_screen_OFF_band_5g_mdns6(self):
245        self.set_connection()
246        self.pkt_gen_config = wputils.create_pkt_config(self)
247        pkt_gen = pkt_utils.Mdns6Generator(**self.pkt_gen_config)
248        packet = pkt_gen.generate()
249        self.sendPacketAndMeasure(packet)
250
251    @test_tracker_info(uuid='dcbb0aec-512d-48bd-b743-024697ce511b')
252    def test_screen_OFF_band_5g_mdns4(self):
253        self.set_connection()
254        self.pkt_gen_config = wputils.create_pkt_config(self)
255        pkt_gen = pkt_utils.Mdns4Generator(**self.pkt_gen_config)
256        packet = pkt_gen.generate()
257        self.sendPacketAndMeasure(packet)
258
259    @test_tracker_info(uuid='16dabe69-27a6-470b-a474-4774cd3e4715')
260    def test_screen_OFF_band_5g_dot3(self):
261        self.set_connection()
262        self.pkt_gen_config = wputils.create_pkt_config(self)
263        pkt_gen = pkt_utils.Dot3Generator(**self.pkt_gen_config)
264        packet = pkt_gen.generate()
265        self.sendPacketAndMeasure(packet)
266
267    @test_tracker_info(uuid='55d00670-f34b-4289-95fa-b618e509c15c')
268    def test_screen_OFF_band_5g_dot3_llc(self):
269        self.set_connection()
270        self.pkt_gen_config = wputils.create_pkt_config(self)
271        pkt_gen = pkt_utils.Dot3Generator(**self.pkt_gen_config)
272        packet = pkt_gen.generate_llc()
273        self.sendPacketAndMeasure(packet)
274
275    @test_tracker_info(uuid='75ba3435-fe63-4a21-8cbe-2f631043f632')
276    def test_screen_OFF_band_5g_dot3_snap(self):
277        self.set_connection()
278        self.pkt_gen_config = wputils.create_pkt_config(self)
279        pkt_gen = pkt_utils.Dot3Generator(**self.pkt_gen_config)
280        packet = pkt_gen.generate_snap()
281        self.sendPacketAndMeasure(packet)
282
283    # Test cases: screen ON
284    @test_tracker_info(uuid='b9550149-bf36-4f86-9b4b-6e900756a90e')
285    def test_screen_ON_band_5g_directed_arp(self):
286        self.set_connection()
287        self.pkt_gen_config = wputils.create_pkt_config(self)
288        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
289        packet = pkt_gen.generate()
290        self.sendPacketAndMeasure(packet)
291
292    @test_tracker_info(uuid='406dffae-104e-46cb-9ec2-910aac7aca39')
293    def test_screen_ON_band_5g_misdirected_arp(self):
294        self.set_connection()
295        self.pkt_gen_config = wputils.create_pkt_config(self)
296        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
297        packet = pkt_gen.generate(self.ipv4_dst_fake)
298        self.sendPacketAndMeasure(packet)
299
300    @test_tracker_info(uuid='be4cb543-c710-4041-a770-819e82a6d164')
301    def test_screen_ON_band_5g_directed_ns(self):
302        self.set_connection()
303        self.pkt_gen_config = wputils.create_pkt_config(self)
304        pkt_gen = pkt_utils.NsGenerator(**self.pkt_gen_config)
305        packet = pkt_gen.generate()
306        self.sendPacketAndMeasure(packet)
307
308    @test_tracker_info(uuid='de21d24f-e03e-47a1-8bbb-11953200e870')
309    def test_screen_ON_band_5g_misdirected_ns(self):
310        self.set_connection()
311        self.pkt_gen_config = wputils.create_pkt_config(self)
312        pkt_gen = pkt_utils.NsGenerator(**self.pkt_gen_config)
313        packet = pkt_gen.generate(self.ipv6_dst_fake)
314        self.sendPacketAndMeasure(packet)
315
316    @test_tracker_info(uuid='b424a170-5095-4b47-82eb-50f7b7fdf35d')
317    def test_screen_ON_band_5g_ra_short(self):
318        self.set_connection()
319        self.pkt_gen_config = wputils.create_pkt_config(self)
320        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
321        packet = pkt_gen.generate(RA_SHORT_LIFETIME)
322        self.sendPacketAndMeasure(packet)
323
324    @test_tracker_info(uuid='ab627e59-2ee8-4c0d-970b-eeb1d1cecdc1')
325    def test_screen_ON_band_5g_ra_long(self):
326        self.set_connection()
327        self.pkt_gen_config = wputils.create_pkt_config(self)
328        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
329        packet = pkt_gen.generate(RA_LONG_LIFETIME)
330        self.sendPacketAndMeasure(packet)
331
332    @test_tracker_info(uuid='ee6514ab-1814-44b9-ba01-63f77ba77c34')
333    def test_screen_ON_band_5g_directed_dhcp_offer(self):
334        self.set_connection()
335        self.pkt_gen_config = wputils.create_pkt_config(self)
336        pkt_gen = pkt_utils.DhcpOfferGenerator(**self.pkt_gen_config)
337        packet = pkt_gen.generate()
338        self.sendPacketAndMeasure(packet)
339
340    @test_tracker_info(uuid='eaebfe98-32da-4ebc-bca7-3b7026d99a4f')
341    def test_screen_ON_band_5g_misdirected_dhcp_offer(self):
342        self.set_connection()
343        self.pkt_gen_config = wputils.create_pkt_config(self)
344        pkt_gen = pkt_utils.DhcpOfferGenerator(**self.pkt_gen_config)
345        packet = pkt_gen.generate(self.mac_dst_fake, self.ipv4_dst_fake)
346        self.sendPacketAndMeasure(packet)
347
348    @test_tracker_info(uuid='f0e2193f-bf6a-441b-b9c1-bb7b65787cd5')
349    def test_screen_ON_band_5g_ra_rnds_short(self):
350        self.set_connection()
351        self.pkt_gen_config = wputils.create_pkt_config(self)
352        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
353        packet = pkt_gen.generate(RA_LONG_LIFETIME,
354                                  enableDNS=True,
355                                  dns_lifetime=DNS_SHORT_LIFETIME)
356        self.sendPacketAndMeasure(packet)
357
358    @test_tracker_info(uuid='62b99cd7-75bf-45be-b93f-bb037a13b3e2')
359    def test_screen_ON_band_5g_ra_rnds_long(self):
360        self.set_connection()
361        self.pkt_gen_config = wputils.create_pkt_config(self)
362        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
363        packet = pkt_gen.generate(RA_LONG_LIFETIME,
364                                  enableDNS=True,
365                                  dns_lifetime=DNS_LONG_LIFETIME)
366        self.sendPacketAndMeasure(packet)
367
368    @test_tracker_info(uuid='4088af4c-a64b-4fc1-848c-688936cc6c12')
369    def test_screen_ON_band_5g_directed_ping6(self):
370        self.set_connection()
371        self.pkt_gen_config = wputils.create_pkt_config(self)
372        pkt_gen = pkt_utils.Ping6Generator(**self.pkt_gen_config)
373        packet = pkt_gen.generate()
374        self.sendPacketAndMeasure(packet)
375
376    @test_tracker_info(uuid='3179e327-e6ac-4dae-bb8a-f3940f21094d')
377    def test_screen_ON_band_5g_misdirected_ping6(self):
378        self.set_connection()
379        self.pkt_gen_config = wputils.create_pkt_config(self)
380        pkt_gen = pkt_utils.Ping6Generator(**self.pkt_gen_config)
381        packet = pkt_gen.generate(self.ipv6_dst_fake, pkt_utils.MAC_BROADCAST)
382        self.sendPacketAndMeasure(packet)
383
384    @test_tracker_info(uuid='90c70e8a-74fd-4878-89c6-5e15c3ede318')
385    def test_screen_ON_band_5g_directed_ping4(self):
386        self.set_connection()
387        self.pkt_gen_config = wputils.create_pkt_config(self)
388        pkt_gen = pkt_utils.Ping4Generator(**self.pkt_gen_config)
389        packet = pkt_gen.generate()
390        self.sendPacketAndMeasure(packet)
391
392    @test_tracker_info(uuid='dcfabbc7-a7e1-4a92-a38d-8ebe7aa2e063')
393    def test_screen_ON_band_5g_misdirected_ping4(self):
394        self.set_connection()
395        self.pkt_gen_config = wputils.create_pkt_config(self)
396        pkt_gen = pkt_utils.Ping4Generator(**self.pkt_gen_config)
397        packet = pkt_gen.generate(self.ipv4_dst_fake, pkt_utils.MAC_BROADCAST)
398        self.sendPacketAndMeasure(packet)
399
400    @test_tracker_info(uuid='117814db-f94d-4239-a7ab-033482b1da52')
401    def test_screen_ON_band_5g_mdns6(self):
402        self.set_connection()
403        self.pkt_gen_config = wputils.create_pkt_config(self)
404        pkt_gen = pkt_utils.Mdns6Generator(**self.pkt_gen_config)
405        packet = pkt_gen.generate()
406        self.sendPacketAndMeasure(packet)
407
408    @test_tracker_info(uuid='ce6ad7e2-21f3-4e68-9c0d-d0e14e0a7c53')
409    def test_screen_ON_band_5g_mdns4(self):
410        self.set_connection()
411        self.pkt_gen_config = wputils.create_pkt_config(self)
412        pkt_gen = pkt_utils.Mdns4Generator(**self.pkt_gen_config)
413        packet = pkt_gen.generate()
414        self.sendPacketAndMeasure(packet)
415