source: trunk/src/allmydata/test/strategies.py

Last change on this file was 1cfe843d, checked in by Alexandre Detiste <alexandre.detiste@…>, at 2024-02-22T23:40:25Z

more python2 removal

  • Property mode set to 100644
File size: 2.6 KB
Line 
1"""
2Hypothesis strategies use for testing Tahoe-LAFS.
3
4Ported to Python 3.
5"""
6
7from hypothesis.strategies import (
8    one_of,
9    builds,
10    binary,
11    integers,
12)
13
14from ..uri import (
15    WriteableSSKFileURI,
16    WriteableMDMFFileURI,
17    DirectoryURI,
18    MDMFDirectoryURI,
19)
20
21from allmydata.util.base32 import (
22    b2a,
23)
24
25
26def write_capabilities():
27    """
28    Build ``IURI`` providers representing all kinds of write capabilities.
29    """
30    return one_of([
31        ssk_capabilities(),
32        mdmf_capabilities(),
33        dir2_capabilities(),
34        dir2_mdmf_capabilities(),
35    ])
36
37
38def ssk_capabilities():
39    """
40    Build ``WriteableSSKFileURI`` instances.
41    """
42    return builds(
43        WriteableSSKFileURI,
44        ssk_writekeys(),
45        ssk_fingerprints(),
46    )
47
48
49def _writekeys(size=16):
50    """
51    Build ``bytes`` representing write keys.
52    """
53    return binary(min_size=size, max_size=size)
54
55
56def ssk_writekeys():
57    """
58    Build ``bytes`` representing SSK write keys.
59    """
60    return _writekeys()
61
62
63def _fingerprints(size=32):
64    """
65    Build ``bytes`` representing fingerprints.
66    """
67    return binary(min_size=size, max_size=size)
68
69
70def ssk_fingerprints():
71    """
72    Build ``bytes`` representing SSK fingerprints.
73    """
74    return _fingerprints()
75
76
77def mdmf_capabilities():
78    """
79    Build ``WriteableMDMFFileURI`` instances.
80    """
81    return builds(
82        WriteableMDMFFileURI,
83        mdmf_writekeys(),
84        mdmf_fingerprints(),
85    )
86
87
88def mdmf_writekeys():
89    """
90    Build ``bytes`` representing MDMF write keys.
91    """
92    return _writekeys()
93
94
95def mdmf_fingerprints():
96    """
97    Build ``bytes`` representing MDMF fingerprints.
98    """
99    return _fingerprints()
100
101
102def dir2_capabilities():
103    """
104    Build ``DirectoryURI`` instances.
105    """
106    return builds(
107        DirectoryURI,
108        ssk_capabilities(),
109    )
110
111
112def dir2_mdmf_capabilities():
113    """
114    Build ``MDMFDirectoryURI`` instances.
115    """
116    return builds(
117        MDMFDirectoryURI,
118        mdmf_capabilities(),
119    )
120
121
122def offsets(min_value=0, max_value=2 ** 16):
123    """
124    Build ``int`` values that could be used as valid offsets into a sequence
125    (such as share data in a share file).
126    """
127    return integers(min_value, max_value)
128
129def lengths(min_value=1, max_value=2 ** 16):
130    """
131    Build ``int`` values that could be used as valid lengths of data (such as
132    share data in a share file).
133    """
134    return integers(min_value, max_value)
135
136
137def base32text():
138    """
139    Build text()s that are valid base32
140    """
141    return builds(
142        lambda b: str(b2a(b), "ascii"),
143        binary(),
144    )
Note: See TracBrowser for help on using the repository browser.