Skip to content

Commit 5628e97

Browse files
authored
Merge pull request #1 from benbuzbee/benbuz/tests
Fix tests and add OID test
2 parents 38b8240 + b156a8c commit 5628e97

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

cli/gencert/gencert_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package gencert
33
import (
44
"io/ioutil"
55
"os"
6+
"strings"
67
"testing"
78

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

217218
}
219+
220+
func TestOidMain(t *testing.T) {
221+
c := cli.Config{
222+
CAFile: "../testdata/ca.pem",
223+
CAKeyFile: "../testdata/ca-key.pem",
224+
}
225+
err := gencertMain([]string{"../testdata/bad_oid_csr.json"}, c)
226+
if err == nil {
227+
t.Fatal("Expected error")
228+
}
229+
if !strings.Contains(err.Error(), "invalid OID part abc") {
230+
t.Fatalf("Unexpected error: %s", err.Error())
231+
}
232+
}

cli/testdata/bad_oid_csr.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"hosts": [
3+
"cloudflare.com",
4+
"www.cloudflare.com"
5+
],
6+
"key": {
7+
"algo": "rsa",
8+
"size": 2048
9+
},
10+
"names": [
11+
{
12+
"C": "US",
13+
"L": "San Francisco",
14+
"O": "CloudFlare",
15+
"OU": "Systems Engineering",
16+
"ST": "California",
17+
"OID": {
18+
"abc": "abc"
19+
}
20+
}
21+
]
22+
}

cli/testdata/csr.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"L": "San Francisco",
1414
"O": "CloudFlare",
1515
"OU": "Systems Engineering",
16-
"ST": "California"
16+
"ST": "California",
17+
"OID": {
18+
"1.2.3.4.5": "abc"
19+
}
1720
}
1821
]
19-
}
22+
}

csr/csr.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ func appendIf(s string, a *[]string) {
160160
}
161161
}
162162

163+
// OIDFromString creates an ASN1 ObjectIdentifier from its string representation
163164
func OIDFromString(s string) (asn1.ObjectIdentifier, error) {
164165
var oid []int
165166
parts := strings.Split(s, ".")
@@ -260,7 +261,6 @@ func ParseRequest(req *CertificateRequest) (csr, key []byte, err error) {
260261
// from an existing certificate. For a root certificate, the CA expiry
261262
// length is calculated as the duration between cert.NotAfter and cert.NotBefore.
262263
func ExtractCertificateRequest(cert *x509.Certificate) *CertificateRequest {
263-
fmt.Printf("ExctractCertificateRequest %+v\n", *cert)
264264
req := New()
265265
req.CN = cert.Subject.CommonName
266266
req.Names = getNames(cert.Subject)

csr/csr_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ func TestPKIXName(t *testing.T) {
7272
KeyRequest: NewKeyRequest(),
7373
}
7474

75-
name := cr.Name()
75+
name, err := cr.Name()
76+
if err != nil {
77+
t.Fatalf("Error getting name: %s", err.Error())
78+
}
7679
if len(name.Country) != 2 {
7780
t.Fatal("Expected two countries in SubjInfo.")
7881
} else if len(name.Province) != 2 {
@@ -113,7 +116,7 @@ func TestParseRequest(t *testing.T) {
113116
KeyRequest: NewKeyRequest(),
114117
Extensions: []pkix.Extension{
115118
pkix.Extension{
116-
Id: asn1.ObjectIdentifier{1, 2, 3, 4, 5},
119+
Id: asn1.ObjectIdentifier{1, 2, 3, 4, 5},
117120
Value: []byte("AgEB"),
118121
},
119122
},
@@ -123,7 +126,7 @@ func TestParseRequest(t *testing.T) {
123126
if err != nil {
124127
t.Fatalf("%v", err)
125128
}
126-
129+
127130
block, _ := pem.Decode(csrBytes)
128131
if block == nil {
129132
t.Fatalf("%v", err)

0 commit comments

Comments
 (0)