-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hibp_test.go
125 lines (113 loc) · 3.79 KB
/
hibp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package hibp
import (
"fmt"
"net/http"
"os"
"testing"
"time"
)
// TestNew tests the New() function
func TestNew(t *testing.T) {
hc := New()
if *hc.PwnedPassAPI.hibp != hc {
t.Errorf("hibp client creation failed")
}
}
// TestNewWithNil tests the New() function with a nil option
func TestNewWithNil(t *testing.T) {
hc := New(nil)
if *hc.PwnedPassAPI.hibp != hc {
t.Errorf("hibp client creation failed")
}
}
// TestNewWithHttpTimeout tests the New() function with the http timeout option
func TestNewWithHttpTimeout(t *testing.T) {
hc := New(WithHTTPTimeout(time.Second * 10))
if hc.to != time.Second*10 {
t.Errorf("hibp client timeout option was not set properly. Expected %d, got: %d",
time.Second*10, hc.to)
}
}
// TestNewWithPwnedPadding tests the New() function with the PwnedPadding option
func TestNewWithPwnedPadding(t *testing.T) {
hc := New(WithPwnedPadding())
if !hc.PwnedPassAPIOpts.WithPadding {
t.Errorf("hibp client pwned padding option was not set properly. Expected %t, got: %t",
true, hc.PwnedPassAPIOpts.WithPadding)
}
}
// TestNewWithPwnedNTLMHash tests the New() function with the PwnedPadding option
func TestNewWithPwnedNTLMHash(t *testing.T) {
hc := New(WithPwnedNTLMHash())
if hc.PwnedPassAPIOpts.HashMode != HashModeNTLM {
t.Errorf("hibp client NTLM hash mode option was not set properly. Expected %d, got: %d",
HashModeNTLM, hc.PwnedPassAPIOpts.HashMode)
}
hc = New()
if hc.PwnedPassAPIOpts.HashMode != HashModeSHA1 {
t.Errorf("hibp client SHA-1 hash mode option was not set properly. Expected %d, got: %d",
HashModeSHA1, hc.PwnedPassAPIOpts.HashMode)
}
}
// TestNewWithApiKey tests the New() function with the API key set
func TestNewWithApiKey(t *testing.T) {
apiKey := os.Getenv("HIBP_API_KEY")
hc := New(WithAPIKey(apiKey), WithRateLimitSleep())
if hc.ak != apiKey {
t.Errorf("hibp client API key was not set properly. Expected %s, got: %s",
apiKey, hc.ak)
}
}
// TestNewWithUserAgent tests the New() function with a custom user agent
func TestNewWithUserAgent(t *testing.T) {
hc := New()
if hc.ua != DefaultUserAgent {
t.Errorf("hibp client default user agent was not set properly. Expected %s, got: %s",
DefaultUserAgent, hc.ua)
}
custUA := fmt.Sprintf("customUA v%s", Version)
hc = New(WithUserAgent(custUA))
if hc.ua != custUA {
t.Errorf("hibp client custom user agent was not set properly. Expected %s, got: %s",
custUA, hc.ua)
}
hc = New(WithUserAgent(""))
if hc.ua != DefaultUserAgent {
t.Errorf("hibp client custom user agent was not set properly. Expected %s, got: %s",
DefaultUserAgent, hc.ua)
}
}
func TestClient_HTTPReq(t *testing.T) {
u1 := "this://is.invalid.tld/with/invalid/chars/" + string([]byte{0x7f})
u2 := "this://is.invalid.tld/"
hc := New()
_, err := hc.HTTPReq(http.MethodGet, u1, map[string]string{"foo": "bar"})
if err == nil {
t.Errorf("HTTP GET request was supposed to fail, but didn't")
}
_, err = hc.HTTPReq("äöü", u2, map[string]string{"foo": "bar"})
if err == nil {
t.Errorf("HTTP GET request was supposed to fail, but didn't")
}
_, err = hc.HTTPReq("POST", u2, map[string]string{"foo": "bar"})
if err != nil {
t.Errorf("HTTP POST request failed: %s", err)
}
}
func TestClient_HTTPResBody(t *testing.T) {
u1 := "this://is.invalid.tld/with/invalid/chars/" + string([]byte{0x7f})
u2 := "this://is.invalid.tld/"
hc := New()
_, _, err := hc.HTTPResBody(http.MethodGet, u1, map[string]string{"foo": "bar"})
if err == nil {
t.Errorf("HTTP GET request was supposed to fail, but didn't")
}
_, _, err = hc.HTTPResBody("äöü", u2, map[string]string{"foo": "bar"})
if err == nil {
t.Errorf("HTTP GET request was supposed to fail, but didn't")
}
_, _, err = hc.HTTPResBody("POST", u2, map[string]string{"foo": "bar"})
if err == nil {
t.Errorf("HTTP POST request was supposed to fail, but didn't")
}
}