add test for create_certs args

add test for create_certs args
This commit is contained in:
Brian Martin 2019-06-10 13:49:39 -04:00
parent d3db3e35e3
commit 990a93e583
2 changed files with 65 additions and 23 deletions

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"fmt"
"bufio" "bufio"
"crypto/rand" "crypto/rand"
"crypto/rsa" "crypto/rsa"
@ -10,25 +9,25 @@ import (
"crypto/x509" "crypto/x509"
"crypto/x509/pkix" "crypto/x509/pkix"
"encoding/pem" "encoding/pem"
"flag"
"fmt"
"log" "log"
"math/big" "math/big"
"net" "net"
"os" "os"
"time"
"flag"
"path/filepath" "path/filepath"
"time"
) )
var InBumperSan string
var OutCertDirectory string
var inBumperSan string func setFlags() {
var outCertDirectory string
func main() {
exePath, _ := os.Executable() exePath, _ := os.Executable()
currentDir, _ := os.Getwd() currentDir, _ := os.Getwd()
//certPath will be current working directory by default //certPath will be current working directory by defaultß
certPath, err := filepath.Abs(currentDir) certPath, err := filepath.Abs(currentDir)
if err != nil { if err != nil {
log.Printf("Error: %v", err) log.Printf("Error: %v", err)
@ -40,17 +39,20 @@ func main() {
log.Printf("Error: %v", err) log.Printf("Error: %v", err)
} }
var inBumperSan string flag.StringVar(&InBumperSan, "inSAN", sanPath, "Input file containing a list of Subject Alternate Names (line separated)")
var outCertDirectory string flag.StringVar(&OutCertDirectory, "out", certPath, "Directory to output certificates to")
flag.StringVar(&inBumperSan, "inSAN", sanPath, "Input file containing a list of Subject Alternate Names (line separated)")
flag.StringVar(&outCertDirectory, "out", certPath, "Directory to output certificates to")
flag.Parse() flag.Parse()
}
func main() {
setFlags()
fmt.Printf("-------- Create_Certs --------\n") fmt.Printf("-------- Create_Certs --------\n")
//get absolute path //get absolute path
outCertDirectory, _ = filepath.Abs(outCertDirectory) outCertDirectory, _ := filepath.Abs(OutCertDirectory)
dexists, isdfile := pathExistsType(outCertDirectory) dexists, isdfile := pathExistsType(outCertDirectory)
if !dexists { if !dexists {
log.Fatalf("Certs directory doesn't exist: %v", outCertDirectory) log.Fatalf("Certs directory doesn't exist: %v", outCertDirectory)
@ -60,7 +62,7 @@ func main() {
} }
//get absolute path //get absolute path
inBumperSan, _ = filepath.Abs(inBumperSan) inBumperSan, _ := filepath.Abs(InBumperSan)
bexists, isbfile := pathExistsType(inBumperSan) bexists, isbfile := pathExistsType(inBumperSan)
if !bexists { if !bexists {
log.Printf("Bumper SAN doesn't exist, certificate won't contain Subject Alternate Names: %v\n", inBumperSan) log.Printf("Bumper SAN doesn't exist, certificate won't contain Subject Alternate Names: %v\n", inBumperSan)
@ -108,7 +110,6 @@ func make_CA(outCertDirectory string) {
log.Fatalf("Create ca failed: %v", err) log.Fatalf("Create ca failed: %v", err)
} }
// Public key // Public key
certOut, err := os.Create(filepath.Join(outCertDirectory, "ca.crt")) certOut, err := os.Create(filepath.Join(outCertDirectory, "ca.crt"))
if err != nil { if err != nil {

View file

@ -0,0 +1,41 @@
package main
import (
"flag"
"os"
"testing"
)
func TestArgs(t *testing.T) {
orArgs := os.Args
flag.CommandLine = flag.NewFlagSet(orArgs[0], flag.ContinueOnError)
os.Args = []string{"cmd", "-inSAN", "123"}
setFlags()
if InBumperSan != "123" {
t.Error("InBumperSan not set by arg")
}
flag.CommandLine = flag.NewFlagSet(orArgs[0], flag.ContinueOnError)
os.Args = []string{"cmd", "-out", "456"}
setFlags()
if OutCertDirectory != "456" {
t.Error("Out path not set by arg")
}
flag.CommandLine = flag.NewFlagSet(orArgs[0], flag.ContinueOnError)
os.Args = []string{"cmd", "-inSAN", "san1", "-out", "out2"}
setFlags()
if InBumperSan != "san1" {
t.Error("InBumperSan not set by arg")
}
if OutCertDirectory != "out2" {
t.Error("Out path not set by arg")
}
os.Args = orArgs
}