stupidgcm: deduplicate tests 2/2

Deduplicate the cipher setup that was identical
for all tests for each cipher.
This commit is contained in:
Jakob Unterwurzacher 2021-09-02 10:04:38 +02:00
parent 676a4ceb87
commit 961b8ca438
3 changed files with 24 additions and 104 deletions

View File

@ -3,10 +3,20 @@ package stupidgcm
import ( import (
"bytes" "bytes"
"crypto/cipher" "crypto/cipher"
"crypto/rand"
"encoding/hex" "encoding/hex"
"log"
"testing" "testing"
) )
func testCiphers(t *testing.T, c1 cipher.AEAD, c2 cipher.AEAD) {
t.Run("testEncryptDecrypt", func(t *testing.T) { testEncryptDecrypt(t, c1, c2) })
t.Run("testInplaceSeal", func(t *testing.T) { testInplaceSeal(t, c1, c2) })
t.Run("testInplaceOpen", func(t *testing.T) { testInplaceOpen(t, c1, c2) })
t.Run("testCorruption_c1", func(t *testing.T) { testCorruption(t, c1) })
t.Run("testCorruption_c2", func(t *testing.T) { testCorruption(t, c2) })
}
// testEncryptDecrypt encrypts and decrypts using both stupidgcm and Go's built-in // testEncryptDecrypt encrypts and decrypts using both stupidgcm and Go's built-in
// GCM implementation and verifies that the results are identical. // GCM implementation and verifies that the results are identical.
func testEncryptDecrypt(t *testing.T, c1 cipher.AEAD, c2 cipher.AEAD) { func testEncryptDecrypt(t *testing.T, c1 cipher.AEAD, c2 cipher.AEAD) {
@ -150,3 +160,13 @@ func testCorruption(t *testing.T, c cipher.AEAD) {
t.Fatalf("Should have gotten error") t.Fatalf("Should have gotten error")
} }
} }
// Get "n" random bytes from /dev/urandom or panic
func randBytes(n int) []byte {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
log.Panic("Failed to read random bytes: " + err.Error())
}
return b
}

View File

@ -11,9 +11,7 @@ import (
"golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/chacha20poly1305"
) )
// TestEncryptDecrypt encrypts and decrypts using both stupidgcm and Go's built-in func TestStupidChacha20poly1305(t *testing.T) {
// GCM implementation and verifies that the results are identical.
func TestEncryptDecryptChacha(t *testing.T) {
key := randBytes(32) key := randBytes(32)
c := newChacha20poly1305(key) c := newChacha20poly1305(key)
ref, err := chacha20poly1305.New(key) ref, err := chacha20poly1305.New(key)
@ -21,42 +19,5 @@ func TestEncryptDecryptChacha(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
testEncryptDecrypt(t, c, ref) testCiphers(t, c, ref)
}
// Seal re-uses the "dst" buffer it is large enough.
// Check that this works correctly by testing different "dst" capacities from
// 5000 to 16 and "in" lengths from 1 to 5000.
func TestInplaceSealChacha(t *testing.T) {
key := randBytes(32)
c := newChacha20poly1305(key)
ref, err := chacha20poly1305.New(key)
if err != nil {
t.Fatal(err)
}
testInplaceSeal(t, c, ref)
}
// Open re-uses the "dst" buffer it is large enough.
// Check that this works correctly by testing different "dst" capacities from
// 5000 to 16 and "in" lengths from 1 to 5000.
func TestInplaceOpenChacha(t *testing.T) {
key := randBytes(32)
c := newChacha20poly1305(key)
ref, err := chacha20poly1305.New(key)
if err != nil {
t.Fatal(err)
}
testInplaceOpen(t, c, ref)
}
// TestCorruption verifies that changes in the ciphertext result in a decryption
// error
func TestCorruptionChacha(t *testing.T) {
key := randBytes(32)
c := newChacha20poly1305(key)
testCorruption(t, c)
} }

View File

@ -8,24 +8,10 @@ package stupidgcm
import ( import (
"crypto/aes" "crypto/aes"
"crypto/cipher" "crypto/cipher"
"crypto/rand"
"log"
"testing" "testing"
) )
// Get "n" random bytes from /dev/urandom or panic func TestStupidGCM(t *testing.T) {
func randBytes(n int) []byte {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
log.Panic("Failed to read random bytes: " + err.Error())
}
return b
}
// TestEncryptDecrypt encrypts and decrypts using both stupidgcm and Go's built-in
// GCM implementation and verifies that the results are identical.
func TestEncryptDecrypt(t *testing.T) {
key := randBytes(32) key := randBytes(32)
sGCM := New(key, false) sGCM := New(key, false)
@ -38,52 +24,5 @@ func TestEncryptDecrypt(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
testEncryptDecrypt(t, sGCM, gGCM) testCiphers(t, sGCM, gGCM)
}
// Seal re-uses the "dst" buffer it is large enough.
// Check that this works correctly by testing different "dst" capacities from
// 5000 to 16 and "in" lengths from 1 to 5000.
func TestInplaceSeal(t *testing.T) {
key := randBytes(32)
sGCM := New(key, false)
gAES, err := aes.NewCipher(key)
if err != nil {
t.Fatal(err)
}
gGCM, err := cipher.NewGCMWithNonceSize(gAES, 16)
if err != nil {
t.Fatal(err)
}
testInplaceSeal(t, sGCM, gGCM)
}
// Open re-uses the "dst" buffer it is large enough.
// Check that this works correctly by testing different "dst" capacities from
// 5000 to 16 and "in" lengths from 1 to 5000.
func TestInplaceOpen(t *testing.T) {
key := randBytes(32)
sGCM := New(key, false)
gAES, err := aes.NewCipher(key)
if err != nil {
t.Fatal(err)
}
gGCM, err := cipher.NewGCMWithNonceSize(gAES, 16)
if err != nil {
t.Fatal(err)
}
testInplaceOpen(t, sGCM, gGCM)
}
// TestCorruption verifies that changes in the ciphertext result in a decryption
// error
func TestCorruption(t *testing.T) {
key := randBytes(32)
sGCM := New(key, false)
testCorruption(t, sGCM)
} }