Skip to content

Commit

Permalink
tests done and passing
Browse files Browse the repository at this point in the history
  • Loading branch information
kayra1 committed Mar 28, 2024
1 parent 2d60254 commit c925d47
Show file tree
Hide file tree
Showing 3 changed files with 179 additions and 94 deletions.
107 changes: 15 additions & 92 deletions internal/certdb/certdb_test.go
Original file line number Diff line number Diff line change
@@ -1,108 +1,31 @@
package certdb
package certdb_test

import (
"testing"

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

func TestConnection(t *testing.T) {
db := new(CertificateRequests)
db := new(certdb.CertificateRequests)
defer db.Disconnect()

Check failure on line 11 in internal/certdb/certdb_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `db.Disconnect` is not checked (errcheck)
if err := db.Connect(":memory:", "CertificateReqs"); err != nil {
t.Fatalf("Can't connect to SQLite: %s", err)
}
}

func TestDatabase(t *testing.T) {
db := new(CertificateRequests)
db := new(certdb.CertificateRequests)
defer db.Disconnect()

Check failure on line 19 in internal/certdb/certdb_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `db.Disconnect` is not checked (errcheck)
db.Connect(":memory:", "CertificateRequests")

Check failure on line 20 in internal/certdb/certdb_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `db.Connect` is not checked (errcheck)

csr1 := `-----BEGIN CERTIFICATE REQUEST-----
MIICszCCAZsCAQAwFjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wggEiMA0GCSqGSIb3
DQEBAQUAA4IBDwAwggEKAoIBAQDC5KgrADpuOUPwSh0YLmpWF66VTcciIGC2HcGn
oJknL7pm5q9qhfWGIdvKKlIA6cBB32jPd0QcYDsx7+AvzEvBuO7mq7v2Q1sPU4Q+
L0s2pLJges6/cnDWvk/p5eBjDLOqHhUNzpMUga9SgIod8yymTZm3eqQvt1ABdwTg
FzBs5QdSm2Ny1fEbbcRE+Rv5rqXyJb2isXSujzSuS22VqslDIyqnY5WaLg+pjZyR
+0j13ecJsdh6/MJMUZWheimV2Yv7SFtxzFwbzBMO9YFS098sy4F896eB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gq7441cx/PuH63w4QVTsssDe7KviN1sAm8gQXDfLjsmntG/6uflCVIqoKXjg
If+GO6iN2GqzI+HJu3cJoglY3nVMIeuc1ch7UFHy3fJxVipfe++ZjQXNconLyg1N
2QeYWdTGbaWzAcGgNcjEhK7P34eSml8CAwEAAaOBnTCBmjAhBgNVHSMEGjAYgBYE
FN/vgl9cAapV7hH9lEyM7qYS958aMB0GA1UdDgQWBBRJJDZkHr64VqTC24DPQVld
Ba3iPDAMBgNVHRMBAf8EAjAAMEgGA1UdEQRBMD+CN3ZhdWx0LWs4cy0wLnZhdWx0
LWs4cy1lbmRwb2ludHMudmF1bHQuc3ZjLmNsdXN0ZXIubG9jYWyHBAqYtzUwDQYJ
KoZIhvcNAQELBQADggEBAEH9NTwDiSsoQt/QXkWPMBrB830K0dlwKl5WBNgVxFP+
hSfQ86xN77jNSp2VxOksgzF9J9u/ubAXvSFsou4xdP8MevBXoFJXeqMERq5RW3gc
WyhXkzguv3dwH+n43GJFP6MQ+n9W/nPZCUQ0Iy7ueAvj0HFhGyZzAE2wxNFZdvCs
gCX3nqYpp70oZIFDrhmYwE5ij5KXlHD4/1IOfNUKCDmQDgGPLI1tVtwQLjeRq7Hg
XVelpl/LXTQawmJyvDaVT/Q9P+WqoDiMjrqF6Sy7DzNeeccWVqvqX5TVS6Ky56iS
Mvo/+PAJHkBciR5Xn+Wg2a+7vrZvT6CBoRSOTozlLSM=
-----END CERTIFICATE-----`

if _, err := db.Create(&csr1); err != nil {
if _, err := db.Create(&ValidCSR1); err != nil {
t.Fatalf("Couldn't complete Create: %s", err)
}
if _, err := db.Create(&csr2); err != nil {
if _, err := db.Create(&ValidCSR2); err != nil {
t.Fatalf("Couldn't complete Create: %s", err)
}
if _, err := db.Create(&csr3); err != nil {
if _, err := db.Create(&ValidCSR3); err != nil {
t.Fatalf("Couldn't complete Create: %s", err)
}

Expand All @@ -113,28 +36,28 @@ Mvo/+PAJHkBciR5Xn+Wg2a+7vrZvT6CBoRSOTozlLSM=
if len(res) != 3 {
t.Fatalf("One or more CSR's weren't found in DB")
}
retrievedCSR, err := db.Retrieve(&csr1)
retrievedCSR, err := db.Retrieve(&ValidCSR1)
if err != nil {
t.Fatalf("Couldn't complete Retrieve: %s", err)
}
if retrievedCSR.CSR != csr1 {
if retrievedCSR.CSR != ValidCSR1 {
t.Fatalf("The CSR from the database doesn't match the CSR that was given")
}

if err = db.Delete(&csr1); err != nil {
if err = db.Delete(&ValidCSR1); err != nil {
t.Fatalf("Couldn't complete Delete: %s", err)
}
res, _ = db.RetrieveAll()
if len(res) != 2 {
t.Fatalf("CSR's weren't deleted from the DB properly")
}

_, err = db.Update(&csr2, &cert2)
_, err = db.Update(&ValidCSR2, &ValidCert2)
if err != nil {
t.Fatalf("Couldn't complete Update: %s", err)
}
retrievedCSR, _ = db.Retrieve(&csr2)
if *retrievedCSR.Certificate != cert2 {
t.Fatalf("The certificate that was uploaded does not match the certificate that was given: Retrieved: %s\nGiven: %s", *retrievedCSR.Certificate, cert2)
retrievedCSR, _ = db.Retrieve(&ValidCSR2)
if *retrievedCSR.Certificate != ValidCert2 {
t.Fatalf("The certificate that was uploaded does not match the certificate that was given: Retrieved: %s\nGiven: %s", *retrievedCSR.Certificate, ValidCert2)
}
}
5 changes: 4 additions & 1 deletion internal/certdb/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ func ValidateCertificate(certString *string, csrString *string) error {
if certBlock == nil {
return errors.New("PEM Certificate string not found or malformed")
}
if certBlock.Type != "CERTIFICATE" {
return errors.New("given PEM string not a certificate")
}
cert, err := x509.ParseCertificate(certBlock.Bytes)
if err != nil {
return err
}
certKey := cert.PublicKey.(*rsa.PublicKey)
csrKey := csr.PublicKey.(*rsa.PublicKey)
if csrKey.Equal(*certKey) {
if !csrKey.Equal(certKey) {
return errors.New("certificate does not match CSR")
}
return nil
Expand Down
161 changes: 160 additions & 1 deletion internal/certdb/validation_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,160 @@
package certdb
package certdb_test

import (
"strings"
"testing"

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

var ValidCSR1 string = `-----BEGIN CERTIFICATE REQUEST-----
MIICszCCAZsCAQAwFjEUMBIGA1UEAwwLZXhhbXBsZS5jb20wggEiMA0GCSqGSIb3
DQEBAQUAA4IBDwAwggEKAoIBAQDC5KgrADpuOUPwSh0YLmpWF66VTcciIGC2HcGn
oJknL7pm5q9qhfWGIdvKKlIA6cBB32jPd0QcYDsx7+AvzEvBuO7mq7v2Q1sPU4Q+
L0s2pLJges6/cnDWvk/p5eBjDLOqHhUNzpMUga9SgIod8yymTZm3eqQvt1ABdwTg
FzBs5QdSm2Ny1fEbbcRE+Rv5rqXyJb2isXSujzSuS22VqslDIyqnY5WaLg+pjZyR
+0j13ecJsdh6/MJMUZWheimV2Yv7SFtxzFwbzBMO9YFS098sy4F896eBHLNe9cUC
+d1JDtLaewlMogjHBHAxmP54dhe6vvc78anElKKP4hm5N5nlAgMBAAGgWDBWBgkq
hkiG9w0BCQ4xSTBHMA4GA1UdDwEB/wQEAwIFoDAdBgNVHSUEFjAUBggrBgEFBQcD
AQYIKwYBBQUHAwIwFgYDVR0RBA8wDYILZXhhbXBsZS5jb20wDQYJKoZIhvcNAQEL
BQADggEBACP1VKEGVYKoVLMDJS+EZ0CPwIYWsO4xBXgK6atHe8WIChVn/8I7eo60
cuMDiy4LR70G++xL1tpmYGRbx21r9d/shL2ehp9VdClX06qxlcGxiC/F8eThRuS5
zHcdNqSVyMoLJ0c7yWHJahN5u2bn1Lov34yOEqGGpWCGF/gT1nEvM+p/v30s89f2
Y/uPl4g3jpGqLCKTASWJDGnZLroLICOzYTVs5P3oj+VueSUwYhGK5tBnS2x5FHID
uMNMgwl0fxGMQZjrlXyCBhXBm1k6PmwcJGJF5LQ31c+5aTTMFU7SyZhlymctB8mS
y+ErBQsRpcQho6Ok+HTXQQUcx7WNcwI=
-----END CERTIFICATE REQUEST-----
`
var ValidCSR2 string = `-----BEGIN CERTIFICATE RE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ZIFDrhmYwE5ij5KXlHD4/1IOfNUKCDmQDgGPLI1tVtwQLjeRq7Hg
XVelpl/LXTQawmJyvDaVT/Q9P+WqoDiMjrqF6Sy7DzNeeccWVqvqX5TVS6Ky56iS
Mvo/+PAJHkBciR5Xn+Wg2a+7vrZvT6CBoRSOTozlLSM=
-----END CERTIFICATE-----`

func TestCSRValidationSuccess(t *testing.T) {
if err := certdb.ValidateCertificateRequest(&ValidCSR1); err != nil {
t.Fatalf("Couldn't verify valid CSR: %s", err)
}
if err := certdb.ValidateCertificateRequest(&ValidCSR2); err != nil {
t.Fatalf("Couldn't verify valid CSR: %s", err)
}
if err := certdb.ValidateCertificateRequest(&ValidCSR3); err != nil {
t.Fatalf("Couldn't verify valid CSR: %s", err)
}
}

func TestCSRValidationFail(t *testing.T) {
var wrongString string = "this is a real csr!!!"
err := certdb.ValidateCertificateRequest(&wrongString)
if err.Error() != "PEM Certificate Request string not found or malformed" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var ValidCSRWithoutWhitespace = strings.ReplaceAll(ValidCSR1, "\n", "")
err = certdb.ValidateCertificateRequest(&ValidCSRWithoutWhitespace)
if err.Error() != "PEM Certificate Request string not found or malformed" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var wrongPemType string = strings.ReplaceAll(ValidCSR1, "CERTIFICATE REQUEST", "SOME RANDOM PEM TYPE")
err = certdb.ValidateCertificateRequest(&wrongPemType)
if err.Error() != "given PEM string not a certificate request" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var InvalidCSR = strings.ReplaceAll(ValidCSR1, "/", "p")
err = certdb.ValidateCertificateRequest(&InvalidCSR)
if err == nil {
t.Fatalf("Expected CSR to fail validation")
}
}

// Fuzz test

func TestCertValidationSuccess(t *testing.T) {
if err := certdb.ValidateCertificate(&ValidCert2, &ValidCSR2); err != nil {
t.Fatalf("Expected cert to be valid")
}
}

func TestCertValidationFail(t *testing.T) {
var wrongString string = "this is a real cert!!!"
err := certdb.ValidateCertificate(&wrongString, &ValidCSR2)
if err.Error() != "PEM Certificate string not found or malformed" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var ValidCertWithoutWhitespace = strings.ReplaceAll(ValidCert2, "\n", "")
err = certdb.ValidateCertificate(&ValidCertWithoutWhitespace, &ValidCSR2)
if err.Error() != "PEM Certificate string not found or malformed" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var wrongPemType string = strings.ReplaceAll(ValidCert2, "CERTIFICATE", "SOME RANDOM PEM TYPE")
err = certdb.ValidateCertificate(&wrongPemType, &ValidCSR2)
if err.Error() != "given PEM string not a certificate" {
t.Fatalf("Expected error not found:\nReceived: %s", err)
}
var InvalidCert = strings.ReplaceAll(ValidCert2, "M", "i")
err = certdb.ValidateCertificate(&InvalidCert, &ValidCSR2)
if err == nil {
t.Fatalf("Expected cert to fail validation")
}
err = certdb.ValidateCertificate(&ValidCert2, &ValidCSR1)
if err == nil || err.Error() != "certificate does not match CSR" {
t.Fatalf("Expected cert to not match CSR")
}
}

// Fuzz test
// Examples

0 comments on commit c925d47

Please sign in to comment.