tests/file_checksum/file_hash_test.go

91 lines
2.2 KiB
Go

package main
import (
"fmt"
"os"
"testing"
"github.com/codingsince1985/checksum"
)
func prepareFile() (string, error) {
file, err := os.CreateTemp("", "prefix")
if err != nil {
return "", err
}
if err := os.WriteFile(file.Name(), []byte("some data"), 0600); err != nil {
return "", err
}
return file.Name(), nil
}
func testFile(t *testing.T, checksumFunc func(string) error) {
file, err := prepareFile()
if err != nil {
t.Logf("could not create test file: %s", err)
t.FailNow()
}
defer func() {
err := os.Remove(file)
if err != nil {
t.Logf("could not remove test file: %s", err)
}
}()
if err := checksumFunc(file); err != nil {
t.Error(err)
}
}
func TestBlake2s256(t *testing.T) {
testFile(t, func(filename string) error {
if result, err := checksum.Blake2s256(filename); err != nil || result != "54fc4fe89148c8f82479348f56168f71c4165eedda67961daec1d46015db3884" {
return fmt.Errorf(result, err)
}
return nil
})
}
func TestSHA1sumFile(t *testing.T) {
testFile(t, func(filename string) error {
if result, err := checksum.SHA1sum(filename); err != nil || result != "baf34551fecb48acc3da868eb85e1b6dac9de356" {
return fmt.Errorf(result, err)
}
return nil
})
}
func TestSHA256sumFile(t *testing.T) {
testFile(t, func(filename string) error {
if result, err := checksum.SHA256sum(filename); err != nil || result != "1307990e6ba5ca145eb35e99182a9bec46531bc54ddf656a602c780fa0240dee" {
return fmt.Errorf(result, err)
}
return nil
})
}
func TestMd5sumFile(t *testing.T) {
testFile(t, func(filename string) error {
if result, err := checksum.MD5sum(filename); err != nil || result != "1e50210a0202497fb79bc38b6ade6c34" {
return fmt.Errorf(result, err)
}
return nil
})
}
func TestCrc32File(t *testing.T) {
testFile(t, func(filename string) error {
if result, err := checksum.CRC32(filename); err != nil || result != "d9c2e91e" {
return fmt.Errorf(result, err)
}
return nil
})
}
func TestMd5sumDir(t *testing.T) {
os.MkdirAll("/tmp/downloads", 777)
defer os.Remove("/tmp/downloads")
if md5sum, err := checksum.MD5sum("/tmp/downloads"); err != nil || md5sum != "" {
t.Error("Md5sum(dir) failed", md5sum, err)
}
}