Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test/initial testing round #15

Merged
merged 8 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 72 additions & 1 deletion tests/crypto/test_askar.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from aries_askar import Key, KeyAlg
from pydid import VerificationMethod
import pytest

from didcomm_messaging.crypto.backend.askar import (
Expand All @@ -21,6 +22,7 @@ def crypto():

@pytest.mark.asyncio
async def test_1pu_round_trip(crypto: AskarCryptoService):
"""Authcrypt."""
alg = KeyAlg.X25519
alice_sk = Key.generate(alg)
alice_pk = Key.from_jwk(alice_sk.get_jwk_public())
Expand All @@ -39,7 +41,7 @@ async def test_1pu_round_trip(crypto: AskarCryptoService):

@pytest.mark.asyncio
async def test_es_round_trip(crypto: AskarCryptoService):
"""Test ECDH-ES round trip."""
"""Test ECDH-ES (Anoncrypt) round trip."""
alg = KeyAlg.X25519
alice_sk = Key.generate(alg)
alice_pk = Key.from_jwk(alice_sk.get_jwk_public())
Expand All @@ -49,6 +51,75 @@ async def test_es_round_trip(crypto: AskarCryptoService):
bob_priv_key = AskarSecretKey(bob_sk, BOB_KID)
alice_key = AskarKey(alice_sk, ALICE_KID)
alice_priv_key = AskarSecretKey(alice_sk, ALICE_KID)

enc_message = await crypto.ecdh_es_encrypt([bob_key], MESSAGE)

plaintext = await crypto.ecdh_es_decrypt(enc_message, bob_priv_key)
assert plaintext == MESSAGE


# to test store/secrets manager stuff
# store = await Store.open("sqlite::memory:")
# async with store.session as session():
# await session.insert_key("kid", alice_sk)


# TODO @pytest.mark.parametrize for the different dicts? See below for more examples
@pytest.mark.asyncio
async def test_key_from_verification_method():
vm = VerificationMethod.deserialize(
{
"id": "#6LSqPZfn",
"type": "X25519KeyAgreementKey2020",
"publicKeyMultibase": "z6LSqPZfn9krvgXma2icTMKf2uVcYhKXsudCmPoUzqGYW24U",
"controller": "did:example:123",
}
)

key = AskarCryptoService.verification_method_to_public_key(vm)
print(key.kid)
assert type(key) == AskarKey
assert key.kid.startswith("did:example:123")


# Verification Methods
# {
# "id": "#6LSqPZfn",
# "type": "X25519KeyAgreementKey2020",
# "publicKeyMultibase": "z6LSqPZfn9krvgXma2icTMKf2uVcYhKXsudCmPoUzqGYW24U"
# },
# {
# "id": "#6MkrCD1c",
# "type": "Ed25519VerificationKey2020",
# "publicKeyMultibase": "z6MkrCD1csqtgdj8sjrsu8jxcbeyP6m7LiK87NzhfWqio5yr"
# }

# {
# "id": "#1",
# "type": "Ed25519VerificationKey2018",
# "publicKeyBase58": "AU2FFjtkVzjFuirgWieqGGqtNrAZWS9LDuB8TDp6EUrG"
# }

# {
# "id": "#1",
# "type": "Ed25519VerificationKey2018",
# "publicKeyBase58": "3dtu2WWtd5ELwRTJEPzmEJUYEp8Qq36N2QA24g9tFXK9",
# "controller": "did:peer:4zQmRMVzDUXhV64pfw3vFaDvyExjzW9oBXCF2n4zYCaHQFAT:zMx3zwMnDECV3GiFs8nmHr38TziMEEkcgFBEDH5PXQ8hxnMrwNfB9wTwskpJMjggeg8NF1jeDSK5772op2zLLdy8TGFCEiYQxpUvvku8qSCZx5Q8V9Li9mDp6WEqGabXLQ9GTinmyQHQyJ6TcfbHaTtJUFjHS962LFPdUwv3aDK673Pci2doTyHVTAsw4m5eToS2dKbtix9f7HNxwvixnbQucWNAWVAF6HTxFYRYmrRPDmeE8n7V1fXFkY7yvR6BWxKiWwHd8Vb1TbBBRStf5niRM2dUAyjJorTstPWSfG2pN5DsRF81NUd7Aif4EhNAQEJCTuAHxQ3rCnNkb9Pf7YTTxbt1t25YgDMioDi4uFhYcnTbHj7D74yNPC2Cfk6WasU69hMxj7Wxro58vtkA6hvDWGtnDyX4PzntBp3fn62R25HW2jadsZMiJpm5ufpYSktEFEHX6gGeF4KPgyU8b2hhyS3FKL4DULYLB6d6CZqUpwrJesGfDtFjfG1btbdmjd6Lm7FCbL3fU9E3AJWEmnFkg16vARiQ1CrzeS9SyNtybKCk4"
# }

# {
# "id": "#z6MkrmNwty5ajKtFqc1U48oL2MMLjWjartwc5sf2AihZwXDN",
# "type": "Ed25519VerificationKey2018",
# "publicKeyBase58": "DK7uJiq9PnPnj7AmNZqVBFoLuwTjT1hFPrk6LSjZ2JRz"
# }

# Here's a case that we probably don't support right now:
# {
# "type": "Ed25519VerificationKey2018",
# "publicKeyJwk": {
# "kty": "OKP",
# "crv": "Ed25519",
# "x": "UTBElpNSZB8dS_R9rzWnWB-ozdtL7Sz96RQZhwnzur8"
# },
# "id": "#z6MkjvBkt8ETnxXGBFPSGgYKb43q7oNHLX8BiYSPcXVG6gY6"
# }
30 changes: 30 additions & 0 deletions tests/test_didresolver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import pytest

from didcomm_messaging.resolver import (
DIDMethodNotSupported,
DIDResolver,
PrefixResolver,
)


class TestResolver(DIDResolver):
async def resolve(self, did: str) -> dict:
return {"did": did}


@pytest.fixture(scope="session")
def test_resolver():
yield TestResolver()


@pytest.mark.asyncio
async def test_prefix_resolver(test_resolver: DIDResolver):
did = "did:test:example_did"

resolver = PrefixResolver(resolvers={"did:test:": test_resolver})

doc = await resolver.resolve(did)
assert doc["did"] == did

with pytest.raises(DIDMethodNotSupported):
await resolver.resolve("This won't work")
34 changes: 34 additions & 0 deletions tests/test_secrets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pytest
from didcomm_messaging.crypto import SecretKey

from didcomm_messaging.crypto.backend.basic import InMemorySecretsManager


class MockSecretKey(SecretKey):
def __init__(self, kid) -> None:
self._kid = kid

@property
def kid(self):
return self._kid


@pytest.fixture()
def in_memory_secrets_manager():
yield InMemorySecretsManager()


@pytest.fixture()
def secret():
kid = "did:example:alice#key-1"
key = MockSecretKey(kid=kid)
yield key


@pytest.mark.asyncio
async def test_in_memory_secrets(
in_memory_secrets_manager: InMemorySecretsManager, secret: MockSecretKey
):
await in_memory_secrets_manager.add_secret(secret)

assert await in_memory_secrets_manager.get_secret_by_kid(secret.kid) == secret