2015-06-13 15:00:18 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2015-09-26 20:59:16 +00:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2015-06-13 15:00:18 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewClient(t *testing.T) {
|
|
|
|
keyBits := 32 // small value keeps test fast
|
|
|
|
key, err := rsa.GenerateKey(rand.Reader, keyBits)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("Could not generate test key:", err)
|
|
|
|
}
|
|
|
|
user := mockUser{
|
|
|
|
email: "test@test.com",
|
|
|
|
regres: new(RegistrationResource),
|
|
|
|
privatekey: key,
|
|
|
|
}
|
2015-09-26 20:59:16 +00:00
|
|
|
|
|
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
data, _ := json.Marshal(directory{NewAuthzURL: "http://test", NewCertURL: "http://test", NewRegURL: "http://test", RevokeCertURL: "http://test"})
|
|
|
|
w.Write(data)
|
|
|
|
}))
|
|
|
|
|
|
|
|
caURL, optPort := ts.URL, "1234"
|
2015-10-27 23:00:42 +00:00
|
|
|
client, err := NewClient(caURL, user, keyBits, optPort)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not create client: %v", err)
|
|
|
|
}
|
2015-06-13 15:00:18 +00:00
|
|
|
|
|
|
|
if client.jws == nil {
|
|
|
|
t.Fatalf("Expected client.jws to not be nil")
|
|
|
|
}
|
|
|
|
if expected, actual := key, client.jws.privKey; actual != expected {
|
|
|
|
t.Errorf("Expected jws.privKey to be %p but was %p", expected, actual)
|
|
|
|
}
|
|
|
|
|
|
|
|
if client.keyBits != keyBits {
|
|
|
|
t.Errorf("Expected keyBits to be %d but was %d", keyBits, client.keyBits)
|
|
|
|
}
|
|
|
|
|
2015-06-14 00:33:21 +00:00
|
|
|
if expected, actual := 1, len(client.solvers); actual != expected {
|
2015-11-06 06:43:42 +00:00
|
|
|
t.Fatalf("Expected %d solver(s), got %d", expected, actual)
|
2015-06-13 15:00:18 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 17:45:52 +00:00
|
|
|
simphttp, ok := client.solvers["simpleHttp"].(*simpleHTTPChallenge)
|
2015-06-13 15:00:18 +00:00
|
|
|
if !ok {
|
|
|
|
t.Fatal("Expected simpleHttps solver to be simpleHTTPChallenge type")
|
|
|
|
}
|
|
|
|
if simphttp.jws != client.jws {
|
|
|
|
t.Error("Expected simpleHTTPChallenge to have same jws as client")
|
|
|
|
}
|
|
|
|
if simphttp.optPort != optPort {
|
|
|
|
t.Errorf("Expected simpleHTTPChallenge to have optPort %s but was %s", optPort, simphttp.optPort)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type mockUser struct {
|
|
|
|
email string
|
|
|
|
regres *RegistrationResource
|
|
|
|
privatekey *rsa.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u mockUser) GetEmail() string { return u.email }
|
|
|
|
func (u mockUser) GetRegistration() *RegistrationResource { return u.regres }
|
|
|
|
func (u mockUser) GetPrivateKey() *rsa.PrivateKey { return u.privatekey }
|