summaryrefslogtreecommitdiff
path: root/app/utilities_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'app/utilities_test.go')
-rw-r--r--app/utilities_test.go106
1 files changed, 106 insertions, 0 deletions
diff --git a/app/utilities_test.go b/app/utilities_test.go
new file mode 100644
index 0000000..1865c61
--- /dev/null
+++ b/app/utilities_test.go
@@ -0,0 +1,106 @@
+// +build all general
+
+package main
+
+import (
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+)
+
+func TestHttpGet(t *testing.T) {
+ answer := "Fake webpage here."
+ ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "text/html")
+ fmt.Fprintln(w, answer)
+ }))
+ defer ts.Close()
+
+ fakeUrl := ts.URL
+
+ resp, content, err := HttpGet(fakeUrl)
+ if err != nil {
+ t.Fatal("Error getting web page.")
+ }
+ if resp.StatusCode != 200 {
+ t.Fatal("Expecting 200 as status code.")
+ }
+ if strings.TrimSpace(content) != answer {
+ t.Log("We got this: ", content)
+ t.Log("We expected this: ", answer)
+ t.Fatal("Webpage returned wrong answer.")
+ }
+}
+
+func TestMd5Hash(t *testing.T) {
+ hash := "f9d08276bc85d30d578e8883f3c7e843"
+ testHash := Md5Hash("md5hash")
+
+ if hash != testHash {
+ t.Fatal("Expected %s as hash. Got %s.", hash, testHash)
+ }
+}
+
+func TestRandomKey(t *testing.T) {
+ m := map[string]bool{}
+ var key string
+ for i := 0; i < 100; i++ {
+ key = RandomKey()
+ t.Log(key) // Uncomment to see every generated key.
+ if m[key] {
+ t.Fatal("Key not random.")
+ } else {
+ m[key] = true
+ }
+ if len(key) != 40 {
+ t.Fatal("Expected a key with length of 40. Got %s.", key)
+ }
+ }
+}
+
+func TestPassword(t *testing.T) {
+
+ testHash, err := HashPassword("password")
+ if err != nil {
+ t.Fatal("Hashing password failed.")
+ }
+ verify := VerifyPassword("password", testHash)
+ if !verify {
+ t.Fatal("Verifying password failed.")
+ }
+
+ testHash, err = HashPassword("")
+ if err == nil {
+ t.Fatal("Accepting empty password.")
+ }
+ verify = VerifyPassword("", testHash)
+ if verify {
+ t.Fatal("Verifying empty password.")
+ }
+}
+
+func BenchmarkMd5Hash(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ Md5Hash("md5hash")
+ }
+}
+
+func BenchmarkRandomKey(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ RandomKey()
+ }
+}
+
+func BenchmarkHashPassword(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ HashPassword("password")
+ }
+}
+
+func BenchmarkVerifyPassword(b *testing.B) {
+ for i := 0; i < b.N; i++ {
+ VerifyPassword("password", "$2a$10$OnsbG0Obaz2af3UkoQ9Jaeky3zfRi.0ZHCJC8DlWnbqbpaXEhWqYe")
+ }
+}