github.com/openshift/installer@v1.4.17/pkg/asset/tls/cabundle_test.go (about) 1 package tls 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 10 "github.com/openshift/installer/pkg/asset" 11 ) 12 13 type mockCertKey struct { 14 cert string 15 key string 16 } 17 18 func (mck *mockCertKey) Cert() []byte { return []byte(mck.cert) } 19 func (mck *mockCertKey) Key() []byte { return []byte(mck.key) } 20 21 func Test_CertBundleGenerate(t *testing.T) { 22 23 tests := []struct { 24 input []string 25 26 expBundle string 27 expErr string 28 }{{ 29 expErr: "atleast one certificate required for a bundle", 30 }, { 31 input: []string{`-----BEGIN CERTIFICATE----- 32 MIICYTCCAcqgAwIBAgIJAI2kA+uXAbhOMA0GCSqGSIb3DQEBCwUAMEgxCzAJBgNV 33 BAYTAlVTMQswCQYDVQQIDAJDQTEWMBQGA1UEBwwNU2FuIEZyYW5jaXNjbzEUMBIG 34 A1UECgwLUmVkIEhhdCBJbmMwHhcNMTkwMjEyMTkzMjUzWhcNMTkwMjEzMTkzMjUz 35 WjBIMQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExFjAUBgNVBAcMDVNhbiBGcmFu 36 Y2lzY28xFDASBgNVBAoMC1JlZCBIYXQgSW5jMIGfMA0GCSqGSIb3DQEBAQUAA4GN 37 ADCBiQKBgQC+HOC0mKig/oINAKPo88LqxDJ4l7lozdLtp5oGeqWrLUXSfkvXAkQY 38 2QYdvPAjpRfH7Ii7G0Asx+HTKdvula7B5fXDjc6NYKuEpTJZRV1ugntI97bozF/E 39 C2BBmxxEnJN3+Xe8RYXMjz5Q4aqPw9vZhlWN+0hrREl1Ea/zHuWFIQIDAQABo1Mw 40 UTAdBgNVHQ4EFgQUvTS1XjlvOdsufSyWxukyQu3LriEwHwYDVR0jBBgwFoAUvTS1 41 XjlvOdsufSyWxukyQu3LriEwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsF 42 AAOBgQB9gFcOXnzJrM65Qq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LMu+WhLkbboK6jEg== 61 -----END CERTIFICATE----- 62 `, 63 }, { 64 input: []string{`-----BEGIN CERTIFICATE----- 65 MIICYTCCAcqgAwIBAgIJAI2kA+uXAbhOMA0GCSqGSIb3DQEBCwUAMEgxCzAJBgNV 66 BAYTAlVTMQswCQYDVQQIDAJDQTEWMBQGA1UEBwwNU2FuIEZyYW5jaXNjbzEUMBIG 67 A1UECgwLUmVkIEhhdCBJbmMwHhcNMTkwMjEyMTkzMjUzWhcNMTkwMjEzMTkzMjUz 68 WjBIMQswCQYDVQQGEwJVUzELMAkGA1UECAwCQ0ExFjAUBgNVBAcMDVNhbiBGcmFu 69 Y2lzY28xFDASBgNVBAoMC1JlZCBIYXQgSW5jMIGfMA0GCSqGSIb3DQEBAQUAA4GN 70 ADCBiQKBgQC+HOC0mKig/oINAKPo88LqxDJ4l7lozdLtp5oGeqWrLUXSfkvXAkQY 71 2QYdvPAjpRfH7Ii7G0Asx+HTKdvula7B5fXDjc6NYKuEpTJZRV1ugntI97bozF/E 72 C2BBmxxEnJN3+Xe8RYXMjz5Q4aqPw9vZhlWN+0hrREl1Ea/zHuWFIQIDAQABo1Mw 73 UTAdBgNVHQ4EFgQUvTS1XjlvOdsufSyWxukyQu3LriEwHwYDVR0jBBgwFoAUvTS1 74 XjlvOdsufSyWxukyQu3LriEwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsF 75 AAOBgQB9gFcOXnzJrM65QqxeCB9Z5l5JMjp45UFC9Bj2cgwDHP80Zvi4omlaacC6 76 aavmnLd67zm9PbYDWRaOIWAMeB916Iwaw/v6I0jwhAk/VxX5Fl6cGlZu9jZ3zbFE 77 2sDqkwzIuSjCG2A23s6d4M1S3IXCCydoCSLMu+WhLkbboK6jEg== 78 -----END CERTIFICATE-----`, `-----BEGIN CERTIFICATE----- 79 MIICVTCCAb6gAwIBAgIJAKDi1rywgIWHMA0GCSqGSIb3DQEBCwUAMEIxCzAJBgNV 80 BAYTAlVTMRUwEwYD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+l 115 mVrNZV3A4+QjswHxwl7jKAeZ/bpTsDh7j/r5zpoY8hD5za9+BZ5Pr0k5q3ONS6Iy 116 Uu8VHCPjIzMETcEJ+LCB0iD4xMAvb7daNmimVQIDAQABo1MwUTAdBgNVHQ4EFgQU 117 pKgTmnFxt741aiUOMy2bbcEP2AgwHwYDVR0jBBgwFoAUpKgTmnFxt741aiUOMy2b 118 bcEP2AgwDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQsFAAOBgQBW/U1DAbEX 119 0zk4FNxl4d/82ax44MVaZ5Owrhgr6kWBXDR2kRyYlq1yfLVHLjMqIkCe5VcBBfwy 120 s5Q0Xv1T6UKcWvIHwNNxo/dYDtnmjdllrEeVKKW0kmotCCsGLU/ZBa++Rl/GYpwv 121 CjH8bTNT3u6KYZKRVH0A2/EpJHC+TSSe3A== 122 -----END CERTIFICATE----- 123 `, 124 }} 125 for idx, test := range tests { 126 t.Run(fmt.Sprintf("#%d", idx), func(t *testing.T) { 127 var certkeys []CertInterface 128 for _, c := range test.input { 129 certkeys = append(certkeys, &mockCertKey{cert: c}) 130 } 131 132 bundle := CertBundle{} 133 err := bundle.Generate(context.Background(), "test-bundle", certkeys...) 134 if test.expErr == "" { 135 assert.NoError(t, err) 136 assert.Equal(t, string(bundle.BundleRaw), test.expBundle) 137 assert.Equal(t, bundle.FileList, []*asset.File{{Filename: "tls/test-bundle.crt", Data: []byte(test.expBundle)}}) 138 } else { 139 assert.EqualError(t, err, test.expErr) 140 } 141 }) 142 } 143 }