1 #!/usr/bin/python3.4 2 # 3 # Copyright 2017 - 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 17 import time 18 19 from acts import asserts 20 from acts.test_decorators import test_tracker_info 21 from acts.test_utils.net import connectivity_const as cconsts 22 from acts.test_utils.wifi.aware import aware_const as aconsts 23 from acts.test_utils.wifi.aware import aware_test_utils as autils 24 from acts.test_utils.wifi.aware.AwareBaseTest import AwareBaseTest 25 26 27 class MacRandomTest(AwareBaseTest): 28 """Set of tests for Wi-Fi Aware MAC address randomization of NMI (NAN 29 management interface) and NDI (NAN data interface).""" 30 31 NUM_ITERATIONS = 10 32 33 # number of second to 'reasonably' wait to make sure that devices synchronize 34 # with each other - useful for OOB test cases, where the OOB discovery would 35 # take some time 36 WAIT_FOR_CLUSTER = 5 37 38 def __init__(self, controllers): 39 AwareBaseTest.__init__(self, controllers) 40 41 def request_network(self, dut, ns): 42 """Request a Wi-Fi Aware network. 43 44 Args: 45 dut: Device 46 ns: Network specifier 47 Returns: the request key 48 """ 49 network_req = {"TransportType": 5, "NetworkSpecifier": ns} 50 return dut.droid.connectivityRequestWifiAwareNetwork(network_req) 51 52 ########################################################################## 53 54 @test_tracker_info(uuid="09964368-146a-48e4-9f33-6a319f9eeadc") 55 def test_nmi_ndi_randomization_on_enable(self): 56 """Validate randomization of the NMI (NAN management interface) and all NDIs 57 (NAN data-interface) on each enable/disable cycle""" 58 dut = self.android_devices[0] 59 60 # DUT: attach and wait for confirmation & identity 10 times 61 mac_addresses = {} 62 for i in range(self.NUM_ITERATIONS): 63 id = dut.droid.wifiAwareAttach(True) 64 autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACHED) 65 ident_event = autils.wait_for_event(dut, 66 aconsts.EVENT_CB_ON_IDENTITY_CHANGED) 67 68 # process NMI 69 mac = ident_event["data"]["mac"] 70 dut.log.info("NMI=%s", mac) 71 if mac in mac_addresses: 72 mac_addresses[mac] = mac_addresses[mac] + 1 73 else: 74 mac_addresses[mac] = 1 75 76 # process NDIs 77 time.sleep(5) # wait for NDI creation to complete 78 for j in range(dut.aware_capabilities[aconsts.CAP_MAX_NDI_INTERFACES]): 79 ndi_interface = "%s%d" % (aconsts.AWARE_NDI_PREFIX, j) 80 ndi_mac = autils.get_mac_addr(dut, ndi_interface) 81 dut.log.info("NDI %s=%s", ndi_interface, ndi_mac) 82 if ndi_mac in mac_addresses: 83 mac_addresses[ndi_mac] = mac_addresses[ndi_mac] + 1 84 else: 85 mac_addresses[ndi_mac] = 1 86 87 dut.droid.wifiAwareDestroy(id) 88 89 # Test for uniqueness 90 for mac in mac_addresses.keys(): 91 if mac_addresses[mac] != 1: 92 asserts.fail("MAC address %s repeated %d times (all=%s)" % (mac, 93 mac_addresses[mac], mac_addresses)) 94 95 # Verify that infra interface (e.g. wlan0) MAC address is not used for NMI 96 infra_mac = autils.get_wifi_mac_address(dut) 97 asserts.assert_false( 98 infra_mac in mac_addresses, 99 "Infrastructure MAC address (%s) is used for Aware NMI (all=%s)" % 100 (infra_mac, mac_addresses)) 101 102 @test_tracker_info(uuid="0fb0b5d8-d9cb-4e37-b9af-51811be5670d") 103 def test_nmi_randomization_on_interval(self): 104 """Validate randomization of the NMI (NAN management interface) on a set 105 interval. Default value is 30 minutes - change to a small value to allow 106 testing in real-time""" 107 RANDOM_INTERVAL = 120 # minimal value in current implementation 108 109 dut = self.android_devices[0] 110 111 # set randomization interval to 5 seconds 112 dut.adb.shell("cmd wifiaware native_api set mac_random_interval_sec %d" % 113 RANDOM_INTERVAL) 114 115 # attach and wait for first identity 116 id = dut.droid.wifiAwareAttach(True) 117 autils.wait_for_event(dut, aconsts.EVENT_CB_ON_ATTACHED) 118 ident_event = autils.wait_for_event(dut, 119 aconsts.EVENT_CB_ON_IDENTITY_CHANGED) 120 mac1 = ident_event["data"]["mac"] 121 122 # wait for second identity callback 123 # Note: exact randomization interval is not critical, just approximate, 124 # hence giving a few more seconds. 125 ident_event = autils.wait_for_event(dut, 126 aconsts.EVENT_CB_ON_IDENTITY_CHANGED, 127 timeout=RANDOM_INTERVAL + 5) 128 mac2 = ident_event["data"]["mac"] 129 130 # validate MAC address is randomized 131 asserts.assert_false( 132 mac1 == mac2, 133 "Randomized MAC addresses (%s, %s) should be different" % (mac1, mac2)) 134 135 # clean-up 136 dut.droid.wifiAwareDestroy(id) 137