tahoe-lafs/integration/test_vectors.py

121 lines
4.0 KiB
Python
Raw Permalink Normal View History

2022-12-21 22:14:08 +00:00
"""
Verify certain results against test vectors with well-known results.
"""
from __future__ import annotations
from functools import partial
from typing import AsyncGenerator, Iterator
from itertools import starmap, product
2023-01-16 20:53:24 +00:00
from attrs import evolve
from pytest import mark
from pytest_twisted import ensureDeferred
2022-12-21 22:14:08 +00:00
2023-01-17 13:41:10 +00:00
from . import vectors
from .vectors import parameters
2023-07-29 19:34:12 +00:00
from .util import upload
from .grid import Client
@mark.parametrize('convergence', parameters.CONVERGENCE_SECRETS)
def test_convergence(convergence):
"""
Convergence secrets are 16 bytes.
"""
2022-12-21 22:14:08 +00:00
assert isinstance(convergence, bytes), "Convergence secret must be bytes"
assert len(convergence) == 16, "Convergence secret must by 16 bytes"
@mark.slow
@mark.parametrize('case,expected', vectors.capabilities.items())
@ensureDeferred
async def test_capability(reactor, request, alice, case, expected):
"""
2022-12-26 22:08:30 +00:00
The capability that results from uploading certain well-known data
with certain well-known parameters results in exactly the previously
computed value.
"""
2022-12-21 22:14:08 +00:00
# rewrite alice's config to match params and convergence
await alice.reconfigure_zfec(
2023-07-25 02:12:01 +00:00
reactor, (1, case.params.required, case.params.total), case.convergence, case.segment_size)
2022-12-21 22:14:08 +00:00
2022-12-26 22:08:30 +00:00
# upload data in the correct format
actual = upload(alice, case.fmt, case.data)
2022-12-21 22:14:08 +00:00
# compare the resulting cap to the expected result
assert actual == expected
@ensureDeferred
async def skiptest_generate(reactor, request, alice):
"""
This is a helper for generating the test vectors.
You can re-generate the test vectors by fixing the name of the test and
running it. Normally this test doesn't run because it ran once and we
captured its output. Other tests run against that output and we want them
to run against the results produced originally, not a possibly
ever-changing set of outputs.
"""
space = starmap(
2023-01-20 21:26:23 +00:00
# segment_size could be a parameter someday but it's not easy to vary
# using the Python implementation so it isn't one for now.
2023-01-20 21:26:23 +00:00
partial(vectors.Case, segment_size=parameters.SEGMENT_SIZE),
product(
parameters.ZFEC_PARAMS,
parameters.CONVERGENCE_SECRETS,
parameters.OBJECT_DESCRIPTIONS,
parameters.FORMATS,
),
)
2023-01-16 20:53:24 +00:00
iterresults = generate(reactor, request, alice, space)
results = []
async for result in iterresults:
# Accumulate the new result
2023-01-16 20:53:24 +00:00
results.append(result)
# Then rewrite the whole output file with the new accumulator value.
# This means that if we fail partway through, we will still have
# recorded partial results -- instead of losing them all.
vectors.save_capabilities(results)
async def generate(
reactor,
request,
alice: Client,
cases: Iterator[vectors.Case],
) -> AsyncGenerator[[vectors.Case, str], None]:
"""
Generate all of the test vectors using the given node.
:param reactor: The reactor to use to restart the Tahoe-LAFS node when it
needs to be reconfigured.
:param request: The pytest request object to use to arrange process
cleanup.
2022-12-26 22:08:30 +00:00
:param format: The name of the encryption/data format to use.
:param alice: The Tahoe-LAFS node to use to generate the test vectors.
:param case: The inputs for which to generate a value.
2022-12-26 22:08:30 +00:00
:return: The capability for the case.
"""
# Share placement doesn't affect the resulting capability. For maximum
2022-12-27 14:03:24 +00:00
# reliability of this generator, be happy if we can put shares anywhere
happy = 1
for case in cases:
await alice.reconfigure_zfec(
reactor,
(happy, case.params.required, case.params.total),
2023-02-07 14:08:06 +00:00
case.convergence,
case.segment_size
)
2022-12-21 22:14:08 +00:00
2023-01-16 20:53:24 +00:00
# Give the format a chance to make an RSA key if it needs it.
case = evolve(case, fmt=case.fmt.customize())
cap = upload(alice.process, case.fmt, case.data)
yield case, cap