Skip to content

Commit

Permalink
testing skelly
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Apr 5, 2024
1 parent 9ffefc2 commit 2fbf884
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 9 deletions.
4 changes: 2 additions & 2 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func loadServerCertificates(certificate, key string) (*tls.Certificate, error) {
}

// NewServer creates a new http server with handlers that Go can start listening to
func NewServer(version int, certificate, key string) (*http.Server, error) {
func NewServer(certificate, key string) (*http.Server, error) {
serverCerts, err := loadServerCertificates(certificate, key)
if err != nil {
return nil, err
Expand All @@ -47,7 +47,7 @@ func NewServer(version int, certificate, key string) (*http.Server, error) {
},
}
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello world"))
w.Write([]byte("Hello World"))
})
return s, nil
}
75 changes: 74 additions & 1 deletion api/server_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
package server
package server_test

import (
"testing"

server "github.com/canonical/gocert/api"
)

func TestNewServerSuccess(t *testing.T) {
testCases := []struct {
desc string
cert string
key string
}{
{
desc: "Correct certificate and key",
cert: "Should be a valid cert",
key: "Should be a valid key",
},
{
desc: "Empty certificate",
cert: "",
key: "Should be a valid key",
},
{
desc: "Empty key",
cert: "Should be a valid cert",
key: "",
},
{
desc: "Empty certificate and key",
cert: "",
key: "",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
s, err := server.NewServer(tC.cert, tC.key)
if err != nil {
t.Errorf("Error occured: %s", err)
}
if s.TLSConfig.Certificates == nil {
t.Errorf("No certificates were configured for server")
}
})
}
}

func TestNewServerFail(t *testing.T) {
testCases := []struct {
desc string
cert string
key string
}{
{
desc: "Wrong certificate",
cert: "Should be invalid",
key: "Should be valid",
},
{
desc: "Wrong key",
cert: "Should be valid",
key: "Should be invalid",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {
_, err := server.NewServer(tC.cert, tC.key)
if err != nil {
t.Errorf("Expected error")
}
})
}
}
9 changes: 3 additions & 6 deletions cmd/gocert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,18 @@ import (
func main() {
certPathPtr := flag.String("cert", "", "A path for a certificate file to be used by the webserver")
keyPathPtr := flag.String("key", "", "The path for a private key for the given certificate")
dbPathPtr := flag.String("db", ":memory:", "The path of the SQLite database for the repository")
dbPathPtr := flag.String("db", "./certs.db", "The path of the SQLite database for the repository")
flag.Parse()

if *certPathPtr == "" || *keyPathPtr == "" {
fmt.Fprintf(os.Stderr, "Usage: --cert <path/to/certificate> --key <path/to/key>")
os.Exit(1)
fmt.Fprintf(os.Stderr, "Providing a certificate and matching private key is extremely highly recommended. To do so, run: --cert <path/to/certificate> --key <path/to/key>. Using self signed certificates.")
}
_, err := certdb.NewCertificateRequestsRepository(*dbPathPtr, "CertificateRequests")
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't connect to database: %s", err)
os.Exit(1)
}

// actually read the strings in the file here
srv, err := server.NewServer(0, "", "")
srv, err := server.NewServer(*certPathPtr, *keyPathPtr)
if err != nil {
fmt.Fprintf(os.Stderr, "Couldn't create server: %s", err)
os.Exit(1)
Expand Down
15 changes: 15 additions & 0 deletions cmd/gocert/main_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
package main

import "testing"

func TestMain(t *testing.T) {
// run gocert
// run gocert --cert openable path
// run gocert --key openable path
// run gocert --cert openable path --key openable path --db unopenable path
// run gocert --cert openable path --key openable path --db openable path
// parametrize below
// run gocert --cert unopenable path --key unopenable path
// run gocert --cert unopenable path --key openable path
// run gocert --cert openable path --key unopenable path
// run gocert --cert openable path --key openable path
}
108 changes: 108 additions & 0 deletions internal/certificates/certificates_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,109 @@
package certificates_test

import (
"testing"

"github.com/canonical/gocert/internal/certificates"
)

func TestGenerateSelfSignedCertificateSuccess(t *testing.T) {
testCases := []struct {
desc string
}{
{
desc: "",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {

})
}
}

func TestGenerateSelfSignedCertificateFail(t *testing.T) {
testCases := []struct {
desc string
}{
{
desc: "",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {

})
}
}

func TestParseCertificateSuccess(t *testing.T) {
testCases := []struct {
desc string
}{
{
desc: "",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {

})
}
}

func TestParseCertificateFail(t *testing.T) {
testCases := []struct {
desc string
}{
{
desc: "",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {

})
}
}

func TestParsePKCS1PrivateKeySuccess(t *testing.T) {
testCases := []struct {
desc string
}{
{
desc: "",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {

})
}
}

func TestParsePKCS1PrivateKeyFail(t *testing.T) {
testCases := []struct {
desc string
}{
{
desc: "",
},
}
for _, tC := range testCases {
t.Run(tC.desc, func(t *testing.T) {

})
}
}
func TestGenerateCACertificateSuccess(t *testing.T) {
caCertPEM, caPKPEM, err := certificates.GenerateCACertificate()
if err != nil {
t.Fatalf("could not generate CA cert and PK")
}
if _, err := certificates.ParseCertificate(caCertPEM); err != nil {
t.Fatalf("generate CA cert cannot be parsed")
}
if _, err := certificates.ParsePKCS1PrivateKey(caPKPEM); err != nil {
t.Fatalf("generate CA private key cannot be parsed")
}
}

0 comments on commit 2fbf884

Please sign in to comment.