2020-07-12 05:29:00 +02:00
|
|
|
package lnencrypt
|
2018-12-10 03:39:31 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
|
2020-07-12 05:29:00 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-10 03:39:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// TestEncryptDecryptPayload tests that given a static key, we're able to
|
|
|
|
// properly decrypt and encrypted payload. We also test that we'll reject a
|
|
|
|
// ciphertext that has been modified.
|
|
|
|
func TestEncryptDecryptPayload(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
payloadCases := []struct {
|
|
|
|
// plaintext is the string that we'll be encrypting.
|
|
|
|
plaintext []byte
|
|
|
|
|
|
|
|
// mutator allows a test case to modify the ciphertext before
|
|
|
|
// we attempt to decrypt it.
|
|
|
|
mutator func(*[]byte)
|
|
|
|
|
|
|
|
// valid indicates if this test should pass or fail.
|
|
|
|
valid bool
|
|
|
|
}{
|
|
|
|
// Proper payload, should decrypt.
|
|
|
|
{
|
|
|
|
plaintext: []byte("payload test plain text"),
|
|
|
|
mutator: nil,
|
|
|
|
valid: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Mutator modifies cipher text, shouldn't decrypt.
|
|
|
|
{
|
|
|
|
plaintext: []byte("payload test plain text"),
|
|
|
|
mutator: func(p *[]byte) {
|
|
|
|
// Flip a byte in the payload to render it invalid.
|
|
|
|
(*p)[0] ^= 1
|
|
|
|
},
|
|
|
|
valid: false,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Cipher text is too small, shouldn't decrypt.
|
|
|
|
{
|
|
|
|
plaintext: []byte("payload test plain text"),
|
|
|
|
mutator: func(p *[]byte) {
|
|
|
|
// Modify the cipher text to be zero length.
|
|
|
|
*p = []byte{}
|
|
|
|
},
|
|
|
|
valid: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-07-12 05:29:00 +02:00
|
|
|
keyRing := &MockKeyRing{}
|
2018-12-10 03:39:31 +01:00
|
|
|
|
|
|
|
for i, payloadCase := range payloadCases {
|
|
|
|
var cipherBuffer bytes.Buffer
|
2020-07-12 05:29:00 +02:00
|
|
|
encrypter, err := KeyRingEncrypter(keyRing)
|
|
|
|
require.NoError(t, err)
|
2018-12-10 03:39:31 +01:00
|
|
|
|
|
|
|
// First, we'll encrypt the passed payload with our scheme.
|
2020-07-12 05:29:00 +02:00
|
|
|
err = encrypter.EncryptPayloadToWriter(
|
|
|
|
payloadCase.plaintext, &cipherBuffer,
|
2018-12-10 03:39:31 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable encrypt paylaod: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a mutator, then we'll wrong the mutator over the
|
|
|
|
// cipher text, then reset the main buffer and re-write the new
|
|
|
|
// cipher text.
|
|
|
|
if payloadCase.mutator != nil {
|
|
|
|
cipherText := cipherBuffer.Bytes()
|
|
|
|
|
|
|
|
payloadCase.mutator(&cipherText)
|
|
|
|
|
|
|
|
cipherBuffer.Reset()
|
|
|
|
cipherBuffer.Write(cipherText)
|
|
|
|
}
|
|
|
|
|
2020-07-12 05:29:00 +02:00
|
|
|
plaintext, err := encrypter.DecryptPayloadFromReader(
|
|
|
|
&cipherBuffer,
|
|
|
|
)
|
2018-12-10 03:39:31 +01:00
|
|
|
|
|
|
|
switch {
|
|
|
|
// If this was meant to be a valid decryption, but we failed,
|
|
|
|
// then we'll return an error.
|
|
|
|
case err != nil && payloadCase.valid:
|
|
|
|
t.Fatalf("unable to decrypt valid payload case %v", i)
|
|
|
|
|
|
|
|
// If this was meant to be an invalid decryption, and we didn't
|
|
|
|
// fail, then we'll return an error.
|
|
|
|
case err == nil && !payloadCase.valid:
|
|
|
|
t.Fatalf("payload was invalid yet was able to decrypt")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only if this case was mean to be valid will we ensure the
|
|
|
|
// resulting decrypted plaintext matches the original input.
|
|
|
|
if payloadCase.valid &&
|
|
|
|
!bytes.Equal(plaintext, payloadCase.plaintext) {
|
|
|
|
t.Fatalf("#%v: expected %v, got %v: ", i,
|
|
|
|
payloadCase.plaintext, plaintext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-12 05:29:00 +02:00
|
|
|
// TestInvalidKeyGeneration tests that key generation fails when deriving the
|
|
|
|
// key fails.
|
|
|
|
func TestInvalidKeyGeneration(t *testing.T) {
|
2018-12-10 03:39:31 +01:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-07-12 05:29:00 +02:00
|
|
|
_, err := KeyRingEncrypter(&MockKeyRing{true})
|
2018-12-10 03:39:31 +01:00
|
|
|
if err == nil {
|
2020-07-12 05:29:00 +02:00
|
|
|
t.Fatal("expected error due to fail key gen")
|
2018-12-10 03:39:31 +01:00
|
|
|
}
|
|
|
|
}
|