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 25, 2023
1 parent d6de2f4 commit 359bb51
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/cert-generator/cmd/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ func main() {
},
}

caKey, ca, err := tls.CreateCACert(&caTemplate, &generator, &generator)
caKey, ca, err := tls.CreateCACertBytes(&caTemplate, &generator, &generator)
if err != nil {
fmt.Printf("Error: %s", err)
}

ceKey, ce, err := tls.CreateCert(&ceTemplate, caKey, ca, &generator, &generator)
ceKey, ce, err := tls.CreateCertBytes(&ceTemplate, caKey, ca, &generator, &generator)
if err != nil {
fmt.Printf("Error: %s", err)
}
Expand Down
36 changes: 18 additions & 18 deletions src/cert-generator/pkg/tls/x509.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ func WriteKeyCertFile(Key []byte, Cert []byte, filePath string) error {
return nil
}

func CreateCACert(ca *CACert, p IPem, x Ix509) ([]byte, []byte, error) {
func CreateCACertBytes(ca *CACert, p IPem, x Ix509) ([]byte, []byte, error) {
template := &x509.Certificate{
SerialNumber: ca.Serial,
Subject: pkix.Name{
Country: removeEmptyString([]string{ca.Subject.Country}),
Organization: removeEmptyString([]string{ca.Subject.Organization}),
OrganizationalUnit: removeEmptyString([]string{ca.Subject.OrganizationalUnit}),
Locality: removeEmptyString([]string{ca.Subject.Locality}),
Province: removeEmptyString([]string{ca.Subject.Province}),
StreetAddress: removeEmptyString([]string{ca.Subject.StreetAddress}),
PostalCode: removeEmptyString([]string{ca.Subject.PostalCode}),
Country: RemoveEmptyString([]string{ca.Subject.Country}),
Organization: RemoveEmptyString([]string{ca.Subject.Organization}),
OrganizationalUnit: RemoveEmptyString([]string{ca.Subject.OrganizationalUnit}),
Locality: RemoveEmptyString([]string{ca.Subject.Locality}),
Province: RemoveEmptyString([]string{ca.Subject.Province}),
StreetAddress: RemoveEmptyString([]string{ca.Subject.StreetAddress}),
PostalCode: RemoveEmptyString([]string{ca.Subject.PostalCode}),
CommonName: ca.Subject.CommonName,
},
NotBefore: time.Now(),
Expand All @@ -51,24 +51,24 @@ func CreateCACert(ca *CACert, p IPem, x Ix509) ([]byte, []byte, error) {
return keyBytes, certBytes, nil
}

func CreateCert(cert *Cert, caKey []byte, caCert []byte, p IPem, x Ix509) ([]byte, []byte, error) {
func CreateCertBytes(cert *Cert, caKey []byte, caCert []byte, p IPem, x Ix509) ([]byte, []byte, error) {
template := &x509.Certificate{
SerialNumber: cert.Serial,
Subject: pkix.Name{
Country: removeEmptyString([]string{cert.Subject.Country}),
Organization: removeEmptyString([]string{cert.Subject.Organization}),
OrganizationalUnit: removeEmptyString([]string{cert.Subject.OrganizationalUnit}),
Locality: removeEmptyString([]string{cert.Subject.Locality}),
Province: removeEmptyString([]string{cert.Subject.Province}),
StreetAddress: removeEmptyString([]string{cert.Subject.StreetAddress}),
PostalCode: removeEmptyString([]string{cert.Subject.PostalCode}),
Country: RemoveEmptyString([]string{cert.Subject.Country}),
Organization: RemoveEmptyString([]string{cert.Subject.Organization}),
OrganizationalUnit: RemoveEmptyString([]string{cert.Subject.OrganizationalUnit}),
Locality: RemoveEmptyString([]string{cert.Subject.Locality}),
Province: RemoveEmptyString([]string{cert.Subject.Province}),
StreetAddress: RemoveEmptyString([]string{cert.Subject.StreetAddress}),
PostalCode: RemoveEmptyString([]string{cert.Subject.PostalCode}),
CommonName: cert.Subject.CommonName,
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(cert.ValidForYears, 0, 0),
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature,
DNSNames: removeEmptyString(cert.DNSNames),
DNSNames: RemoveEmptyString(cert.DNSNames),
}

caKeyParsed, err := key.PrivateKeyPemToRSA(caKey)
Expand Down Expand Up @@ -120,7 +120,7 @@ func createCert(template *x509.Certificate, caKey *rsa.PrivateKey, caCert *x509.
return keyOut.Bytes(), certOut.Bytes(), nil
}

func removeEmptyString(input []string) []string {
func RemoveEmptyString(input []string) []string {
if len(input) == 1 && input[0] == "" {
return []string{}
}
Expand Down
28 changes: 28 additions & 0 deletions src/cert-generator/tests/pkg/tls/pem_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tests

import (
"crypto/x509"
"encoding/pem"
"fmt"
"io"
)

type mockPemOK struct{}
type mockPemFail struct{}
type mockX509OK struct{}
type mockX509Fail struct{}

func (m *mockPemOK) Decode(input []byte) (*pem.Block, []byte) {
b := pem.Block{Bytes: input}
return &b, nil
}
func (m *mockPemOK) Encode(out io.Writer, b *pem.Block) error {
return nil
}

func (m *mockPemFail) Decode(input []byte) (*pem.Block, []byte) {
return nil, nil
}

func (m *mockPemFail) Encode(out io.Writer, b *pem.Block) error {
return fmt.Errorf("cannot encode buffer")
3 changes: 2 additions & 1 deletion src/cert-generator/tests/pkg/tls/pem_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tests

import (
"encoding/pem"
"testing"

"github.com/zLukas/CloudTools/src/cert-generator/pkg/tls"
Expand All @@ -15,7 +16,7 @@ func TestPemToX509_ok(t *testing.T) {
t.Errorf("err expected to be nil, got %s ", err)
}
if results == nil {
t.Errorf("results var execept to be %v, got nil ", tls.Block{Bytes: false_bytes})
t.Errorf("results var execept to be %v, got nil ", pem.Block{Bytes: false_bytes})
}

}
Expand Down
49 changes: 0 additions & 49 deletions src/cert-generator/tests/pkg/tls/types.go

This file was deleted.

28 changes: 28 additions & 0 deletions src/cert-generator/tests/pkg/tls/x509_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tests

import (
"crypto/x509"
"fmt"
"io"
)

type mockCreateCertificateCA struct{}
type mockCreateCertificateCE struct{}

func (m *mockCreateCertificateCA) CreateCertificate(rand io.Reader, template *x509.Certificate, parent *x509.Certificate, pub any, priv any) ([]byte, error) {
b := []byte{0xAA}
return b, nil
}

func (m *mockX509Fail) CreateCertificate(rand io.Reader, template *x509.Certificate, parent *x509.Certificate, pub any, priv any) ([]byte, error) {
b := []byte{0x56, 0xAA, 0x21}
return b, nil
}

func (m *mockX509OK) ParseCertificate(der []byte) (*x509.Certificate, error) {
return &x509.Certificate{}, nil
}

func (m *mockX509Fail) ParseCertificate(der []byte) (*x509.Certificate, error) {
return nil, fmt.Errorf("x509: malformed certificate")
}
25 changes: 25 additions & 0 deletions src/cert-generator/tests/pkg/tls/x509_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tests

import (
"testing"

"github.com/zLukas/CloudTools/src/cert-generator/pkg/tls"
)

func TestRemoveEmptyStringEmptyString(t *testing.T) {
empty_string := []string{""}
result := tls.RemoveEmptyString(empty_string)

if len(result) != 0 {
t.Errorf("string array should have 0 lenght, go %v", len(result))
}
}

func TestRemoveEmptyStringNotEmptyString(t *testing.T) {
empty_string := []string{"asd", "asd", "wersfds"}
result := tls.RemoveEmptyString(empty_string)

if len(result) != 3 {
t.Errorf("string array should have 3 lenght, go %v", len(result))
}
}

0 comments on commit 359bb51

Please sign in to comment.