2015-11-12 22:51:07 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rsa"
|
|
|
|
"io/ioutil"
|
2016-02-10 11:19:29 +00:00
|
|
|
"os"
|
2015-11-12 22:51:07 +00:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2015-12-05 14:53:53 +00:00
|
|
|
func TestHTTPChallenge(t *testing.T) {
|
2015-11-12 22:51:07 +00:00
|
|
|
privKey, _ := generatePrivateKey(rsakey, 512)
|
2015-12-05 14:53:53 +00:00
|
|
|
j := &jws{privKey: privKey.(*rsa.PrivateKey)}
|
2016-01-15 04:06:25 +00:00
|
|
|
clientChallenge := challenge{Type: HTTP01, Token: "http1"}
|
2015-12-27 18:26:47 +00:00
|
|
|
mockValidate := func(_ *jws, _, _ string, chlng challenge) error {
|
2015-12-05 14:53:53 +00:00
|
|
|
uri := "http://localhost:23457/.well-known/acme-challenge/" + chlng.Token
|
2015-12-30 22:01:21 +00:00
|
|
|
resp, err := httpGet(uri)
|
2015-12-05 14:53:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-11-12 22:51:07 +00:00
|
|
|
}
|
2015-12-05 14:53:53 +00:00
|
|
|
defer resp.Body.Close()
|
2015-11-12 22:51:07 +00:00
|
|
|
|
2015-12-05 14:53:53 +00:00
|
|
|
if want := "text/plain"; resp.Header.Get("Content-Type") != want {
|
|
|
|
t.Errorf("Get(%q) Content-Type: got %q, want %q", uri, resp.Header.Get("Content-Type"), want)
|
2015-11-12 22:51:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 14:53:53 +00:00
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-11-12 22:51:07 +00:00
|
|
|
}
|
2015-12-05 14:53:53 +00:00
|
|
|
bodyStr := string(body)
|
2015-11-12 22:51:07 +00:00
|
|
|
|
2015-12-05 14:53:53 +00:00
|
|
|
if bodyStr != chlng.KeyAuthorization {
|
|
|
|
t.Errorf("Get(%q) Body: got %q, want %q", uri, bodyStr, chlng.KeyAuthorization)
|
2015-11-12 22:51:07 +00:00
|
|
|
}
|
|
|
|
|
2015-12-05 14:53:53 +00:00
|
|
|
return nil
|
2015-11-12 22:51:07 +00:00
|
|
|
}
|
2016-01-15 04:06:25 +00:00
|
|
|
solver := &httpChallenge{jws: j, validate: mockValidate, provider: &httpChallengeServer{port: "23457"}}
|
2015-11-12 22:51:07 +00:00
|
|
|
|
2015-12-05 14:53:53 +00:00
|
|
|
if err := solver.Solve(clientChallenge, "localhost:23457"); err != nil {
|
|
|
|
t.Errorf("Solve error: got %v, want nil", err)
|
2015-11-12 22:51:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 14:53:53 +00:00
|
|
|
func TestHTTPChallengeInvalidPort(t *testing.T) {
|
|
|
|
privKey, _ := generatePrivateKey(rsakey, 128)
|
|
|
|
j := &jws{privKey: privKey.(*rsa.PrivateKey)}
|
2016-01-15 04:06:25 +00:00
|
|
|
clientChallenge := challenge{Type: HTTP01, Token: "http2"}
|
|
|
|
solver := &httpChallenge{jws: j, validate: stubValidate, provider: &httpChallengeServer{port: "123456"}}
|
2015-12-05 14:53:53 +00:00
|
|
|
|
|
|
|
if err := solver.Solve(clientChallenge, "localhost:123456"); err == nil {
|
2015-12-30 22:01:21 +00:00
|
|
|
t.Errorf("Solve error: got %v, want error", err)
|
2015-12-05 14:53:53 +00:00
|
|
|
} else if want := "invalid port 123456"; !strings.HasSuffix(err.Error(), want) {
|
|
|
|
t.Errorf("Solve error: got %q, want suffix %q", err.Error(), want)
|
2015-11-12 22:51:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-10 11:19:29 +00:00
|
|
|
|
|
|
|
func TestHTTPChallengeWebRoot(t *testing.T) {
|
|
|
|
privKey, _ := generatePrivateKey(rsakey, 512)
|
|
|
|
j := &jws{privKey: privKey.(*rsa.PrivateKey)}
|
|
|
|
clientChallenge := challenge{Type: HTTP01, Token: "http1"}
|
|
|
|
mockValidate := func(_ *jws, _, _ string, chlng challenge) error {
|
|
|
|
challengeFilePath := "webroot/.well-known/acme-challenge/" + chlng.Token
|
|
|
|
|
|
|
|
if _, err := os.Stat(challengeFilePath); os.IsNotExist(err) {
|
|
|
|
t.Error("Challenge file was not created in webroot")
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(challengeFilePath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dataStr := string(data)
|
|
|
|
|
|
|
|
if dataStr != chlng.KeyAuthorization {
|
|
|
|
t.Errorf("Challenge file content: got %q, want %q", dataStr, chlng.KeyAuthorization)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
solver := &httpChallenge{jws: j, validate: mockValidate, provider: &httpChallengeWebRoot{path: "webroot"}}
|
|
|
|
|
|
|
|
os.MkdirAll("webroot/.well-known/acme-challenge", 0777)
|
|
|
|
if err := solver.Solve(clientChallenge, "localhost:23457"); err != nil {
|
|
|
|
t.Errorf("Solve error: got %v, want nil", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll("webroot")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestHTTPChallengeWebRootInvalidPath(t *testing.T) {
|
|
|
|
privKey, _ := generatePrivateKey(rsakey, 128)
|
|
|
|
j := &jws{privKey: privKey.(*rsa.PrivateKey)}
|
|
|
|
clientChallenge := challenge{Type: HTTP01, Token: "http2"}
|
|
|
|
solver := &httpChallenge{jws: j, validate: stubValidate, provider: &httpChallengeWebRoot{path: "/invalid-path-123456"}}
|
|
|
|
|
|
|
|
if err := solver.Solve(clientChallenge, "localhost:123456"); err == nil {
|
|
|
|
t.Errorf("Solve error: got %v, want error", err)
|
|
|
|
} else if want := "Could not write file in webroot"; !strings.Contains(err.Error(), want) {
|
|
|
|
t.Errorf("Solve error: got %q, want content %q", err.Error(), want)
|
|
|
|
}
|
|
|
|
}
|