1 # Copyright 2016 The Chromium OS Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import filecmp 6 import os 7 import shutil 8 import tempfile 9 10 from autotest_lib.client.common_lib import error 11 from autotest_lib.server import test 12 13 14 _DATA_STR_A = 'Alluminum, linoleum, magnesium, petrolium.' 15 _DATA_STR_B = ('A basket of biscuits, a basket of mixed biscuits,' 16 'and a biscuit mixer.') 17 _DATA_STR_C = 'foo\nbar\nbaz' 18 19 20 class brillo_ADBDirectoryTransfer(test.test): 21 """Verify that ADB directory transfers work.""" 22 version = 1 23 24 25 def setup(self): 26 # Create a test directory tree to send and receive: 27 # test_dir/ 28 # file_a 29 # file_b 30 # test_subdir/ 31 # file_c 32 self.temp_dir = tempfile.mkdtemp() 33 self.test_dir = os.path.join(self.temp_dir, 'test_dir') 34 os.mkdir(self.test_dir) 35 os.mkdir(os.path.join(self.test_dir, 'subdir')) 36 37 with open(os.path.join(self.test_dir, 'file_a'), 'w') as f: 38 f.write(_DATA_STR_A) 39 40 with open(os.path.join(self.test_dir, 'file_b'), 'w') as f: 41 f.write(_DATA_STR_B) 42 43 with open(os.path.join(self.test_dir, 'subdir', 'file_c'), 'w') as f: 44 f.write(_DATA_STR_C) 45 46 47 def run_once(self, host=None): 48 """Body of the test.""" 49 device_temp_dir = host.get_tmp_dir() 50 device_test_dir = os.path.join(device_temp_dir, 'test_dir') 51 52 return_dir = os.path.join(self.temp_dir, 'return_dir') 53 return_test_dir = os.path.join(return_dir, 'test_dir') 54 55 # Copy test_dir to the device then back into return_dir. 56 host.send_file(self.test_dir, device_temp_dir, delete_dest=True) 57 host.get_file(device_test_dir, return_dir, delete_dest=True) 58 59 for path in ('file_a', 'file_b', os.path.join('subdir', 'file_c')): 60 original = os.path.join(self.test_dir, path) 61 returned = os.path.join(return_test_dir, path) 62 if not filecmp.cmp(original, returned, shallow=False): 63 raise error.TestFail(path + ' changed in transit') 64 65 66 def cleanup(self): 67 shutil.rmtree(self.temp_dir) 68