MAC address verification in pcap fixing

1. Device needs reboot before querying for factory mac
2. all verify mac is found/not found in pcap tests never fails,
   fixing that with proper return statements. As a result, bugreport
   will now get collected

Test: Yes, locally
Bug: 141255121
Bug: 142821661
Change-Id: I6d359d9781ebc9105dfaeb59e005de19ae3c97ab
diff --git a/acts/framework/acts/test_utils/wifi/p2p/WifiP2pBaseTest.py b/acts/framework/acts/test_utils/wifi/p2p/WifiP2pBaseTest.py
index 7aca3c6..92e03dd 100644
--- a/acts/framework/acts/test_utils/wifi/p2p/WifiP2pBaseTest.py
+++ b/acts/framework/acts/test_utils/wifi/p2p/WifiP2pBaseTest.py
@@ -32,8 +32,6 @@
     def setup_class(self):
         self.dut1 = self.android_devices[0]
         self.dut2 = self.android_devices[1]
-        self.dut1_mac = self.get_p2p_mac_address(self.dut1)
-        self.dut2_mac = self.get_p2p_mac_address(self.dut2)
 
         #init location before init p2p
         acts.utils.set_location_service(self.dut1, True)
@@ -102,8 +100,3 @@
         for ad in self.android_devices:
             ad.take_bug_report(test_name, begin_time)
             ad.cat_adb_log(test_name, begin_time)
-
-    def get_p2p_mac_address(self, dut):
-        """Gets the current MAC address being used for Wi-Fi Direct."""
-        out = dut.adb.shell("ifconfig p2p0")
-        return re.match(".* HWaddr (\S+).*", out, re.S).group(1)
diff --git a/acts/framework/acts/test_utils/wifi/wifi_test_utils.py b/acts/framework/acts/test_utils/wifi/wifi_test_utils.py
index c502ded..459d211 100755
--- a/acts/framework/acts/test_utils/wifi/wifi_test_utils.py
+++ b/acts/framework/acts/test_utils/wifi/wifi_test_utils.py
@@ -2101,30 +2101,33 @@
     if test_status:
         shutil.rmtree(os.path.dirname(fname))
 
-def verify_mac_not_found_in_pcap(mac, packets):
+def verify_mac_not_found_in_pcap(ad, mac, packets):
     """Verify that a mac address is not found in the captured packets.
 
     Args:
+        ad: android device object
         mac: string representation of the mac address
         packets: packets obtained by rdpcap(pcap_fname)
     """
     for pkt in packets:
         logging.debug("Packet Summary = %s", pkt.summary())
         if mac in pkt.summary():
-            asserts.fail("Caught Factory MAC: %s in packet sniffer."
-                         "Packet = %s" % (mac, pkt.show()))
+            asserts.fail("Device %s caught Factory MAC: %s in packet sniffer."
+                         "Packet = %s" % (ad.serial, mac, pkt.show()))
 
-def verify_mac_is_found_in_pcap(mac, packets):
+def verify_mac_is_found_in_pcap(ad, mac, packets):
     """Verify that a mac address is found in the captured packets.
 
     Args:
+        ad: android device object
         mac: string representation of the mac address
         packets: packets obtained by rdpcap(pcap_fname)
     """
     for pkt in packets:
         if mac in pkt.summary():
             return
-    asserts.fail("Did not find MAC = %s in packet sniffer." % mac)
+    asserts.fail("Did not find MAC = %s in packet sniffer."
+                 "for device %s" % (mac, ad.serial))
 
 def start_cnss_diags(ads):
     for ad in ads:
diff --git a/acts/tests/google/wifi/WifiMacRandomizationTest.py b/acts/tests/google/wifi/WifiMacRandomizationTest.py
index 4485220..47554bf 100644
--- a/acts/tests/google/wifi/WifiMacRandomizationTest.py
+++ b/acts/tests/google/wifi/WifiMacRandomizationTest.py
@@ -241,8 +241,9 @@
         for pkt in packets:
             self.log.debug("Packet Summary = %s" % pkt.summary())
             if mac in pkt.summary():
-                raise signals.TestFailure("Caught Factory MAC in packet sniffer."
-                                          "Packet = %s" % pkt.show())
+                raise signals.TestFailure("Caught Factory MAC in packet sniffer"
+                                          "Packet = %s Device = %s"
+                                           % (pkt.show(), self.dut))
 
     def verify_mac_is_found_in_pcap(self, mac, packets):
         for pkt in packets:
@@ -250,7 +251,7 @@
             if mac in pkt.summary():
                 return
         raise signals.TestFailure("Did not find MAC = %s in packet sniffer."
-                                  % mac)
+                                  "for device %s" % (mac, self.dut))
 
     def get_sta_mac_address(self):
         """Gets the current MAC address being used for client mode."""
@@ -283,7 +284,8 @@
         time.sleep(SHORT_TIMEOUT)
         wutils.stop_pcap(self.packet_capture, self.pcap_procs, False)
         packets = rdpcap(pcap_fname)
-        self.verify_mac_is_found_in_pcap(self.sta_factory_mac, packets)
+        self.verify_mac_is_found_in_pcap(self.dut, self.sta_factory_mac,
+                                         packets)
 
     @test_tracker_info(uuid="d9e64202-02d5-421a-967c-42e45f1f7f91")
     def test_mac_randomization_wpapsk(self):
diff --git a/acts/tests/google/wifi/aware/functional/MacRandomNoLeakageTest.py b/acts/tests/google/wifi/aware/functional/MacRandomNoLeakageTest.py
index 074ca48..3898832 100644
--- a/acts/tests/google/wifi/aware/functional/MacRandomNoLeakageTest.py
+++ b/acts/tests/google/wifi/aware/functional/MacRandomNoLeakageTest.py
@@ -70,12 +70,13 @@
         pcaps = pcap_5g + pcap_2g
 
         # Verify factory MAC is not leaked in both 2G and 5G pcaps
-        for mac in factory_mac_addresses:
-            wutils.verify_mac_not_found_in_pcap(mac, pcaps)
+        ads = [self.android_devices[0], self.android_devices[1]]
+        for i, mac in enumerate(factory_mac_addresses):
+            wutils.verify_mac_not_found_in_pcap(ads[i], mac, pcaps)
 
         # Verify random MACs are being used and in pcaps
-        for mac in mac_addresses:
-            wutils.verify_mac_is_found_in_pcap(mac, pcaps)
+        for i, mac in enumerate(mac_addresses):
+            wutils.verify_mac_is_found_in_pcap(ads[i], mac, pcaps)
 
     def transfer_mac_format(self, mac):
         """add ':' to mac String, and transfer to lower case
diff --git a/acts/tests/google/wifi/p2p/functional/WifiP2pSnifferTest.py b/acts/tests/google/wifi/p2p/functional/WifiP2pSnifferTest.py
index f9d2b23..7fd331e 100644
--- a/acts/tests/google/wifi/p2p/functional/WifiP2pSnifferTest.py
+++ b/acts/tests/google/wifi/p2p/functional/WifiP2pSnifferTest.py
@@ -17,6 +17,7 @@
 import acts.test_utils.wifi.wifi_test_utils as wutils
 import acts.utils
 import time
+import re
 
 from acts import asserts
 from acts import utils
@@ -32,6 +33,7 @@
 WPS_DISPLAY = wp2putils.WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_DISPLAY
 WPS_KEYPAD = wp2putils.WifiP2PEnums.WpsInfo.WIFI_WPS_INFO_KEYPAD
 DEFAULT_TIMEOUT = 10
+WAIT_TIME = 60
 
 class WifiP2pSnifferTest(WifiP2pBaseTest):
     """Tests factory MAC is not leaked for p2p discovery and associated cases.
@@ -46,6 +48,8 @@
 
     def setup_class(self):
         super(WifiP2pSnifferTest, self).setup_class()
+        self.dut1_mac = self.get_p2p_mac_address(self.dut1)
+        self.dut2_mac = self.get_p2p_mac_address(self.dut2)
         wp2putils.wifi_p2p_set_channels_for_current_group(self.dut1, 6, 6)
         wp2putils.wifi_p2p_set_channels_for_current_group(self.dut2, 6, 6)
         self.configure_packet_capture()
@@ -56,7 +60,6 @@
             self.packet_capture, '2g', self.test_name)
 
     def teardown_test(self):
-        self.verify_mac_no_leakage()
         super(WifiP2pSnifferTest, self).teardown_test()
 
     def configure_packet_capture(self):
@@ -66,6 +69,13 @@
         if not result:
             raise ValueError("Failed to configure channel for 2G band")
 
+    def get_p2p_mac_address(self, dut):
+        """Gets the current MAC address being used for Wi-Fi Direct."""
+        dut.reboot()
+        time.sleep(WAIT_TIME)
+        out = dut.adb.shell("ifconfig p2p0")
+        return re.match(".* HWaddr (\S+).*", out, re.S).group(1)
+
     def verify_mac_no_leakage(self):
         time.sleep(DEFAULT_TIMEOUT)
         self.log.info("Stopping packet capture")
@@ -74,8 +84,8 @@
         pcap_fname = '%s_%s.pcap' % (self.pcap_procs[BAND_2G][1],
                                      BAND_2G.upper())
         packets = rdpcap(pcap_fname)
-        wutils.verify_mac_not_found_in_pcap(self.dut1_mac, packets)
-        wutils.verify_mac_not_found_in_pcap(self.dut2_mac, packets)
+        wutils.verify_mac_not_found_in_pcap(self.dut1, self.dut1_mac, packets)
+        wutils.verify_mac_not_found_in_pcap(self.dut2, self.dut2_mac, packets)
 
     """Test Cases"""
     @test_tracker_info(uuid=" d04e62dc-e1ef-4cea-86e6-39f0dd08fb6b")
@@ -88,6 +98,7 @@
         self.log.info("Device discovery")
         wp2putils.find_p2p_device(self.dut1, self.dut2)
         wp2putils.find_p2p_device(self.dut2, self.dut1)
+        self.verify_mac_no_leakage()
 
     @test_tracker_info(uuid="6a02be84-912d-4b5b-8dfa-fd80d2554c55")
     def test_p2p_connect_via_pbc_and_ping_and_reconnect_sniffer(self):
@@ -136,3 +147,6 @@
         wp2putils.p2p_disconnect(gc_dut)
         wp2putils.check_disconnect(go_dut)
         time.sleep(p2pconsts.DEFAULT_FUNCTION_SWITCH_TIME)
+
+        # teardown
+        self.verify_mac_no_leakage()