Skip to main content

E2E Encryption — Testing

Test strategy

Encryption tests verify cryptographic correctness of key generation, message encryption/decryption, key wrapping, and transport-layer encryption. These are primarily unit tests on the crypto service layer and do not require a running server.


Unit tests — crypto services

Key generation (peer-key-service.ts)

ScenarioExpected result
Generate key pairReturns { publicKey: Uint8Array(32), secretKey: Uint8Array(32) }
Two calls produce different key pairsPublic keys are not equal
Public key lengthExactly 32 bytes (Curve25519)

Key derivation (key-derivation.ts)

ScenarioExpected result
Same password + same salt → same DeviceKeyKeys are equal
Different password → different DeviceKeyKeys are not equal
Different salt → different DeviceKeyKeys are not equal

Key wrapping / unwrapping (local-encryption-service.ts)

ScenarioExpected result
Wrap private key with DeviceKeyReturns ciphertext + nonce
Unwrap with same DeviceKeyReturns original private key
Unwrap with wrong DeviceKeyReturns null or throws
Wrapped blob differs from plaintext keywrappedKey !== secretKey

Message encryption (tcp-encryption.ts, ws-encryption.ts)

ScenarioExpected result
Encrypt with Alice's private + Bob's publicCiphertext ≠ plaintext
Decrypt with Bob's private + Alice's publicReturns original plaintext
Decrypt with wrong key pairReturns null
Same plaintext encrypted twiceDifferent ciphertexts (random nonce)
Tampered ciphertextDecryption returns null

Integration tests — key registration and lookup

ScenarioExpected result
POST /keys/register with valid public key200, signed_credential returned
GET /keys/<peer_id> after registration200, returns registered public key
GET /keys/<peer_id> for unknown peer404 Not Found
SERVER_ED25519_SEED setsigned_credential is a valid Ed25519 signature
SERVER_ED25519_SEED unsetsigned_credential is null or absent

Key recovery round-trip tests

ScenarioExpected result
Wrap WrappedKey under recovery credentialProduces distinct ciphertext
Unwrap with correct recovery credentialReturns original WrappedKey
Unwrap with wrong recovery credentialReturns null
Full round-trip: wrap → store → retrieve → unwrapOriginal private key recovered

Coverage targets

  • Key generation: 100% branch coverage.
  • Encrypt/decrypt: happy path, wrong key, tampered ciphertext — all branches covered.
  • Key derivation: same-input determinism and different-input divergence verified.
  • No test may log or assert on real private key material.

Test conventions

  • Use tweetnacl.box.keyPair() to generate synthetic key pairs in tests.
  • Mock expo-secure-store — do not write to the device keystore in unit tests.
  • Reset peer_key and wrapped_key tables between integration test runs.