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