1
0
Fork 0
mirror of https://github.com/Luzifer/go-openssl.git synced 2024-12-29 23:31:21 +00:00

Test: Sort tests

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-04-29 16:48:46 +02:00
parent 32b48c865b
commit a261c590f2
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E

View file

@ -18,90 +18,19 @@ var testTable = []struct {
{"SHA256", "sha256", DigestSHA256Sum},
}
func TestDecryptFromString(t *testing.T) {
func TestBinaryEncryptToDecryptWithCustomSalt(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
salt := []byte("saltsalt")
o := New()
for _, tc := range testTable {
t.Run(tc.tName, func(t *testing.T) {
var out bytes.Buffer
cmd := exec.Command(
"openssl", "aes-256-cbc",
"-base64",
"-pass", fmt.Sprintf("pass:%s", passphrase),
"-md", tc.tMdParam,
)
cmd.Stdout = &out
cmd.Stdin = strings.NewReader(plaintext)
if err := cmd.Run(); err != nil {
t.Fatalf("Running openssl CLI failed: %v", err)
}
data, err := o.DecryptBytes(passphrase, out.Bytes(), tc.tMdFunc)
if err != nil {
t.Fatalf("Decryption failed: %v", err)
}
if string(data) != plaintext {
t.Logf("Data: %s\nPlaintext: %s", string(data), plaintext)
t.Errorf("Decryption output did not equal expected output.")
}
})
}
}
func TestDecryptBinaryFromString(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
o := New()
for _, tc := range testTable {
t.Run(tc.tName, func(t *testing.T) {
var out bytes.Buffer
cmd := exec.Command(
"openssl", "aes-256-cbc",
"-pass", fmt.Sprintf("pass:%s", passphrase),
"-md", tc.tMdParam,
"-in", "/dev/stdin",
)
cmd.Stdout = &out
cmd.Stdin = strings.NewReader(plaintext)
if err := cmd.Run(); err != nil {
t.Fatalf("Running openssl CLI failed: %v", err)
}
data, err := o.DecryptBinaryBytes(passphrase, out.Bytes(), tc.tMdFunc)
if err != nil {
t.Fatalf("Decryption failed: %v", err)
}
if string(data) != plaintext {
t.Logf("Data: %s\nPlaintext: %s", string(data), plaintext)
t.Errorf("Decryption output did not equal expected output.")
}
})
}
}
func TestEncryptToDecrypt(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
o := New()
enc, err := o.EncryptBytes(passphrase, []byte(plaintext), DigestSHA256Sum)
enc, err := o.EncryptBinaryBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
dec, err := o.DecryptBytes(passphrase, enc, DigestSHA256Sum)
dec, err := o.DecryptBinaryBytes(passphrase, enc, DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at decrypt: %s", err)
}
@ -132,138 +61,6 @@ func TestBinaryEncryptToDecrypt(t *testing.T) {
}
}
func TestEncryptToDecryptWithCustomSalt(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
salt := []byte("saltsalt")
o := New()
enc, err := o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
dec, err := o.DecryptBytes(passphrase, enc, DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at decrypt: %s", err)
}
if string(dec) != plaintext {
t.Errorf("Decrypted text did not match input.")
}
}
func TestBinaryEncryptToDecryptWithCustomSalt(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
salt := []byte("saltsalt")
o := New()
enc, err := o.EncryptBinaryBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
dec, err := o.DecryptBinaryBytes(passphrase, enc, DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at decrypt: %s", err)
}
if string(dec) != plaintext {
t.Errorf("Decrypted text did not match input.")
}
}
func TestEncryptWithSaltShouldHaveSameOutput(t *testing.T) {
plaintext := "outputshouldbesame"
passphrase := "passphrasesupersecure"
salt := []byte("saltsalt")
o := New()
enc1, err := o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
enc2, err := o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
if string(enc1) != string(enc2) {
t.Errorf("Encrypted outputs are not same.")
}
}
func TestBinaryEncryptWithSaltShouldHaveSameOutput(t *testing.T) {
plaintext := "outputshouldbesame"
passphrase := "passphrasesupersecure"
salt := []byte("saltsalt")
o := New()
enc1, err := o.EncryptBinaryBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
enc2, err := o.EncryptBinaryBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
if string(enc1) != string(enc2) {
t.Errorf("Encrypted outputs are not same.")
}
}
func TestEncryptToOpenSSL(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
for _, tc := range testTable {
t.Run(tc.tName, func(t *testing.T) {
o := New()
salt, err := o.GenerateSalt()
if err != nil {
t.Fatalf("Failed to generate salt: %s", err)
}
enc, err := o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), tc.tMdFunc)
if err != nil {
t.Fatalf("Test errored at encrypt (%s): %s", tc.tMdParam, err)
}
enc = append(enc, '\n')
var out bytes.Buffer
cmd := exec.Command(
"openssl", "aes-256-cbc",
"-base64", "-d",
"-pass", fmt.Sprintf("pass:%s", passphrase),
"-md", tc.tMdParam,
"-in", "/dev/stdin",
)
cmd.Stdout = &out
cmd.Stdin = bytes.NewReader(enc)
err = cmd.Run()
if err != nil {
t.Errorf("OpenSSL errored (%s): %s", tc.tMdParam, err)
}
if out.String() != plaintext {
t.Errorf("OpenSSL output did not match input.\nOutput was (%s): %s", tc.tMdParam, out.String())
}
})
}
}
func TestBinaryEncryptToOpenSSL(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
@ -308,6 +105,209 @@ func TestBinaryEncryptToOpenSSL(t *testing.T) {
}
}
func TestBinaryEncryptWithSaltShouldHaveSameOutput(t *testing.T) {
plaintext := "outputshouldbesame"
passphrase := "passphrasesupersecure"
salt := []byte("saltsalt")
o := New()
enc1, err := o.EncryptBinaryBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
enc2, err := o.EncryptBinaryBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
if string(enc1) != string(enc2) {
t.Errorf("Encrypted outputs are not same.")
}
}
func TestDecryptBinaryFromString(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
o := New()
for _, tc := range testTable {
t.Run(tc.tName, func(t *testing.T) {
var out bytes.Buffer
cmd := exec.Command(
"openssl", "aes-256-cbc",
"-pass", fmt.Sprintf("pass:%s", passphrase),
"-md", tc.tMdParam,
"-in", "/dev/stdin",
)
cmd.Stdout = &out
cmd.Stdin = strings.NewReader(plaintext)
if err := cmd.Run(); err != nil {
t.Fatalf("Running openssl CLI failed: %v", err)
}
data, err := o.DecryptBinaryBytes(passphrase, out.Bytes(), tc.tMdFunc)
if err != nil {
t.Fatalf("Decryption failed: %v", err)
}
if string(data) != plaintext {
t.Logf("Data: %s\nPlaintext: %s", string(data), plaintext)
t.Errorf("Decryption output did not equal expected output.")
}
})
}
}
func TestDecryptFromString(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
o := New()
for _, tc := range testTable {
t.Run(tc.tName, func(t *testing.T) {
var out bytes.Buffer
cmd := exec.Command(
"openssl", "aes-256-cbc",
"-base64",
"-pass", fmt.Sprintf("pass:%s", passphrase),
"-md", tc.tMdParam,
)
cmd.Stdout = &out
cmd.Stdin = strings.NewReader(plaintext)
if err := cmd.Run(); err != nil {
t.Fatalf("Running openssl CLI failed: %v", err)
}
data, err := o.DecryptBytes(passphrase, out.Bytes(), tc.tMdFunc)
if err != nil {
t.Fatalf("Decryption failed: %v", err)
}
if string(data) != plaintext {
t.Logf("Data: %s\nPlaintext: %s", string(data), plaintext)
t.Errorf("Decryption output did not equal expected output.")
}
})
}
}
func TestEncryptToDecrypt(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
o := New()
enc, err := o.EncryptBytes(passphrase, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
dec, err := o.DecryptBytes(passphrase, enc, DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at decrypt: %s", err)
}
if string(dec) != plaintext {
t.Errorf("Decrypted text did not match input.")
}
}
func TestEncryptToDecryptWithCustomSalt(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
salt := []byte("saltsalt")
o := New()
enc, err := o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
dec, err := o.DecryptBytes(passphrase, enc, DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at decrypt: %s", err)
}
if string(dec) != plaintext {
t.Errorf("Decrypted text did not match input.")
}
}
func TestEncryptToOpenSSL(t *testing.T) {
plaintext := "hallowelt"
passphrase := "z4yH36a6zerhfE5427ZV"
for _, tc := range testTable {
t.Run(tc.tName, func(t *testing.T) {
o := New()
salt, err := o.GenerateSalt()
if err != nil {
t.Fatalf("Failed to generate salt: %s", err)
}
enc, err := o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), tc.tMdFunc)
if err != nil {
t.Fatalf("Test errored at encrypt (%s): %s", tc.tMdParam, err)
}
enc = append(enc, '\n')
var out bytes.Buffer
cmd := exec.Command(
"openssl", "aes-256-cbc",
"-base64", "-d",
"-pass", fmt.Sprintf("pass:%s", passphrase),
"-md", tc.tMdParam,
"-in", "/dev/stdin",
)
cmd.Stdout = &out
cmd.Stdin = bytes.NewReader(enc)
err = cmd.Run()
if err != nil {
t.Errorf("OpenSSL errored (%s): %s", tc.tMdParam, err)
}
if out.String() != plaintext {
t.Errorf("OpenSSL output did not match input.\nOutput was (%s): %s", tc.tMdParam, out.String())
}
})
}
}
func TestEncryptWithSaltShouldHaveSameOutput(t *testing.T) {
plaintext := "outputshouldbesame"
passphrase := "passphrasesupersecure"
salt := []byte("saltsalt")
o := New()
enc1, err := o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
enc2, err := o.EncryptBytesWithSaltAndDigestFunc(passphrase, salt, []byte(plaintext), DigestSHA256Sum)
if err != nil {
t.Fatalf("Test errored at encrypt: %s", err)
}
if string(enc1) != string(enc2) {
t.Errorf("Encrypted outputs are not same.")
}
}
func TestGenerateSalt(t *testing.T) {
knownSalts := [][]byte{}
@ -347,6 +347,10 @@ func TestSaltValidation(t *testing.T) {
}
}
//
// Benchmarks
//
func benchmarkDecrypt(ciphertext []byte, kdf DigestFunc, b *testing.B) {
passphrase := "z4yH36a6zerhfE5427ZV"
o := New()