Home | History | Annotate | Download | only in system_tests
      1 #/usr/bin/env python3.4
      2 #
      3 # Copyright (C) 2016 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 """
     17 Test script to execute Bluetooth basic functionality test cases.
     18 This test was designed to be run in a shield box.
     19 """
     20 
     21 import time
     22 from random import randint
     23 
     24 from queue import Empty
     25 from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
     26 from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest
     27 from acts.test_utils.bt.bt_test_utils import orchestrate_rfcomm_connection
     28 from acts.test_utils.bt.bt_test_utils import write_read_verify_data
     29 
     30 
     31 class RfcommLongevityTest(BluetoothBaseTest):
     32     default_timeout = 10
     33     longev_iterations = 200
     34     write_iterations = 5000
     35     generic_message = (
     36         "Space: the final frontier. These are the voyages of "
     37         "the starship Enterprise. Its continuing mission: to explore "
     38         "strange new worlds, to seek out new life and new civilizations,"
     39         " to boldly go where no man has gone before.")
     40 
     41     def __init__(self, controllers):
     42         BluetoothBaseTest.__init__(self, controllers)
     43         self.client_ad = self.android_devices[0]
     44         self.server_ad = self.android_devices[1]
     45 
     46     def test_rfcomm_longev_read_write_message(self):
     47         """Longevity test an RFCOMM connection's I/O with a generic message
     48 
     49         Test the longevity of RFCOMM with a basic read/write
     50         connect/disconnect sequence.
     51 
     52         Steps:
     53         1. Establish a bonding between two Android devices.
     54         2. Write data to RFCOMM from the client droid.
     55         3. Read data from RFCOMM from the server droid.
     56         4. Verify data written matches data read.
     57         5. Repeat steps 2-4 5000 times.
     58         6. Disconnect RFCOMM connection.
     59         7. Repeat steps 1-6 1000 times.
     60 
     61         Expected Result:
     62         Each iteration should read and write to the RFCOMM connection
     63         successfully. Each connect and disconnect should be successful.
     64 
     65         Returns:
     66           Pass if True
     67           Fail if False
     68 
     69         TAGS: Classic, Longevity, RFCOMM
     70         Priority: 2
     71         """
     72 
     73         for i in range(self.longev_iterations):
     74             self.log.info("iteration {} connection".format(i+1))
     75             if not orchestrate_rfcomm_connection(self.client_ad,
     76                                                  self.server_ad):
     77                 return False
     78             for n in range(self.write_iterations):
     79                 self.log.info("iteration {} data".format(((n + 1) + (
     80                     i * self.write_iterations))))
     81                 if not write_read_verify_data(self.client_ad, self.server_ad,
     82                                               self.generic_message, False):
     83                     return False
     84                 self.log.info("Iteration {} completed".format(n))
     85             self.client_ad.droid.bluetoothRfcommStop()
     86             self.server_ad.droid.bluetoothRfcommStop()
     87         return True
     88 
     89     def test_rfcomm_longev_read_write_small_message(self):
     90         """Longevity test an RFCOMM connection's I/O with a small message
     91 
     92         Test the longevity of RFCOMM with a basic read/write
     93         connect/disconnect sequence. The data being transfered is only
     94         one character in size.
     95 
     96         Steps:
     97         1. Establish a bonding between two Android devices.
     98         2. Write data to RFCOMM from the client droid.
     99         3. Read data from RFCOMM from the server droid.
    100         4. Verify data written matches data read.
    101         5. Repeat steps 2-4 5000 times.
    102         6. Disconnect RFCOMM connection.
    103         7. Repeat steps 1-6 1000 times.
    104 
    105         Expected Result:
    106         Each iteration should read and write to the RFCOMM connection
    107         successfully. Each connect and disconnect should be successful.
    108 
    109         Returns:
    110           Pass if True
    111           Fail if False
    112 
    113         TAGS: Classic, Longevity, RFCOMM
    114         Priority: 2
    115         """
    116         message = "x"
    117         for i in range(self.longev_iterations):
    118             self.log.info("iteration {} connection".format(i+1))
    119             if not orchestrate_rfcomm_connection(self.client_ad,
    120                                                  self.server_ad):
    121                 return False
    122             for n in range(self.write_iterations):
    123                 self.log.info("iteration {} data".format(((n + 1) + (
    124                     i * self.write_iterations))))
    125                 if not write_read_verify_data(self.client_ad, self.server_ad,
    126                                               message, False):
    127                     return False
    128                 self.log.info("Iteration {} completed".format(n))
    129             self.client_ad.droid.bluetoothRfcommStop()
    130             self.server_ad.droid.bluetoothRfcommStop()
    131         return True
    132 
    133     def test_rfcomm_longev_read_write_binary_message(self):
    134         """Longevity test an RFCOMM connection's I/O with a binary message
    135 
    136         Test the longevity of RFCOMM with a basic read/write
    137         connect/disconnect sequence. The data being transfered is in a
    138         binary format.
    139 
    140         Steps:
    141         1. Establish a bonding between two Android devices.
    142         2. Write data to RFCOMM from the client droid.
    143         3. Read data from RFCOMM from the server droid.
    144         4. Verify data written matches data read.
    145         5. Repeat steps 2-4 5000 times.
    146         6. Disconnect RFCOMM connection.
    147         7. Repeat steps 1-6 1000 times.
    148 
    149         Expected Result:
    150         Each iteration should read and write to the RFCOMM connection
    151         successfully. Each connect and disconnect should be successful.
    152 
    153         Returns:
    154           Pass if True
    155           Fail if False
    156 
    157         TAGS: Classic, Longevity, RFCOMM
    158         Priority: 2
    159         """
    160         binary_message = "11010101"
    161         for i in range(self.longev_iterations):
    162             self.log.info("iteration {} connection".format(i+1))
    163             if not orchestrate_rfcomm_connection(self.client_ad,
    164                                                  self.server_ad):
    165                 return False
    166             for n in range(self.write_iterations):
    167                 self.log.info("iteration {} data".format(((n + 1) + (
    168                     i * self.write_iterations))))
    169                 if not write_read_verify_data(self.client_ad, self.server_ad,
    170                                               binary_message, True):
    171                     return False
    172                 self.log.info("Iteration {} completed".format(n))
    173             self.client_ad.droid.bluetoothRfcommStop()
    174             self.server_ad.droid.bluetoothRfcommStop()
    175         return True
    176 
    177     def test_rfcomm_longev_read_write_large_message(self):
    178         """Longevity test an RFCOMM connection's I/O with a large message
    179 
    180         Test the longevity of RFCOMM with a basic read/write
    181         connect/disconnect sequence. The data being transfered is 990 chars
    182         in size.
    183 
    184         Steps:
    185         1. Establish a bonding between two Android devices.
    186         2. Write data to RFCOMM from the client droid.
    187         3. Read data from RFCOMM from the server droid.
    188         4. Verify data written matches data read.
    189         5. Repeat steps 2-4 5000 times.
    190         6. Disconnect RFCOMM connection.
    191         7. Repeat steps 1-6 1000 times.
    192 
    193         Expected Result:
    194         Each iteration should read and write to the RFCOMM connection
    195         successfully. Each connect and disconnect should be successful.
    196 
    197         Returns:
    198           Pass if True
    199           Fail if False
    200 
    201         TAGS: Classic, Longevity, RFCOMM
    202         Priority: 2
    203         """
    204         message = "x" * 990  #largest message size till sl4a fixed
    205         for i in range(self.longev_iterations):
    206             self.log.info("iteration {} connection".format(i+1))
    207             if not orchestrate_rfcomm_connection(self.client_ad,
    208                                                  self.server_ad):
    209                 return False
    210             for n in range(self.write_iterations):
    211                 self.log.info("iteration {} data".format(((n + 1) + (
    212                     i * self.write_iterations))))
    213                 if not write_read_verify_data(self.client_ad, self.server_ad,
    214                                               message, False):
    215                     return False
    216                 self.log.info("Iteration {} completed".format(n))
    217             self.client_ad.droid.bluetoothRfcommStop()
    218             self.server_ad.droid.bluetoothRfcommStop()
    219         return True
    220 
    221     def test_rfcomm_longev_connection_interuption(self):
    222         """Longevity test an RFCOMM connection's with socket interuptions
    223 
    224         Test the longevity of RFCOMM with a basic read/write
    225         connect/disconnect sequence. Randomly in the sequence of reads and
    226         writes the socket on the client side will close. There should be
    227         an exception thrown for writing the next set of data and the
    228         test should start up a new connection and continue.
    229 
    230         Steps:
    231         1. Establish a bonding between two Android devices.
    232         2. Write data to RFCOMM from the client droid.
    233         3. Read data from RFCOMM from the server droid.
    234         4. Verify data written matches data read.
    235         5. Repeat steps 2-4 5000 times or until the random interupt occurs.
    236         6. Re-establish an RFCOMM connection.
    237         7. Repeat steps 1-6 1000 times.
    238 
    239         Expected Result:
    240         Each iteration should read and write to the RFCOMM connection
    241         successfully. Each connect and disconnect should be successful.
    242         Devices should recover a new connection after each interruption.
    243 
    244         Returns:
    245           Pass if True
    246           Fail if False
    247 
    248         TAGS: Classic, Longevity, RFCOMM
    249         Priority: 2
    250         """
    251         for i in range(self.longev_iterations):
    252             try:
    253                 self.log.info("iteration {} connection".format(i+1))
    254                 if not orchestrate_rfcomm_connection(self.client_ad,
    255                                                      self.server_ad):
    256                     return False
    257                 random_interup_iteration = randint(0, self.write_iterations)
    258                 for n in range(self.write_iterations):
    259                     self.log.info("iteration {} data".format(((n + 1) + (
    260                         i * self.write_iterations))))
    261                     if not write_read_verify_data(self.client_ad,
    262                                                   self.server_ad,
    263                                                   self.generic_message, False):
    264                         return False
    265                     self.log.info("Iteration {} completed".format(n))
    266                     if n > random_interup_iteration:
    267                         self.client_ad.droid.bluetoothRfcommCloseSocket()
    268                 self.client_ad.droid.bluetoothRfcommStop()
    269                 self.server_ad.droid.bluetoothRfcommStop()
    270             except Exception:
    271                 self.log.info("Exception found as expected. Continuing...")
    272                 try:
    273                     self.client_ad.droid.bluetoothRfcommStop()
    274                 except Exception as err:
    275                     self.log.error(
    276                         "Error closing client connection: {}".format(err))
    277                     return False
    278                 try:
    279                     self.server_ad.droid.bluetoothRfcommStop()
    280                 except Exception as err:
    281                     self.log.error(
    282                         "Error closing server connection: {}".format(err))
    283                     return False
    284         return True
    285 
    286     def test_rfcomm_longev_data_elasticity(self):
    287         """Longevity test an RFCOMM connection's I/O with changing data size
    288 
    289         Test the longevity of RFCOMM with a basic read/write
    290         connect/disconnect sequence. The data being transfered changes
    291         in size after each write/read sequence to increase up to 990
    292         chars in size and decrease down to 1 in size. This repeats through
    293         the entire test in order to exercise different size values being
    294         written.
    295 
    296         Steps:
    297         1. Establish a bonding between two Android devices.
    298         2. Write data to RFCOMM from the client droid.
    299         3. Read data from RFCOMM from the server droid.
    300         4. Verify data written matches data read.
    301         5. Change data size according to above description.
    302         6. Repeat steps 2-5 5000 times.
    303         7. Disconnect RFCOMM connection.
    304         8. Repeat steps 1-6 1000 times.
    305 
    306         Expected Result:
    307         Each iteration should read and write to the RFCOMM connection
    308         successfully. Each connect and disconnect should be successful.
    309 
    310         Returns:
    311           Pass if True
    312           Fail if False
    313 
    314         TAGS: Classic, Longevity, RFCOMM
    315         Priority: 2
    316         """
    317         message = "x"
    318         resize_toggle = 1
    319         for i in range(self.longev_iterations):
    320             try:
    321                 self.log.info("iteration {} connection".format(i+1))
    322                 if not orchestrate_rfcomm_connection(self.client_ad,
    323                                                      self.server_ad):
    324                     return False
    325                 for n in range(self.write_iterations):
    326                     self.log.info("iteration {} data".format(((n + 1) + (
    327                         i * self.write_iterations))))
    328                     if not write_read_verify_data(
    329                             self.client_ad, self.server_ad, message, False):
    330                         return False
    331                     self.log.info("Iteration {} completed".format(n))
    332                     size_of_message = len(message)
    333                     #max size is 990 due to a bug in sl4a.
    334                     if size_of_message >= 990:
    335                         resize_toggle = 0
    336                     elif size_of_message <= 1:
    337                         resize_toggle = 1
    338                     if resize_toggle == 0:
    339                         message = "x" * (size_of_message - 1)
    340                     else:
    341                         message = "x" * (size_of_message + 1)
    342                 self.client_ad.droid.bluetoothRfcommStop()
    343                 self.server_ad.droid.bluetoothRfcommStop()
    344             except Exception as err:
    345                 self.log.info("Error in longevity test: {}".format(err))
    346                 return False
    347         return True
    348