tests/test-hashutil.py
author Manuel Jacob <me@manueljacob.de>
Mon, 11 Jul 2022 01:51:20 +0200
branchstable
changeset 49378 094a5fa3cf52
parent 48946 642e31cb55f0
permissions -rw-r--r--
procutil: make stream detection in make_line_buffered more correct and strict In make_line_buffered(), we don’t want to wrap the stream if we know that lines get flushed to the underlying raw stream already. Previously, the heuristic was too optimistic. It assumed that any stream which is not an instance of io.BufferedIOBase doesn’t need wrapping. However, there are buffered streams that aren’t instances of io.BufferedIOBase, like Mercurial’s own winstdout. The new logic is different in two ways: First, only for the check, if unwraps any combination of WriteAllWrapper and winstdout. Second, it skips wrapping the stream only if it is an instance of io.RawIOBase (or already wrapped). If it is an instance of io.BufferedIOBase, it gets wrapped. In any other case, the function raises an exception. This ensures that, if an unknown stream is passed or we add another wrapper in the future, we don’t wrap the stream if it’s already line buffered or not wrap the stream if it’s not line buffered. In fact, this was already helpful during development of this change. Without it, I possibly would have forgot that WriteAllWrapper needs to be ignored for the check, leading to unnecessary wrapping if stdout is unbuffered. The alternative would have been to always wrap unknown streams. However, I don’t think that anyone would benefit from being less strict. We can expect streams from the standard library to be subclassing either io.RawIOBase or io.BufferedIOBase, so running Mercurial in the standard way should not regress by this change. Py2exe might replace sys.stdout and sys.stderr, but that currently breaks Mercurial anyway and also these streams don’t claim to be interactive, so this function is not called for them.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
# Tests to ensure that sha1dc.sha1 is exactly a drop-in for
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
# hashlib.sha1 for our needs.
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
import hashlib
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
import unittest
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
import silenttestrunner
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
     9
try:
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    10
    from mercurial.thirdparty import sha1dc
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    11
except ImportError:
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
    sha1dc = None
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
48946
642e31cb55f0 py3: use class X: instead of class X(object):
Gregory Szorc <gregory.szorc@gmail.com>
parents: 48875
diff changeset
    15
class hashertestsbase:
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
    def test_basic_hash(self):
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
        h = self.hasher()
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
        h.update(b'foo')
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
        self.assertEqual(
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
            '0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33', h.hexdigest()
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
        )
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
        h.update(b'bar')
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
        self.assertEqual(
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    24
            '8843d7f92416211de9ebb963ff4ce28125932878', h.hexdigest()
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    25
        )
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    26
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    27
    def test_copy_hasher(self):
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    28
        h = self.hasher()
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    29
        h.update(b'foo')
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    30
        h2 = h.copy()
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    31
        h.update(b'baz')
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    32
        h2.update(b'bar')
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    33
        self.assertEqual(
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    34
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    35
        )
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    36
        self.assertEqual(
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    37
            '8843d7f92416211de9ebb963ff4ce28125932878', h2.hexdigest()
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    38
        )
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    39
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    40
    def test_init_hasher(self):
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    41
        h = self.hasher(b'initial string')
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    42
        self.assertEqual(
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    43
            b'\xc9y|n\x1f3S\xa4:\xbaJ\xca,\xc1\x1a\x9e\xb8\xd8\xdd\x86',
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    44
            h.digest(),
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    45
        )
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    46
44087
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    47
    def test_bytes_like_types(self):
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    48
        h = self.hasher()
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    49
        h.update(bytearray(b'foo'))
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    50
        h.update(memoryview(b'baz'))
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    51
        self.assertEqual(
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    52
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    53
        )
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    54
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    55
        h = self.hasher(bytearray(b'foo'))
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    56
        h.update(b'baz')
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    57
        self.assertEqual(
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    58
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    59
        )
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    60
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    61
        h = self.hasher(memoryview(b'foo'))
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    62
        h.update(b'baz')
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    63
        self.assertEqual(
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    64
            '21eb6533733a5e4763acacd1d45a60c2e0e404e1', h.hexdigest()
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    65
        )
dc9b53482689 sha1dc: use buffer protocol when parsing arguments
Gregory Szorc <gregory.szorc@gmail.com>
parents: 44058
diff changeset
    66
44058
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    67
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    68
class hashlibtests(unittest.TestCase, hashertestsbase):
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    69
    hasher = hashlib.sha1
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    70
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    71
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    72
if sha1dc:
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    73
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    74
    class sha1dctests(unittest.TestCase, hashertestsbase):
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    75
        hasher = sha1dc.sha1
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    76
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    77
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    78
if __name__ == '__main__':
bde1cd4c99d9 sha1dc: initial implementation of Python extension
Augie Fackler <augie@google.com>
parents:
diff changeset
    79
    silenttestrunner.main(__name__)