OLD | NEW |
| (Empty) |
1 # -*- coding: utf-8 -*- | |
2 # Copyright 2014 Google Inc. All Rights Reserved. | |
3 # | |
4 # Licensed under the Apache License, Version 2.0 (the "License"); | |
5 # you may not use this file except in compliance with the License. | |
6 # You may obtain a copy of the License at | |
7 # | |
8 # http://www.apache.org/licenses/LICENSE-2.0 | |
9 # | |
10 # Unless required by applicable law or agreed to in writing, software | |
11 # distributed under the License is distributed on an "AS IS" BASIS, | |
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 # See the License for the specific language governing permissions and | |
14 # limitations under the License. | |
15 """Unit tests for hash command.""" | |
16 | |
17 import os | |
18 | |
19 from gslib.exception import CommandException | |
20 import gslib.tests.testcase as testcase | |
21 | |
22 | |
23 class TestHash(testcase.GsUtilUnitTestCase): | |
24 """Unit tests for hash command.""" | |
25 | |
26 _TEST_FILE_CONTENTS = '123456\n' | |
27 _TEST_FILE_B64_CRC = 'nYmSiA==' | |
28 _TEST_FILE_B64_MD5 = '9EeyCn/L9TpdW+AT6gsVrw==' | |
29 _TEST_FILE_HEX_CRC = '9D899288' | |
30 _TEST_FILE_HEX_MD5 = 'f447b20a7fcbf53a5d5be013ea0b15af' | |
31 | |
32 def testHashContents(self): | |
33 tmp_file = self.CreateTempFile(contents=self._TEST_FILE_CONTENTS) | |
34 stdout = self.RunCommand('hash', args=[tmp_file], return_stdout=True) | |
35 self.assertIn('Hashes [base64]', stdout) | |
36 self.assertIn('\tHash (crc32c):\t\t%s' % self._TEST_FILE_B64_CRC, stdout) | |
37 self.assertIn('\tHash (md5):\t\t%s' % self._TEST_FILE_B64_MD5, stdout) | |
38 | |
39 def testHashNoMatch(self): | |
40 try: | |
41 self.RunCommand('hash', args=['non-existent-file']) | |
42 self.fail('Did not get expected CommandException') | |
43 except CommandException, e: | |
44 self.assertRaisesRegexp(e, r'No files matched') | |
45 | |
46 def testHashCloudObject(self): | |
47 try: | |
48 self.RunCommand('hash', args=['gs://bucket/object']) | |
49 self.fail('Did not get expected CommandException') | |
50 except CommandException, e: | |
51 self.assertEquals('"hash" command requires a file URL', e.reason) | |
52 | |
53 def testHashHexFormat(self): | |
54 tmp_file = self.CreateTempFile(contents=self._TEST_FILE_CONTENTS) | |
55 stdout = self.RunCommand('hash', args=['-h', tmp_file], return_stdout=True) | |
56 self.assertIn('Hashes [hex]', stdout) | |
57 self.assertIn('\tHash (crc32c):\t\t%s' % self._TEST_FILE_HEX_CRC, stdout) | |
58 self.assertIn('\tHash (md5):\t\t%s' % self._TEST_FILE_HEX_MD5, stdout) | |
59 | |
60 def testHashWildcard(self): | |
61 num_test_files = 2 | |
62 tmp_dir = self.CreateTempDir(test_files=num_test_files) | |
63 stdout = self.RunCommand('hash', args=[os.path.join(tmp_dir, '*')], | |
64 return_stdout=True) | |
65 # One summary line and two hash lines per file. | |
66 num_expected_lines = num_test_files * (1 + 2) | |
67 self.assertEquals(len(stdout.splitlines()), num_expected_lines) | |
68 | |
69 def testHashSelectAlg(self): | |
70 tmp_file = self.CreateTempFile(contents=self._TEST_FILE_CONTENTS) | |
71 stdout_crc = self.RunCommand('hash', args=['-c', tmp_file], | |
72 return_stdout=True) | |
73 stdout_md5 = self.RunCommand('hash', args=['-m', tmp_file], | |
74 return_stdout=True) | |
75 stdout_both = self.RunCommand('hash', args=['-c', '-m', tmp_file], | |
76 return_stdout=True) | |
77 for stdout in (stdout_crc, stdout_both): | |
78 self.assertIn('\tHash (crc32c):\t\t%s' % self._TEST_FILE_B64_CRC, stdout) | |
79 for stdout in (stdout_md5, stdout_both): | |
80 self.assertIn('\tHash (md5):\t\t%s' % self._TEST_FILE_B64_MD5, stdout) | |
81 self.assertNotIn('md5', stdout_crc) | |
82 self.assertNotIn('crc32c', stdout_md5) | |
83 | |
OLD | NEW |