def test_huge_split(): max_ = gfshare._MAX_SHARECOUNT assert gfshare.split(max_, max_, b"secret") with pytest.raises(ValueError) as exc: gfshare.split(max_ + 1, max_ + 1, b"secret") assert str(exc.value) == "sharecount must be < {}".format(max_)
def test_exceed_buffer(): secret = b"X" * ((gfshare._BUFFER_SIZE * 2) + 1) split = gfshare.split(10, 10, secret) for x in split.values(): assert len(x) == len(secret) assert gfshare.combine(split) == secret
def test_smoketest(): secret = b"secret" result = gfshare.split(10, 10, secret) assert len(result) == 10 assert isinstance(result, dict) for k, v in result.items(): assert isinstance(k, int) assert isinstance(v, bytes) assert len(v) == len(secret)
def splitSecret(bits=250, k=3, n=5): """ Generate a new secret and split into shares """ if k > n: raise ValueError(f"Quorum K cannot be larger than total number " "of shares N. Instead found K={k} and N={n}") secret = Crypto.randomBytes(bits=bits, noNullBytes=True) print("secret=", secret) print(len(secret)) return gfshare.split(k, n, secret)
def test_allow_str(): gfshare.split(10, 10, "str")
def test_validate_sharecount_threshold(): with pytest.raises(ValueError) as exc: gfshare.split(3, 2, b"secret") assert str(exc.value) == "sharecount must be >= threshold"
def test_validate_sharecount(): with pytest.raises(ValueError) as exc: gfshare.split(1, 1, b"secret") assert str(exc.value) == "sharecount must be >= 2"
def test_validate_threshold(): with pytest.raises(ValueError) as exc: gfshare.split(0, 10, b"secret") assert str(exc.value) == "threshold must be >= 1"
def test_random_results(): assert gfshare.split(10, 10, b"secret") != \ gfshare.split(10, 10, b"secret")
def test_roundtrip(): assert gfshare.combine(gfshare.split(10, 10, b"secret")) == b"secret"
def test_breaks(): shares = gfshare.split(10, 10, b"secret") shares.popitem() assert gfshare.combine(shares) != b"secret"
def test_validate_type(): with pytest.raises(TypeError) as exc: gfshare.split(10, 10, "str") assert str(exc.value) == "a bytes-like object is required, not 'str'"
def encode(self, data): shares = gfshare.split(self.n_blocks, self.threshold, data) encoded = map(lambda (x, y): str(x) + "<-->" + y, shares) return encoded
def test_embedded_null_byte(): assert gfshare.combine(gfshare.split(10, 10, b"sec\x00ret")) == b"sec\x00ret"