test for invalid url

This commit is contained in:
Umputun
2018-06-07 15:11:13 -05:00
parent fff845a2cf
commit 2511581b7d
2 changed files with 21 additions and 11 deletions
+13 -5
View File
@@ -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) {
+8 -6
View File
@@ -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))
}