2021-09-21 11:21:04 +02:00
|
|
|
package hibp
|
|
|
|
|
|
|
|
import (
|
2022-05-08 12:02:58 +02:00
|
|
|
"fmt"
|
2021-09-21 11:21:04 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-06-08 17:26:41 +02:00
|
|
|
const (
|
|
|
|
// PwStringInsecure is the string representation of an insecure password
|
|
|
|
PwStringInsecure = "test"
|
|
|
|
|
|
|
|
// PwStringSecure is the string representation of an insecure password
|
|
|
|
PwStringSecure = "F/0Ws#.%{Z/NVax=OU8Ajf1qTRLNS12p/?s/adX"
|
|
|
|
|
|
|
|
// PwHashInsecure is the SHA1 checksum of an insecure password
|
|
|
|
// Represents the string: test
|
|
|
|
PwHashInsecure = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"
|
|
|
|
|
|
|
|
// PwHashSecure is the SHA1 checksum of a secure password
|
|
|
|
// Represents the string: F/0Ws#.%{Z/NVax=OU8Ajf1qTRLNS12p/?s/adX
|
|
|
|
PwHashSecure = "90efc095c82eab44e882fda507cfab1a2cd31fc0"
|
|
|
|
)
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// TestPwnedPassAPI_CheckPassword verifies the Pwned Passwords API with the CheckPassword method
|
|
|
|
func TestPwnedPassAPI_CheckPassword(t *testing.T) {
|
2021-09-21 11:21:04 +02:00
|
|
|
testTable := []struct {
|
|
|
|
testName string
|
|
|
|
pwString string
|
|
|
|
isLeaked bool
|
|
|
|
}{
|
2022-06-08 17:26:41 +02:00
|
|
|
{"weak password 'test123' is expected to be leaked", PwStringInsecure, true},
|
2022-10-29 15:32:12 +02:00
|
|
|
{
|
|
|
|
"strong, unknown password is expected to be not leaked",
|
|
|
|
PwStringSecure, false,
|
|
|
|
},
|
2021-09-21 11:21:04 +02:00
|
|
|
}
|
|
|
|
hc := New()
|
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
2022-10-29 15:32:12 +02:00
|
|
|
m, _, err := hc.PwnedPassAPI.CheckPassword(tc.pwString)
|
2021-09-21 11:21:04 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
if m == nil && tc.isLeaked {
|
|
|
|
t.Errorf("password is expected to be leaked but 0 leaks were returned in Pwned Passwords DB")
|
|
|
|
}
|
|
|
|
if m != nil && m.Count > 0 && !tc.isLeaked {
|
|
|
|
t.Errorf("password is not expected to be leaked but %d leaks were found in Pwned Passwords DB",
|
|
|
|
m.Count)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// TestPwnedPassAPI_CheckSHA1 verifies the Pwned Passwords API with the CheckSHA1 method
|
|
|
|
func TestPwnedPassAPI_CheckSHA1(t *testing.T) {
|
2021-09-21 11:21:04 +02:00
|
|
|
testTable := []struct {
|
2022-05-08 12:44:20 +02:00
|
|
|
testName string
|
|
|
|
pwHash string
|
|
|
|
isLeaked bool
|
|
|
|
shouldFail bool
|
2021-09-21 11:21:04 +02:00
|
|
|
}{
|
2022-10-29 15:32:12 +02:00
|
|
|
{
|
|
|
|
"weak password 'test' is expected to be leaked",
|
|
|
|
PwHashInsecure, true, false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"strong, unknown password is expected to be not leaked",
|
|
|
|
PwHashSecure, false, false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"empty string should fail",
|
|
|
|
"", false, true,
|
|
|
|
},
|
2021-09-21 11:21:04 +02:00
|
|
|
}
|
|
|
|
hc := New()
|
|
|
|
for _, tc := range testTable {
|
|
|
|
t.Run(tc.testName, func(t *testing.T) {
|
2022-10-29 15:32:12 +02:00
|
|
|
m, _, err := hc.PwnedPassAPI.CheckSHA1(tc.pwHash)
|
2022-05-08 12:44:20 +02:00
|
|
|
if err != nil && !tc.shouldFail {
|
2021-09-21 11:21:04 +02:00
|
|
|
t.Error(err)
|
2021-09-21 19:46:48 +02:00
|
|
|
return
|
2021-09-21 11:21:04 +02:00
|
|
|
}
|
|
|
|
if m == nil && tc.isLeaked {
|
|
|
|
t.Errorf("password is expected to be leaked but 0 leaks were returned in Pwned Passwords DB")
|
|
|
|
}
|
|
|
|
if m != nil && m.Count > 0 && !tc.isLeaked {
|
|
|
|
t.Errorf("password is not expected to be leaked but %d leaks were found in Pwned Passwords DB",
|
|
|
|
m.Count)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-05-08 12:02:58 +02:00
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// TestPwnedPassAPI_ListHashesPrefix tests the ListHashesPrefix method (especially for failures that are not
|
2022-05-08 12:44:20 +02:00
|
|
|
// tested by the other tests already)
|
2022-10-29 15:32:12 +02:00
|
|
|
func TestPwnedPassAPI_ListHashesPrefix(t *testing.T) {
|
2022-05-08 12:44:20 +02:00
|
|
|
hc := New()
|
|
|
|
|
2022-06-08 17:26:41 +02:00
|
|
|
// Should return at least 1 restults
|
2022-10-29 15:32:12 +02:00
|
|
|
l, _, err := hc.PwnedPassAPI.ListHashesPrefix("a94a8")
|
2022-06-08 17:26:41 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ListHashesPrefix was not supposed to fail, but did: %s", err)
|
|
|
|
}
|
|
|
|
if len(l) <= 0 {
|
|
|
|
t.Errorf("ListHashesPrefix was supposed to return a list longer than 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prefix has wrong size
|
2022-10-29 15:32:12 +02:00
|
|
|
_, _, err = hc.PwnedPassAPI.ListHashesPrefix("ZZZZZZZZZZZZZZ")
|
2022-05-08 12:44:20 +02:00
|
|
|
if err == nil {
|
2022-06-08 17:26:41 +02:00
|
|
|
t.Errorf("ListHashesPrefix was supposed to fail, but didn't")
|
2022-05-08 12:44:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Non allowed characters
|
2022-10-29 15:32:12 +02:00
|
|
|
_, _, err = hc.PwnedPassAPI.ListHashesPrefix(string([]byte{0, 0, 0, 0, 0}))
|
2022-06-08 17:26:41 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("ListHashesPrefix was supposed to fail, but didn't")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// TestPwnedPassApi_ListHashesSHA1 tests the PwnedPassAPI.ListHashesSHA1 metethod
|
|
|
|
func TestPwnedPassAPI_ListHashesSHA1(t *testing.T) {
|
2022-06-08 17:26:41 +02:00
|
|
|
hc := New()
|
|
|
|
|
|
|
|
// List length should be >0
|
2022-10-29 15:32:12 +02:00
|
|
|
l, _, err := hc.PwnedPassAPI.ListHashesSHA1(PwHashInsecure)
|
2022-06-08 17:26:41 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ListHashesSHA1 was not supposed to fail, but did: %s", err)
|
|
|
|
}
|
|
|
|
if len(l) <= 0 {
|
|
|
|
t.Errorf("ListHashesSHA1 was supposed to return a list longer than 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash has wrong size
|
2022-10-29 15:32:12 +02:00
|
|
|
_, _, err = hc.PwnedPassAPI.ListHashesSHA1(PwStringInsecure)
|
2022-06-08 17:26:41 +02:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("ListHashesSHA1 was supposed to fail, but didn't")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// TestPwnedPassAPI_ListHashesPassword tests the PwnedPassAPI.ListHashesPassword metethod
|
|
|
|
func TestPwnedPassAPI_ListHashesPassword(t *testing.T) {
|
2022-06-08 17:26:41 +02:00
|
|
|
hc := New()
|
|
|
|
|
|
|
|
// List length should be >0
|
2022-10-29 15:32:12 +02:00
|
|
|
l, _, err := hc.PwnedPassAPI.ListHashesPassword(PwStringInsecure)
|
2022-06-08 17:26:41 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("ListHashesPassword was not supposed to fail, but did: %s", err)
|
|
|
|
}
|
|
|
|
if len(l) <= 0 {
|
|
|
|
t.Errorf("ListHashesPassword was supposed to return a list longer than 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Empty string has no checksum
|
2022-10-29 15:32:12 +02:00
|
|
|
_, _, err = hc.PwnedPassAPI.ListHashesSHA1("")
|
2022-05-08 12:44:20 +02:00
|
|
|
if err == nil {
|
2022-06-08 17:26:41 +02:00
|
|
|
t.Errorf("ListHashesPassword was supposed to fail, but didn't")
|
2022-05-08 12:44:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// ExamplePwnedPassAPI_CheckPassword is a code example to show how to check a given password
|
2022-05-08 12:02:58 +02:00
|
|
|
// against the HIBP passwords API
|
2022-10-29 15:32:12 +02:00
|
|
|
func ExamplePwnedPassAPI_CheckPassword() {
|
2022-05-08 12:02:58 +02:00
|
|
|
hc := New()
|
2022-10-29 15:32:12 +02:00
|
|
|
m, _, err := hc.PwnedPassAPI.CheckPassword("test")
|
2022-05-08 12:02:58 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if m != nil && m.Count != 0 {
|
|
|
|
fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n",
|
|
|
|
m.Hash, m.Count)
|
2022-10-01 15:04:06 +02:00
|
|
|
// Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86495 times in the pwned passwords DB
|
2022-05-08 12:02:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// ExamplePwnedPassAPI_CheckPassword_withPadding is a code example to show how to check a given password
|
2022-05-08 12:02:58 +02:00
|
|
|
// against the HIBP passwords API with the WithPadding() option set
|
2022-10-29 15:32:12 +02:00
|
|
|
func ExamplePwnedPassAPI_CheckPassword_withPadding() {
|
2022-05-08 12:02:58 +02:00
|
|
|
hc := New(WithPwnedPadding())
|
2022-10-29 15:32:12 +02:00
|
|
|
m, _, err := hc.PwnedPassAPI.CheckPassword("test")
|
2022-05-08 12:02:58 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if m != nil && m.Count != 0 {
|
|
|
|
fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n",
|
|
|
|
m.Hash, m.Count)
|
2022-10-01 15:04:06 +02:00
|
|
|
// Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86495 times in the pwned passwords DB
|
2022-05-08 12:02:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-29 15:32:12 +02:00
|
|
|
// ExamplePwnedPassAPI_checkSHA1 is a code example to show how to check a given password SHA1 hash
|
2022-05-08 12:02:58 +02:00
|
|
|
// against the HIBP passwords API using the CheckSHA1() method
|
2022-10-29 15:32:12 +02:00
|
|
|
func ExamplePwnedPassAPI_checkSHA1() {
|
2022-05-08 12:02:58 +02:00
|
|
|
hc := New()
|
|
|
|
pwHash := "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" // represents the PW: "test"
|
2022-10-29 15:32:12 +02:00
|
|
|
m, _, err := hc.PwnedPassAPI.CheckSHA1(pwHash)
|
2022-05-08 12:02:58 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if m != nil && m.Count != 0 {
|
|
|
|
fmt.Printf("Your password with the hash %q was found %d times in the pwned passwords DB\n",
|
|
|
|
m.Hash, m.Count)
|
2022-10-01 15:04:06 +02:00
|
|
|
// Output: Your password with the hash "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3" was found 86495 times in the pwned passwords DB
|
2022-05-08 12:02:58 +02:00
|
|
|
}
|
|
|
|
}
|