From 70d080b69a1bce3125d8d0b83e23775880241763 Mon Sep 17 00:00:00 2001 From: Horus_Arch Date: Mon, 23 Feb 2015 12:13:32 +0100 Subject: Refactor and more unit tests. --- app/utilities_test.go | 68 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 3 deletions(-) (limited to 'app/utilities_test.go') diff --git a/app/utilities_test.go b/app/utilities_test.go index 5405309..22f1f5e 100644 --- a/app/utilities_test.go +++ b/app/utilities_test.go @@ -3,9 +3,37 @@ 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") @@ -16,9 +44,19 @@ func TestMd5Hash(t *testing.T) { } func TestRandomKey(t *testing.T) { - key := RandomKey() - if len(key) != 40 { - t.Fatal("Expected a key with length of 40. Got %s.", key) + m := map[string]bool{} + var key string + for i := 0; i < 100; i++ { + key = RandomKey() + t.Log(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) + } } } @@ -42,3 +80,27 @@ func TestPassword(t *testing.T) { 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") + } +} -- cgit v1.2.3