...
1 package ricrypto
2
3 import (
4 "bytes"
5 "fmt"
6 "testing"
7
8 "crypto/sha1"
9 "hash/fnv"
10 "io/ioutil"
11
12 "github.com/dsoprea/go-logging"
13 )
14
15 func TestReaderHashProxy(t *testing.T) {
16 b := bytes.NewBufferString("abc")
17 h := sha1.New()
18 rhp := NewReaderHashProxy(b, h)
19
20 data, err := ioutil.ReadAll(rhp)
21 log.PanicIf(err)
22
23 if bytes.Compare(data, []byte{'a', 'b', 'c'}) != 0 {
24 t.Fatalf("Data was not read correctly: %v\n", data)
25 }
26
27 digestPhrase := fmt.Sprintf("%020x", rhp.Sum())
28 if digestPhrase != "a9993e364706816aba3e25717850c26c9cd0d89d" {
29 t.Fatalf("hash sum not correct: [%s]", digestPhrase)
30 }
31 }
32
33 func TestReaderHash32Proxy(t *testing.T) {
34 b := bytes.NewBufferString("abc")
35 h := fnv.New32a()
36 rhp := NewReaderHash32Proxy(b, h)
37
38 data, err := ioutil.ReadAll(rhp)
39 log.PanicIf(err)
40
41 if bytes.Compare(data, []byte{'a', 'b', 'c'}) != 0 {
42 t.Fatalf("Data was not read correctly: %v\n", data)
43 }
44
45 checksum := rhp.Sum32()
46 if checksum != 440920331 {
47 t.Fatalf("checksum not correct: (%d)", checksum)
48 }
49 }
50
View as plain text