Пример #1
0
    def test_success(self, tmpdir):
        """Make sure no error is raised when at least one hash matches.

        Test check_against_path because it calls everything else.

        """
        file = tmpdir / 'to_hash'
        file.write('hello')
        hashes = Hashes({
            'sha256': ['2cf24dba5fb0a30e26e83b2ac5b9e29e'
                       '1b161e5c1fa7425e73043362938b9824'],
            'sha224': ['wrongwrong'],
            'md5': ['5d41402abc4b2a76b9719d911017c592']})
        hashes.check_against_path(file)
Пример #2
0
    def hashes(self, trust_internet=True):
        """Return a hash-comparer that considers my option- and URL-based
        hashes to be known-good.

        Hashes in URLs--ones embedded in the requirements file, not ones
        downloaded from an index server--are almost peers with ones from
        flags. They satisfy --require-hashes (whether it was implicitly or
        explicitly activated) but do not activate it. md5 and sha224 are not
        allowed in flags, which should nudge people toward good algos. We
        always OR all hashes together, even ones from URLs.

        :param trust_internet: Whether to trust URL-based (#md5=...) hashes
            downloaded from the internet, as by populate_link()

        """
        good_hashes = self.options.get('hashes', {}).copy()
        link = self.link if trust_internet else self.original_link
        if link and link.hash:
            good_hashes.setdefault(link.hash_name, []).append(link.hash)
        return Hashes(good_hashes)
Пример #3
0
def test_unpack_http_url_bad_downloaded_checksum(mock_unpack_file):
    """
    If already-downloaded file has bad checksum, re-download.
    """
    base_url = 'http://www.example.com/somepackage.tgz'
    contents = b'downloaded'
    download_hash = hashlib.new('sha1', contents)
    link = Link(base_url + '#sha1=' + download_hash.hexdigest())

    session = Mock()
    session.get = Mock()
    response = session.get.return_value = MockResponse(contents)
    response.headers = {'content-type': 'application/x-tar'}
    response.url = base_url

    download_dir = mkdtemp()
    try:
        downloaded_file = os.path.join(download_dir, 'somepackage.tgz')
        _write_file(downloaded_file, 'some contents')

        unpack_http_url(
            link,
            'location',
            download_dir=download_dir,
            session=session,
            hashes=Hashes({'sha1': [download_hash.hexdigest()]})
        )

        # despite existence of downloaded file with bad hash, downloaded again
        session.get.assert_called_once_with(
            'http://www.example.com/somepackage.tgz',
            headers={"Accept-Encoding": "identity"},
            stream=True,
        )
        # cached file is replaced with newly downloaded file
        with open(downloaded_file) as fh:
            assert fh.read() == 'downloaded'

    finally:
        rmtree(download_dir)
Пример #4
0
 def test_non_zero(self):
     """Test that truthiness tests tell whether any known-good hashes
     exist."""
     assert Hashes({'sha256': 'dummy'})
     assert not Hashes()
     assert not Hashes({})
Пример #5
0
 def test_unknown_hash(self):
     """Hashes should raise InstallationError when it encounters an unknown
     hash."""
     hashes = Hashes({'badbad': ['dummy']})
     with pytest.raises(InstallationError):
         hashes.check_against_file(BytesIO(b'hello'))
Пример #6
0
 def test_failure(self):
     """Hashes should raise HashMismatch when no hashes match."""
     hashes = Hashes({'sha256': ['wrongwrong']})
     with pytest.raises(HashMismatch):
         hashes.check_against_file(BytesIO(b'hello'))
Пример #7
0
 def test_unknown_hash(self):
     """Hashes should raise InstallationError when it encounters an unknown
     hash."""
     hashes = Hashes({'badbad': ['dummy']})
     with pytest.raises(InstallationError):
         hashes.check_against_file(BytesIO(b'hello'))
Пример #8
0
 def test_failure(self):
     """Hashes should raise HashMismatch when no hashes match."""
     hashes = Hashes({'sha256': ['wrongwrong']})
     with pytest.raises(HashMismatch):
         hashes.check_against_file(BytesIO(b'hello'))