Skip to content

Commit

Permalink
Merge pull request #1 from benbuzbee/benbuz/tests
Browse files Browse the repository at this point in the history
Fix tests and add OID test
  • Loading branch information
akgmartin committed Jan 29, 2021
2 parents 38b8240 + b156a8c commit 5628e97
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
15 changes: 15 additions & 0 deletions cli/gencert/gencert_test.go
Expand Up @@ -3,6 +3,7 @@ package gencert
import (
"io/ioutil"
"os"
"strings"
"testing"

"github.com/cloudflare/cfssl/cli"
Expand Down Expand Up @@ -215,3 +216,17 @@ func TestBadGencertMain(t *testing.T) {
}

}

func TestOidMain(t *testing.T) {
c := cli.Config{
CAFile: "../testdata/ca.pem",
CAKeyFile: "../testdata/ca-key.pem",
}
err := gencertMain([]string{"../testdata/bad_oid_csr.json"}, c)
if err == nil {
t.Fatal("Expected error")
}
if !strings.Contains(err.Error(), "invalid OID part abc") {
t.Fatalf("Unexpected error: %s", err.Error())
}
}
22 changes: 22 additions & 0 deletions cli/testdata/bad_oid_csr.json
@@ -0,0 +1,22 @@
{
"hosts": [
"cloudflare.com",
"www.cloudflare.com"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "US",
"L": "San Francisco",
"O": "CloudFlare",
"OU": "Systems Engineering",
"ST": "California",
"OID": {
"abc": "abc"
}
}
]
}
7 changes: 5 additions & 2 deletions cli/testdata/csr.json
Expand Up @@ -13,7 +13,10 @@
"L": "San Francisco",
"O": "CloudFlare",
"OU": "Systems Engineering",
"ST": "California"
"ST": "California",
"OID": {
"1.2.3.4.5": "abc"
}
}
]
}
}
2 changes: 1 addition & 1 deletion csr/csr.go
Expand Up @@ -160,6 +160,7 @@ func appendIf(s string, a *[]string) {
}
}

// OIDFromString creates an ASN1 ObjectIdentifier from its string representation
func OIDFromString(s string) (asn1.ObjectIdentifier, error) {
var oid []int
parts := strings.Split(s, ".")
Expand Down Expand Up @@ -260,7 +261,6 @@ func ParseRequest(req *CertificateRequest) (csr, key []byte, err error) {
// from an existing certificate. For a root certificate, the CA expiry
// length is calculated as the duration between cert.NotAfter and cert.NotBefore.
func ExtractCertificateRequest(cert *x509.Certificate) *CertificateRequest {
fmt.Printf("ExctractCertificateRequest %+v\n", *cert)
req := New()
req.CN = cert.Subject.CommonName
req.Names = getNames(cert.Subject)
Expand Down
9 changes: 6 additions & 3 deletions csr/csr_test.go
Expand Up @@ -72,7 +72,10 @@ func TestPKIXName(t *testing.T) {
KeyRequest: NewKeyRequest(),
}

name := cr.Name()
name, err := cr.Name()
if err != nil {
t.Fatalf("Error getting name: %s", err.Error())
}
if len(name.Country) != 2 {
t.Fatal("Expected two countries in SubjInfo.")
} else if len(name.Province) != 2 {
Expand Down Expand Up @@ -113,7 +116,7 @@ func TestParseRequest(t *testing.T) {
KeyRequest: NewKeyRequest(),
Extensions: []pkix.Extension{
pkix.Extension{
Id: asn1.ObjectIdentifier{1, 2, 3, 4, 5},
Id: asn1.ObjectIdentifier{1, 2, 3, 4, 5},
Value: []byte("AgEB"),
},
},
Expand All @@ -123,7 +126,7 @@ func TestParseRequest(t *testing.T) {
if err != nil {
t.Fatalf("%v", err)
}

block, _ := pem.Decode(csrBytes)
if block == nil {
t.Fatalf("%v", err)
Expand Down

0 comments on commit 5628e97

Please sign in to comment.