feat: Add key generation support to parameterized tests
This commit is contained in:
@@ -2,9 +2,11 @@ package fs
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -20,7 +22,8 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
verbose = true
|
||||
readOnly = false
|
||||
verbose = false
|
||||
signaturePassword = "testSignaturePassword"
|
||||
encryptionPassword = "testEncryptionPassword"
|
||||
)
|
||||
@@ -31,65 +34,67 @@ var (
|
||||
stfsConfigs = []stfsConfig{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
// TODO: Generate encryption and signature keys here and re-use them
|
||||
// for _, signature := range config.KnownSignatureFormats {
|
||||
// for _, encryption := range config.KnownEncryptionFormats {
|
||||
for _, compression := range config.KnownCompressionFormats {
|
||||
for _, compressionLevel := range config.KnownCompressionLevels {
|
||||
for _, writeCacheType := range config.KnownWriteCacheTypes {
|
||||
for _, fileSystemCacheType := range config.KnownFileSystemCacheTypes {
|
||||
for _, fileSystemCacheDuration := range fileSystemCacheDurations {
|
||||
for _, recordSize := range recordSizes {
|
||||
stfsConfigs = append(stfsConfigs, stfsConfig{
|
||||
recordSize,
|
||||
false,
|
||||
type stfsConfig struct {
|
||||
recordSize int
|
||||
readOnly bool
|
||||
|
||||
config.NoneKey, // encryption,
|
||||
config.NoneKey, // signature,
|
||||
compression,
|
||||
signature string
|
||||
signatureRecipient interface{}
|
||||
signatureIdentity interface{}
|
||||
|
||||
compressionLevel,
|
||||
encryption string
|
||||
encryptionRecipient interface{}
|
||||
encryptionIdentity interface{}
|
||||
|
||||
writeCacheType,
|
||||
fileSystemCacheType,
|
||||
compression string
|
||||
|
||||
fileSystemCacheDuration,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// }
|
||||
// }
|
||||
compressionLevel string
|
||||
|
||||
writeCache string
|
||||
fileSystemCache string
|
||||
|
||||
fileSystemCacheDuration time.Duration
|
||||
}
|
||||
|
||||
func createSTFS(
|
||||
drive string,
|
||||
metadata string,
|
||||
type stfsPermutation struct {
|
||||
recordSize int
|
||||
readOnly bool
|
||||
|
||||
recordSize int,
|
||||
readOnly bool,
|
||||
verbose bool,
|
||||
signature string
|
||||
encryption string
|
||||
compression string
|
||||
compressionLevel string
|
||||
|
||||
signature string,
|
||||
signaturePassword string,
|
||||
writeCache string
|
||||
fileSystemCache string
|
||||
|
||||
encryption string,
|
||||
encryptionPassword string,
|
||||
fileSystemCacheDuration time.Duration
|
||||
}
|
||||
|
||||
compression string,
|
||||
compressionLevel string,
|
||||
type fsConfig struct {
|
||||
stfsConfig stfsConfig
|
||||
fs afero.Fs
|
||||
cleanup func() error
|
||||
}
|
||||
|
||||
writeCache string,
|
||||
writeCacheDir string,
|
||||
type cryptoConfig struct {
|
||||
name string
|
||||
recipient interface{}
|
||||
identity interface{}
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
signatures := []cryptoConfig{}
|
||||
encryptions := []cryptoConfig{}
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
for _, signature := range config.KnownSignatureFormats {
|
||||
wg.Add(1)
|
||||
|
||||
go func(signature string) {
|
||||
log.Println("Generating signature keys for format", signature)
|
||||
|
||||
fileSystemCache string,
|
||||
fileSystemCacheDir string,
|
||||
fileSystemCacheDuration time.Duration,
|
||||
) (afero.Fs, error) {
|
||||
signaturePrivkey := []byte{}
|
||||
signaturePubkey := []byte{}
|
||||
|
||||
@@ -105,24 +110,37 @@ func createSTFS(
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
signatureRecipient, err := keys.ParseSignerRecipient(signature, signaturePubkey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
panic(err)
|
||||
}
|
||||
|
||||
signatureIdentity, err := keys.ParseSignerIdentity(signature, signaturePrivkey, signaturePassword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
panic(err)
|
||||
}
|
||||
|
||||
signatures = append(signatures, cryptoConfig{signature, signatureRecipient, signatureIdentity})
|
||||
|
||||
wg.Done()
|
||||
}(signature)
|
||||
}
|
||||
|
||||
for _, encryption := range config.KnownEncryptionFormats {
|
||||
wg.Add(1)
|
||||
|
||||
go func(encryption string) {
|
||||
log.Println("Generating encryption keys for format", encryption)
|
||||
|
||||
encryptionPrivkey := []byte{}
|
||||
encryptionPubkey := []byte{}
|
||||
|
||||
if encryption != config.NoneKey {
|
||||
var err error
|
||||
encryptionPrivkey, encryptionPubkey, err = utility.Keygen(
|
||||
config.PipeConfig{
|
||||
Signature: config.NoneKey,
|
||||
@@ -133,20 +151,97 @@ func createSTFS(
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
encryptionRecipient, err := keys.ParseRecipient(encryption, encryptionPubkey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
panic(err)
|
||||
}
|
||||
|
||||
encryptionIdentity, err := keys.ParseIdentity(encryption, encryptionPrivkey, encryptionPassword)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
panic(err)
|
||||
}
|
||||
|
||||
encryptions = append(encryptions, cryptoConfig{encryption, encryptionRecipient, encryptionIdentity})
|
||||
|
||||
wg.Done()
|
||||
}(encryption)
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
for _, signature := range signatures {
|
||||
for _, encryption := range encryptions {
|
||||
for _, compression := range config.KnownCompressionFormats {
|
||||
for _, compressionLevel := range config.KnownCompressionLevels {
|
||||
for _, writeCacheType := range config.KnownWriteCacheTypes {
|
||||
for _, fileSystemCacheType := range config.KnownFileSystemCacheTypes {
|
||||
for _, fileSystemCacheDuration := range fileSystemCacheDurations {
|
||||
for _, recordSize := range recordSizes {
|
||||
stfsConfigs = append(stfsConfigs, stfsConfig{
|
||||
recordSize,
|
||||
readOnly,
|
||||
|
||||
signature.name,
|
||||
signature.recipient,
|
||||
signature.identity,
|
||||
|
||||
encryption.name,
|
||||
encryption.recipient,
|
||||
encryption.identity,
|
||||
|
||||
compression,
|
||||
|
||||
compressionLevel,
|
||||
|
||||
writeCacheType,
|
||||
fileSystemCacheType,
|
||||
|
||||
fileSystemCacheDuration,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("Starting filesystem tests for", len(stfsConfigs), "filesystem permutations")
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
func createSTFS(
|
||||
drive string,
|
||||
metadata string,
|
||||
|
||||
recordSize int,
|
||||
readOnly bool,
|
||||
verbose bool,
|
||||
|
||||
signature string,
|
||||
signatureRecipient interface{},
|
||||
signatureIdentity interface{},
|
||||
|
||||
encryption string,
|
||||
encryptionRecipient interface{},
|
||||
encryptionIdentity interface{},
|
||||
|
||||
compression string,
|
||||
compressionLevel string,
|
||||
|
||||
writeCache string,
|
||||
writeCacheDir string,
|
||||
|
||||
fileSystemCache string,
|
||||
fileSystemCacheDir string,
|
||||
fileSystemCacheDuration time.Duration,
|
||||
) (afero.Fs, error) {
|
||||
tm := tape.NewTapeManager(
|
||||
drive,
|
||||
recordSize,
|
||||
@@ -253,47 +348,32 @@ func createSTFS(
|
||||
)
|
||||
}
|
||||
|
||||
type stfsConfig struct {
|
||||
recordSize int
|
||||
readOnly bool
|
||||
|
||||
signature string
|
||||
encryption string
|
||||
compression string
|
||||
|
||||
compressionLevel string
|
||||
|
||||
writeCache string
|
||||
fileSystemCache string
|
||||
|
||||
fileSystemCacheDuration time.Duration
|
||||
}
|
||||
|
||||
type fsConfig struct {
|
||||
stfsConfig stfsConfig
|
||||
fs afero.Fs
|
||||
}
|
||||
|
||||
func createFss() ([]fsConfig, func() error, error) {
|
||||
func createFss() ([]fsConfig, error) {
|
||||
fss := []fsConfig{}
|
||||
|
||||
tmp, err := os.MkdirTemp(os.TempDir(), "stfs-test-*")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
osfsDir := filepath.Join(tmp, "osfs")
|
||||
|
||||
if err := os.MkdirAll(osfsDir, os.ModePerm); err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fss = append(fss, fsConfig{stfsConfig{}, afero.NewBasePathFs(afero.NewOsFs(), osfsDir)})
|
||||
fss = append(fss, fsConfig{
|
||||
stfsConfig{},
|
||||
afero.NewBasePathFs(afero.NewOsFs(), osfsDir),
|
||||
func() error {
|
||||
return os.RemoveAll(tmp)
|
||||
},
|
||||
})
|
||||
|
||||
for _, config := range stfsConfigs {
|
||||
tmp, err := os.MkdirTemp(os.TempDir(), "stfs-test-*")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
drive := filepath.Join(tmp, "drive.tar")
|
||||
@@ -311,10 +391,12 @@ func createFss() ([]fsConfig, func() error, error) {
|
||||
verbose,
|
||||
|
||||
config.signature,
|
||||
signaturePassword,
|
||||
config.signatureRecipient,
|
||||
config.signatureIdentity,
|
||||
|
||||
config.encryption,
|
||||
encryptionPassword,
|
||||
config.encryptionRecipient,
|
||||
config.encryptionIdentity,
|
||||
|
||||
config.compression,
|
||||
config.compressionLevel,
|
||||
@@ -327,31 +409,55 @@ func createFss() ([]fsConfig, func() error, error) {
|
||||
config.fileSystemCacheDuration,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fss = append(fss, fsConfig{config, stfs})
|
||||
}
|
||||
|
||||
return fss,
|
||||
fss = append(fss, fsConfig{
|
||||
config,
|
||||
stfs,
|
||||
func() error {
|
||||
return os.RemoveAll(tmp)
|
||||
},
|
||||
nil
|
||||
})
|
||||
}
|
||||
|
||||
return fss, nil
|
||||
}
|
||||
|
||||
func runForAllFss(t *testing.T, name string, action func(t *testing.T, fs fsConfig)) {
|
||||
fss, cleanup, err := createFss()
|
||||
fss, err := createFss()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
t.Fatal(err)
|
||||
|
||||
return
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
for _, fs := range fss {
|
||||
t.Run(fmt.Sprintf(`%v for filesystem with config %v and name %v`, name, fs.stfsConfig, fs.fs.Name()), func(t *testing.T) {
|
||||
t.Run(fmt.Sprintf(`%v filesystem=%v config=%v`, name, fs.fs.Name(), stfsPermutation{
|
||||
fs.stfsConfig.recordSize,
|
||||
fs.stfsConfig.readOnly,
|
||||
|
||||
fs.stfsConfig.signature,
|
||||
fs.stfsConfig.encryption,
|
||||
fs.stfsConfig.compression,
|
||||
fs.stfsConfig.compressionLevel,
|
||||
|
||||
fs.stfsConfig.writeCache,
|
||||
fs.stfsConfig.fileSystemCache,
|
||||
|
||||
fs.stfsConfig.fileSystemCacheDuration,
|
||||
}), func(t *testing.T) {
|
||||
fs := fs
|
||||
|
||||
t.Parallel()
|
||||
|
||||
action(t, fs)
|
||||
|
||||
if err := fs.cleanup(); err != nil {
|
||||
t.Fatal(err)
|
||||
|
||||
return
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user