contrib/fuzz/manifest_corpus.py
author Matt Harbison <matt_harbison@yahoo.com>
Thu, 24 Oct 2024 22:47:31 -0400
changeset 52127 fd200f5bcaea
parent 48875 6000f5b25c9b
permissions -rw-r--r--
wireprototypes: make `baseprotocolhandler` methods abstract The documentation says it's an abstract base class, so let's enforce it. The `typing.Protocol` class is already an ABC, but it only prevents instantiation if there are abstract attrs that are missing. For example, from `hg debugshell`: >>> from mercurial import wireprototypes >>> x = wireprototypes.baseprotocolhandler() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: Can't instantiate abstract class baseprotocolhandler with abstract method name >>> class fake(wireprototypes.baseprotocolhandler): ... pass ... >>> x = fake() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: Can't instantiate abstract class fake with abstract method name That's great, but it doesn't protect against calling non-abstract methods at runtime, rather it depends on the protocol type hint being added to method signatures or class attrs, and then running a type checker to notice when an instance is assigned that doesn't conform to the protocol. We don't widely use type hints yet, and do have a lot of class hierarchy in the repository area, which could lead to surprises like this: >>> class fake(wireprototypes.baseprotocolhandler): ... @property ... def name(self) -> bytes: ... return b'name' ... >>> z = fake() >>> z.client() >>> print(z.client()) None Oops. That was supposed to return `bytes`. So not only is a bad/unexpected value returned, but it's one that violates the type hints (since the base client() method will be annotated to return bytes). With this change, we get: >>> from mercurial import wireprototypes >>> class fake(wireprototypes.baseprotocolhandler): ... @property ... def name(self) -> bytes: ... return b'name' ... >>> x = fake() Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: Can't instantiate abstract class fake with abstract methods addcapabilities, checkperm, client, getargs, getpayload, getprotocaps, mayberedirectstdio So this looks like a reasonable safety harness to me, and lets us catch problems by running the standard tests while the type hints are being added, and pytype is improved. We should probably do this for all Protocol class methods that don't supply a method implementation.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40053
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     1
import argparse
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     2
import zipfile
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     3
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     4
ap = argparse.ArgumentParser()
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     5
ap.add_argument("out", metavar="some.zip", type=str, nargs=1)
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     6
args = ap.parse_args()
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     7
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
     8
with zipfile.ZipFile(args.out[0], "w", zipfile.ZIP_STORED) as zf:
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40053
diff changeset
     9
    zf.writestr(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40053
diff changeset
    10
        "manifest_zero",
44986
0ff59434af72 fuzz: tell manifest fuzzer about longer node hashes
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    11
        '''\0PKG-INFO\09b3ed8f2b81095a13064402e930565f083346e9a
40053
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    12
README\080b6e76643dcb44d4bc729e932fc464b3e36dbe3
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    13
hg\0b6444347c629cc058d478023905cfb83b7f5bb9d
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    14
mercurial/__init__.py\0b80de5d138758541c5f05265ad144ab9fa86d1db
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    15
mercurial/byterange.py\017f5a9fbd99622f31a392c33ac1e903925dc80ed
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    16
mercurial/fancyopts.py\0b6f52e23e356748c5039313d8b639cda16bf67ba
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    17
mercurial/hg.py\023cc12f225f1b42f32dc0d897a4f95a38ddc8f4a
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    18
mercurial/mdiff.py\0a05f65c44bfbeec6a42336cd2ff0b30217899ca3
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    19
mercurial/revlog.py\0217bc3fde6d82c0210cf56aeae11d05a03f35b2b
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    20
mercurial/transaction.py\09d180df101dc14ce3dd582fd998b36c98b3e39aa
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    21
notes.txt\0703afcec5edb749cf5cec67831f554d6da13f2fb
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    22
setup.py\0ccf3f6daf0f13101ca73631f7a1769e328b472c9
8c692a6b5ad1 fuzz: new fuzzer for cext/manifest.c
Augie Fackler <augie@google.com>
parents:
diff changeset
    23
tkmerge\03c922edb43a9c143682f7bc7b00f98b3c756ebe7
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40053
diff changeset
    24
''',
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40053
diff changeset
    25
    )
44986
0ff59434af72 fuzz: tell manifest fuzzer about longer node hashes
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    26
    zf.writestr("badmanifest_shorthashes", "\0narf\0aa\nnarf2\0aaa\n")
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40053
diff changeset
    27
    zf.writestr(
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40053
diff changeset
    28
        "badmanifest_nonull",
44986
0ff59434af72 fuzz: tell manifest fuzzer about longer node hashes
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    29
        "\0narf\0cccccccccccccccccccccccccccccccccccccccc\n"
43076
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40053
diff changeset
    30
        "narf2aaaaaaaaaaaaaaaaaaaa\n",
2372284d9457 formatting: blacken the codebase
Augie Fackler <augie@google.com>
parents: 40053
diff changeset
    31
    )
44986
0ff59434af72 fuzz: tell manifest fuzzer about longer node hashes
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    32
0ff59434af72 fuzz: tell manifest fuzzer about longer node hashes
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    33
    zf.writestr(
0ff59434af72 fuzz: tell manifest fuzzer about longer node hashes
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    34
        "manifest_long_nodes",
0ff59434af72 fuzz: tell manifest fuzzer about longer node hashes
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    35
        "\1a\0ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff\n",
0ff59434af72 fuzz: tell manifest fuzzer about longer node hashes
Augie Fackler <augie@google.com>
parents: 43076
diff changeset
    36
    )