Skip to content

Commit

Permalink
Update digest_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn committed Jul 16, 2020
1 parent ac68bbf commit 67f5433
Showing 1 changed file with 25 additions and 41 deletions.
66 changes: 25 additions & 41 deletions digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,23 @@ var cnonce = "0a4f113b"
func TestH(t *testing.T) {
testCases := map[string]map[string]string{
"MD5": {
"r1": "Mufasa:[email protected]:Circle Of Life",
"r2": "GET:/dir/index.html",
"r1": "Mufasa:[email protected]:Circle Of Life",
"r2": "GET:/dir/index.html",
"expected_r1": "939e7578ed9e3c518a452acee763bce9",
"expected_r2": "39aff3a2bab6126f332b942af96d3366",
"expected_r3": "6629fae49393a05397450978507c4ef1",
},
"SHA-256": {
"r1": "Mufasa:[email protected]:Circle Of Life",
"r2": "GET:/dir/index.html",
"r1": "Mufasa:[email protected]:Circle Of Life",
"r2": "GET:/dir/index.html",
"expected_r1": "3ba6cd94661c5ef34598040c868f13b8775df29109986be50ad35ae537dd3aa4",
"expected_r2": "9a3fdae9a622fe8de177c24fa9c070f2b181ec85e15dcbdc32e10c82ad450b04",
"expected_r3": "5abdd07184ba512a22c53f41470e5eea7dcaa3a93a59b630c13dfe0a5dc6e38b",
},
}
for testType, values := range testCases {
t.Run(testType, func(t *testing.T) {
var hashingFunc hashingFunc
if testType == "MD5" {
hashingFunc = md5.New
} else if testType == "SHA-256" {
hashingFunc = sha256.New
}
hashingFunc := testHashingFunc(testType)
r1 := h(values["r1"], hashingFunc)
if r1 != values["expected_r1"] {
t.Errorf("expected=%s, but got=%s\n", values["expected_r1"], r1)
Expand All @@ -74,25 +69,20 @@ func TestH(t *testing.T) {
func TestKd(t *testing.T) {
testCases := map[string]map[string]string{
"MD5": {
"secret": "939e7578ed9e3c518a452acee763bce9",
"data": "dcd98b7102dd2f0e8b11d0f600bfb0c093:00000001:0a4f113b:auth:39aff3a2bab6126f332b942af96d3366",
"secret": "939e7578ed9e3c518a452acee763bce9",
"data": "dcd98b7102dd2f0e8b11d0f600bfb0c093:00000001:0a4f113b:auth:39aff3a2bab6126f332b942af96d3366",
"expected": "6629fae49393a05397450978507c4ef1",
},
"SHA-256": {
"secret": "939e7578ed9e3c518a452acee763bce9",
"data": "dcd98b7102dd2f0e8b11d0f600bfb0c093:00000001:0a4f113b:auth:39aff3a2bab6126f332b942af96d3366",
"secret": "939e7578ed9e3c518a452acee763bce9",
"data": "dcd98b7102dd2f0e8b11d0f600bfb0c093:00000001:0a4f113b:auth:39aff3a2bab6126f332b942af96d3366",
"expected": "ca165e8478c14bd2a5c64cc86ffe17c277ee2cff3e98c330ee5565e8e206ca3e",
},
}
for testType, values := range testCases {
t.Run(testType, func(t *testing.T) {
var hashingFunc hashingFunc
if testType == "MD5" {
hashingFunc = md5.New
} else if testType == "SHA-256" {
hashingFunc = sha256.New
}
if r1 := kd(values["secret"],values["data"], hashingFunc); r1 != values["expected"] {
hashingFunc := testHashingFunc(testType)
if r1 := kd(values["secret"], values["data"], hashingFunc); r1 != values["expected"] {
t.Errorf("expected=%s, but got=%s\n", values["expected"], r1)
}
})
Expand All @@ -110,12 +100,7 @@ func TestHa1(t *testing.T) {
}
for testType, values := range testCases {
t.Run(testType, func(t *testing.T) {
var hashingFunc hashingFunc
if testType == "MD5" {
hashingFunc = md5.New
} else if testType == "SHA-256" {
hashingFunc = sha256.New
}
hashingFunc := testHashingFunc(testType)
cred := &credentials{
Username: "Mufasa",
Realm: "[email protected]",
Expand All @@ -128,7 +113,7 @@ func TestHa1(t *testing.T) {
password: "Circle Of Life",
impl: hashingFunc,
}
if r1 := cred.ha1(); r1 != values["expected"] {
if r1 := cred.ha1(); r1 != values["expected"] {
t.Errorf("expected=%s, but got=%s\n", values["expected"], r1)
}
})
Expand All @@ -146,12 +131,7 @@ func TestHa2(t *testing.T) {
}
for testType, values := range testCases {
t.Run(testType, func(t *testing.T) {
var hashingFunc hashingFunc
if testType == "MD5" {
hashingFunc = md5.New
} else if testType == "SHA-256" {
hashingFunc = sha256.New
}
hashingFunc := testHashingFunc(testType)
cred := &credentials{
Username: "Mufasa",
Realm: "[email protected]",
Expand All @@ -164,7 +144,7 @@ func TestHa2(t *testing.T) {
password: "Circle Of Life",
impl: hashingFunc,
}
if r1 := cred.ha2(); r1 != values["expected"] {
if r1 := cred.ha2(); r1 != values["expected"] {
t.Errorf("expected=%s, but got=%s\n", values["expected"], r1)
}
})
Expand All @@ -182,12 +162,7 @@ func TestResp(t *testing.T) {
}
for testType, values := range testCases {
t.Run(testType, func(t *testing.T) {
var hashingFunc hashingFunc
if testType == "MD5" {
hashingFunc = md5.New
} else if testType == "SHA-256" {
hashingFunc = sha256.New
}
hashingFunc := testHashingFunc(testType)
cred := &credentials{
Username: "Mufasa",
Realm: "[email protected]",
Expand All @@ -206,3 +181,12 @@ func TestResp(t *testing.T) {
})
}
}

func testHashingFunc(testType string) hashingFunc {
if testType == "MD5" {
return md5.New
} else if testType == "SHA-256" {
return sha256.New
}
return nil
}

0 comments on commit 67f5433

Please sign in to comment.