# Path: gslib/exception.py
# class CommandException(StandardError):
#   """Exception raised when a problem is encountered running a gsutil command.
# 
#   This exception should be used to signal user errors or system failures
#   (like timeouts), not bugs (like an incorrect param value). For the
#   latter you should raise Exception so we can see where/how it happened
#   via gsutil -D (which will include a stack trace for raised Exceptions).
#   """
# 
#   def __init__(self, reason, informational=False):
#     """Instantiate a CommandException.
# 
#     Args:
#       reason: Text describing the problem.
#       informational: Indicates reason should be printed as FYI, not a failure.
#     """
#     StandardError.__init__(self)
#     self.reason = reason
#     self.informational = informational
# 
#   def __repr__(self):
#     return str(self)
# 
#   def __str__(self):
#     return 'CommandException: %s' % self.reason
# 
# Path: gslib/storage_url.py
# def StorageUrlFromString(url_str):
#   """Static factory function for creating a StorageUrl from a string."""
# 
#   scheme = GetSchemeFromUrlString(url_str)
# 
#   if not IsKnownUrlScheme(scheme):
#     raise InvalidUrlError('Unrecognized scheme "%s"' % scheme)
#   if scheme == 'file':
#     path = _GetPathFromUrlString(url_str)
#     is_stream = (path == '-')
#     is_fifo = False
#     try:
#       is_fifo = stat.S_ISFIFO(os.stat(path).st_mode)
#     except OSError:
#       pass
#     return _FileUrl(url_str, is_stream=is_stream, is_fifo=is_fifo)
#   return _CloudUrl(url_str)


import hashlib
import os
import pkgutil
import gslib.tests.testcase as testcase
from unittest import mock
from gslib.exception import CommandException
from gslib.storage_url import StorageUrlFromString
from gslib.utils.constants import TRANSFER_BUFFER_SIZE
from gslib.utils.hashing_helper import CalculateMd5FromContents
from gslib.utils.hashing_helper import GetMd5
from gslib.utils.hashing_helper import HashingFileUploadWrapper
_TEST_FILE = 'test.txt'


class TestGetMd5(testcase.GsUtilUnitTestCase):
  """Unit tests for the GetMd5 function."""

  @mock.patch.object(hashlib, 'md5')
  def testGetsMd5HashOnNonRedHatSystem(self, mock_md5):
    # Can't actually compare output to calling hashlib.md5 because that could
    # trigger an error on a Red Hat system.
    mock_md5.return_value = 'hash'
    self.assertEqual(GetMd5(b''), 'hash')
    mock_md5.assert_called_once_with(b'')

  @mock.patch.object(hashlib, 'md5')
  def testGetsMd5HashOnRedHatSystem(self, mock_md5):
    # Can't actually compare output to calling hashlib.md5 because that could
    # trigger an error on a non-Red Hat system.
    # Return one ValueError to simulate a FIPS-mode distribution.
    mock_md5.side_effect = [ValueError, 'hash']
    self.assertEqual(GetMd5(b''), 'hash')
    self.assertEqual(
        mock_md5.mock_calls,
        [mock.call(b''), mock.call(b'', usedforsecurity=False)])


class TestHashingFileUploadWrapper(testcase.GsUtilUnitTestCase):
  """Unit tests for the HashingFileUploadWrapper class."""

  _temp_test_file = None

# Path: gslib/exception.py
# class CommandException(StandardError):
#   """Exception raised when a problem is encountered running a gsutil command.
# 
#   This exception should be used to signal user errors or system failures
#   (like timeouts), not bugs (like an incorrect param value). For the
#   latter you should raise Exception so we can see where/how it happened
#   via gsutil -D (which will include a stack trace for raised Exceptions).
#   """
# 
#   def __init__(self, reason, informational=False):
#     """Instantiate a CommandException.
# 
#     Args:
#       reason: Text describing the problem.
#       informational: Indicates reason should be printed as FYI, not a failure.
#     """
#     StandardError.__init__(self)
#     self.reason = reason
#     self.informational = informational
# 
#   def __repr__(self):
#     return str(self)
# 
#   def __str__(self):
#     return 'CommandException: %s' % self.reason
# 
# Path: gslib/storage_url.py
# def StorageUrlFromString(url_str):
#   """Static factory function for creating a StorageUrl from a string."""
# 
#   scheme = GetSchemeFromUrlString(url_str)
# 
#   if not IsKnownUrlScheme(scheme):
#     raise InvalidUrlError('Unrecognized scheme "%s"' % scheme)
#   if scheme == 'file':
#     path = _GetPathFromUrlString(url_str)
#     is_stream = (path == '-')
#     is_fifo = False
#     try:
#       is_fifo = stat.S_ISFIFO(os.stat(path).st_mode)
#     except OSError:
#       pass
#     return _FileUrl(url_str, is_stream=is_stream, is_fifo=is_fifo)
#   return _CloudUrl(url_str)


# Path: gslib/tests/test_hashing_helper.py
import hashlib
import os
import pkgutil
import gslib.tests.testcase as testcase
from unittest import mock
from gslib.exception import CommandException
from gslib.storage_url import StorageUrlFromString
from gslib.utils.constants import TRANSFER_BUFFER_SIZE
from gslib.utils.hashing_helper import CalculateMd5FromContents
from gslib.utils.hashing_helper import GetMd5
from gslib.utils.hashing_helper import HashingFileUploadWrapper

_TEST_FILE = 'test.txt'


class TestGetMd5(testcase.GsUtilUnitTestCase):
  """Unit tests for the GetMd5 function."""

  @mock.patch.object(hashlib, 'md5')
  def testGetsMd5HashOnNonRedHatSystem(self, mock_md5):
    # Can't actually compare output to calling hashlib.md5 because that could
    # trigger an error on a Red Hat system.
    mock_md5.return_value = 'hash'
    self.assertEqual(GetMd5(b''), 'hash')
    mock_md5.assert_called_once_with(b'')

  @mock.patch.object(hashlib, 'md5')
  def testGetsMd5HashOnRedHatSystem(self, mock_md5):
    # Can't actually compare output to calling hashlib.md5 because that could
    # trigger an error on a non-Red Hat system.
    # Return one ValueError to simulate a FIPS-mode distribution.
    mock_md5.side_effect = [ValueError, 'hash']
    self.assertEqual(GetMd5(b''), 'hash')
    self.assertEqual(
        mock_md5.mock_calls,
        [mock.call(b''), mock.call(b'', usedforsecurity=False)])


class TestHashingFileUploadWrapper(testcase.GsUtilUnitTestCase):
  """Unit tests for the HashingFileUploadWrapper class."""

  _temp_test_file = None

  _dummy_url = StorageUrlFromString('gs://bucket/object')