blob: 739f6ca0c2d160301ce86377345e98e77622e86c [file] [log] [blame]
jerrypcchen9f03c152021-03-11 11:57:21 +00001#
2# Copyright 2021 - The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15import time
16
17from acts import asserts
18from acts.controllers.openwrt_ap import MOBLY_CONTROLLER_CONFIG_NAME as OPENWRT
19from acts_contrib.test_utils.wifi import wifi_test_utils as wutils
20from acts_contrib.test_utils.wifi.WifiBaseTest import WifiBaseTest
21
22WLAN = "wlan0"
23PING_ADDR = "google.com"
24
25
26class DhcpTest(WifiBaseTest):
27 """DHCP related test for Android."""
28
29 def setup_class(self):
30 self.dut = self.android_devices[0]
31
32 wutils.wifi_test_device_init(self.dut)
33 asserts.assert_true(OPENWRT in self.user_params,
34 "OpenWrtAP is not in testbed.")
35 self.openwrt = self.access_points[0]
36 self.configure_openwrt_ap_and_start(wpa_network=True)
37 self.wifi_network = self.openwrt.get_wifi_network()
38 self.openwrt.network_setting.setup_ipv6_bridge()
39 asserts.assert_true(self.openwrt.verify_wifi_status(),
40 "OpenWrt Wifi interface is not ready.")
41 def teardown_class(self):
42 """Reset wifi to make sure VPN tears down cleanly."""
43 wutils.reset_wifi(self.dut)
44
45 def teardown_test(self):
46 """Reset wifi to make sure VPN tears down cleanly."""
47 wutils.reset_wifi(self.dut)
48
49 def _verify_ping(self, option="", dest=PING_ADDR):
50 try:
51 out = self.dut.adb.shell("ping%s -c1 %s" % (option, dest))
52 return "100%" not in out
53 except Exception as e:
54 self.dut.log.debug(e)
55 return False
56
57 def _verify_device_address(self, ipv4=True, ipv6=True, timeout=15):
58 """Verify device get assign address on wireless interface."""
59 current_time = time.time()
60 while time.time() < current_time + timeout:
61 try:
62 if ipv4:
63 ipv4_addr = self.dut.droid.connectivityGetIPv4Addresses(WLAN)[0]
64 self.dut.log.info("ipv4_address is %s" % ipv4_addr)
65 if ipv6:
66 ipv6_addr = self.dut.droid.connectivityGetIPv6Addresses(WLAN)[0]
67 self.dut.log.info("ipv6_address is %s" % ipv6_addr)
68 return True
69 except:
70 time.sleep(1)
71 return False
72
73 def test_ipv4_ipv6_network(self):
74 """Verify device can get both ipv4 ipv6 address."""
75 wutils.connect_to_wifi_network(self.dut, self.wifi_network)
76
77 asserts.assert_true(self._verify_device_address(),
78 "Fail to get ipv4/ipv6 address.")
79 asserts.assert_true(self._verify_ping(), "Fail to ping on ipv4.")
80 asserts.assert_true(self._verify_ping("6"), "Fail to ping on ipv6.")
81
82 def test_ipv6_only_prefer_option(self):
83 """Verify DUT can only get ipv6 address and ping out."""
84 self.openwrt.network_setting.add_ipv6_prefer_option()
85 wutils.connect_to_wifi_network(self.dut, self.wifi_network)
86
87 asserts.assert_true(self._verify_device_address(ipv4=False),
88 "Fail to get ipv6 address.")
89 asserts.assert_false(self._verify_ping(),
90 "Should not ping on success on ipv4.")
91 asserts.assert_true(self._verify_ping("6"),
92 "Fail to ping on ipv6.")