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 mock.patch('os.path.exists', |
| 28 new=mock.Mock(return_value=True)), |
| 29 ] |
| 30 for p in self._patchers: |
| 31 p.start() |
| 32 |
| 33 def tearDown(self): |
| 34 for p in self._patchers: |
| 35 p.stop() |
| 36 |
| 37 def testCalculateHostMd5Sums_singlePath(self): |
| 38 test_path = '/test/host/file.dat' |
| 39 mock_get_cmd_output = mock.Mock( |
| 40 return_value='0123456789abcdeffedcba9876543210 /test/host/file.dat') |
| 41 with mock.patch('pylib.cmd_helper.GetCmdOutput', new=mock_get_cmd_output): |
| 42 out = md5sum.CalculateHostMd5Sums(test_path) |
| 43 self.assertEquals(1, len(out)) |
| 44 self.assertTrue('/test/host/file.dat' in out) |
| 45 self.assertEquals('0123456789abcdeffedcba9876543210', |
| 46 out['/test/host/file.dat']) |
| 47 mock_get_cmd_output.assert_called_once_with( |
| 48 [HOST_MD5_EXECUTABLE, '/test/host/file.dat']) |
| 49 |
| 50 def testCalculateHostMd5Sums_list(self): |
| 51 test_paths = ['/test/host/file0.dat', '/test/host/file1.dat'] |
| 52 mock_get_cmd_output = mock.Mock( |
| 53 return_value='0123456789abcdeffedcba9876543210 /test/host/file0.dat\n' |
| 54 '123456789abcdef00fedcba987654321 /test/host/file1.dat\n') |
| 55 with mock.patch('pylib.cmd_helper.GetCmdOutput', new=mock_get_cmd_output): |
| 56 out = md5sum.CalculateHostMd5Sums(test_paths) |
| 57 self.assertEquals(2, len(out)) |
| 58 self.assertTrue('/test/host/file0.dat' in out) |
| 59 self.assertEquals('0123456789abcdeffedcba9876543210', |
| 60 out['/test/host/file0.dat']) |
| 61 self.assertTrue('/test/host/file1.dat' in out) |
| 62 self.assertEquals('123456789abcdef00fedcba987654321', |
| 63 out['/test/host/file1.dat']) |
| 64 mock_get_cmd_output.assert_called_once_with( |
| 65 [HOST_MD5_EXECUTABLE, '/test/host/file0.dat', |
| 66 '/test/host/file1.dat']) |
| 67 |
| 68 def testCalculateHostMd5Sums_generator(self): |
| 69 test_paths = ('/test/host/' + p for p in ['file0.dat', 'file1.dat']) |
| 70 mock_get_cmd_output = mock.Mock( |
| 71 return_value='0123456789abcdeffedcba9876543210 /test/host/file0.dat\n' |
| 72 '123456789abcdef00fedcba987654321 /test/host/file1.dat\n') |
| 73 with mock.patch('pylib.cmd_helper.GetCmdOutput', new=mock_get_cmd_output): |
| 74 out = md5sum.CalculateHostMd5Sums(test_paths) |
| 75 self.assertEquals(2, len(out)) |
| 76 self.assertTrue('/test/host/file0.dat' in out) |
| 77 self.assertEquals('0123456789abcdeffedcba9876543210', |
| 78 out['/test/host/file0.dat']) |
| 79 self.assertTrue('/test/host/file1.dat' in out) |
| 80 self.assertEquals('123456789abcdef00fedcba987654321', |
| 81 out['/test/host/file1.dat']) |
| 82 mock_get_cmd_output.assert_called_once_with( |
| 83 [HOST_MD5_EXECUTABLE, '/test/host/file0.dat', '/test/host/file1.dat']) |
| 84 |
| 85 def testCalculateDeviceMd5Sums_singlePath(self): |
| 86 test_path = '/storage/emulated/legacy/test/file.dat' |
| 87 |
| 88 device = mock.NonCallableMock() |
| 89 device.adb = mock.NonCallableMock() |
| 90 device.adb.Push = mock.Mock() |
| 91 device_md5sum_output = [ |
| 92 '0123456789abcdeffedcba9876543210 ' |
| 93 '/storage/emulated/legacy/test/file.dat', |
| 94 ] |
| 95 device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) |
| 96 |
| 97 mock_temp_file = mock.mock_open() |
| 98 mock_temp_file.return_value.name = '/tmp/test/script/file.sh' |
| 99 |
| 100 mock_device_temp_file = mock.mock_open() |
| 101 mock_device_temp_file.return_value.name = ( |
| 102 '/data/local/tmp/test/script/file.sh') |
| 103 |
| 104 with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( |
| 105 mock.patch('pylib.utils.device_temp_file.DeviceTempFile', |
| 106 new=mock_device_temp_file)): |
| 107 out = md5sum.CalculateDeviceMd5Sums(test_path, device) |
| 108 self.assertEquals(1, len(out)) |
| 109 self.assertTrue('/storage/emulated/legacy/test/file.dat' in out) |
| 110 self.assertEquals('0123456789abcdeffedcba9876543210', |
| 111 out['/storage/emulated/legacy/test/file.dat']) |
| 112 device.adb.Push.assert_called_once_with( |
| 113 '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') |
| 114 device.RunShellCommand.assert_called_once_with( |
| 115 ['sh', '/data/local/tmp/test/script/file.sh']) |
| 116 |
| 117 def testCalculateDeviceMd5Sums_list(self): |
| 118 test_path = ['/storage/emulated/legacy/test/file0.dat', |
| 119 '/storage/emulated/legacy/test/file1.dat'] |
| 120 device = mock.NonCallableMock() |
| 121 device.adb = mock.NonCallableMock() |
| 122 device.adb.Push = mock.Mock() |
| 123 device_md5sum_output = [ |
| 124 '0123456789abcdeffedcba9876543210 ' |
| 125 '/storage/emulated/legacy/test/file0.dat', |
| 126 '123456789abcdef00fedcba987654321 ' |
| 127 '/storage/emulated/legacy/test/file1.dat', |
| 128 ] |
| 129 device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) |
| 130 |
| 131 mock_temp_file = mock.mock_open() |
| 132 mock_temp_file.return_value.name = '/tmp/test/script/file.sh' |
| 133 |
| 134 mock_device_temp_file = mock.mock_open() |
| 135 mock_device_temp_file.return_value.name = ( |
| 136 '/data/local/tmp/test/script/file.sh') |
| 137 |
| 138 with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( |
| 139 mock.patch('pylib.utils.device_temp_file.DeviceTempFile', |
| 140 new=mock_device_temp_file)): |
| 141 out = md5sum.CalculateDeviceMd5Sums(test_path, device) |
| 142 self.assertEquals(2, len(out)) |
| 143 self.assertTrue('/storage/emulated/legacy/test/file0.dat' in out) |
| 144 self.assertEquals('0123456789abcdeffedcba9876543210', |
| 145 out['/storage/emulated/legacy/test/file0.dat']) |
| 146 self.assertTrue('/storage/emulated/legacy/test/file1.dat' in out) |
| 147 self.assertEquals('123456789abcdef00fedcba987654321', |
| 148 out['/storage/emulated/legacy/test/file1.dat']) |
| 149 device.adb.Push.assert_called_once_with( |
| 150 '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') |
| 151 device.RunShellCommand.assert_called_once_with( |
| 152 ['sh', '/data/local/tmp/test/script/file.sh']) |
| 153 |
| 154 def testCalculateDeviceMd5Sums_generator(self): |
| 155 test_path = ('/storage/emulated/legacy/test/file%d.dat' % n |
| 156 for n in xrange(0, 2)) |
| 157 |
| 158 device = mock.NonCallableMock() |
| 159 device.adb = mock.NonCallableMock() |
| 160 device.adb.Push = mock.Mock() |
| 161 device_md5sum_output = [ |
| 162 '0123456789abcdeffedcba9876543210 ' |
| 163 '/storage/emulated/legacy/test/file0.dat', |
| 164 '123456789abcdef00fedcba987654321 ' |
| 165 '/storage/emulated/legacy/test/file1.dat', |
| 166 ] |
| 167 device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) |
| 168 |
| 169 mock_temp_file = mock.mock_open() |
| 170 mock_temp_file.return_value.name = '/tmp/test/script/file.sh' |
| 171 |
| 172 mock_device_temp_file = mock.mock_open() |
| 173 mock_device_temp_file.return_value.name = ( |
| 174 '/data/local/tmp/test/script/file.sh') |
| 175 |
| 176 with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( |
| 177 mock.patch('pylib.utils.device_temp_file.DeviceTempFile', |
| 178 new=mock_device_temp_file)): |
| 179 out = md5sum.CalculateDeviceMd5Sums(test_path, device) |
| 180 self.assertEquals(2, len(out)) |
| 181 self.assertTrue('/storage/emulated/legacy/test/file0.dat' in out) |
| 182 self.assertEquals('0123456789abcdeffedcba9876543210', |
| 183 out['/storage/emulated/legacy/test/file0.dat']) |
| 184 self.assertTrue('/storage/emulated/legacy/test/file1.dat' in out) |
| 185 self.assertEquals('123456789abcdef00fedcba987654321', |
| 186 out['/storage/emulated/legacy/test/file1.dat']) |
| 187 device.adb.Push.assert_called_once_with( |
| 188 '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') |
| 189 device.RunShellCommand.assert_called_once_with( |
| 190 ['sh', '/data/local/tmp/test/script/file.sh']) |
| 191 |
| 192 def testCalculateDeviceMd5Sums_singlePath_linkerWarning(self): |
| 193 # See crbug/479966 |
| 194 test_path = '/storage/emulated/legacy/test/file.dat' |
| 195 |
| 196 device = mock.NonCallableMock() |
| 197 device.adb = mock.NonCallableMock() |
| 198 device.adb.Push = mock.Mock() |
| 199 device_md5sum_output = [ |
| 200 'WARNING: linker: /data/local/tmp/md5sum/md5sum_bin: ' |
| 201 'unused DT entry: type 0x1d arg 0x15db', |
| 202 'THIS_IS_NOT_A_VALID_CHECKSUM_ZZZ some random text', |
| 203 '0123456789abcdeffedcba9876543210 ' |
| 204 '/storage/emulated/legacy/test/file.dat', |
| 205 ] |
| 206 device.RunShellCommand = mock.Mock(return_value=device_md5sum_output) |
| 207 |
| 208 mock_temp_file = mock.mock_open() |
| 209 mock_temp_file.return_value.name = '/tmp/test/script/file.sh' |
| 210 |
| 211 mock_device_temp_file = mock.mock_open() |
| 212 mock_device_temp_file.return_value.name = ( |
| 213 '/data/local/tmp/test/script/file.sh') |
| 214 |
| 215 with mock.patch('tempfile.NamedTemporaryFile', new=mock_temp_file), ( |
| 216 mock.patch('pylib.utils.device_temp_file.DeviceTempFile', |
| 217 new=mock_device_temp_file)): |
| 218 out = md5sum.CalculateDeviceMd5Sums(test_path, device) |
| 219 self.assertEquals(1, len(out)) |
| 220 self.assertTrue('/storage/emulated/legacy/test/file.dat' in out) |
| 221 self.assertEquals('0123456789abcdeffedcba9876543210', |
| 222 out['/storage/emulated/legacy/test/file.dat']) |
| 223 device.adb.Push.assert_called_once_with( |
| 224 '/tmp/test/script/file.sh', '/data/local/tmp/test/script/file.sh') |
| 225 device.RunShellCommand.assert_called_once_with( |
| 226 ['sh', '/data/local/tmp/test/script/file.sh']) |
| 227 |
| 228 |
| 229 if __name__ == '__main__': |
| 230 unittest.main(verbosity=2) |
| 231 |
OLD | NEW |