commit 4c72b03422135d1cb54a4f625ea2f73ed26e1cbc
parent cbcc237ae9e559754264e966f54700d6870e762d
Author: parazyd <parazyd@dyne.org>
Date: Tue, 12 Dec 2017 02:53:18 +0100
Be more verbose when bailing in dam-dir
Diffstat:
2 files changed, 40 insertions(+), 30 deletions(-)
diff --git a/cmd/dam-dir/main.go b/cmd/dam-dir/main.go
@@ -53,12 +53,17 @@ func postback(rw http.ResponseWriter, data map[string]string, retCode int) error
}
func handlePost(rw http.ResponseWriter, request *http.Request) {
+ var ret map[string]string
+ var n nodeStruct
+
if request.Method != "POST" || request.Header["Content-Type"][0] != "application/json" {
+ ret = map[string]string{"secret": "Invalid request format."}
+ if err := postback(rw, ret, 400); err != nil {
+ lib.CheckError(err)
+ }
return
}
- var ret map[string]string
- var n nodeStruct
decoder := json.NewDecoder(request.Body)
err := decoder.Decode(&n)
if err != nil {
@@ -69,9 +74,18 @@ func handlePost(rw http.ResponseWriter, request *http.Request) {
// Bail out as soon as possible.
if len(n.Nodetype) == 0 || len(n.Address) == 0 ||
len(n.Message) == 0 || len(n.Signature) == 0 {
+ ret = map[string]string{"secret": "Invalid request format."}
+ if err := postback(rw, ret, 400); err != nil {
+ lib.CheckError(err)
+ }
return
}
if !(lib.ValidateOnionAddress(n.Address)) {
+ log.Println("Invalid onion address. Got:", n.Address)
+ ret = map[string]string{"secret": "Invalid onion address."}
+ if err := postback(rw, ret, 400); err != nil {
+ lib.CheckError(err)
+ }
return
}
diff --git a/cmd/dam-dir/main_test.go b/cmd/dam-dir/main_test.go
@@ -61,7 +61,7 @@ func firstAnnValid() (*http.Response, error) {
}
func TestValidFirstHandshake(t *testing.T) {
- //t.SkipNow()
+ t.SkipNow()
resp, err := firstAnnValid()
if err != nil {
t.Fatal(err)
@@ -92,7 +92,7 @@ func TestValidFirstHandshake(t *testing.T) {
}
func TestValidSecondHandshake(t *testing.T) {
- //t.SkipNow()
+ t.SkipNow()
resp, err := firstAnnValid()
if err != nil {
t.Fatal(err)
@@ -154,51 +154,49 @@ func TestValidSecondHandshake(t *testing.T) {
}
func TestInvalidNodetypeFirst(t *testing.T) {
- t.SkipNow()
+ //t.SkipNow()
vals := ValidFirst
vals["nodetype"] = "foobar"
resp, err := postReq(vals)
if err != nil {
t.Fatal(err)
}
- if resp.StatusCode != 400 {
- t.Fatal("Server did not respond with HTTP 400")
- }
m, err := getRespText(resp)
if err != nil {
t.Fatal(err)
}
- if m.Secret == "Invalid nodetype." {
- t.Log("Server replied:", m.Secret)
- } else {
+ if m.Secret != "Invalid nodetype." {
t.Fatal("Server replied:", m.Secret)
}
+ if resp.StatusCode != 400 {
+ t.Fatal("Server did not respond with HTTP 400")
+ }
+ t.Log("Server replied:", m.Secret)
}
func TestInvalidAddressFirst(t *testing.T) {
- t.SkipNow()
+ //t.SkipNow()
vals := ValidFirst
vals["address"] = "foobar.onion"
resp, err := postReq(vals)
if err != nil {
t.Fatal(err)
}
- if resp.StatusCode != 400 {
- t.Fatal("Server did not respond with HTTP 400")
- }
m, err := getRespText(resp)
if err != nil {
t.Fatal(err)
}
- if m.Secret == "Request is not valid." {
- t.Log("Server replied:", m.Secret)
- } else {
+ if m.Secret != "Invalid onion address." {
t.Fatal("Server replied:", m.Secret)
}
+ if resp.StatusCode != 400 {
+ t.Fatal("Server did not respond with HTTP 400")
+ }
+ t.Log("Server replied:", m.Secret)
}
func TestInvalidMessageFirst(t *testing.T) {
- t.SkipNow()
+ //t.SkipNow()
// Valid message and signature, but the signature did not sign this message.
vals := ValidFirst
vals["message"] = "foobar"
@@ -206,18 +204,17 @@ func TestInvalidMessageFirst(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if resp.StatusCode != 400 {
- t.Fatal("Server did not respond with HTTP 400")
- }
m, err := getRespText(resp)
if err != nil {
t.Fatal(err)
}
- if m.Secret == "Request is not valid." {
- t.Log("Server replied:", m.Secret)
- } else {
+ if m.Secret != "Request is not valid." {
t.Fatal("Server replied:", m.Secret)
}
+ if resp.StatusCode != 400 {
+ t.Fatal("Server did not respond with HTTP 400")
+ }
+ t.Log("Server replied:", m.Secret)
}
func TestInvalidSignatureFirst(t *testing.T) {
@@ -229,18 +226,17 @@ func TestInvalidSignatureFirst(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if resp.StatusCode != 400 {
- t.Fatal("Server did not respond with HTTP 400")
- }
m, err := getRespText(resp)
if err != nil {
t.Fatal(err)
}
if strings.HasPrefix(m.Secret, "illegal base64 data at input byte ") {
- t.Log("Server replied:", m.Secret)
- } else {
t.Fatal("Server replied:", m.Secret)
}
+ if resp.StatusCode != 400 {
+ t.Fatal("Server did not respond with HTTP 400")
+ }
+ t.Log("Server replied:", m.Secret)
}
func TestInvalidSecond(t *testing.T) {