From 2511581b7d22f041146e68de48e43a5b334edcb5 Mon Sep 17 00:00:00 2001 From: Umputun Date: Thu, 7 Jun 2018 15:11:13 -0500 Subject: [PATCH] test for invalid url --- app/main_test.go | 18 +++++++++++++----- app/store/user.go | 14 ++++++++------ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/app/main_test.go b/app/main_test.go index 31b41a71..da4b1bbd 100644 --- a/app/main_test.go +++ b/app/main_test.go @@ -55,11 +55,19 @@ func TestApplicationFailed(t *testing.T) { "open /dev/null/remark.db: not a directory") t.Log(err) - //p.ParseArgs([]string{"--secret=123456", "--url=https://demo.remark42.com", "--bolt=/tmp", "--backup=/not-writable"}) - //_, err = New(opts) - //assert.EqualError(t, err, "can't initialize data store: failed to make boltdb for /dev/null/remark.db: "+ - // "open /dev/null/remark.db: not a directory") - //t.Log(err) + // RO backup location + opts = Opts{} + p.ParseArgs([]string{"--secret=123456", "--url=https://demo.remark42.com", "--bolt=/tmp", "--backup=/dev/null/not-writable"}) + _, err = New(opts) + assert.EqualError(t, err, "can't check directory status for /dev/null/not-writable: stat /dev/null/not-writable: not a directory") + t.Log(err) + + // invalid url + opts = Opts{} + p.ParseArgs([]string{"--secret=123456", "--url=demo.remark42.com", "--bolt=/tmp"}) + _, err = New(opts) + assert.EqualError(t, err, "invalid remark42 url demo.remark42.com") + t.Log(err) } func TestApplicationShutdown(t *testing.T) { diff --git a/app/store/user.go b/app/store/user.go index cd725f82..4fab9a29 100644 --- a/app/store/user.go +++ b/app/store/user.go @@ -3,8 +3,10 @@ package store import ( "crypto/hmac" "crypto/sha1" + "encoding/hex" "fmt" "hash/crc64" + "io" "log" "regexp" ) @@ -29,30 +31,30 @@ func (u *User) HashIP(secret string) { // HashValue makes hmac with secret func HashValue(val string, secret string) string { - if val == "" || reValidSha.Match([]byte(val)) { + if val == "" || reValidSha.MatchString(val) { return val // already hashed or empty } key := []byte(secret) h := hmac.New(sha1.New, key) - if _, err := h.Write([]byte(val)); err != nil { + if _, err := io.WriteString(h, val); err != nil { // fail back to crc64 log.Printf("[WARN] can't hash ip, %s", err) return fmt.Sprintf("%x", crc64.Checksum([]byte(val), crc64.MakeTable(crc64.ECMA))) } - return fmt.Sprintf("%x", h.Sum(nil)) + return hex.EncodeToString(h.Sum(nil)) } // EncodeID hashes id to sha1. The function intentionally left outside of User struct because in some cases // we need hashing for parts of id, in some others hashing for non-User values. func EncodeID(id string) string { - if reValidSha.Match([]byte(id)) { + if reValidSha.MatchString(id) { return id // already hashed or empty } h := sha1.New() - if _, err := h.Write([]byte(id)); err != nil { + if _, err := io.WriteString(h, id); err != nil { // fail back to crc64 log.Printf("[WARN] can't hash id %s, %s", id, err) return fmt.Sprintf("%x", crc64.Checksum([]byte(id), crc64.MakeTable(crc64.ECMA))) } - return fmt.Sprintf("%x", h.Sum(nil)) + return hex.EncodeToString(h.Sum(nil)) }