Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gon committed Aug 16, 2024
1 parent da06a5b commit a30c143
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions tests/test_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,35 @@ def test_box():
c.crypto_box_open(message + b"!", nonce, A_pubkey, A_secretkey)


def test_box_easy():
A_pubkey, A_secretkey = c.crypto_box_keypair()
assert len(A_secretkey) == c.crypto_box_SECRETKEYBYTES
assert len(A_pubkey) == c.crypto_box_PUBLICKEYBYTES
B_pubkey, B_secretkey = c.crypto_box_keypair()

k1 = c.crypto_box_beforenm(B_pubkey, A_secretkey)
assert len(k1) == c.crypto_box_BEFORENMBYTES
k2 = c.crypto_box_beforenm(A_pubkey, B_secretkey)
assert tohex(k1) == tohex(k2)

message = b"message"
nonce = b"\x01" * c.crypto_box_NONCEBYTES
ct1 = c.crypto_box_easy_afternm(message, nonce, k1)
assert len(ct1) == len(message) + c.crypto_box_BOXZEROBYTES

ct2 = c.crypto_box_easy(message, nonce, B_pubkey, A_secretkey)
assert tohex(ct2) == tohex(ct1)

m1 = c.crypto_box_open_easy(ct1, nonce, A_pubkey, B_secretkey)
assert m1 == message

m2 = c.crypto_box_open_easy_afternm(ct1, nonce, k1)
assert m2 == message

with pytest.raises(CryptoError):
c.crypto_box_open_easy(message + b"!", nonce, A_pubkey, A_secretkey)


def test_box_wrong_lengths():
A_pubkey, A_secretkey = c.crypto_box_keypair()
with pytest.raises(ValueError):
Expand Down Expand Up @@ -154,6 +183,48 @@ def test_box_wrong_lengths():
c.crypto_box_open_afternm(b"", b"\x00" * c.crypto_box_NONCEBYTES, b"")


def test_box_easy_wrong_lengths():
A_pubkey, A_secretkey = c.crypto_box_keypair()
with pytest.raises(ValueError):
c.crypto_box_easy(b"abc", b"\x00", A_pubkey, A_secretkey)
with pytest.raises(ValueError):
c.crypto_box_easy(
b"abc", b"\x00" * c.crypto_box_NONCEBYTES, b"", A_secretkey
)
with pytest.raises(ValueError):
c.crypto_box_easy(
b"abc", b"\x00" * c.crypto_box_NONCEBYTES, A_pubkey, b""
)

with pytest.raises(ValueError):
c.crypto_box_open_easy(b"", b"", b"", b"")
with pytest.raises(ValueError):
c.crypto_box_open_easy(
b"", b"\x00" * c.crypto_box_NONCEBYTES, b"", b""
)
with pytest.raises(ValueError):
c.crypto_box_open_easy(
b"", b"\x00" * c.crypto_box_NONCEBYTES, A_pubkey, b""
)

with pytest.raises(ValueError):
c.crypto_box_beforenm(b"", b"")
with pytest.raises(ValueError):
c.crypto_box_beforenm(A_pubkey, b"")

with pytest.raises(ValueError):
c.crypto_box_easy_afternm(b"", b"", b"")
with pytest.raises(ValueError):
c.crypto_box_easy_afternm(b"", b"\x00" * c.crypto_box_NONCEBYTES, b"")

with pytest.raises(ValueError):
c.crypto_box_open_easy_afternm(b"", b"", b"")
with pytest.raises(ValueError):
c.crypto_box_open_easy_afternm(
b"", b"\x00" * c.crypto_box_NONCEBYTES, b""
)


def test_sign():
seed = b"\x00" * c.crypto_sign_SEEDBYTES
pubkey, secretkey = c.crypto_sign_seed_keypair(seed)
Expand Down

0 comments on commit a30c143

Please sign in to comment.