package go_pgp import ( "crypto" "math/rand" "testing" "github.com/ProtonMail/go-crypto/openpgp/packet" ) func TestHashOption(t *testing.T) { testCases := []struct { in crypto.Hash }{ {}, {crypto.MD4}, {crypto.MD5}, {crypto.SHA1}, {crypto.SHA224}, {crypto.SHA256}, {crypto.SHA384}, {crypto.SHA512}, {crypto.MD5SHA1}, {crypto.RIPEMD160}, {crypto.SHA3_224}, {crypto.SHA3_256}, {crypto.SHA3_384}, {crypto.SHA3_512}, {crypto.SHA512_224}, {crypto.SHA512_256}, {crypto.BLAKE2b_256}, {crypto.BLAKE2b_384}, {crypto.BLAKE2b_512}, } for i, testCase := range testCases { testConfig := defaultConfig optionFunc := HashOption(testCase.in) optionFunc(&testConfig) if testCase.in.Available() { if testConfig.DefaultHash != testCase.in { t.Errorf("TestHashOption: test case %d: expected hash %d but got %d", i+1, testCase.in, testConfig.DefaultHash) } } else if testConfig.DefaultHash != 0 { t.Errorf("TestHashOption: test case %d: expected hash 0 but got %d", i+1, testConfig.DefaultHash) } } } func TestCipherOption(t *testing.T) { testCases := []struct { in packet.CipherFunction }{ {}, {packet.Cipher3DES}, {packet.CipherCAST5}, {packet.CipherAES128}, {packet.CipherAES192}, {packet.CipherAES256}, } for i, testCase := range testCases { testConfig := defaultConfig optionFunc := CipherOption(testCase.in) optionFunc(&testConfig) if testCase.in.IsSupported() { if testConfig.DefaultCipher != testCase.in { t.Errorf("TestCipherOption: test case %d: expected cipher %d but got %d", i+1, testCase.in, testConfig.DefaultCipher) } } else if testConfig.DefaultCipher != 0 { t.Errorf("TestCipherOption: test case %d: expected cipher 0 but got %d", i+1, testConfig.DefaultCipher) } } } func TestRSABitsOption(t *testing.T) { testCases := []struct { in int }{ {}, {1}, {MinRSABits - 1}, {MinRSABits}, {MinRSABits + 1}, {rand.Intn(10480)}, {rand.Intn(10480)}, {rand.Intn(10480)}, {rand.Intn(10480)}, {rand.Intn(10480)}, } for i, testCase := range testCases { testConfig := defaultConfig optionFunc := RSABitsOption(testCase.in) optionFunc(&testConfig) if testCase.in > MinRSABits { if testConfig.RSABits != testCase.in { t.Errorf("TestRSABitsOption: test case %d: expected %d RSA bits but got %d", i+1, testCase.in, testConfig.DefaultCipher) } } else if testConfig.RSABits != MinRSABits { t.Errorf("TestRSABitsOption: test case %d: expected %d RSA bits but got %d", i+1, MinRSABits, testConfig.DefaultCipher) } } } func TestCompressionOption(t *testing.T) { testCases := []struct { in int }{ {-1}, {0}, {1}, {2}, {3}, {4}, {5}, {6}, {7}, {8}, {9}, {10}, } for i, testCase := range testCases { testConfig := defaultConfig optionFunc := CompressionOption(CompressionLevel(testCase.in)) optionFunc(&testConfig) if testCase.in >= int(DefaultCompression) && testCase.in <= int(CompressionLevelMax) { if testConfig.CompressionConfig == nil { t.Errorf("TestCompressionOption: test case %d: the compression is not set", i+1) } else if testConfig.CompressionConfig.Level != testCase.in { t.Errorf("TestCompressionOption: test case %d: expected compression level %d but got %d", i+1, testCase.in, testConfig.CompressionConfig.Level) } } else if testConfig.CompressionConfig.Level != int(CompressionLevel5) { t.Errorf("TestCompressionOption: test case %d: expected compression level %d but got %d", i+1, int(CompressionLevel5), testConfig.CompressionConfig.Level) } } }