Home | History | Annotate | Download | only in tests
      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       # assertRaisesRegexp causes issues with python 2.6.
     45       self.assertIn('No files matched', e.reason)
     46 
     47   def testHashCloudObject(self):
     48     try:
     49       self.RunCommand('hash', args=['gs://bucket/object'])
     50       self.fail('Did not get expected CommandException')
     51     except CommandException, e:
     52       self.assertEquals('"hash" command requires a file URL', e.reason)
     53 
     54   def testHashHexFormat(self):
     55     tmp_file = self.CreateTempFile(contents=self._TEST_FILE_CONTENTS)
     56     stdout = self.RunCommand('hash', args=['-h', tmp_file], return_stdout=True)
     57     self.assertIn('Hashes [hex]', stdout)
     58     self.assertIn('\tHash (crc32c):\t\t%s' % self._TEST_FILE_HEX_CRC, stdout)
     59     self.assertIn('\tHash (md5):\t\t%s' % self._TEST_FILE_HEX_MD5, stdout)
     60 
     61   def testHashWildcard(self):
     62     num_test_files = 2
     63     tmp_dir = self.CreateTempDir(test_files=num_test_files)
     64     stdout = self.RunCommand('hash', args=[os.path.join(tmp_dir, '*')],
     65                              return_stdout=True)
     66     # One summary line and two hash lines per file.
     67     num_expected_lines = num_test_files * (1 + 2)
     68     self.assertEquals(len(stdout.splitlines()), num_expected_lines)
     69 
     70   def testHashSelectAlg(self):
     71     tmp_file = self.CreateTempFile(contents=self._TEST_FILE_CONTENTS)
     72     stdout_crc = self.RunCommand('hash', args=['-c', tmp_file],
     73                                  return_stdout=True)
     74     stdout_md5 = self.RunCommand('hash', args=['-m', tmp_file],
     75                                  return_stdout=True)
     76     stdout_both = self.RunCommand('hash', args=['-c', '-m', tmp_file],
     77                                   return_stdout=True)
     78     for stdout in (stdout_crc, stdout_both):
     79       self.assertIn('\tHash (crc32c):\t\t%s' % self._TEST_FILE_B64_CRC, stdout)
     80     for stdout in (stdout_md5, stdout_both):
     81       self.assertIn('\tHash (md5):\t\t%s' % self._TEST_FILE_B64_MD5, stdout)
     82     self.assertNotIn('md5', stdout_crc)
     83     self.assertNotIn('crc32c', stdout_md5)
     84 
     85