go_encryption/global_test.go
2024-05-10 21:23:51 +02:00

157 lines
4.5 KiB
Go

package go_encryption
import (
"bytes"
"encoding/hex"
"os"
"path/filepath"
"strings"
"sync"
"testing"
)
func Test_GlobalSetProvider(t *testing.T) {
SetProvider(NewAES128EncryptionProvider())
wg := sync.WaitGroup{}
wg.Add(1)
var err any
go func() {
defer func() {
err = recover()
wg.Done()
}()
SetProvider(nil)
}()
wg.Wait()
if err == nil {
t.FailNow()
}
}
func Test_GlobalSetEncryptionKey(t *testing.T) {
binaryKey := append([]byte{}, std.(*aes128encryption).encryptionKey...)
SetEncryptionKey(binaryKey)
if !strings.EqualFold(defaultEncryptionKey, hex.EncodeToString(std.(*aes128encryption).encryptionKey)) {
t.Error("Test_GlobalSetEncryptionKey: test case 1: encryption key should not have changed")
}
binaryKey[4] = 42
binaryKey[5] = 42
binaryKey[6] = 42
SetEncryptionKey(binaryKey)
if strings.EqualFold(defaultEncryptionKey, hex.EncodeToString(std.(*aes128encryption).encryptionKey)) {
t.Error("Test_GlobalSetEncryptionKey: test case 2: encryption key was not changed")
}
binaryKey = binaryKey[:3]
SetEncryptionKey(binaryKey)
if strings.EqualFold(defaultEncryptionKey, hex.EncodeToString(std.(*aes128encryption).encryptionKey)) {
t.Error("Test_GlobalSetEncryptionKey: test case 3: encryption key was not changed")
}
if binaryKey, err := hex.DecodeString(defaultEncryptionKey); err == nil {
SetEncryptionKey(binaryKey)
if !strings.EqualFold(defaultEncryptionKey, hex.EncodeToString(std.(*aes128encryption).encryptionKey)) {
t.Error("Test_GlobalSetEncryptionKey: test case 4.2: Failed to reset encryption key")
}
} else {
t.Error("Test_GlobalSetEncryptionKey: test case 4.1: Failed to reset encryption key")
}
}
func Fuzz_GlobalSetContext(f *testing.F) {
testCases := [][]byte{
nil,
{},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17},
}
for i := 0; i < 5; i++ {
testCases = append(testCases, randomByteArray())
}
for _, tc := range testCases {
f.Add(tc)
}
f.Fuzz(func(t *testing.T, a []byte) {
result := SetContext(a)
if 12 > len(a) || len(a) > 16 {
if result == nil {
t.Errorf("got no error for %v", a)
}
} else if result != nil {
t.Errorf("got an error for %v", a)
}
})
if SetContext(defaultContext) != nil {
f.Error("unable to reset context")
}
}
func Test_GlobalEncryptData(t *testing.T) {
if encrypted, err := EncryptData(defaultTestStruct); err != nil {
t.Error("unexpected error in default data encryption")
} else if !bytes.Equal(encrypted, []byte(defaultTestStructEncrypted)) {
t.Error("unexpected data for encrypted content")
}
}
func Test_GlobalDecryptData(t *testing.T) {
target := &testStruct{}
if err := DecryptData([]byte(defaultTestStructEncrypted), target); err != nil {
t.Error("unexpected error in default data decryption")
} else if target.BoolVal != defaultTestStruct.BoolVal ||
target.FloatVal != defaultTestStruct.FloatVal ||
target.IntVal != defaultTestStruct.IntVal ||
target.StringVal != defaultTestStruct.StringVal {
t.Error("retrieved struct does not match the expectations")
}
}
func getTestFilePathGlobal(encrypt bool) string {
fileName := "global_encryption_test_encrypted.data"
if !encrypt {
fileName = "global_encryption_test_decryptable.data"
}
return filepath.Join(os.Getenv("temp"), fileName)
}
func Test_GlobalEncryptToFile(t *testing.T) {
targetPath := getTestFilePathGlobal(true)
if err := EncryptToFile(defaultTestStruct, targetPath); err != nil {
t.Error("unexpected error in default encryption")
} else if encrypted, errRead := os.ReadFile(targetPath); errRead == nil {
os.Remove(targetPath)
if !bytes.Equal(encrypted, []byte(defaultTestStructEncrypted)) {
t.Error("unexpected data for encrypted content")
}
} else {
t.Errorf("expected file could not be read: %s", errRead)
if !os.IsNotExist(errRead) {
os.Remove(targetPath)
}
}
}
func Test_GlobalDecryptFile(t *testing.T) {
sourcePath := getTestFilePathGlobal(false)
if os.WriteFile(sourcePath, []byte(defaultTestStructEncrypted), os.ModePerm) == nil {
target := &testStruct{}
if err := DecryptFile(sourcePath, target); err != nil {
t.Error("unexpected error in default decryption")
} else if target.BoolVal != defaultTestStruct.BoolVal ||
target.FloatVal != defaultTestStruct.FloatVal ||
target.IntVal != defaultTestStruct.IntVal ||
target.StringVal != defaultTestStruct.StringVal {
t.Error("retrieved struct does not match the expectations")
}
os.Remove(sourcePath)
} else {
t.Error("could not prepare test file for decryption")
}
}