This repository has been archived by the owner on Jun 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from reinventer/improvements
Case insensitive algorithm name and small optimization
- Loading branch information
Showing
2 changed files
with
194 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
package digest_auth_client | ||
|
||
import "testing" | ||
|
||
func TestHash(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
algorithm string | ||
expRes string | ||
}{ | ||
{ | ||
name: "empty algorithm", | ||
algorithm: "", | ||
expRes: "1a79a4d60de6718e8e5b326e338ae533", | ||
}, | ||
{ | ||
name: "MD5 algorithm", | ||
algorithm: "MD5", | ||
expRes: "1a79a4d60de6718e8e5b326e338ae533", | ||
}, | ||
{ | ||
name: "MD5-sess algorithm", | ||
algorithm: "MD5", | ||
expRes: "1a79a4d60de6718e8e5b326e338ae533", | ||
}, | ||
{ | ||
name: "SHA256 algorithm", | ||
algorithm: "SHA-256", | ||
expRes: "50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c", | ||
}, | ||
{ | ||
name: "SHA256-sess algorithm", | ||
algorithm: "SHA-256", | ||
expRes: "50d858e0985ecc7f60418aaf0cc5ab587f42c2570a884095a9e8ccacd0f6545c", | ||
}, | ||
{ | ||
name: "md5 algorithm", | ||
algorithm: "md5", | ||
expRes: "1a79a4d60de6718e8e5b326e338ae533", | ||
}, | ||
{ | ||
name: "unknown algorithm", | ||
algorithm: "unknown", | ||
expRes: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
ah := &authorization{Algorithm: tc.algorithm} | ||
res := ah.hash("example") | ||
if res != tc.expRes { | ||
t.Errorf("got: %q, want: %q", res, tc.expRes) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestComputeA1(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
algorithm string | ||
expRes string | ||
}{ | ||
{ | ||
name: "empty algorithm", | ||
algorithm: "", | ||
expRes: "username:realm:secret", | ||
}, | ||
{ | ||
name: "MD5 algorithm", | ||
algorithm: "MD5", | ||
expRes: "username:realm:secret", | ||
}, | ||
{ | ||
name: "MD5-sess algorithm", | ||
algorithm: "MD5", | ||
expRes: "username:realm:secret", | ||
}, | ||
{ | ||
name: "SHA256 algorithm", | ||
algorithm: "SHA-256", | ||
expRes: "username:realm:secret", | ||
}, | ||
{ | ||
name: "SHA256-sess algorithm", | ||
algorithm: "SHA-256", | ||
expRes: "username:realm:secret", | ||
}, | ||
{ | ||
name: "md5 algorithm", | ||
algorithm: "md5", | ||
expRes: "username:realm:secret", | ||
}, | ||
{ | ||
name: "unknown algorithm", | ||
algorithm: "unknown", | ||
expRes: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
dr := &DigestRequest{Password: "secret"} | ||
ah := &authorization{ | ||
Algorithm: tc.algorithm, | ||
Nonce: "nonce", | ||
Cnonce: "cnonce", | ||
Username: "username", | ||
Realm: "realm", | ||
} | ||
res := ah.computeA1(dr) | ||
if res != tc.expRes { | ||
t.Errorf("got: %q, want: %q", res, tc.expRes) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestComputeA2(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
qop string | ||
expRes string | ||
expAuthQop string | ||
}{ | ||
{ | ||
name: "empty qop", | ||
qop: "", | ||
expRes: "method:uri", | ||
expAuthQop: "auth", | ||
}, | ||
{ | ||
name: "qop is auth", | ||
qop: "auth", | ||
expRes: "method:uri", | ||
expAuthQop: "auth", | ||
}, | ||
{ | ||
name: "qop is auth-int", | ||
qop: "qop is auth-int", | ||
expRes: "method:uri:841a2d689ad86bd1611447453c22c6fc", | ||
expAuthQop: "auth-int", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
dr := &DigestRequest{ | ||
Method: "method", | ||
Body: "body", | ||
Wa: &wwwAuthenticate{ | ||
Qop: tc.qop, | ||
}, | ||
} | ||
ah := &authorization{ | ||
Algorithm: "MD5", | ||
Nonce: "nonce", | ||
Cnonce: "cnonce", | ||
Username: "username", | ||
Realm: "realm", | ||
URI: "uri", | ||
Qop: tc.qop, | ||
} | ||
res := ah.computeA2(dr) | ||
if res != tc.expRes { | ||
t.Errorf("wrong result, got: %q, want: %q", res, tc.expRes) | ||
} | ||
if ah.Qop != tc.expAuthQop { | ||
t.Errorf("wrong qop, got: %q, want: %q", ah.Qop, tc.expAuthQop) | ||
} | ||
}) | ||
} | ||
} |