Skip to content

Commit

Permalink
new tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
zLukas committed Sep 29, 2023
1 parent a96ed83 commit 78e4036
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/cert-generator/tests/pkg/tls/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,12 @@ func (i *pemMock) Encode(out io.Writer, b *pem.Block) error {
}

func (i *pemMock) Decode(data []byte) (*pem.Block, []byte) {
if i.decodePass {
fmt.Print(("Entering decode mock"))
if i.decodePass == true {
fmt.Print("returning pass")
return &pem.Block{Type: "test", Bytes: []byte{0xDE, 0xAD, 0xBE, 0xEF}}, nil
} else {
fmt.Print("returning fail")
return nil, nil
}
}
Expand Down
70 changes: 68 additions & 2 deletions src/cert-generator/tests/pkg/tls/x509_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
}
)

func TestSetup(t *testing.T) {
func SetUp(t *testing.T) {
tls.Ix509 = &testX509
tls.Ipem = &testPem
tls.Irsa = &testRsa
Expand Down Expand Up @@ -56,6 +56,7 @@ func TestRemoveEmptyStringNotEmptyString(t *testing.T) {
}

func TestPemToX509DecodeFail(t *testing.T) {
SetUp(t)
defer TearDown()
testPem.decodePass = false

Expand All @@ -69,12 +70,77 @@ func TestPemToX509DecodeFail(t *testing.T) {

}
func TestPemToX509DecodePass(t *testing.T) {
SetUp(t)
defer TearDown()
cert, err := tls.PemToX509(nil)
if reflect.TypeOf(cert).String() == string("*x509.Certificate") {

if reflect.TypeOf(cert).String() != string("*x509.Certificate") {
t.Errorf("cert should be type *x509.Certificate, got %T", cert)
}
if err != nil {
t.Errorf(" error should be nil, got %v", err)
}
}

func TestPemToX509ParseFail(t *testing.T) {
SetUp(t)
defer TearDown()
testX509.parseCertificatePass = false
cert, err := tls.PemToX509(nil)

if cert != nil {
t.Errorf("cert should be nil, got %v", cert)
}
if err.Error() != "FAIL" {
t.Errorf(" error message should be \"FAIL\", got %v", err)
}
}

func TestPemToX509ParsePass(t *testing.T) {
SetUp(t)
defer TearDown()
cert, err := tls.PemToX509(nil)

if cert == nil {
t.Errorf("cert should be \"*x509.Certificate\" type, got %v", cert)
}
if err != nil {
t.Errorf(" error message should be nil, got %v", err)
}
}

func TestCreateCACertBytesNullTemplate(t *testing.T) {
SetUp(t)
defer TearDown()

key, cert, err := tls.CreateCACertBytes(nil)
if key != nil {
t.Errorf(" key should be nil, got %v", err)
}
if cert != nil {
t.Errorf(" cert should be nil, got %v", err)
}
if err == nil {
t.Errorf(" error should be %s got nil", "tempalte must contain CommonName Field")
} else if err.Error() != "template is nil" {
t.Errorf(" error should be \"template is nil\" got %s", err)
}
}

func TestCreateCACertBytesNullNoCommonName(t *testing.T) {
SetUp(t)
defer TearDown()
ca := &tls.CACert{}
key, cert, err := tls.CreateCACertBytes(ca)
if key != nil {
t.Errorf(" key should be <nil>, got <%v>", err)
}
if cert != nil {
t.Errorf(" cert should be <nil>, got <%v>", err)
}
if err == nil {
t.Errorf(" error should be %s got nil", "tempalte must contain CommonName Field")
} else if err.Error() != "template must contain CommonName Field" {
t.Errorf(" error should be <template must contain CommonName Field> got <%s>", err)
}
}

0 comments on commit 78e4036

Please sign in to comment.