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 set_connection(self):
31        """Setup connection between AP and client.
32
33        Setup connection between AP and phone, change DTIMx1 and get information
34        such as IP addresses to prepare packet construction.
35
36        """
37        attrs = ['screen_status', 'wifi_band']
38        indices = [2, 4]
39        self.decode_test_configs(attrs, indices)
40        # Change DTIMx1 on the phone to receive all Multicast packets
41        rebooted = wputils.change_dtim(
42            self.dut, gEnableModulatedDTIM=1, gMaxLIModulatedDTIM=10)
43        self.dut.log.info('DTIM value of the phone is now DTIMx1')
44        if rebooted:
45            self.dut_rockbottom()
46
47        self.setup_ap_connection(
48            self.main_network[self.test_configs.wifi_band])
49        # Wait for DHCP with timeout of 60 seconds
50        wputils.wait_for_dhcp(self.pkt_sender.interface)
51
52        # Set the desired screen status
53        if self.test_configs.screen_status == 'OFF':
54            self.dut.droid.goToSleepNow()
55            self.dut.log.info('Screen is OFF')
56        time.sleep(5)
57
58    def sendPacketAndMeasure(self, packet):
59        """Packet injection template function
60
61        Args:
62            packet: packet to be sent/inject
63        """
64        # Start sending packets and measure power
65        self.pkt_sender.start_sending(packet, self.interval)
66        self.measure_power_and_validate()
67
68    # Test cases - screen OFF
69    @test_tracker_info(uuid='b5378aaf-7949-48ac-95fb-ee94c85d49c3')
70    def test_screen_OFF_band_5g_directed_arp(self):
71        self.set_connection()
72        self.pkt_gen_config = wputils.create_pkt_config(self)
73        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
74        packet = pkt_gen.generate()
75        self.sendPacketAndMeasure(packet)
76
77    @test_tracker_info(uuid='3b5d348d-70bf-483d-8736-13da569473aa')
78    def test_screen_OFF_band_5g_misdirected_arp(self):
79        self.set_connection()
80        self.pkt_gen_config = wputils.create_pkt_config(self)
81        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
82        packet = pkt_gen.generate(self.ipv4_dst_fake)
83        self.sendPacketAndMeasure(packet)
84
85    @test_tracker_info(uuid='8e534d3b-5a25-429a-a1bb-8119d7d28b5a')
86    def test_screen_OFF_band_5g_directed_ns(self):
87        self.set_connection()
88        self.pkt_gen_config = wputils.create_pkt_config(self)
89        pkt_gen = pkt_utils.NsGenerator(**self.pkt_gen_config)
90        packet = pkt_gen.generate()
91        self.sendPacketAndMeasure(packet)
92
93    @test_tracker_info(uuid='536d716d-f30b-4d20-9976-e2cbc36c3415')
94    def test_screen_OFF_band_5g_misdirected_ns(self):
95        self.set_connection()
96        self.pkt_gen_config = wputils.create_pkt_config(self)
97        pkt_gen = pkt_utils.NsGenerator(**self.pkt_gen_config)
98        packet = pkt_gen.generate(self.ipv6_dst_fake)
99        self.sendPacketAndMeasure(packet)
100
101    @test_tracker_info(uuid='5eed3174-8e94-428e-8527-19a9b5a90322')
102    def test_screen_OFF_band_5g_ra_short(self):
103        self.set_connection()
104        self.pkt_gen_config = wputils.create_pkt_config(self)
105        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
106        packet = pkt_gen.generate(RA_SHORT_LIFETIME)
107        self.sendPacketAndMeasure(packet)
108
109    @test_tracker_info(uuid='67867bae-f1c5-44a4-9bd0-2b832ac8059c')
110    def test_screen_OFF_band_5g_ra_long(self):
111        self.set_connection()
112        self.pkt_gen_config = wputils.create_pkt_config(self)
113        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
114        packet = pkt_gen.generate(RA_LONG_LIFETIME)
115        self.sendPacketAndMeasure(packet)
116
117    @test_tracker_info(uuid='db19bc94-3513-45c4-b3a5-d6219649d0bb')
118    def test_screen_OFF_band_5g_directed_dhcp_offer(self):
119        self.set_connection()
120        self.pkt_gen_config = wputils.create_pkt_config(self)
121        pkt_gen = pkt_utils.DhcpOfferGenerator(**self.pkt_gen_config)
122        packet = pkt_gen.generate()
123        self.sendPacketAndMeasure(packet)
124
125    @test_tracker_info(uuid='a8059869-40ee-4cf3-a957-4b7aed03fcf9')
126    def test_screen_OFF_band_5g_misdirected_dhcp_offer(self):
127        self.set_connection()
128        self.pkt_gen_config = wputils.create_pkt_config(self)
129        pkt_gen = pkt_utils.DhcpOfferGenerator(**self.pkt_gen_config)
130        packet = pkt_gen.generate(self.mac_dst_fake, self.ipv4_dst_fake)
131        self.sendPacketAndMeasure(packet)
132
133    @test_tracker_info(uuid='6e663f0a-3eb5-46f6-a79e-311baebd5d2a')
134    def test_screen_OFF_band_5g_ra_rnds_short(self):
135        self.set_connection()
136        self.pkt_gen_config = wputils.create_pkt_config(self)
137        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
138        packet = pkt_gen.generate(
139            RA_LONG_LIFETIME, enableDNS=True, dns_lifetime=DNS_SHORT_LIFETIME)
140        self.sendPacketAndMeasure(packet)
141
142    @test_tracker_info(uuid='84d2f1ff-bd4f-46c6-9b06-826d9b14909c')
143    def test_screen_OFF_band_5g_ra_rnds_long(self):
144        self.set_connection()
145        self.pkt_gen_config = wputils.create_pkt_config(self)
146        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
147        packet = pkt_gen.generate(
148            RA_LONG_LIFETIME, enableDNS=True, dns_lifetime=DNS_LONG_LIFETIME)
149        self.sendPacketAndMeasure(packet)
150
151    @test_tracker_info(uuid='4a17e74f-3e7f-4e90-ac9e-884a7c13cede')
152    def test_screen_OFF_band_5g_directed_ping6(self):
153        self.set_connection()
154        self.pkt_gen_config = wputils.create_pkt_config(self)
155        pkt_gen = pkt_utils.Ping6Generator(**self.pkt_gen_config)
156        packet = pkt_gen.generate()
157        self.sendPacketAndMeasure(packet)
158
159    @test_tracker_info(uuid='ab249e0d-58ba-4b55-8a81-e1e4fb04780a')
160    def test_screen_OFF_band_5g_misdirected_ping6(self):
161        self.set_connection()
162        self.pkt_gen_config = wputils.create_pkt_config(self)
163        pkt_gen = pkt_utils.Ping6Generator(**self.pkt_gen_config)
164        packet = pkt_gen.generate(self.ipv6_dst_fake, pkt_utils.MAC_BROADCAST)
165        self.sendPacketAndMeasure(packet)
166
167    @test_tracker_info(uuid='e37112e6-5c35-4c89-8d15-f5a44e69be0b')
168    def test_screen_OFF_band_5g_directed_ping4(self):
169        self.set_connection()
170        self.pkt_gen_config = wputils.create_pkt_config(self)
171        pkt_gen = pkt_utils.Ping4Generator(**self.pkt_gen_config)
172        packet = pkt_gen.generate()
173        self.sendPacketAndMeasure(packet)
174
175    @test_tracker_info(uuid='afd4a011-63a9-46c3-8a75-13f515ba8475')
176    def test_screen_OFF_band_5g_misdirected_ping4(self):
177        self.set_connection()
178        self.pkt_gen_config = wputils.create_pkt_config(self)
179        pkt_gen = pkt_utils.Ping4Generator(**self.pkt_gen_config)
180        packet = pkt_gen.generate(self.ipv4_dst_fake, pkt_utils.MAC_BROADCAST)
181        self.sendPacketAndMeasure(packet)
182
183    @test_tracker_info(uuid='03f0e845-fd66-4120-a79d-5eb64d49b6cd')
184    def test_screen_OFF_band_5g_mdns6(self):
185        self.set_connection()
186        self.pkt_gen_config = wputils.create_pkt_config(self)
187        pkt_gen = pkt_utils.Mdns6Generator(**self.pkt_gen_config)
188        packet = pkt_gen.generate()
189        self.sendPacketAndMeasure(packet)
190
191    @test_tracker_info(uuid='dcbb0aec-512d-48bd-b743-024697ce511b')
192    def test_screen_OFF_band_5g_mdns4(self):
193        self.set_connection()
194        self.pkt_gen_config = wputils.create_pkt_config(self)
195        pkt_gen = pkt_utils.Mdns4Generator(**self.pkt_gen_config)
196        packet = pkt_gen.generate()
197        self.sendPacketAndMeasure(packet)
198
199    # Test cases: screen ON
200    @test_tracker_info(uuid='b9550149-bf36-4f86-9b4b-6e900756a90e')
201    def test_screen_ON_band_5g_directed_arp(self):
202        self.set_connection()
203        self.pkt_gen_config = wputils.create_pkt_config(self)
204        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
205        packet = pkt_gen.generate()
206        self.sendPacketAndMeasure(packet)
207
208    @test_tracker_info(uuid='406dffae-104e-46cb-9ec2-910aac7aca39')
209    def test_screen_ON_band_5g_misdirected_arp(self):
210        self.set_connection()
211        self.pkt_gen_config = wputils.create_pkt_config(self)
212        pkt_gen = pkt_utils.ArpGenerator(**self.pkt_gen_config)
213        packet = pkt_gen.generate(self.ipv4_dst_fake)
214        self.sendPacketAndMeasure(packet)
215
216    @test_tracker_info(uuid='be4cb543-c710-4041-a770-819e82a6d164')
217    def test_screen_ON_band_5g_directed_ns(self):
218        self.set_connection()
219        self.pkt_gen_config = wputils.create_pkt_config(self)
220        pkt_gen = pkt_utils.NsGenerator(**self.pkt_gen_config)
221        packet = pkt_gen.generate()
222        self.sendPacketAndMeasure(packet)
223
224    @test_tracker_info(uuid='de21d24f-e03e-47a1-8bbb-11953200e870')
225    def test_screen_ON_band_5g_misdirected_ns(self):
226        self.set_connection()
227        self.pkt_gen_config = wputils.create_pkt_config(self)
228        pkt_gen = pkt_utils.NsGenerator(**self.pkt_gen_config)
229        packet = pkt_gen.generate(self.ipv6_dst_fake)
230        self.sendPacketAndMeasure(packet)
231
232    @test_tracker_info(uuid='b424a170-5095-4b47-82eb-50f7b7fdf35d')
233    def test_screen_ON_band_5g_ra_short(self):
234        self.set_connection()
235        self.pkt_gen_config = wputils.create_pkt_config(self)
236        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
237        packet = pkt_gen.generate(RA_SHORT_LIFETIME)
238        self.sendPacketAndMeasure(packet)
239
240    @test_tracker_info(uuid='ab627e59-2ee8-4c0d-970b-eeb1d1cecdc1')
241    def test_screen_ON_band_5g_ra_long(self):
242        self.set_connection()
243        self.pkt_gen_config = wputils.create_pkt_config(self)
244        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
245        packet = pkt_gen.generate(RA_LONG_LIFETIME)
246        self.sendPacketAndMeasure(packet)
247
248    @test_tracker_info(uuid='ee6514ab-1814-44b9-ba01-63f77ba77c34')
249    def test_screen_ON_band_5g_directed_dhcp_offer(self):
250        self.set_connection()
251        self.pkt_gen_config = wputils.create_pkt_config(self)
252        pkt_gen = pkt_utils.DhcpOfferGenerator(**self.pkt_gen_config)
253        packet = pkt_gen.generate()
254        self.sendPacketAndMeasure(packet)
255
256    @test_tracker_info(uuid='eaebfe98-32da-4ebc-bca7-3b7026d99a4f')
257    def test_screen_ON_band_5g_misdirected_dhcp_offer(self):
258        self.set_connection()
259        self.pkt_gen_config = wputils.create_pkt_config(self)
260        pkt_gen = pkt_utils.DhcpOfferGenerator(**self.pkt_gen_config)
261        packet = pkt_gen.generate(self.mac_dst_fake, self.ipv4_dst_fake)
262        self.sendPacketAndMeasure(packet)
263
264    @test_tracker_info(uuid='f0e2193f-bf6a-441b-b9c1-bb7b65787cd5')
265    def test_screen_ON_band_5g_ra_rnds_short(self):
266        self.set_connection()
267        self.pkt_gen_config = wputils.create_pkt_config(self)
268        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
269        packet = pkt_gen.generate(
270            RA_LONG_LIFETIME, enableDNS=True, dns_lifetime=DNS_SHORT_LIFETIME)
271        self.sendPacketAndMeasure(packet)
272
273    @test_tracker_info(uuid='62b99cd7-75bf-45be-b93f-bb037a13b3e2')
274    def test_screen_ON_band_5g_ra_rnds_long(self):
275        self.set_connection()
276        self.pkt_gen_config = wputils.create_pkt_config(self)
277        pkt_gen = pkt_utils.RaGenerator(**self.pkt_gen_config)
278        packet = pkt_gen.generate(
279            RA_LONG_LIFETIME, enableDNS=True, dns_lifetime=DNS_LONG_LIFETIME)
280        self.sendPacketAndMeasure(packet)
281
282    @test_tracker_info(uuid='4088af4c-a64b-4fc1-848c-688936cc6c12')
283    def test_screen_ON_band_5g_directed_ping6(self):
284        self.set_connection()
285        self.pkt_gen_config = wputils.create_pkt_config(self)
286        pkt_gen = pkt_utils.Ping6Generator(**self.pkt_gen_config)
287        packet = pkt_gen.generate()
288        self.sendPacketAndMeasure(packet)
289
290    @test_tracker_info(uuid='3179e327-e6ac-4dae-bb8a-f3940f21094d')
291    def test_screen_ON_band_5g_misdirected_ping6(self):
292        self.set_connection()
293        self.pkt_gen_config = wputils.create_pkt_config(self)
294        pkt_gen = pkt_utils.Ping6Generator(**self.pkt_gen_config)
295        packet = pkt_gen.generate(self.ipv6_dst_fake, pkt_utils.MAC_BROADCAST)
296        self.sendPacketAndMeasure(packet)
297
298    @test_tracker_info(uuid='90c70e8a-74fd-4878-89c6-5e15c3ede318')
299    def test_screen_ON_band_5g_directed_ping4(self):
300        self.set_connection()
301        self.pkt_gen_config = wputils.create_pkt_config(self)
302        pkt_gen = pkt_utils.Ping4Generator(**self.pkt_gen_config)
303        packet = pkt_gen.generate()
304        self.sendPacketAndMeasure(packet)
305
306    @test_tracker_info(uuid='dcfabbc7-a7e1-4a92-a38d-8ebe7aa2e063')
307    def test_screen_ON_band_5g_misdirected_ping4(self):
308        self.set_connection()
309        self.pkt_gen_config = wputils.create_pkt_config(self)
310        pkt_gen = pkt_utils.Ping4Generator(**self.pkt_gen_config)
311        packet = pkt_gen.generate(self.ipv4_dst_fake, pkt_utils.MAC_BROADCAST)
312        self.sendPacketAndMeasure(packet)
313
314    @test_tracker_info(uuid='117814db-f94d-4239-a7ab-033482b1da52')
315    def test_screen_ON_band_5g_mdns6(self):
316        self.set_connection()
317        self.pkt_gen_config = wputils.create_pkt_config(self)
318        pkt_gen = pkt_utils.Mdns6Generator(**self.pkt_gen_config)
319        packet = pkt_gen.generate()
320        self.sendPacketAndMeasure(packet)
321
322    @test_tracker_info(uuid='ce6ad7e2-21f3-4e68-9c0d-d0e14e0a7c53')
323    def test_screen_ON_band_5g_mdns4(self):
324        self.set_connection()
325        self.pkt_gen_config = wputils.create_pkt_config(self)
326        pkt_gen = pkt_utils.Mdns4Generator(**self.pkt_gen_config)
327        packet = pkt_gen.generate()
328        self.sendPacketAndMeasure(packet)
329