| 1 | """ |
|---|
| 2 | Hypothesis strategies use for testing Tahoe-LAFS. |
|---|
| 3 | |
|---|
| 4 | Ported to Python 3. |
|---|
| 5 | """ |
|---|
| 6 | |
|---|
| 7 | from hypothesis.strategies import ( |
|---|
| 8 | one_of, |
|---|
| 9 | builds, |
|---|
| 10 | binary, |
|---|
| 11 | integers, |
|---|
| 12 | ) |
|---|
| 13 | |
|---|
| 14 | from ..uri import ( |
|---|
| 15 | WriteableSSKFileURI, |
|---|
| 16 | WriteableMDMFFileURI, |
|---|
| 17 | DirectoryURI, |
|---|
| 18 | MDMFDirectoryURI, |
|---|
| 19 | ) |
|---|
| 20 | |
|---|
| 21 | from allmydata.util.base32 import ( |
|---|
| 22 | b2a, |
|---|
| 23 | ) |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | def 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 | |
|---|
| 38 | def ssk_capabilities(): |
|---|
| 39 | """ |
|---|
| 40 | Build ``WriteableSSKFileURI`` instances. |
|---|
| 41 | """ |
|---|
| 42 | return builds( |
|---|
| 43 | WriteableSSKFileURI, |
|---|
| 44 | ssk_writekeys(), |
|---|
| 45 | ssk_fingerprints(), |
|---|
| 46 | ) |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | def _writekeys(size=16): |
|---|
| 50 | """ |
|---|
| 51 | Build ``bytes`` representing write keys. |
|---|
| 52 | """ |
|---|
| 53 | return binary(min_size=size, max_size=size) |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | def ssk_writekeys(): |
|---|
| 57 | """ |
|---|
| 58 | Build ``bytes`` representing SSK write keys. |
|---|
| 59 | """ |
|---|
| 60 | return _writekeys() |
|---|
| 61 | |
|---|
| 62 | |
|---|
| 63 | def _fingerprints(size=32): |
|---|
| 64 | """ |
|---|
| 65 | Build ``bytes`` representing fingerprints. |
|---|
| 66 | """ |
|---|
| 67 | return binary(min_size=size, max_size=size) |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | def ssk_fingerprints(): |
|---|
| 71 | """ |
|---|
| 72 | Build ``bytes`` representing SSK fingerprints. |
|---|
| 73 | """ |
|---|
| 74 | return _fingerprints() |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | def mdmf_capabilities(): |
|---|
| 78 | """ |
|---|
| 79 | Build ``WriteableMDMFFileURI`` instances. |
|---|
| 80 | """ |
|---|
| 81 | return builds( |
|---|
| 82 | WriteableMDMFFileURI, |
|---|
| 83 | mdmf_writekeys(), |
|---|
| 84 | mdmf_fingerprints(), |
|---|
| 85 | ) |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | def mdmf_writekeys(): |
|---|
| 89 | """ |
|---|
| 90 | Build ``bytes`` representing MDMF write keys. |
|---|
| 91 | """ |
|---|
| 92 | return _writekeys() |
|---|
| 93 | |
|---|
| 94 | |
|---|
| 95 | def mdmf_fingerprints(): |
|---|
| 96 | """ |
|---|
| 97 | Build ``bytes`` representing MDMF fingerprints. |
|---|
| 98 | """ |
|---|
| 99 | return _fingerprints() |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | def dir2_capabilities(): |
|---|
| 103 | """ |
|---|
| 104 | Build ``DirectoryURI`` instances. |
|---|
| 105 | """ |
|---|
| 106 | return builds( |
|---|
| 107 | DirectoryURI, |
|---|
| 108 | ssk_capabilities(), |
|---|
| 109 | ) |
|---|
| 110 | |
|---|
| 111 | |
|---|
| 112 | def dir2_mdmf_capabilities(): |
|---|
| 113 | """ |
|---|
| 114 | Build ``MDMFDirectoryURI`` instances. |
|---|
| 115 | """ |
|---|
| 116 | return builds( |
|---|
| 117 | MDMFDirectoryURI, |
|---|
| 118 | mdmf_capabilities(), |
|---|
| 119 | ) |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | def 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 | |
|---|
| 129 | def 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 | |
|---|
| 137 | def base32text(): |
|---|
| 138 | """ |
|---|
| 139 | Build text()s that are valid base32 |
|---|
| 140 | """ |
|---|
| 141 | return builds( |
|---|
| 142 | lambda b: str(b2a(b), "ascii"), |
|---|
| 143 | binary(), |
|---|
| 144 | ) |
|---|