1#!/usr/bin/env python3
2#
3#   Copyright 2019 - 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 acts.test_utils.wifi.wifi_test_utils as wutils
18import acts.utils
19import time
20import re
21
22from acts import asserts
23from acts import utils
24
25from acts.test_decorators import test_tracker_info
26from acts.test_utils.wifi.p2p.WifiP2pBaseTest import WifiP2pBaseTest
27from acts.test_utils.wifi.p2p import wifi_p2p_test_utils as wp2putils
28from acts.test_utils.wifi.p2p import wifi_p2p_const as p2pconsts
29from acts.controllers.ap_lib.hostapd_constants import BAND_2G
30from scapy.all import *
31
32WPS_PBC = wp2putils.WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_PBC
33WPS_DISPLAY = wp2putils.WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_DISPLAY
34WPS_KEYPAD = wp2putils.WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_KEYPAD
35DEFAULT_TIMEOUT = 10
36
37
38class WifiP2pSnifferTest(WifiP2pBaseTest):
39    """Tests factory MAC is not leaked for p2p discovery and associated cases.
40
41    Test Bed Requirement:
42    * At least two Android devices
43    * An access point as sniffer
44    """
45    def __init__(self, controllers):
46        WifiP2pBaseTest.__init__(self, controllers)
47
48    def setup_class(self):
49        super(WifiP2pSnifferTest, self).setup_class()
50        wp2putils.wifi_p2p_set_channels_for_current_group(self.dut1, 6, 6)
51        wp2putils.wifi_p2p_set_channels_for_current_group(self.dut2, 6, 6)
52        self.configure_packet_capture()
53
54    def setup_test(self):
55        super(WifiP2pSnifferTest, self).setup_test()
56        self.pcap_procs = wutils.start_pcap(self.packet_capture, '2g',
57                                            self.test_name)
58
59    def teardown_test(self):
60        if self.pcap_procs:
61            wutils.stop_pcap(self.packet_capture, self.pcap_procs, False)
62        super(WifiP2pSnifferTest, self).teardown_test()
63
64    def configure_packet_capture(self):
65        """Configure packet capture on the social channels."""
66        self.packet_capture = self.packet_capture[0]
67        result = self.packet_capture.configure_monitor_mode(BAND_2G, 6)
68        if not result:
69            raise ValueError("Failed to configure channel for 2G band")
70
71    def verify_mac_no_leakage(self):
72        time.sleep(DEFAULT_TIMEOUT)
73        self.log.info("Stopping packet capture")
74        wutils.stop_pcap(self.packet_capture, self.pcap_procs, False)
75        # Verify factory MAC is not leaked in 2G pcaps
76        pcap_fname = '%s_%s.pcap' % (self.pcap_procs[BAND_2G][1],
77                                     BAND_2G.upper())
78        self.pcap_procs = None
79        packets = rdpcap(pcap_fname)
80        wutils.verify_mac_not_found_in_pcap(self.dut1, self.dut1_mac, packets)
81        wutils.verify_mac_not_found_in_pcap(self.dut2, self.dut2_mac, packets)
82
83    """Test Cases"""
84    @test_tracker_info(uuid="d04e62dc-e1ef-4cea-86e6-39f0dd08fb6b")
85    def test_p2p_discovery_sniffer(self):
86        """Verify the p2p discovery functionality
87        Steps:
88        1. Discover the target device
89        2. Check the target device in peer list
90        """
91        self.log.info("Device discovery")
92        wp2putils.find_p2p_device(self.dut1, self.dut2)
93        wp2putils.find_p2p_device(self.dut2, self.dut1)
94        self.verify_mac_no_leakage()
95
96    @test_tracker_info(uuid="6a02be84-912d-4b5b-8dfa-fd80d2554c55")
97    def test_p2p_connect_via_pbc_and_ping_and_reconnect_sniffer(self):
98        """Verify the p2p connect via pbc functionality
99
100        Steps:
101        1. Request the connection which include discover the target device
102        2. check which dut is GO and which dut is GC
103        3. connection check via ping from GC to GO
104        4. disconnect
105        5. Trigger connect again from GO for reconnect test.
106        6. GO trigger disconnect
107        7. Trigger connect again from GC for reconnect test.
108        8. GC trigger disconnect
109        """
110        # Request the connection
111        wp2putils.p2p_connect(self.dut1, self.dut2, False, WPS_PBC)
112
113        if wp2putils.is_go(self.dut1):
114            go_dut = self.dut1
115            gc_dut = self.dut2
116        elif wp2putils.is_go(self.dut2):
117            go_dut = self.dut2
118            gc_dut = self.dut1
119
120        go_ip = wp2putils.p2p_go_ip(gc_dut)
121        wp2putils.p2p_connection_ping_test(gc_dut, go_ip)
122
123        # trigger disconnect
124        wp2putils.p2p_disconnect(self.dut1)
125        wp2putils.check_disconnect(self.dut2)
126        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
127
128        self.log.info("Reconnect test, triggered by GO")
129        # trigger reconnect from GO
130        go_dut.ed.clear_all_events()
131        gc_dut.ed.clear_all_events()
132        wp2putils.p2p_connect(go_dut, gc_dut, True, WPS_PBC)
133        wp2putils.p2p_disconnect(go_dut)
134        wp2putils.check_disconnect(gc_dut)
135        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
136
137        # trigger reconnect from GC
138        self.log.info("Reconnect test, triggered by GC")
139        go_dut.ed.clear_all_events()
140        gc_dut.ed.clear_all_events()
141        wp2putils.p2p_connect(gc_dut, go_dut, True, WPS_PBC)
142        wp2putils.p2p_disconnect(gc_dut)
143        wp2putils.check_disconnect(go_dut)
144        time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
145
146        # teardown
147        self.verify_mac_no_leakage()
148