github.com/Venafi/vcert/v5@v5.10.2/pkg/certificate/keyType_test.go (about) 1 package certificate 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/suite" 8 "gopkg.in/yaml.v3" 9 ) 10 11 type KeyTypeSuite struct { 12 suite.Suite 13 testYaml string 14 testCases []struct { 15 keyType KeyType 16 strValue string 17 } 18 } 19 20 func (s *KeyTypeSuite) SetupTest() { 21 s.testCases = []struct { 22 keyType KeyType 23 strValue string 24 }{ 25 {keyType: KeyTypeECDSA, strValue: strKeyTypeECDSA}, 26 {keyType: KeyTypeRSA, strValue: strKeyTypeRSA}, 27 {keyType: KeyTypeED25519, strValue: strKeyTypeED25519}, 28 } 29 30 s.testYaml = `--- 31 cn: foobar 32 keyType: %s 33 ` 34 } 35 36 func TestKeyType(t *testing.T) { 37 suite.Run(t, new(KeyTypeSuite)) 38 } 39 40 func (s *KeyTypeSuite) TestKeyType_MarshalYAML() { 41 for _, tc := range s.testCases { 42 s.Run(tc.strValue, func() { 43 data, err := tc.keyType.MarshalYAML() 44 s.Nil(err) 45 s.Equal(tc.strValue, data.(string)) 46 }) 47 } 48 } 49 50 func (s *KeyTypeSuite) TestKeyType_String() { 51 for _, tc := range s.testCases { 52 s.Run(tc.strValue, func() { 53 str := tc.keyType.String() 54 s.Equal(tc.strValue, str) 55 }) 56 } 57 } 58 59 func (s *KeyTypeSuite) TestEllipticCurve_UnmarshalYAML() { 60 for _, tc := range s.testCases { 61 s.Run(tc.strValue, func() { 62 result := struct { 63 Cn string `yaml:"cn"` 64 KeyType KeyType `yaml:"keyType"` 65 }{} 66 parsedYaml := fmt.Sprintf(s.testYaml, tc.strValue) 67 err := yaml.Unmarshal([]byte(parsedYaml), &result) 68 69 s.Nil(err) 70 s.Equal(tc.keyType, result.KeyType) 71 }) 72 } 73 }