1#!/usr/bin/env python3 2# 3# Copyright (C) 2018 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); you may not 6# use this file except in compliance with the License. You may obtain a copy of 7# 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, WITHOUT 13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14# License for the specific language governing permissions and limitations under 15# the License. 16""" 17Script for exercising various ping scenarios 18 19""" 20from acts.base_test import BaseTestClass 21 22import os 23import threading 24import uuid 25 26from acts import signals 27from acts.controllers.access_point import setup_ap 28from acts.controllers.ap_lib import hostapd_constants 29from acts_contrib.test_utils.abstract_devices.wlan_device import create_wlan_device 30from acts_contrib.test_utils.tel.tel_test_utils import setup_droid_properties 31from acts_contrib.test_utils.fuchsia import utils 32from acts.utils import rand_ascii_str 33 34 35class PingStressTest(BaseTestClass): 36 # Timeout for ping thread in seconds 37 ping_thread_timeout_s = 60 * 5 38 39 # List to capture ping results 40 ping_threads_result = [] 41 42 # IP addresses used in pings 43 google_dns_1 = '8.8.8.8' 44 google_dns_2 = '8.8.4.4' 45 46 def setup_class(self): 47 super().setup_class() 48 49 self.ssid = rand_ascii_str(10) 50 self.fd = self.fuchsia_devices[0] 51 self.wlan_device = create_wlan_device(self.fd) 52 self.ap = self.access_points[0] 53 setup_ap(access_point=self.ap, 54 profile_name='whirlwind', 55 channel=hostapd_constants.AP_DEFAULT_CHANNEL_2G, 56 ssid=self.ssid, 57 setup_bridge=True) 58 self.wlan_device.associate(self.ssid) 59 60 def teardown_class(self): 61 self.wlan_device.disconnect() 62 self.wlan_device.reset_wifi() 63 self.ap.stop_all_aps() 64 65 def send_ping(self, 66 dest_ip, 67 count=3, 68 interval=1000, 69 timeout=1000, 70 size=25): 71 self.log.info('Attempting to ping %s...' % dest_ip) 72 ping_result = self.wlan_device.can_ping(dest_ip, count, interval, 73 timeout, size) 74 if ping_result: 75 self.log.info('Ping was successful.') 76 else: 77 if '8.8' in dest_ip: 78 raise signals.TestFailure('Ping was unsuccessful. Consider ' 79 'possibility of server failure.') 80 else: 81 raise signals.TestFailure('Ping was unsuccessful.') 82 return True 83 84 def ping_thread(self, dest_ip): 85 self.log.info('Attempting to ping %s...' % dest_ip) 86 ping_result = self.wlan_device.can_ping(dest_ip, count=10, size=50) 87 if ping_result: 88 self.log.info('Success pinging: %s' % dest_ip) 89 else: 90 self.log.info('Failure pinging: %s' % dest_ip) 91 92 self.ping_threads_result.append(ping_result) 93 94 def test_simple_ping(self): 95 return self.send_ping(self.google_dns_1) 96 97 def test_ping_local(self): 98 return self.send_ping('127.0.0.1') 99 100 def test_ping_AP(self): 101 return self.send_ping(self.ap.ssh_settings.hostname) 102 103 def test_ping_with_params(self): 104 return self.send_ping(self.google_dns_1, 105 count=5, 106 interval=800, 107 size=50) 108 109 def test_long_ping(self): 110 return self.send_ping(self.google_dns_1, count=50) 111 112 def test_medium_packet_ping(self): 113 return self.send_ping(self.google_dns_1, size=64) 114 115 def test_medium_packet_long_ping(self): 116 return self.send_ping(self.google_dns_1, 117 count=50, 118 timeout=1500, 119 size=64) 120 121 def test_large_packet_ping(self): 122 return self.send_ping(self.google_dns_1, size=500) 123 124 def test_large_packet_long_ping(self): 125 return self.send_ping(self.google_dns_1, 126 count=50, 127 timeout=5000, 128 size=500) 129 130 def test_simultaneous_pings(self): 131 ping_urls = [ 132 self.google_dns_1, self.google_dns_2, self.google_dns_1, 133 self.google_dns_2 134 ] 135 ping_threads = [] 136 137 try: 138 # Start multiple ping at the same time 139 for index, url in enumerate(ping_urls): 140 self.log.info('Create and start thread %d.' % index) 141 t = threading.Thread(target=self.ping_thread, args=(url, )) 142 ping_threads.append(t) 143 t.start() 144 145 # Wait for all threads to complete or timeout 146 for t in ping_threads: 147 t.join(self.ping_thread_timeout_s) 148 149 finally: 150 is_alive = False 151 152 for index, t in enumerate(ping_threads): 153 if t.isAlive(): 154 t = None 155 is_alive = True 156 157 if is_alive: 158 raise signals.TestFailure('Thread %d timedout' % index) 159 160 for index in range(0, len(self.ping_threads_result)): 161 if not self.ping_threads_result[index]: 162 self.log.info("Ping failed for %d" % index) 163 raise signals.TestFailure('Thread %d failed to ping. ' 164 'Consider possibility of server ' 165 'failure' % index) 166 return True 167