tests/test-arbitraryfilectx.t
author Gregory Szorc <gregory.szorc@gmail.com>
Tue, 28 Aug 2018 15:02:48 -0700
changeset 39411 aeb551a3bb8a
parent 37318 9954d0e2ad00
child 41334 5361f9ed8a30
permissions -rw-r--r--
cborutil: implement sans I/O decoder The vendored CBOR package decodes by calling read(n) on an object. There are a number of disadvantages to this: * Uses blocking I/O. If sufficient data is not available, the decoder will hang until it is. * No support for partial reads. If the read(n) returns less data than requested, the decoder raises an error. * Requires the use of a file like object. If the original data is in say a buffer, we need to "cast" it to e.g. a BytesIO to appease the decoder. In addition, the vendored CBOR decoder doesn't provide flexibility that we desire. Specifically: * It buffers indefinite length bytestrings instead of streaming them. * It doesn't allow limiting the set of types that can be decoded. This property is useful when implementing a "hardened" decoder that is less susceptible to abusive input. * It doesn't provide sufficient "hook points" and introspection to institute checks around behavior. These are useful for implementing a "hardened" decoder. This all adds up to a reasonable set of justifications for writing our own decoder. So, this commit implements our own CBOR decoder. At the heart of the decoder is a function that decodes a single "item" from a buffer. This item can be a complete simple value or a special value, such as "start of array." Using this function, we can build a decoder that effectively iterates over the stream of decoded items and builds up higher-level values, such as arrays, maps, sets, and indefinite length bytestrings. And we can do this without performing I/O in the decoder itself. The core of the sans I/O decoder will probably not be used directly. Instead, it is expected that we'll build utility functions for invoking the decoder given specific input types. This will allow extreme flexibility in how data is delivered to the decoder. I'm pretty happy with the state of the decoder modulo the TODO items to track wanted features to help with a "hardened" decoder. The one thing I could be convinced to change is the handling of semantic tags. Since we only support a single semantic tag (sets), I thought it would be easier to handle them inline in decodeitem(). This is simpler now. But if we add support for other semantic tags, it will likely be easier to move semantic tag handling outside of decodeitem(). But, properly supporting semantic tags opens up a whole can of worms, as many semantic tags imply new types. I'm optimistic we won't need these in Mercurial. But who knows. I'm also pretty happy with the test coverage. Writing comprehensive tests for partial decoding did flush out a handful of bugs. One general improvement to testing would be fuzz testing for partial decoding. I may implement that later. I also anticipate switching the wire protocol code to this new decoder will flush out any lingering bugs. Differential Revision: https://phab.mercurial-scm.org/D4414
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34835
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
     1
Setup:
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
     2
  $ cat > eval.py <<EOF
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
     3
  > from __future__ import absolute_import
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
     4
  > import filecmp
37318
9954d0e2ad00 py3: use pycompat.bytestr() intsead of str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36382
diff changeset
     5
  > from mercurial import commands, context, pycompat, registrar
34835
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
     6
  > cmdtable = {}
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
     7
  > command = registrar.command(cmdtable)
36382
b4d1c09b754b py3: add missing b'' in test-arbitraryfilectx.t
Pulkit Goyal <7895pulkit@gmail.com>
parents: 35947
diff changeset
     8
  > @command(b'eval', [], b'hg eval CMD')
34835
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
     9
  > def eval_(ui, repo, *cmds, **opts):
35947
a36d3c8a0e41 py3: add b'' prefixes to string literals in test files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34936
diff changeset
    10
  >     cmd = b" ".join(cmds)
37318
9954d0e2ad00 py3: use pycompat.bytestr() intsead of str
Pulkit Goyal <7895pulkit@gmail.com>
parents: 36382
diff changeset
    11
  >     res = pycompat.bytestr(eval(cmd, globals(), locals()))
35947
a36d3c8a0e41 py3: add b'' prefixes to string literals in test files
Pulkit Goyal <7895pulkit@gmail.com>
parents: 34936
diff changeset
    12
  >     ui.warn(b"%s" % res)
34835
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    13
  > EOF
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    14
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    15
  $ echo "[extensions]" >> $HGRCPATH
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    16
  $ echo "eval=`pwd`/eval.py" >> $HGRCPATH
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    17
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    18
Arbitraryfilectx.cmp does not follow symlinks:
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    19
  $ mkdir case1
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    20
  $ cd case1
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    21
  $ hg init
34936
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    22
#if symlink
34835
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    23
  $ printf "A" > real_A
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    24
  $ printf "foo" > A
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    25
  $ printf "foo" > B
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    26
  $ ln -s A sym_A
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    27
  $ hg add .
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    28
  adding A
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    29
  adding B
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    30
  adding real_A
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    31
  adding sym_A
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    32
  $ hg commit -m "base"
34936
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    33
#else
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    34
  $ hg import -q --bypass - <<EOF
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    35
  > # HG changeset patch
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    36
  > # User test
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    37
  > # Date 0 0
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    38
  > base
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    39
  > 
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    40
  > diff --git a/A b/A
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    41
  > new file mode 100644
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    42
  > --- /dev/null
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    43
  > +++ b/A
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    44
  > @@ -0,0 +1,1 @@
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    45
  > +foo
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    46
  > \ No newline at end of file
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    47
  > diff --git a/B b/B
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    48
  > new file mode 100644
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    49
  > --- /dev/null
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    50
  > +++ b/B
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    51
  > @@ -0,0 +1,1 @@
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    52
  > +foo
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    53
  > \ No newline at end of file
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    54
  > diff --git a/real_A b/real_A
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    55
  > new file mode 100644
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    56
  > --- /dev/null
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    57
  > +++ b/real_A
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    58
  > @@ -0,0 +1,1 @@
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    59
  > +A
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    60
  > \ No newline at end of file
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    61
  > diff --git a/sym_A b/sym_A
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    62
  > new file mode 120000
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    63
  > --- /dev/null
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    64
  > +++ b/sym_A
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    65
  > @@ -0,0 +1,1 @@
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    66
  > +A
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    67
  > \ No newline at end of file
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    68
  > EOF
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    69
  $ hg up -q
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    70
#endif
34835
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    71
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    72
These files are different and should return True (different):
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    73
(Note that filecmp.cmp's return semantics are inverted from ours, so we invert
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    74
for simplicity):
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    75
  $ hg eval "context.arbitraryfilectx('A', repo).cmp(repo[None]['real_A'])"
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    76
  True (no-eol)
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    77
  $ hg eval "not filecmp.cmp('A', 'real_A')"
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    78
  True (no-eol)
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    79
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    80
These files are identical and should return False (same):
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    81
  $ hg eval "context.arbitraryfilectx('A', repo).cmp(repo[None]['A'])"
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    82
  False (no-eol)
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    83
  $ hg eval "context.arbitraryfilectx('A', repo).cmp(repo[None]['B'])"
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    84
  False (no-eol)
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    85
  $ hg eval "not filecmp.cmp('A', 'B')"
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    86
  False (no-eol)
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    87
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    88
This comparison should also return False, since A and sym_A are substantially
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    89
the same in the eyes of ``filectx.cmp``, which looks at data only.
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    90
  $ hg eval "context.arbitraryfilectx('real_A', repo).cmp(repo[None]['sym_A'])"
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    91
  False (no-eol)
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    92
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    93
A naive use of filecmp on those two would wrongly return True, since it follows
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    94
the symlink to "A", which has different contents.
34936
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    95
#if symlink
34835
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    96
  $ hg eval "not filecmp.cmp('real_A', 'sym_A')"
14c87708f432 arbitraryfilecontext: skip the cmp fast path if any side is a symlink
Phil Cohen <phillco@fb.com>
parents:
diff changeset
    97
  True (no-eol)
34936
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    98
#else
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
    99
  $ hg eval "not filecmp.cmp('real_A', 'sym_A')"
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
   100
  False (no-eol)
9645c2a2bc2a test-arbitraryfilectx: stabilize for Windows
Matt Harbison <matt_harbison@yahoo.com>
parents: 34835
diff changeset
   101
#endif