1#!/usr/bin/python3 2# 3# Copyright 2020 - 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 acts.test_utils.wifi.wifi_test_utils as wutils 18 19import time 20import random 21import re 22import logging 23import acts.controllers.packet_capture as packet_capture 24 25from acts import asserts 26from acts.test_decorators import test_tracker_info 27from acts.test_utils.net import nsd_const as nconsts 28from acts.test_utils.wifi.aware import aware_const as aconsts 29from acts.test_utils.wifi.aware import aware_test_utils as autils 30from acts.test_utils.wifi.aware.AwareBaseTest import AwareBaseTest 31from acts.test_utils.wifi import wifi_test_utils as wutils 32from acts.controllers.ap_lib.hostapd_constants import CHANNEL_MAP 33 34WifiEnums = wutils.WifiEnums 35 36class ProtocolsMultiCountryTest(AwareBaseTest): 37 def __init__(self, controllers): 38 AwareBaseTest.__init__(self, controllers) 39 self.basetest_name = ( 40 "ping6_ib_unsolicited_passive_multicountry", 41 "ping6_ib_solicited_active_multicountry", 42 ) 43 44 self.generate_tests() 45 46 def generate_testcase(self, basetest_name, country): 47 """Generates a single test case from the given data. 48 49 Args: 50 basetest_name: The name of the base test case. 51 country: The information about the country code under test. 52 """ 53 base_test = getattr(self, basetest_name) 54 test_tracker_uuid = "" 55 56 testcase_name = 'test_%s_%s' % (basetest_name, country) 57 test_case = test_tracker_info(uuid=test_tracker_uuid)( 58 lambda: base_test(country)) 59 setattr(self, testcase_name, test_case) 60 self.tests.append(testcase_name) 61 62 def generate_tests(self): 63 for country in self.user_params['wifi_country_code']: 64 for basetest_name in self.basetest_name: 65 self.generate_testcase(basetest_name, country) 66 67 def setup_class(self): 68 super().setup_class() 69 for ad in self.android_devices: 70 ad.droid.wakeLockAcquireBright() 71 ad.droid.wakeUpNow() 72 wutils.wifi_test_device_init(ad) 73 74 if hasattr(self, 'packet_capture'): 75 self.packet_capture = self.packet_capture[0] 76 self.channel_list_2g = WifiEnums.ALL_2G_FREQUENCIES 77 self.channel_list_5g = WifiEnums.ALL_5G_FREQUENCIES 78 79 def setup_test(self): 80 super(ProtocolsMultiCountryTest, self).setup_test() 81 for ad in self.android_devices: 82 ad.ed.clear_all_events() 83 84 def test_time(self,begin_time): 85 super(ProtocolsMultiCountryTest, self).setup_test() 86 for ad in self.android_devices: 87 ad.cat_adb_log(begin_time) 88 89 def teardown_test(self): 90 super(ProtocolsMultiCountryTest, self).teardown_test() 91 for ad in self.android_devices: 92 ad.adb.shell("cmd wifiaware reset") 93 94 95 """Set of tests for Wi-Fi Aware data-paths: validating protocols running on 96 top of a data-path""" 97 98 SERVICE_NAME = "GoogleTestServiceXY" 99 100 def run_ping6(self, dut, peer_ipv6): 101 """Run a ping6 over the specified device/link 102 Args: 103 dut: Device on which to execute ping6 104 peer_ipv6: Scoped IPv6 address of the peer to ping 105 """ 106 cmd = "ping6 -c 3 -W 5 %s" % peer_ipv6 107 results = dut.adb.shell(cmd) 108 self.log.info("cmd='%s' -> '%s'", cmd, results) 109 if results == "": 110 asserts.fail("ping6 empty results - seems like a failure") 111 112 ######################################################################## 113 114 def get_ndp_freq(self, dut): 115 """ get aware interface status""" 116 get_nda0 = "timeout 3 logcat | grep getNdpConfirm | grep Channel" 117 out_nda01 = dut.adb.shell(get_nda0) 118 out_nda0 = re.findall("Channel = (\d+)", out_nda01) 119 #out_nda0 = dict(out_nda02[-1:]) 120 return out_nda0 121 122 123 def conf_packet_capture(self, band, channel): 124 """Configure packet capture on necessary channels.""" 125 freq_to_chan = wutils.WifiEnums.freq_to_channel[int(channel)] 126 logging.info("Capturing packets from " 127 "frequency:{}, Channel:{}".format(channel, freq_to_chan)) 128 result = self.packet_capture.configure_monitor_mode(band, freq_to_chan) 129 if not result: 130 logging.error("Failed to configure channel " 131 "for {} band".format(band)) 132 self.pcap_procs = wutils.start_pcap( 133 self.packet_capture, band, self.test_name) 134 time.sleep(5) 135 ######################################################################## 136 137 @test_tracker_info(uuid="3b09e666-c526-4879-8180-77d9a55a2833") 138 def ping6_ib_unsolicited_passive_multicountry(self, country): 139 """Validate that ping6 works correctly on an NDP created using Aware 140 discovery with UNSOLICITED/PASSIVE sessions.""" 141 p_dut = self.android_devices[0] 142 s_dut = self.android_devices[1] 143 wutils.set_wifi_country_code(p_dut, country) 144 wutils.set_wifi_country_code(s_dut, country) 145 #p_dut.adb.shell("timeout 12 logcat -c") 146 # create NDP 147 (p_req_key, s_req_key, p_aware_if, s_aware_if, p_ipv6, 148 s_ipv6) = autils.create_ib_ndp( 149 p_dut, 150 s_dut, 151 p_config=autils.create_discovery_config( 152 self.SERVICE_NAME, aconsts.PUBLISH_TYPE_UNSOLICITED), 153 s_config=autils.create_discovery_config( 154 self.SERVICE_NAME, aconsts.SUBSCRIBE_TYPE_PASSIVE), 155 device_startup_offset=self.device_startup_offset) 156 self.log.info("Interface names: P=%s, S=%s", p_aware_if, s_aware_if) 157 self.log.info("Interface addresses (IPv6): P=%s, S=%s", p_ipv6, s_ipv6) 158 ndpfreg =int(self.get_ndp_freq(p_dut)[-1]) 159 ndp_channel = str(CHANNEL_MAP[ndpfreg]) 160 n = int(ndp_channel) 161 if n in range(len(self.channel_list_2g)): 162 ndp_band = '2g' 163 else: 164 ndp_band = '5g' 165 p_dut.log.info('ndp frequency : {}'.format(ndpfreg)) 166 p_dut.log.info('ndp channel : {}'.format(ndp_channel)) 167 p_dut.log.info('ndp band : {}'.format(ndp_band)) 168 if hasattr(self, 'packet_capture'): 169 self.conf_packet_capture(ndp_band, ndpfreg) 170 # run ping6 171 self.run_ping6(p_dut, s_ipv6) 172 self.run_ping6(s_dut, p_ipv6) 173 174 # clean-up 175 p_dut.droid.connectivityUnregisterNetworkCallback(p_req_key) 176 s_dut.droid.connectivityUnregisterNetworkCallback(s_req_key) 177 if hasattr(self, 'packet_capture'): 178 wutils.stop_pcap(self.packet_capture, self.pcap_procs, False) 179 time.sleep(10) 180 181 @test_tracker_info(uuid="6b54951f-bf0b-4d26-91d6-c9b3b8452873") 182 def ping6_ib_solicited_active_multicountry(self, country): 183 """Validate that ping6 works correctly on an NDP created using Aware 184 discovery with SOLICITED/ACTIVE sessions.""" 185 p_dut = self.android_devices[0] 186 s_dut = self.android_devices[1] 187 wutils.set_wifi_country_code(p_dut, country) 188 wutils.set_wifi_country_code(s_dut, country) 189 190 # create NDP 191 (p_req_key, s_req_key, p_aware_if, s_aware_if, p_ipv6, 192 s_ipv6) = autils.create_ib_ndp( 193 p_dut, 194 s_dut, 195 p_config=autils.create_discovery_config( 196 self.SERVICE_NAME, aconsts.PUBLISH_TYPE_SOLICITED), 197 s_config=autils.create_discovery_config( 198 self.SERVICE_NAME, aconsts.SUBSCRIBE_TYPE_ACTIVE), 199 device_startup_offset=self.device_startup_offset) 200 self.log.info("Interface names: P=%s, S=%s", p_aware_if, s_aware_if) 201 self.log.info("Interface addresses (IPv6): P=%s, S=%s", p_ipv6, s_ipv6) 202 ndpfreg =int(self.get_ndp_freq(p_dut)[-1]) 203 ndp_channel = str(CHANNEL_MAP[ndpfreg]) 204 n = int(ndp_channel) 205 if n in range(len(self.channel_list_2g)): 206 ndp_band = '2g' 207 else: 208 ndp_band = '5g' 209 p_dut.log.info('ndp frequency : {}'.format(ndpfreg)) 210 p_dut.log.info('ndp channel : {}'.format(ndp_channel)) 211 p_dut.log.info('ndp band : {}'.format(ndp_band)) 212 if hasattr(self, 'packet_capture'): 213 self.conf_packet_capture(ndp_band, ndpfreg) 214 # run ping6 215 self.run_ping6(p_dut, s_ipv6) 216 self.run_ping6(s_dut, p_ipv6) 217 218 # clean-up 219 p_dut.droid.connectivityUnregisterNetworkCallback(p_req_key) 220 s_dut.droid.connectivityUnregisterNetworkCallback(s_req_key) 221 if hasattr(self, 'packet_capture'): 222 wutils.stop_pcap(self.packet_capture, self.pcap_procs, False) 223 time.sleep(10) 224