44package pwn
55
66import (
7- "math/rand/v2"
87 "net/http"
9- "strings"
108 "testing"
119 "time"
1210
11+ "github.com/h2non/gock"
1312 "github.com/stretchr/testify/assert"
1413)
1514
@@ -18,86 +17,34 @@ var client = New(WithHTTP(&http.Client{
1817}))
1918
2019func TestPassword (t * testing.T ) {
21- // Check input error
22- _ , err := client .CheckPassword ("" , false )
20+ defer gock .Off ()
21+
22+ count , err := client .CheckPassword ("" , false )
2323 assert .ErrorIs (t , err , ErrEmptyPassword , "blank input should return ErrEmptyPassword" )
24+ assert .Equal (t , - 1 , count )
2425
25- // Should fail
26- fail := "password1234"
27- count , err := client .CheckPassword (fail , false )
28- assert .NotEmpty (t , count , "%s should fail as a password" , fail )
26+ gock .New ("https://api.pwnedpasswords.com" ).Get ("/range/5c1d8" ).Times (1 ).Reply (200 ).BodyString ("EAF2F254732680E8AC339B84F3266ECCBB5:1\r \n FC446EB88938834178CB9322C1EE273C2A7:2" )
27+ count , err = client .CheckPassword ("pwned" , false )
2928 assert .NoError (t , err )
29+ assert .Equal (t , 1 , count )
3030
31- // Should fail (with padding)
32- failPad := "administrator"
33- count , err = client .CheckPassword (failPad , true )
34- assert .NotEmpty (t , count , "%s should fail as a password" , failPad )
31+ gock .New ("https://api.pwnedpasswords.com" ).Get ("/range/ba189" ).Times (1 ).Reply (200 ).BodyString ("FD4CB34F0378BCB15D23F6FFD28F0775C9E:3\r \n FDF342FCD8C3611DAE4D76E8A992A3E4169:4" )
32+ count , err = client .CheckPassword ("notpwned" , false )
3533 assert .NoError (t , err )
34+ assert .Equal (t , 0 , count )
3635
37- // Checking for a "good" password isn't going to be perfect, but we can give it a good try
38- // with hopefully minimal error. Try five times?
39- assert .Condition (t , func () bool {
40- for i := 0 ; i <= 5 ; i ++ {
41- count , err = client .CheckPassword (testPassword (), false )
42- assert .NoError (t , err )
43- if count == 0 {
44- return true
45- }
46- }
47- return false
48- }, "no generated passwords passed. there is a chance this is a fluke" )
49-
50- // Again, but with padded responses
51- assert .Condition (t , func () bool {
52- for i := 0 ; i <= 5 ; i ++ {
53- count , err = client .CheckPassword (testPassword (), true )
54- assert .NoError (t , err )
55- if count == 0 {
56- return true
57- }
58- }
59- return false
60- }, "no generated passwords passed. there is a chance this is a fluke" )
61- }
62-
63- // Credit to https://golangbyexample.com/generate-random-password-golang/
64- // DO NOT USE THIS FOR AN ACTUAL PASSWORD GENERATOR
65- var (
66- lowerCharSet = "abcdedfghijklmnopqrst"
67- upperCharSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
68- specialCharSet = "!@#$%&*"
69- numberSet = "0123456789"
70- allCharSet = lowerCharSet + upperCharSet + specialCharSet + numberSet
71- )
72-
73- func testPassword () string {
74- var password strings.Builder
75-
76- // Set special character
77- for i := 0 ; i < 5 ; i ++ {
78- random := rand .IntN (len (specialCharSet ))
79- password .WriteString (string (specialCharSet [random ]))
80- }
81-
82- // Set numeric
83- for i := 0 ; i < 5 ; i ++ {
84- random := rand .IntN (len (numberSet ))
85- password .WriteString (string (numberSet [random ]))
86- }
36+ gock .New ("https://api.pwnedpasswords.com" ).Get ("/range/a1733" ).Times (1 ).Reply (200 ).BodyString ("C4CE0F1F0062B27B9E2F41AF0C08218017C:1\r \n FC446EB88938834178CB9322C1EE273C2A7:2\r \n FE81480327C992FE62065A827429DD1318B:0" )
37+ count , err = client .CheckPassword ("paddedpwned" , true )
38+ assert .NoError (t , err )
39+ assert .Equal (t , 1 , count )
8740
88- // Set uppercase
89- for i := 0 ; i < 5 ; i ++ {
90- random := rand .IntN (len (upperCharSet ))
91- password .WriteString (string (upperCharSet [random ]))
92- }
41+ gock .New ("https://api.pwnedpasswords.com" ).Get ("/range/5617b" ).Times (1 ).Reply (200 ).BodyString ("FD4CB34F0378BCB15D23F6FFD28F0775C9E:3\r \n FDF342FCD8C3611DAE4D76E8A992A3E4169:4\r \n FE81480327C992FE62065A827429DD1318B:0" )
42+ count , err = client .CheckPassword ("paddednotpwned" , true )
43+ assert .NoError (t , err )
44+ assert .Equal (t , 0 , count )
9345
94- for i := 0 ; i < 5 ; i ++ {
95- random := rand .IntN (len (allCharSet ))
96- password .WriteString (string (allCharSet [random ]))
97- }
98- inRune := []rune (password .String ())
99- rand .Shuffle (len (inRune ), func (i , j int ) {
100- inRune [i ], inRune [j ] = inRune [j ], inRune [i ]
101- })
102- return string (inRune )
46+ gock .New ("https://api.pwnedpasswords.com" ).Get ("/range/79082" ).Times (1 ).Reply (200 ).BodyString ("FDF342FCD8C3611DAE4D76E8A992A3E4169:4\r \n FE81480327C992FE62065A827429DD1318B:0\r \n AFEF386F56EB0B4BE314E07696E5E6E6536:0" )
47+ count , err = client .CheckPassword ("paddednotpwnedzero" , true )
48+ assert .NoError (t , err )
49+ assert .Equal (t , 0 , count )
10350}
0 commit comments