2015-03-22 19:00:08 -07:00
|
|
|
package types
|
2014-10-16 16:00:48 -07:00
|
|
|
|
|
|
|
import (
|
2014-10-17 01:01:59 -07:00
|
|
|
"io/ioutil"
|
2014-10-16 16:00:48 -07:00
|
|
|
"testing"
|
2014-10-17 01:01:59 -07:00
|
|
|
|
2018-07-11 05:57:35 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-07-01 19:36:49 -07:00
|
|
|
cmn "github.com/tendermint/tendermint/libs/common"
|
2014-10-16 16:00:48 -07:00
|
|
|
)
|
|
|
|
|
2016-09-16 09:20:07 -07:00
|
|
|
const (
|
|
|
|
testPartSize = 65536 // 64KB ... 4096 // 4KB
|
|
|
|
)
|
|
|
|
|
2014-10-16 16:00:48 -07:00
|
|
|
func TestBasicPartSet(t *testing.T) {
|
2014-10-17 01:01:59 -07:00
|
|
|
// Construct random data of size partSize * 100
|
2017-10-04 13:40:45 -07:00
|
|
|
data := cmn.RandBytes(testPartSize * 100)
|
2016-09-16 09:20:07 -07:00
|
|
|
partSet := NewPartSetFromData(data, testPartSize)
|
2018-07-11 05:57:35 -07:00
|
|
|
|
|
|
|
assert.NotEmpty(t, partSet.Hash())
|
|
|
|
assert.Equal(t, 100, partSet.Total())
|
|
|
|
assert.Equal(t, 100, partSet.BitArray().Size())
|
|
|
|
assert.True(t, partSet.HashesTo(partSet.Hash()))
|
|
|
|
assert.True(t, partSet.IsComplete())
|
|
|
|
assert.Equal(t, 100, partSet.Count())
|
2014-10-17 01:01:59 -07:00
|
|
|
|
|
|
|
// Test adding parts to a new partSet.
|
2014-10-31 18:35:38 -07:00
|
|
|
partSet2 := NewPartSetFromHeader(partSet.Header())
|
2014-10-17 01:01:59 -07:00
|
|
|
|
2018-07-11 05:57:35 -07:00
|
|
|
assert.True(t, partSet2.HasHeader(partSet.Header()))
|
2015-06-25 20:28:34 -07:00
|
|
|
for i := 0; i < partSet.Total(); i++ {
|
2014-10-17 01:01:59 -07:00
|
|
|
part := partSet.GetPart(i)
|
|
|
|
//t.Logf("\n%v", part)
|
2018-05-17 10:17:50 -07:00
|
|
|
added, err := partSet2.AddPart(part)
|
2014-10-17 01:01:59 -07:00
|
|
|
if !added || err != nil {
|
|
|
|
t.Errorf("Failed to add part %v, error: %v", i, err)
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 05:57:35 -07:00
|
|
|
// adding part with invalid index
|
|
|
|
added, err := partSet2.AddPart(&Part{Index: 10000})
|
|
|
|
assert.False(t, added)
|
|
|
|
assert.Error(t, err)
|
|
|
|
// adding existing part
|
|
|
|
added, err = partSet2.AddPart(partSet2.GetPart(0))
|
|
|
|
assert.False(t, added)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, partSet.Hash(), partSet2.Hash())
|
|
|
|
assert.Equal(t, 100, partSet2.Total())
|
|
|
|
assert.True(t, partSet2.IsComplete())
|
2014-10-17 01:01:59 -07:00
|
|
|
|
|
|
|
// Reconstruct data, assert that they are equal.
|
|
|
|
data2Reader := partSet2.GetReader()
|
|
|
|
data2, err := ioutil.ReadAll(data2Reader)
|
2018-07-11 05:57:35 -07:00
|
|
|
require.NoError(t, err)
|
2014-10-17 01:01:59 -07:00
|
|
|
|
2018-07-11 05:57:35 -07:00
|
|
|
assert.Equal(t, data, data2)
|
2014-10-17 01:01:59 -07:00
|
|
|
}
|
|
|
|
|
2015-06-18 20:19:39 -07:00
|
|
|
func TestWrongProof(t *testing.T) {
|
2014-10-17 01:01:59 -07:00
|
|
|
// Construct random data of size partSize * 100
|
2017-10-04 13:40:45 -07:00
|
|
|
data := cmn.RandBytes(testPartSize * 100)
|
2016-09-16 09:20:07 -07:00
|
|
|
partSet := NewPartSetFromData(data, testPartSize)
|
2014-10-17 01:01:59 -07:00
|
|
|
|
|
|
|
// Test adding a part with wrong data.
|
2014-10-31 18:35:38 -07:00
|
|
|
partSet2 := NewPartSetFromHeader(partSet.Header())
|
2014-10-17 01:01:59 -07:00
|
|
|
|
|
|
|
// Test adding a part with wrong trail.
|
|
|
|
part := partSet.GetPart(0)
|
2015-12-10 11:09:25 -08:00
|
|
|
part.Proof.Aunts[0][0] += byte(0x01)
|
2018-05-17 10:17:50 -07:00
|
|
|
added, err := partSet2.AddPart(part)
|
2014-10-17 01:01:59 -07:00
|
|
|
if added || err == nil {
|
|
|
|
t.Errorf("Expected to fail adding a part with bad trail.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test adding a part with wrong bytes.
|
|
|
|
part = partSet.GetPart(1)
|
|
|
|
part.Bytes[0] += byte(0x01)
|
2018-05-17 10:17:50 -07:00
|
|
|
added, err = partSet2.AddPart(part)
|
2014-10-17 01:01:59 -07:00
|
|
|
if added || err == nil {
|
|
|
|
t.Errorf("Expected to fail adding a part with bad bytes.")
|
|
|
|
}
|
2014-10-16 16:00:48 -07:00
|
|
|
}
|