1 # -*- coding: utf-8 -*-
2 # Copyright 2014 Google Inc. All Rights Reserved.
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
8 # http://www.apache.org/licenses/LICENSE-2.0
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."""
19 from gslib
.exception
import CommandException
20 import gslib
.tests
.testcase
as testcase
23 class TestHash(testcase
.GsUtilUnitTestCase
):
24 """Unit tests for hash command."""
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'
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
_B
64_CRC
, stdout
)
37 self
.assertIn('\tHash (md5):\t\t%s' % self
._TEST
_FILE
_B
64_MD
5, stdout
)
39 def testHashNoMatch(self
):
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')
46 def testHashCloudObject(self
):
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
)
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
_MD
5, stdout
)
60 def testHashWildcard(self
):
62 tmp_dir
= self
.CreateTempDir(test_files
=num_test_files
)
63 stdout
= self
.RunCommand('hash', args
=[os
.path
.join(tmp_dir
, '*')],
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
)
69 def testHashSelectAlg(self
):
70 tmp_file
= self
.CreateTempFile(contents
=self
._TEST
_FILE
_CONTENTS
)
71 stdout_crc
= self
.RunCommand('hash', args
=['-c', tmp_file
],
73 stdout_md5
= self
.RunCommand('hash', args
=['-m', tmp_file
],
75 stdout_both
= self
.RunCommand('hash', args
=['-c', '-m', tmp_file
],
77 for stdout
in (stdout_crc
, stdout_both
):
78 self
.assertIn('\tHash (crc32c):\t\t%s' % self
._TEST
_FILE
_B
64_CRC
, stdout
)
79 for stdout
in (stdout_md5
, stdout_both
):
80 self
.assertIn('\tHash (md5):\t\t%s' % self
._TEST
_FILE
_B
64_MD
5, stdout
)
81 self
.assertNotIn('md5', stdout_crc
)
82 self
.assertNotIn('crc32c', stdout_md5
)