blob: 329ead4a4ea377fa85d70940742f3dbf551a730f [file] [log] [blame]
Etan Cohenbfb149f2017-06-06 10:13:17 -07001#!/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
Etan Cohenbfb149f2017-06-06 10:13:17 -070017import time
18
19from acts import asserts
Etan Cohenf02703c2017-08-24 11:11:44 -070020from acts.test_decorators import test_tracker_info
Etan Cohenbfb149f2017-06-06 10:13:17 -070021from acts.test_utils.net import connectivity_const as cconsts
22from acts.test_utils.wifi.aware import aware_const as aconsts
23from acts.test_utils.wifi.aware import aware_test_utils as autils
24from acts.test_utils.wifi.aware.AwareBaseTest import AwareBaseTest
25
26
27class 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
Etan Cohen62a3c172017-06-07 11:08:35 -070041 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
Etan Cohenbfb149f2017-06-06 10:13:17 -070052 ##########################################################################
53
Etan Cohen185b3cf2017-08-22 14:10:10 -070054 @test_tracker_info(uuid="09964368-146a-48e4-9f33-6a319f9eeadc")
Etan Cohend4a30d82017-06-22 10:28:05 -070055 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"""
Etan Cohenbfb149f2017-06-06 10:13:17 -070058 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)
Etan Cohend4a30d82017-06-22 10:28:05 -070067
68 # process NMI
Etan Cohenbfb149f2017-06-06 10:13:17 -070069 mac = ident_event["data"]["mac"]
Etan Cohend4a30d82017-06-22 10:28:05 -070070 dut.log.info("NMI=%s", mac)
Etan Cohenbfb149f2017-06-06 10:13:17 -070071 if mac in mac_addresses:
72 mac_addresses[mac] = mac_addresses[mac] + 1
73 else:
74 mac_addresses[mac] = 1
Etan Cohend4a30d82017-06-22 10:28:05 -070075
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
Etan Cohenbfb149f2017-06-06 10:13:17 -070087 dut.droid.wifiAwareDestroy(id)
88
89 # Test for uniqueness
90 for mac in mac_addresses.keys():
91 if mac_addresses[mac] != 1:
Etan Cohen41801552017-06-07 14:57:49 -070092 asserts.fail("MAC address %s repeated %d times (all=%s)" % (mac,
Etan Cohenbfb149f2017-06-06 10:13:17 -070093 mac_addresses[mac], mac_addresses))
Etan Cohen62a3c172017-06-07 11:08:35 -070094
Etan Cohen41801552017-06-07 14:57:49 -070095 # 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
Etan Cohen185b3cf2017-08-22 14:10:10 -0700102 @test_tracker_info(uuid="0fb0b5d8-d9cb-4e37-b9af-51811be5670d")
Etan Cohen641d8222017-06-07 13:57:49 -0700103 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)