OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 import os |
| 7 import sys |
| 8 import unittest |
| 9 |
| 10 from pylib import cmd_helper |
| 11 from pylib import constants |
| 12 from pylib.utils import md5sum |
| 13 |
| 14 sys.path.append( |
| 15 os.path.join(constants.DIR_SOURCE_ROOT, 'third_party', 'pymock')) |
| 16 import mock |
| 17 |
| 18 TEST_OUT_DIR = os.path.join('test', 'out', 'directory') |
| 19 HOST_MD5_EXECUTABLE = os.path.join(TEST_OUT_DIR, 'md5sum_bin_host') |
| 20 |
| 21 class Md5SumTest(unittest.TestCase): |
| 22 |
| 23 def setUp(self): |
| 24 self._patchers = [ |
| 25 mock.patch('pylib.constants.GetOutDirectory', |
| 26 new=mock.Mock(return_value=TEST_OUT_DIR)), |
| 27 ] |
| 28 for p in self._patchers: |
| 29 p.start() |
| 30 |
| 31 def tearDown(self): |
| 32 for p in self._patchers: |
| 33 p.stop() |
| 34 |
| 35 def testCalculateHostMd5Sums_singlePath(self): |
| 36 test_path = '/test/host/file.dat' |
| 37 mock_get_cmd_output = mock.Mock( |
| 38 return_value='0123456789abcdeffedcba9876543210 /test/host/file.dat') |
| 39 with mock.patch('pylib.cmd_helper.GetCmdOutput', new=mock_get_cmd_output): |
| 40 out = md5sum.CalculateHostMd5Sums(test_path) |
| 41 self.assertEquals(1, len(out)) |
| 42 self.assertEquals('0123456789abcdeffedcba9876543210', out[0].hash) |
| 43 self.assertEquals('/test/host/file.dat', out[0].path) |
| 44 mock_get_cmd_output.assert_called_once_with( |
| 45 [HOST_MD5_EXECUTABLE, '/test/host/file.dat']) |
| 46 |
| 47 def testCalculateHostMd5Sums_list(self): |
| 48 test_paths = ['/test/host/file0.dat', '/test/host/file1.dat'] |
| 49 mock_get_cmd_output = mock.Mock( |
| 50 return_value='0123456789abcdeffedcba9876543210 /test/host/file0.dat\n' |
| 51 '123456789abcdef00fedcba987654321 /test/host/file1.dat\n') |
| 52 with mock.patch('pylib.cmd_helper.GetCmdOutput', new=mock_get_cmd_output): |
| 53 out = md5sum.CalculateHostMd5Sums(test_paths) |
| 54 self.assertEquals(2, len(out)) |
| 55 self.assertEquals('0123456789abcdeffedcba9876543210', out[0].hash) |
| 56 self.assertEquals('/test/host/file0.dat', out[0].path) |
| 57 self.assertEquals('123456789abcdef00fedcba987654321', out[1].hash) |
| 58 self.assertEquals('/test/host/file1.dat', out[1].path) |
| 59 mock_get_cmd_output.assert_called_once_with( |
| 60 [HOST_MD5_EXECUTABLE, '/test/host/file0.dat', |
| 61 '/test/host/file1.dat']) |
| 62 |
| 63 def testCalculateHostMd5Sums_generator(self): |
| 64 test_paths = ('/test/host/' + p for p in ['file0.dat', 'file1.dat']) |
| 65 mock_get_cmd_output = mock.Mock( |
| 66 return_value='0123456789abcdeffedcba9876543210 /test/host/file0.dat\n' |
| 67 '123456789abcdef00fedcba987654321 /test/host/file1.dat\n') |
| 68 with mock.patch('pylib.cmd_helper.GetCmdOutput', new=mock_get_cmd_output): |
| 69 out = md5sum.CalculateHostMd5Sums(test_paths) |
| 70 self.assertEquals(2, len(out)) |
| 71 self.assertEquals('0123456789abcdeffedcba9876543210', out[0].hash) |
| 72 self.assertEquals('/test/host/file0.dat', out[0].path) |
| 73 self.assertEquals('123456789abcdef00fedcba987654321', out[1].hash) |
| 74 self.assertEquals('/test/host/file1.dat', out[1].path) |
| 75 mock_get_cmd_output.assert_called_once_with( |
| 76 [HOST_MD5_EXECUTABLE, '/test/host/file0.dat', '/test/host/file1.dat']) |
| 77 |
| 78 def testCalculateDeviceMd5Sums_singlePath(self): |
| 79 test_path = '/storage/emulated/legacy/test/file.dat' |
| 80 |
| 81 device = mock.NonCallableMock() |
| 82 device.adb = mock.NonCallableMock() |
| 83 device.adb.Push = mock.Mock() |
| 84 device_md5sum_output = [ |
| 85 '0123456789abcdeffedcba9876543210 ' |
| 86 '/storage/emulated/legacy/test/file.dat', |
| 87 ] |
| 88 device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) |
| 89 |
| 90 mock_temp_file = mock.mock_open() |
| 91 mock_temp_file.return_value.name = '/tmp/test/script/file.sh' |
| 92 |
| 93 mock_device_temp_file = mock.mock_open() |
| 94 mock_device_temp_file.return_value.name = ( |
| 95 '/data/local/tmp/test/script/file.sh') |
| 96 |
| 97 with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( |
| 98 mock.patch('pylib.utils.device_temp_file.DeviceTempFile', |
| 99 new=mock_device_temp_file)): |
| 100 out = md5sum.CalculateDeviceMd5Sums(test_path, device) |
| 101 self.assertEquals(1, len(out)) |
| 102 self.assertEquals('0123456789abcdeffedcba9876543210', out[0].hash) |
| 103 self.assertEquals('/storage/emulated/legacy/test/file.dat', out[0].path) |
| 104 device.adb.Push.assert_called_once_with( |
| 105 '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') |
| 106 device.RunShellCommand.assert_called_once_with( |
| 107 ['sh', '/data/local/tmp/test/script/file.sh']) |
| 108 |
| 109 def testCalculateDeviceMd5Sums_list(self): |
| 110 test_path = ['/storage/emulated/legacy/test/file0.dat', |
| 111 '/storage/emulated/legacy/test/file1.dat'] |
| 112 device = mock.NonCallableMock() |
| 113 device.adb = mock.NonCallableMock() |
| 114 device.adb.Push = mock.Mock() |
| 115 device_md5sum_output = [ |
| 116 '0123456789abcdeffedcba9876543210 ' |
| 117 '/storage/emulated/legacy/test/file0.dat', |
| 118 '123456789abcdef00fedcba987654321 ' |
| 119 '/storage/emulated/legacy/test/file1.dat', |
| 120 ] |
| 121 device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) |
| 122 |
| 123 mock_temp_file = mock.mock_open() |
| 124 mock_temp_file.return_value.name = '/tmp/test/script/file.sh' |
| 125 |
| 126 mock_device_temp_file = mock.mock_open() |
| 127 mock_device_temp_file.return_value.name = ( |
| 128 '/data/local/tmp/test/script/file.sh') |
| 129 |
| 130 with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( |
| 131 mock.patch('pylib.utils.device_temp_file.DeviceTempFile', |
| 132 new=mock_device_temp_file)): |
| 133 out = md5sum.CalculateDeviceMd5Sums(test_path, device) |
| 134 self.assertEquals(2, len(out)) |
| 135 self.assertEquals('0123456789abcdeffedcba9876543210', out[0].hash) |
| 136 self.assertEquals('/storage/emulated/legacy/test/file0.dat', out[0].path) |
| 137 self.assertEquals('123456789abcdef00fedcba987654321', out[1].hash) |
| 138 self.assertEquals('/storage/emulated/legacy/test/file1.dat', out[1].path) |
| 139 device.adb.Push.assert_called_once_with( |
| 140 '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') |
| 141 device.RunShellCommand.assert_called_once_with( |
| 142 ['sh', '/data/local/tmp/test/script/file.sh']) |
| 143 |
| 144 def testCalculateDeviceMd5Sums_generator(self): |
| 145 test_path = ('/storage/emulated/legacy/test/file%d.dat' % n |
| 146 for n in xrange(0, 2)) |
| 147 |
| 148 device = mock.NonCallableMock() |
| 149 device.adb = mock.NonCallableMock() |
| 150 device.adb.Push = mock.Mock() |
| 151 device_md5sum_output = [ |
| 152 '0123456789abcdeffedcba9876543210 ' |
| 153 '/storage/emulated/legacy/test/file0.dat', |
| 154 '123456789abcdef00fedcba987654321 ' |
| 155 '/storage/emulated/legacy/test/file1.dat', |
| 156 ] |
| 157 device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) |
| 158 |
| 159 mock_temp_file = mock.mock_open() |
| 160 mock_temp_file.return_value.name = '/tmp/test/script/file.sh' |
| 161 |
| 162 mock_device_temp_file = mock.mock_open() |
| 163 mock_device_temp_file.return_value.name = ( |
| 164 '/data/local/tmp/test/script/file.sh') |
| 165 |
| 166 with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( |
| 167 mock.patch('pylib.utils.device_temp_file.DeviceTempFile', |
| 168 new=mock_device_temp_file)): |
| 169 out = md5sum.CalculateDeviceMd5Sums(test_path, device) |
| 170 self.assertEquals(2, len(out)) |
| 171 self.assertEquals('0123456789abcdeffedcba9876543210', out[0].hash) |
| 172 self.assertEquals('/storage/emulated/legacy/test/file0.dat', out[0].path) |
| 173 self.assertEquals('123456789abcdef00fedcba987654321', out[1].hash) |
| 174 self.assertEquals('/storage/emulated/legacy/test/file1.dat', out[1].path) |
| 175 device.adb.Push.assert_called_once_with( |
| 176 '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') |
| 177 device.RunShellCommand.assert_called_once_with( |
| 178 ['sh', '/data/local/tmp/test/script/file.sh']) |
| 179 |
| 180 |
| 181 if __name__ == '__main__': |
| 182 unittest.main(verbosity=2) |
| 183 |
OLD | NEW |