file-store/pkg/erasureencode/merkle_test.go

90 lines
2.3 KiB
Go
Raw Permalink Normal View History

2022-08-24 02:54:01 +00:00
package erasureencode
import (
"bytes"
"crypto/sha256"
"testing"
)
func TestMerkle1(t *testing.T) {
i1 := sha256sum([]byte("1input data1"))
i2 := make([]byte, sha256.Size)
expected := sha256sum(append(i1, i2...))
input := [][]byte{i1}
actual := merkleSha256(input)
if !bytes.Equal(actual, expected) {
t.Fatalf(`Expected %x to equal %x`, actual, expected)
}
}
func TestMerkle2(t *testing.T) {
i1 := sha256sum([]byte("1input data1"))
i2 := sha256sum([]byte("2input data2"))
expected := sha256sum(append(i1, i2...))
input := [][]byte{i1, i2}
actual := merkleSha256(input)
if !bytes.Equal(actual, expected) {
t.Fatalf(`Expected %x to equal %x`, actual, expected)
}
}
func TestMerkle3(t *testing.T) {
i1_1 := sha256sum([]byte("1input data1"))
i1_2 := sha256sum([]byte("2input data2"))
i1 := sha256sum(append(i1_1, i1_2...))
i2_1 := sha256sum([]byte("3input data3"))
i2_2 := make([]byte, sha256.Size)
i2 := sha256sum(append(i2_1, i2_2...))
expected := sha256sum(append(i1, i2...))
input := [][]byte{i1_1, i1_2, i2_1}
actual := merkleSha256(input)
if !bytes.Equal(actual, expected) {
t.Fatalf(`Expected %x to equal %x`, actual, expected)
}
}
func TestMerkle4(t *testing.T) {
i1_1 := sha256sum([]byte("1input data1"))
i1_2 := sha256sum([]byte("2input data2"))
i1 := sha256sum(append(i1_1, i1_2...))
i2_1 := sha256sum([]byte("3input data3"))
i2_2 := sha256sum([]byte("4input data4"))
i2 := sha256sum(append(i2_1, i2_2...))
expected := sha256sum(append(i1, i2...))
input := [][]byte{i1_1, i1_2, i2_1, i2_2}
actual := merkleSha256(input)
if !bytes.Equal(actual, expected) {
t.Fatalf(`Expected %x to equal %x`, actual, expected)
}
}
func TestMerkle5(t *testing.T) {
i1_1_1 := sha256sum([]byte("1input data1"))
i1_1_2 := sha256sum([]byte("2input data2"))
i1_2_1 := sha256sum([]byte("3input data3"))
i1_2_2 := sha256sum([]byte("4input data4"))
i1_1 := sha256sum(append(i1_1_1, i1_1_2...))
i1_2 := sha256sum(append(i1_2_1, i1_2_2...))
i1 := sha256sum(append(i1_1, i1_2...))
i2_1 := sha256sum([]byte("5input data5"))
i2_2 := make([]byte, sha256.Size)
i2 := sha256sum(append(i2_1, i2_2...))
expected := sha256sum(append(i1, i2...))
input := [][]byte{i1_1, i1_2, i2_1, i2_2}
actual := merkleSha256(input)
if !bytes.Equal(actual, expected) {
t.Fatalf(`Expected %x to equal %x`, actual, expected)
}
}