feat: cmd add some tests
This commit is contained in:
parent
0bf26d2019
commit
25526ac993
38
cmd/main.go
38
cmd/main.go
|
@ -87,28 +87,50 @@ var rootCmd = &cobra.Command{
|
|||
log.Fatalf("rootCA.WithClientCert: %s", err)
|
||||
}
|
||||
|
||||
exportPem("root-ca.pem", rootCA.PEM())
|
||||
exportPem("root-key.pem", rootCA.Key())
|
||||
outputPath, err := exportPem("root-ca.pem", rootCA.PEM())
|
||||
if err != nil {
|
||||
log.Fatalf("exportPem: %s\n", err)
|
||||
}
|
||||
log.Printf("file created successfully: %s\n", outputPath)
|
||||
|
||||
exportPem("client-cert.pem", clientCert.PEM())
|
||||
exportPem("client-key.pem", clientCert.Key())
|
||||
outputPath, err = exportPem("root-key.pem", rootCA.Key())
|
||||
if err != nil {
|
||||
log.Fatalf("exportPem: %s\n", err)
|
||||
}
|
||||
log.Printf("file created successfully: %s\n", outputPath)
|
||||
|
||||
outputPath, err = exportPem("client-cert.pem", clientCert.PEM())
|
||||
if err != nil {
|
||||
log.Fatalf("exportPem: %s\n", err)
|
||||
}
|
||||
log.Printf("file created successfully: %s\n", outputPath)
|
||||
|
||||
outputPath, err = exportPem("client-key.pem", clientCert.Key())
|
||||
if err != nil {
|
||||
log.Fatalf("exportPem: %s\n", err)
|
||||
}
|
||||
log.Printf("file created successfully: %s\n", outputPath)
|
||||
},
|
||||
}
|
||||
|
||||
func getExtKeyUsage(intKeyUsageSlice []int) []x509.ExtKeyUsage {
|
||||
extKeyUsage := make([]x509.ExtKeyUsage, len(intKeyUsageSlice))
|
||||
if intKeyUsageSlice == nil || len(intKeyUsageSlice) <= 0 {
|
||||
return []x509.ExtKeyUsage{}
|
||||
}
|
||||
|
||||
extKeyUsage := make([]x509.ExtKeyUsage, 0, len(intKeyUsageSlice))
|
||||
for _, v := range intKeyUsageSlice {
|
||||
extKeyUsage = append(extKeyUsage, x509.ExtKeyUsage(v))
|
||||
}
|
||||
return extKeyUsage
|
||||
}
|
||||
|
||||
func exportPem(filename string, data []byte) {
|
||||
func exportPem(filename string, data []byte) (string, error) {
|
||||
outputPath, err := writer.WriteFile(filename, data)
|
||||
if err != nil {
|
||||
log.Fatalf("rootCA.WithClientCert: %s", err)
|
||||
return "", fmt.Errorf("rootCA.WithClientCert: %s", err)
|
||||
}
|
||||
log.Printf("file created successfully: %s\n", outputPath)
|
||||
return outputPath, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
|
|
@ -1,7 +1,62 @@
|
|||
package cmd
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"crypto/x509"
|
||||
"log"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gitlab.com/urkob/go-cert-gen/internal/io"
|
||||
)
|
||||
|
||||
func TestExecute(t *testing.T) {
|
||||
Execute()
|
||||
}
|
||||
|
||||
func Test_getExtKeyUsage(t *testing.T) {
|
||||
intKeyUsageSlice := make([]int, 0, 1)
|
||||
intKeyUsageSlice = append(intKeyUsageSlice, int(x509.ExtKeyUsageClientAuth))
|
||||
|
||||
keyUsage := getExtKeyUsage(intKeyUsageSlice)
|
||||
assert.Len(t, keyUsage, len(intKeyUsageSlice))
|
||||
assert.Equal(t, keyUsage[0], x509.ExtKeyUsageClientAuth)
|
||||
|
||||
intKeyUsageSlice = make([]int, 0)
|
||||
keyUsage = getExtKeyUsage(intKeyUsageSlice)
|
||||
assert.Len(t, keyUsage, 0)
|
||||
|
||||
keyUsage = getExtKeyUsage(nil)
|
||||
assert.Len(t, keyUsage, 0)
|
||||
}
|
||||
|
||||
var testFile = "test-file.txt"
|
||||
|
||||
func init() {
|
||||
wd, err := os.Getwd()
|
||||
if err != nil {
|
||||
log.Fatalf("os.Getwd: %s\n", err)
|
||||
}
|
||||
writer = io.NewWriter(wd)
|
||||
}
|
||||
|
||||
func Test_exportPem(t *testing.T) {
|
||||
defer func() {
|
||||
os.Remove(testFile)
|
||||
// err := os.Remove(testFile)
|
||||
// require.NoError(t, err)
|
||||
}()
|
||||
|
||||
data := []byte("test data")
|
||||
outputPath, err := exportPem(testFile, data)
|
||||
require.NoError(t, err)
|
||||
require.NotEmpty(t, outputPath)
|
||||
}
|
||||
|
||||
func Test_exportPemError(t *testing.T) {
|
||||
data := []byte("test data")
|
||||
outputPath, err := exportPem("", data)
|
||||
require.Error(t, err)
|
||||
require.Empty(t, outputPath)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue