1#
2#   Copyright 2019 - 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.
15
16"""Test script to test Bluetooth Tethering testcases."""
17import time
18
19from acts import asserts
20from acts.test_decorators import test_tracker_info
21from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
22from acts.test_utils.bt.bt_test_utils import orchestrate_and_verify_pan_connection
23from acts.test_utils.bt.bt_test_utils import setup_multiple_devices_for_bt_test
24from acts.test_utils.net import net_test_utils as nutils
25from acts.test_utils.tel.tel_test_utils import wait_for_cell_data_connection
26from acts.test_utils.wifi import wifi_test_utils as wutils
27
28DEFAULT_PING_URL = "https://www.google.com/robots.txt"
29
30
31class BluetoothTetheringTest(BluetoothBaseTest):
32  """Tests for Bluetooth Tethering."""
33
34  def setup_class(self):
35    """Setup devices for tethering tests."""
36
37    self.tmobile_device = self.android_devices[0]  # T-mobile SIM, only IPv6
38    self.verizon_device = self.android_devices[1]  # Verizon SIM, IPv4 & IPv6
39    req_params = ("url",)
40    self.unpack_userparams(req_params)
41
42    nutils.verify_lte_data_and_tethering_supported(self.tmobile_device)
43    nutils.verify_lte_data_and_tethering_supported(self.verizon_device)
44
45  def setup_test(self):
46    """Setup devices for tethering before each test."""
47    setup_multiple_devices_for_bt_test([self.tmobile_device,
48                                        self.verizon_device])
49
50  def on_fail(self, test_name, begin_time):
51    self.tmobile_device.take_bug_report(test_name, begin_time)
52    self.verizon_device.take_bug_report(test_name, begin_time)
53
54  def _verify_ipv6_tethering(self, dut):
55    """Verify IPv6 tethering.
56
57    Args:
58      dut: Android device that is being checked
59    """
60
61    # httpRequestString() returns the IP address when visiting the URL
62    http_response = dut.droid.httpRequestString(self.url)
63    self.log.info("IP address %s " % http_response)
64    active_link_addrs = dut.droid.connectivityGetAllAddressesOfActiveLink()
65
66    if dut == self.hotspot_device and nutils.carrier_supports_ipv6(dut) \
67      or nutils.supports_ipv6_tethering(self.hotspot_device):
68      asserts.assert_true(nutils.is_ipaddress_ipv6(http_response),
69                          "The http response did not return IPv6 address")
70      asserts.assert_true(
71          active_link_addrs and http_response in str(active_link_addrs),
72          "Could not find IPv6 address in link properties")
73      asserts.assert_true(
74          dut.droid.connectivityHasIPv6DefaultRoute(),
75          "Could not find IPv6 default route in link properties")
76    else:
77      asserts.assert_false(dut.droid.connectivityHasIPv6DefaultRoute(),
78                           "Found IPv6 default route in link properties")
79
80  def _bluetooth_tethering_then_disconnect(self, pan_ad, panu_ad):
81    """Test bluetooth PAN tethering connection then disconnect service.
82
83    Test basic PAN tethering connection between two devices then disconnect
84    service.
85
86    Steps:
87    1. Enable data connection on PAN device
88    2. Disable data connection on PANU device
89    (steps 3-7: orchestrate_and_verify_pan_connection())
90    3. Enable Airplane mode on PANU device. Enable Bluetooth only.
91    4. Enable Bluetooth tethering on PAN Service device.
92    5. Pair the PAN Service device to the PANU device.
93    6. Verify that Bluetooth tethering is enabled on PAN Service device.
94    7. Verify HTTP connection on PANU device.
95    8. Disable Bluetooth tethering on PAN Service device.
96    9. Verify no HTTP connection on PANU device.
97
98    Args:
99      pan_ad: Android device providing tethering via Bluetooth
100      panu_ad: Android device receiving tethering via Bluetooth
101
102    Expected Result:
103    PANU device has internet access only with Bluetooth tethering
104
105    Returns:
106      Pass if True
107      Fail if False
108    """
109
110    if not wait_for_cell_data_connection(self.log, pan_ad, True):
111      self.log.error("Failed to enable data connection.")
112      return False
113
114    panu_ad.droid.telephonyToggleDataConnection(False)
115    if not wait_for_cell_data_connection(self.log, panu_ad, False):
116      self.log.error("Failed to disable data connection.")
117      return False
118
119    if not orchestrate_and_verify_pan_connection(pan_ad, panu_ad):
120      self.log.error("Could not establish a PAN connection.")
121      return False
122    internet = wutils.validate_connection(panu_ad, DEFAULT_PING_URL)
123    if not internet:
124      self.log.error("Internet is not connected with Bluetooth")
125      return False
126
127    # disable bluetooth tethering and verify internet is not working
128    internet = None
129    pan_ad.droid.bluetoothPanSetBluetoothTethering(False)
130    try:
131      internet = wutils.validate_connection(panu_ad, DEFAULT_PING_URL)
132    except Exception as e:
133      self.log.error(e)
134    if internet:
135      self.log.error("Internet is working without Bluetooth tethering")
136      return False
137
138    return True
139
140  def _do_bluetooth_tethering_then_disconnect(self, hotspot_device,
141                                              tethered_device):
142    """Test bluetooth tethering.
143
144    Steps:
145    1. Enables Data Connection on hotspot device.
146    2. Verifies IPv6 tethering is supported
147    3. Execute Bluetooth tethering test
148
149    Args:
150      hotspot_device: device providing internet service
151      tethered_device: device receiving internet service
152
153    Returns:
154      True: if tethering test is successful
155      False: otherwise
156    """
157
158    self.hotspot_device = hotspot_device
159    wutils.wifi_toggle_state(self.hotspot_device, False)
160    wutils.wifi_toggle_state(tethered_device, False)
161    self.hotspot_device.droid.telephonyToggleDataConnection(True)
162    time.sleep(20)  # allowing time for Data Connection to stabilize
163    operator = nutils.get_operator_name(self.log, self.hotspot_device)
164    self.log.info("Carrier is %s" % operator)
165    self._verify_ipv6_tethering(self.hotspot_device)
166    return self._bluetooth_tethering_then_disconnect(self.hotspot_device,
167                                                     tethered_device)
168
169  @test_tracker_info(uuid="433c7b62-3a60-4cae-8f3b-446d60c3ee9a")
170  def test_bluetooth_tethering_then_disconnect_source_verizon(self):
171    """Test bluetooth tethering from Verizon SIM."""
172
173    return self._do_bluetooth_tethering_then_disconnect(self.verizon_device,
174                                                        self.tmobile_device)
175
176  @test_tracker_info(uuid="e12fa5a5-f2c6-49e3-b255-f007be6319b0")
177  def test_bluetooth_tethering_then_disconnect_source_tmobile(self):
178    """Test bluetooth tethering from T-Mobile SIM."""
179
180    return self._do_bluetooth_tethering_then_disconnect(self.tmobile_device,
181                                                        self.verizon_device)
182
183