From ca21cce19844949a719189a5ef82a33027d6a69e Mon Sep 17 00:00:00 2001 From: Roland Shoemaker Date: Sun, 3 May 2015 15:19:06 -0700 Subject: [PATCH] some registration + authorization tests --- sa/storage-authority.go | 4 +- sa/storage-authority_test.go | 87 +++++++++++++++++++++++++++++++++++- 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/sa/storage-authority.go b/sa/storage-authority.go index afe3c8ab4..4dec50946 100644 --- a/sa/storage-authority.go +++ b/sa/storage-authority.go @@ -294,11 +294,11 @@ func (ssa *SQLStorageAuthority) existingRegistration(id string) (bool) { func (ssa *SQLStorageAuthority) GetRegistration(id string) (reg core.Registration, err error) { regObj, err := ssa.dbMap.Get(Registration{}, id) if err != nil { - err = fmt.Errorf("No registrations with ID %s", id) return } if regObj == nil { - + err = fmt.Errorf("No registrations with ID %s", id) + return } regD := regObj.(*Registration) reg = regD.Registration diff --git a/sa/storage-authority_test.go b/sa/storage-authority_test.go index 93e638d43..4150e59ac 100644 --- a/sa/storage-authority_test.go +++ b/sa/storage-authority_test.go @@ -6,14 +6,19 @@ package sa import ( + "encoding/json" + "net/url" + "time" + _ "github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/mattn/go-sqlite3" "github.com/letsencrypt/boulder/core" + "github.com/letsencrypt/boulder/jose" "github.com/letsencrypt/boulder/test" "io/ioutil" "testing" ) -func TestAddCertificate(t *testing.T) { +func initSA(t *testing.T) (*SQLStorageAuthority) { sa, err := NewSQLStorageAuthority("sqlite3", ":memory:") if err != nil { t.Fatalf("Failed to create SA") @@ -21,6 +26,86 @@ func TestAddCertificate(t *testing.T) { if err = sa.InitTables(); err != nil { t.Fatalf("Failed to create SA") } + return sa +} + +var theKey string = `{ + "kty": "RSA", + "n": "n4EPtAOCc9AlkeQHPzHStgAbgs7bTZLwUBZdR8_KuKPEHLd4rHVTeT-O-XV2jRojdNhxJWTDvNd7nqQ0VEiZQHz_AJmSCpMaJMRBSFKrKb2wqVwGU_NsYOYL-QtiWN2lbzcEe6XC0dApr5ydQLrHqkHHig3RBordaZ6Aj-oBHqFEHYpPe7Tpe-OfVfHd1E6cS6M1FZcD1NNLYD5lFHpPI9bTwJlsde3uhGqC0ZCuEHg8lhzwOHrtIQbS0FVbb9k3-tVTU4fg_3L_vniUFAKwuCLqKnS2BYwdq_mzSnbLY7h_qixoR7jig3__kRhuaxwUkRz5iaiQkqgc5gHdrNP5zw", + "e": "AQAB" +}` + +func TestAddRegistration(t *testing.T) { + sa := initSA(t) + + regID, err := sa.NewRegistration() + test.AssertNotError(t, err, "Couldn't create new registration") + test.Assert(t, regID != "", "ID shouldn't be blank") + + dbReg, err := sa.GetRegistration(regID) + test.AssertNotError(t, err, "Couldn't get registration with ID "+regID) + + expectedReg := core.Registration{ID: regID} + test.AssertEquals(t, dbReg.ID, expectedReg.ID) + + var jwk jose.JsonWebKey + err = json.Unmarshal([]byte(theKey), &jwk) + if err != nil { + t.Errorf("JSON unmarshal error: %+v", err) + return + } + + uu, err := url.Parse("test.com") + u := core.AcmeURL(*uu) + + newReg := core.Registration{ID: regID, Key: jwk, RecoveryToken: "RBNvo1WzZ4oRRq0W9", Contact: []core.AcmeURL{u}, Agreement: "yes"} + err = sa.UpdateRegistration(newReg) + test.AssertNotError(t, err, "Couldn't update registration with ID "+regID) +} + +func TestAddAuthorization(t *testing.T) { + sa := initSA(t) + + paID, err := sa.NewPendingAuthorization() + test.AssertNotError(t, err, "Couldn't create new pending authorization") + test.Assert(t, paID != "", "ID shouldn't be blank") + + dbPa, err := sa.GetAuthorization(paID) + test.AssertNotError(t, err, "Couldn't get pending authorization with ID "+paID) + + expectedPa := core.Authorization{ID: paID} + test.AssertEquals(t, dbPa.ID, expectedPa.ID) + + var jwk jose.JsonWebKey + err = json.Unmarshal([]byte(theKey), &jwk) + if err != nil { + t.Errorf("JSON unmarshal error: %+v", err) + return + } + + uu, err := url.Parse("test.com") + u := core.AcmeURL(*uu) + + chall := core.Challenge{Type: "simpleHttps", Status: core.StatusPending, URI: u, Token: "THISWOULDNTBEAGOODTOKEN", Path: "test-me"} + + combos := make([][]int, 1) + combos[0] = []int{0,1} + + + newPa := core.Authorization{ID: paID, Identifier: core.AcmeIdentifier{Type: core.IdentifierDNS, Value: "wut.com"}, Key: jwk, Status: core.StatusPending, Expires: time.Now().AddDate(0, 0, 1), Challenges: []core.Challenge{chall}, Combinations: combos, Contact: []core.AcmeURL{u}} + err = sa.UpdatePendingAuthorization(newPa) + test.AssertNotError(t, err, "Couldn't update pending authorization with ID "+paID) + + newPa.Status = core.StatusValid + err = sa.FinalizeAuthorization(newPa) + test.AssertNotError(t, err, "Couldn't finalize pending authorization with ID "+paID) + + dbPa, err = sa.GetAuthorization(paID) + test.AssertNotError(t, err, "Couldn't get authorization with ID "+paID) +} + +func TestAddCertificate(t *testing.T) { + sa := initSA(t) // An example cert taken from EFF's website certDER, err := ioutil.ReadFile("www.eff.org.der")