58 lines
822 B
Go
58 lines
822 B
Go
package hash
|
|
|
|
import (
|
|
"crypto/md5"
|
|
"github.com/tursom/checksum/assert"
|
|
"hash"
|
|
"testing"
|
|
)
|
|
|
|
func TestMd5_Finish(t *testing.T) {
|
|
type fields struct {
|
|
d hash.Hash
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
fields fields
|
|
builder func(m *Md5)
|
|
want []byte
|
|
}{
|
|
{
|
|
"nil",
|
|
fields{},
|
|
nil,
|
|
md5.New().Sum(nil),
|
|
},
|
|
{
|
|
"hello",
|
|
fields{},
|
|
func(m *Md5) {
|
|
m.Append([]byte("hello"))
|
|
},
|
|
md5sum([]byte("hello")),
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
m := &Md5{
|
|
d: tt.fields.d,
|
|
}
|
|
if tt.builder != nil {
|
|
tt.builder(m)
|
|
}
|
|
|
|
assert.Equals(t, m.Finish(), tt.want)
|
|
})
|
|
}
|
|
}
|
|
|
|
func md5sum(b []byte) []byte {
|
|
h := md5.New()
|
|
h.Write(b)
|
|
return h.Sum(nil)
|
|
}
|
|
|
|
func TestMd5_String(t *testing.T) {
|
|
assert.Equals(t, (&Md5{}).String(), "md5")
|
|
}
|