Skip to content

Commit

Permalink
完善单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
FishGoddess committed May 19, 2024
1 parent 675ffed commit 345c83d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
15 changes: 7 additions & 8 deletions hmac/hmac_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
package hmac

import (
"bytes"
"crypto/md5"
"encoding/hex"
"testing"
)

// go test -v -cover -run=^TestHMAC$
func TestHMAC(t *testing.T) {
testCases := map[string]string{
"": "63530468a04e386459855da0063b6596",
"123": "52851cb05258c8d98da1672d95729e53",
"你好,世界": "e76d8f84103533dc5d22a6e00cef74f3",
testCases := map[string][]byte{
"": {99, 83, 4, 104, 160, 78, 56, 100, 89, 133, 93, 160, 6, 59, 101, 150},
"123": {82, 133, 28, 176, 82, 88, 200, 217, 141, 161, 103, 45, 149, 114, 158, 83},
"你好,世界": {231, 109, 143, 132, 16, 53, 51, 220, 93, 34, 166, 224, 12, 239, 116, 243},
}

key := []byte("key")
Expand All @@ -25,9 +25,8 @@ func TestHMAC(t *testing.T) {
t.Error(err)
}

sumHex := hex.EncodeToString(sum)
if sumHex != expect {
t.Errorf("input %s: sumHex %s != expect %s", input, sumHex, expect)
if !bytes.Equal(sum, expect) {
t.Errorf("input %s: sum %+v != expect %+v", input, sum, expect)
}
}
}
51 changes: 46 additions & 5 deletions hmac/md5_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,33 @@
package hmac

import (
"encoding/hex"
"bytes"
"testing"
)

// go test -v -cover -run=^TestMD5$
func TestMD5(t *testing.T) {
testCases := map[string][]byte{
"": {99, 83, 4, 104, 160, 78, 56, 100, 89, 133, 93, 160, 6, 59, 101, 150},
"123": {82, 133, 28, 176, 82, 88, 200, 217, 141, 161, 103, 45, 149, 114, 158, 83},
"你好,世界": {231, 109, 143, 132, 16, 53, 51, 220, 93, 34, 166, 224, 12, 239, 116, 243},
}

key := []byte("key")
for input, expect := range testCases {
sum, err := MD5(key, []byte(input))
if err != nil {
t.Error(err)
}

if !bytes.Equal(sum, expect) {
t.Errorf("input %s: sum %+v != expect %+v", input, sum, expect)
}
}
}

// go test -v -cover -run=^TestMD5Hex$
func TestMD5Hex(t *testing.T) {
testCases := map[string]string{
"": "63530468a04e386459855da0063b6596",
"123": "52851cb05258c8d98da1672d95729e53",
Expand All @@ -19,14 +40,34 @@ func TestMD5(t *testing.T) {

key := []byte("key")
for input, expect := range testCases {
sum, err := MD5(key, []byte(input))
sum, err := MD5Hex(key, []byte(input))
if err != nil {
t.Error(err)
}

if sum != expect {
t.Errorf("input %s: sum %s != expect %s", input, sum, expect)
}
}
}

// go test -v -cover -run=^TestMD5Base64$
func TestMD5Base64(t *testing.T) {
testCases := map[string]string{
"": "Y1MEaKBOOGRZhV2gBjtllg==",
"123": "UoUcsFJYyNmNoWctlXKeUw==",
"你好,世界": "522PhBA1M9xdIqbgDO908w==",
}

key := []byte("key")
for input, expect := range testCases {
sum, err := MD5Base64(key, []byte(input))
if err != nil {
t.Error(err)
}

sumHex := hex.EncodeToString(sum)
if sumHex != expect {
t.Errorf("input %s: sumHex %s != expect %s", input, sumHex, expect)
if sum != expect {
t.Errorf("input %s: sum %s != expect %s", input, sum, expect)
}
}
}

0 comments on commit 345c83d

Please sign in to comment.