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
5import filecmp
6import os
7import tempfile
8
9from autotest_lib.client.common_lib import error
10from autotest_lib.server import test
11
12
13_DATA_SIZE = 20 * 1000 * 1000
14_DATA_STR = ''.join(chr(i) for i in range(256))
15_DATA_STR *= (len(_DATA_STR) - 1 + _DATA_SIZE) / len(_DATA_STR)
16_DATA_STR = _DATA_STR[:_DATA_SIZE]
17
18
19class brillo_ADBFileTransfer(test.test):
20    """Verify that ADB file transfers work."""
21    version = 1
22
23
24    def setup(self):
25        self.temp_file = tempfile.NamedTemporaryFile()
26        self.temp_file.write(_DATA_STR)
27        self.temp_file.flush()
28        os.fsync(self.temp_file.file.fileno())
29
30
31    def run_once(self, host=None, iterations=10):
32        """
33        Conduct a file transfer and verify that it succeeds.
34
35        @param iterations: Number of times to perform the file transfer.
36        """
37
38        device_temp_file = os.path.join(host.get_tmp_dir(), 'adb_test_file')
39
40        while iterations:
41            iterations -= 1
42            with tempfile.NamedTemporaryFile() as return_temp_file:
43                host.send_file(self.temp_file.name, device_temp_file,
44                               delete_dest=True)
45                host.get_file(device_temp_file, return_temp_file.name,
46                              delete_dest=True)
47
48                if not filecmp.cmp(self.temp_file.name,
49                                   return_temp_file.name, shallow=False):
50                    raise error.TestFail('Got back different file than we sent')
51
52            r = host.run('cat %s' % device_temp_file)
53            if r.stdout != _DATA_STR:
54                raise error.TestFail('Cat did not return same contents we sent')
55
56
57    def cleanup(self):
58        self.temp_file.close()
59