Skip to content

Commit

Permalink
Merge pull request #30 from wneessen/better_testcoverage
Browse files Browse the repository at this point in the history
Improved test coverage
  • Loading branch information
wneessen authored Feb 9, 2023
2 parents 2e13557 + 00d56b9 commit f89cc5f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@

# Dependency directories (remove the comment below to include it)
# vendor/

examples/
54 changes: 51 additions & 3 deletions password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ func TestPwnedPassAPI_CheckPassword_NTLM(t *testing.T) {
}
}

// TestPwnedPassAPI_CheckPassword_failed verifies the Pwned Passwords API with the CheckPassword method
// with intentionally failing requests
func TestPwnedPassAPI_CheckPassword_failed(t *testing.T) {
hc := New()
hc.PwnedPassAPIOpts.HashMode = 99
_, _, err := hc.PwnedPassAPI.CheckPassword(PwStringInsecure)
if err == nil {
t.Error("CheckPassword with unsupported HashMode was supposed to fail, but didn't")
}
if !errors.Is(err, ErrUnsupportedHashMode) {
t.Errorf("CheckPassword wrong error, expected: %s, got: %s", ErrUnsupportedHashMode, err)
}
}

// TestPwnedPassAPI_CheckSHA1 verifies the Pwned Passwords API with the CheckSHA1 method
func TestPwnedPassAPI_CheckSHA1(t *testing.T) {
testTable := []struct {
Expand Down Expand Up @@ -204,6 +218,16 @@ func TestPwnedPassAPI_ListHashesPrefix(t *testing.T) {
if err == nil {
t.Errorf("ListHashesPrefix was supposed to fail, but didn't")
}

// Should fall back to SHA-1
hc.PwnedPassAPIOpts.HashMode = 99
l, _, err = hc.PwnedPassAPI.ListHashesPrefix("a94a8")
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")
}
}

// TestPwnedPassAPI_ListHashesPrefix_Errors tests the ListHashesPrefix method's errors
Expand Down Expand Up @@ -364,11 +388,35 @@ func TestPwnedPassAPI_ListHashesPassword(t *testing.T) {
if len(l) <= 0 {
t.Errorf("ListHashesPassword was supposed to return a list longer than 0")
}
}

// Empty string has no checksum
_, _, err = hc.PwnedPassAPI.ListHashesSHA1("")
// TestPwnedPassAPI_ListHashesPassword_failed tests the PwnedPassAPI.ListHashesPassword metethod
// with a unsupported HashMode
func TestPwnedPassAPI_ListHashesPassword_failed(t *testing.T) {
hc := New()
hc.PwnedPassAPIOpts.HashMode = 99

_, _, err := hc.PwnedPassAPI.ListHashesPassword(PwStringInsecure)
if err == nil {
t.Errorf("ListHashesPassword was supposed to fail, but didn't")
t.Error("ListHashesPassword with unspported HashMode was supposed to fail, but didn't")
}
if !errors.Is(err, ErrUnsupportedHashMode) {
t.Errorf("ListHashesPassword error does not match, expected: %s, got: %s", ErrUnsupportedHashMode, err)
}
}

// TestPwnedPassAPI_ListHashesPasswordNTLM tests the PwnedPassAPI.ListHashesPassword metethod
// with NTLM HashMode
func TestPwnedPassAPI_ListHashesPasswordNTLM(t *testing.T) {
hc := New(WithPwnedNTLMHash())

// List length should be >0
l, _, err := hc.PwnedPassAPI.ListHashesPassword(PwStringInsecure)
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")
}
}

Expand Down

0 comments on commit f89cc5f

Please sign in to comment.