http: test all seal endpoints

This commit is contained in:
Mitchell Hashimoto 2015-03-12 11:12:44 -07:00
parent 352ad00e68
commit 1baf86b061
3 changed files with 136 additions and 3 deletions

View File

@ -1,7 +1,9 @@
package http
import (
"bytes"
"encoding/json"
"io"
"net"
"net/http"
"testing"
@ -50,9 +52,38 @@ func testServer(t *testing.T, core *vault.Core) (net.Listener, string) {
return ln, addr
}
func testHttpPut(t *testing.T, addr string, body interface{}) *http.Response {
bodyReader := new(bytes.Buffer)
if body != nil {
enc := json.NewEncoder(bodyReader)
if err := enc.Encode(body); err != nil {
t.Fatalf("err:%s", err)
}
}
req, err := http.NewRequest("PUT", addr, bodyReader)
if err != nil {
t.Fatalf("err: %s", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("err: %s", err)
}
return resp
}
func testResponseStatus(t *testing.T, resp *http.Response, code int) {
if resp.StatusCode != code {
t.Fatalf("Expected status %d, got %d", code, resp.StatusCode)
body := new(bytes.Buffer)
io.Copy(body, resp.Body)
resp.Body.Close()
t.Fatalf(
"Expected status %d, got %d. Body:\n\n%s",
code, resp.StatusCode, body.String())
}
}

View File

@ -1,6 +1,7 @@
package http
import (
"encoding/hex"
"errors"
"net/http"
@ -32,7 +33,7 @@ func handleSysUnseal(core *vault.Core) http.Handler {
// Parse the request
var req UnsealRequest
if err := parseRequest(r, req); err != nil {
if err := parseRequest(r, &req); err != nil {
respondError(w, http.StatusBadRequest, err)
return
}
@ -43,8 +44,17 @@ func handleSysUnseal(core *vault.Core) http.Handler {
return
}
// Decode the key, which is hex encoded
key, err := hex.DecodeString(req.Key)
if err != nil {
respondError(
w, http.StatusBadRequest,
errors.New("'key' must be a valid hex-string"))
return
}
// Attempt the unseal
if _, err := core.Unseal([]byte(req.Key)); err != nil {
if _, err := core.Unseal(key); err != nil {
respondError(w, http.StatusInternalServerError, err)
return
}

View File

@ -1,6 +1,7 @@
package http
import (
"encoding/hex"
"net/http"
"reflect"
"testing"
@ -30,3 +31,94 @@ func TestSysSealStatus(t *testing.T) {
t.Fatalf("bad: %#v", actual)
}
}
func TestSysSeal(t *testing.T) {
core := testCore(t)
testCoreInit(t, core)
ln, addr := testServer(t, core)
defer ln.Close()
resp := testHttpPut(t, addr+"/v1/sys/seal", nil)
testResponseStatus(t, resp, 204)
check, err := core.Sealed()
if err != nil {
t.Fatalf("err: %s", err)
}
if !check {
t.Fatal("should be sealed")
}
}
func TestSysSeal_unsealed(t *testing.T) {
core := testCore(t)
ln, addr := testServer(t, core)
defer ln.Close()
keys := testCoreInit(t, core)
if _, err := core.Unseal(keys[0]); err != nil {
t.Fatalf("err: %s", err)
}
resp := testHttpPut(t, addr+"/v1/sys/seal", nil)
testResponseStatus(t, resp, 204)
check, err := core.Sealed()
if err != nil {
t.Fatalf("err: %s", err)
}
if !check {
t.Fatal("should be sealed")
}
}
func TestSysUnseal(t *testing.T) {
core := testCore(t)
keys := testCoreInit(t, core)
ln, addr := testServer(t, core)
defer ln.Close()
resp := testHttpPut(t, addr+"/v1/sys/unseal", map[string]interface{}{
"key": hex.EncodeToString(keys[0]),
})
var actual map[string]interface{}
expected := map[string]interface{}{
"sealed": false,
"t": float64(1),
"n": float64(1),
"progress": float64(0),
}
testResponseStatus(t, resp, 200)
testResponseBody(t, resp, &actual)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}
func TestSysUnseal_badKey(t *testing.T) {
// TODO: wait on Armon to fix error message from core
t.Skip()
core := testCore(t)
testCoreInit(t, core)
ln, addr := testServer(t, core)
defer ln.Close()
resp := testHttpPut(t, addr+"/v1/sys/unseal", map[string]interface{}{
"key": "foo",
})
var actual map[string]interface{}
expected := map[string]interface{}{
"sealed": true,
"t": float64(1),
"n": float64(1),
"progress": float64(0),
}
testResponseStatus(t, resp, 200)
testResponseBody(t, resp, &actual)
if !reflect.DeepEqual(actual, expected) {
t.Fatalf("bad: %#v", actual)
}
}