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
Use hgrc within $TESTTMP
$ HGRCPATH=`pwd`/hgrc
$ export HGRCPATH
hide outer repo
$ hg init
Use an alternate var for scribbling on hgrc to keep check-code from
complaining about the important settings we may be overwriting:
$ HGRC=`pwd`/hgrc
$ export HGRC
Basic syntax error
$ echo "invalid" > $HGRC
$ hg version
hg: parse error at $TESTTMP/hgrc:1: invalid
[255]
$ echo "" > $HGRC
Issue1199: Can't use '%' in hgrc (eg url encoded username)
$ hg init "foo%bar"
$ hg clone "foo%bar" foobar
updating to branch default
0 files updated, 0 files merged, 0 files removed, 0 files unresolved
$ cd foobar
$ cat .hg/hgrc
# example repository config (see 'hg help config' for more info)
[paths]
default = $TESTTMP/foo%bar
# path aliases to other clones of this repo in URLs or filesystem paths
# (see 'hg help config.paths' for more info)
#
# default:pushurl = ssh://jdoe@example.net/hg/jdoes-fork
# my-fork = ssh://jdoe@example.net/hg/jdoes-fork
# my-clone = /home/jdoe/jdoes-clone
[ui]
# name and email (local to this repository, optional), e.g.
# username = Jane Doe <jdoe@example.com>
$ hg paths
default = $TESTTMP/foo%bar
$ hg showconfig
bundle.mainreporoot=$TESTTMP/foobar
paths.default=$TESTTMP/foo%bar
$ cd ..
issue1829: wrong indentation
$ echo '[foo]' > $HGRC
$ echo ' x = y' >> $HGRC
$ hg version
hg: parse error at $TESTTMP/hgrc:2: x = y
unexpected leading whitespace
[255]
$ $PYTHON -c "from __future__ import print_function; print('[foo]\nbar = a\n b\n c \n de\n fg \nbaz = bif cb \n')" \
> > $HGRC
$ hg showconfig foo
foo.bar=a\nb\nc\nde\nfg
foo.baz=bif cb
$ FAKEPATH=/path/to/nowhere
$ export FAKEPATH
$ echo '%include $FAKEPATH/no-such-file' > $HGRC
$ hg version
Mercurial Distributed SCM (version *) (glob)
(see https://mercurial-scm.org for more information)
Copyright (C) 2005-* Matt Mackall and others (glob)
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ unset FAKEPATH
make sure global options given on the cmdline take precedence
$ hg showconfig --config ui.verbose=True --quiet
bundle.mainreporoot=$TESTTMP
ui.verbose=False
ui.debug=False
ui.quiet=True
$ touch foobar/untracked
$ cat >> foobar/.hg/hgrc <<EOF
> [ui]
> verbose=True
> EOF
$ hg -R foobar st -q
username expansion
$ olduser=$HGUSER
$ unset HGUSER
$ FAKEUSER='John Doe'
$ export FAKEUSER
$ echo '[ui]' > $HGRC
$ echo 'username = $FAKEUSER' >> $HGRC
$ hg init usertest
$ cd usertest
$ touch bar
$ hg commit --addremove --quiet -m "added bar"
$ hg log --template "{author}\n"
John Doe
$ cd ..
$ hg showconfig
bundle.mainreporoot=$TESTTMP
ui.username=$FAKEUSER
$ unset FAKEUSER
$ HGUSER=$olduser
$ export HGUSER
showconfig with multiple arguments
$ echo "[alias]" > $HGRC
$ echo "log = log -g" >> $HGRC
$ echo "[defaults]" >> $HGRC
$ echo "identify = -n" >> $HGRC
$ hg showconfig alias defaults
alias.log=log -g
defaults.identify=-n
$ hg showconfig alias alias
alias.log=log -g
$ hg showconfig alias.log alias.log
alias.log=log -g
$ hg showconfig alias defaults.identify
alias.log=log -g
defaults.identify=-n
$ hg showconfig alias.log defaults.identify
alias.log=log -g
defaults.identify=-n
HGPLAIN
$ echo "[ui]" > $HGRC
$ echo "debug=true" >> $HGRC
$ echo "fallbackencoding=ASCII" >> $HGRC
$ echo "quiet=true" >> $HGRC
$ echo "slash=true" >> $HGRC
$ echo "traceback=true" >> $HGRC
$ echo "verbose=true" >> $HGRC
$ echo "style=~/.hgstyle" >> $HGRC
$ echo "logtemplate={node}" >> $HGRC
$ echo "[defaults]" >> $HGRC
$ echo "identify=-n" >> $HGRC
$ echo "[alias]" >> $HGRC
$ echo "log=log -g" >> $HGRC
customized hgrc
$ hg showconfig
read config from: $TESTTMP/hgrc
$TESTTMP/hgrc:13: alias.log=log -g
repo: bundle.mainreporoot=$TESTTMP
$TESTTMP/hgrc:11: defaults.identify=-n
$TESTTMP/hgrc:2: ui.debug=true
$TESTTMP/hgrc:3: ui.fallbackencoding=ASCII
$TESTTMP/hgrc:4: ui.quiet=true
$TESTTMP/hgrc:5: ui.slash=true
$TESTTMP/hgrc:6: ui.traceback=true
$TESTTMP/hgrc:7: ui.verbose=true
$TESTTMP/hgrc:8: ui.style=~/.hgstyle
$TESTTMP/hgrc:9: ui.logtemplate={node}
plain hgrc
$ HGPLAIN=; export HGPLAIN
$ hg showconfig --config ui.traceback=True --debug
read config from: $TESTTMP/hgrc
repo: bundle.mainreporoot=$TESTTMP
--config: ui.traceback=True
--verbose: ui.verbose=False
--debug: ui.debug=True
--quiet: ui.quiet=False
with environment variables
$ PAGER=p1 EDITOR=e1 VISUAL=e2 hg showconfig --debug
set config by: $EDITOR
set config by: $VISUAL
set config by: $PAGER
read config from: $TESTTMP/hgrc
repo: bundle.mainreporoot=$TESTTMP
$PAGER: pager.pager=p1
$VISUAL: ui.editor=e2
--verbose: ui.verbose=False
--debug: ui.debug=True
--quiet: ui.quiet=False
plain mode with exceptions
$ cat > plain.py <<EOF
> from mercurial import commands, extensions
> def _config(orig, ui, repo, *values, **opts):
> ui.write(b'plain: %r\n' % ui.plain())
> return orig(ui, repo, *values, **opts)
> def uisetup(ui):
> extensions.wrapcommand(commands.table, b'config', _config)
> EOF
$ echo "[extensions]" >> $HGRC
$ echo "plain=./plain.py" >> $HGRC
$ HGPLAINEXCEPT=; export HGPLAINEXCEPT
$ hg showconfig --config ui.traceback=True --debug
plain: True
read config from: $TESTTMP/hgrc
repo: bundle.mainreporoot=$TESTTMP
$TESTTMP/hgrc:15: extensions.plain=./plain.py
--config: ui.traceback=True
--verbose: ui.verbose=False
--debug: ui.debug=True
--quiet: ui.quiet=False
$ unset HGPLAIN
$ hg showconfig --config ui.traceback=True --debug
plain: True
read config from: $TESTTMP/hgrc
repo: bundle.mainreporoot=$TESTTMP
$TESTTMP/hgrc:15: extensions.plain=./plain.py
--config: ui.traceback=True
--verbose: ui.verbose=False
--debug: ui.debug=True
--quiet: ui.quiet=False
$ HGPLAINEXCEPT=i18n; export HGPLAINEXCEPT
$ hg showconfig --config ui.traceback=True --debug
plain: True
read config from: $TESTTMP/hgrc
repo: bundle.mainreporoot=$TESTTMP
$TESTTMP/hgrc:15: extensions.plain=./plain.py
--config: ui.traceback=True
--verbose: ui.verbose=False
--debug: ui.debug=True
--quiet: ui.quiet=False
source of paths is not mangled
$ cat >> $HGRCPATH <<EOF
> [paths]
> foo = bar
> EOF
$ hg showconfig --debug paths
plain: True
read config from: $TESTTMP/hgrc
$TESTTMP/hgrc:17: paths.foo=$TESTTMP/bar