Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

247 lines (222 sloc) 5.205 kB
package main
import (
"bytes"
"encoding/hex"
"errors"
"io/ioutil"
"math/rand"
"os"
"reflect"
"strings"
"sync"
"testing"
)
var once = &sync.Once{}
var randomData []byte
var hashOfRandomData string
func initData() {
randomData = make([]byte, 64*1024)
randomSrc := randomDataMaker{rand.NewSource(1028890720402726901)}
n, err := randomSrc.Read(randomData)
if err != nil {
panic(err)
}
if n != len(randomData) {
panic("Didn't initialize random data properly")
}
sh := getHash()
written, err := sh.Write(randomData)
if err != nil {
panic(err)
}
if written != len(randomData) {
panic("short write")
}
hashOfRandomData = hex.EncodeToString(sh.Sum([]byte{}))
}
func benchHash(h string, b *testing.B) {
once.Do(initData)
globalConfig.Hash = h
b.SetBytes(int64(len(randomData)))
for i := 0; i < b.N; i++ {
sh := getHash()
written, err := sh.Write(randomData)
if err != nil {
b.Fatalf("Error writing data: %v", err)
}
if written != len(randomData) {
b.Fatalf("Didn't write the correct amount of data: %v != %v",
written, len(randomData))
}
}
}
func BenchmarkHashSHA1(b *testing.B) {
benchHash("sha1", b)
}
func BenchmarkHashSHA256(b *testing.B) {
benchHash("sha256", b)
}
func BenchmarkHashSHA512(b *testing.B) {
benchHash("sha512", b)
}
func BenchmarkHashMD5(b *testing.B) {
benchHash("md5", b)
}
func testWithTempDir(t *testing.T, f func(string)) {
once.Do(initData)
t.Parallel()
tmpdir, err := ioutil.TempDir("", "hashtest")
if err != nil {
t.Fatalf("Error getting temp dir: %v", err)
}
defer os.RemoveAll(tmpdir)
f(tmpdir)
}
func validateHashFile(fn string) error {
f, err := os.Open(fn)
if err != nil {
return err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
if !reflect.DeepEqual(randomData, b) {
return errors.New("Didn't read the same data")
}
return nil
}
func TestHashWriterClose(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr, err := NewHashRecord(tmpdir, "")
if err != nil {
t.Fatalf("Error establishing hash record: %v", err)
}
hr.Close()
})
}
func TestHashWriterDoubleClose(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr, err := NewHashRecord(tmpdir, "")
if err != nil {
t.Fatalf("Error establishing hash record: %v", err)
}
hr.Close()
hr.Close()
})
}
func TestHashWriterUninitClose(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr := hashRecord{}
hr.Close()
})
}
func TestHashWriterNilClose(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr := (*hashRecord)(nil)
hr.Close()
})
}
func TestHashWriterNoHash(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr, err := NewHashRecord(tmpdir, "")
if err != nil {
t.Fatalf("Error establishing hash record: %v", err)
}
defer hr.Close()
hr.base = tmpdir
h, l, err := hr.Process(bytes.NewReader(randomData))
if err != nil {
t.Fatalf("Error processing: %v", err)
}
if int(l) != len(randomData) {
t.Fatalf("Processing was short: %v != %v",
l, len(randomData))
}
if h != hashOfRandomData {
t.Fatalf("Expected hash %v, got %v",
hashOfRandomData, h)
}
err = validateHashFile(hashFilename(tmpdir, hashOfRandomData))
if err != nil {
t.Fatalf("Didn't find valid hash: %v", err)
}
})
}
func TestHashWriterGoodHash(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr, err := NewHashRecord(tmpdir, hashOfRandomData)
if err != nil {
t.Fatalf("Error establishing hash record: %v", err)
}
defer hr.Close()
hr.base = tmpdir
h, l, err := hr.Process(bytes.NewReader(randomData))
if err != nil {
t.Fatalf("Error processing: %v", err)
}
if int(l) != len(randomData) {
t.Fatalf("Processing was short: %v != %v",
l, len(randomData))
}
if h != hashOfRandomData {
t.Fatalf("Expected hash %v, got %v",
hashOfRandomData, h)
}
err = validateHashFile(hashFilename(tmpdir, hashOfRandomData))
if err != nil {
t.Fatalf("Didn't find valid hash: %v", err)
}
})
}
func TestHashWriterWithBadHash(t *testing.T) {
testWithTempDir(t, func(tmpdir string) {
hr, err := NewHashRecord(tmpdir, "fde65ea0f4a6d1b0eb20c3b6b7e054512d2c45dc")
if err != nil {
t.Fatalf("Error establishing hash record: %v", err)
}
defer hr.Close()
hr.base = tmpdir
_, l, err := hr.Process(bytes.NewReader(randomData))
if err == nil {
t.Fatalf("Expected error processing")
} else {
if !strings.Contains(err.Error(), "Invalid hash") {
t.Fatalf("Expected invalid hash error, got %v", err)
}
}
if int(l) != len(randomData) {
t.Fatalf("Processing was short: %v != %v",
l, len(randomData))
}
err = validateHashFile(hashFilename(tmpdir, hashOfRandomData))
if err == nil {
t.Fatalf("Unexpectedly found valid hash.")
}
})
}
func TestValidHash(t *testing.T) {
tests := []struct {
hash string
valid bool
}{
{
"a7592f6920e3bda2e4ab080b2d7f24b4e5c320be",
true,
},
{
"../../../etc/passwd",
false,
},
{
"",
false,
},
}
for _, test := range tests {
valid := validHash(test.hash)
if valid != test.valid {
t.Errorf("expected hash validity %t, got %t for hash '%s'", test.valid, valid, test.hash)
}
}
}
Jump to Line
Something went wrong with that request. Please try again.