Browse Source

Test validation of word banks upon load

pull/1782/head
Ethan Frey 8 years ago
parent
commit
65c880e753
2 changed files with 54 additions and 2 deletions
  1. +7
    -1
      keys/wordcodec.go
  2. +47
    -1
      keys/wordcodec_test.go

+ 7
- 1
keys/wordcodec.go View File

@ -109,7 +109,13 @@ func (c WordCodec) BytesToWords(raw []byte) (words []string, err error) {
for i := 0; i < numWords; i++ { for i := 0; i < numWords; i++ {
nData.DivMod(nData, n2048, nRem) nData.DivMod(nData, n2048, nRem)
rem := nRem.Int64() rem := nRem.Int64()
words = append(words, c.words[rem])
w := c.words[rem]
// double-check bank on generation...
_, err := c.GetIndex(w)
if err != nil {
return nil, err
}
words = append(words, w)
} }
return words, nil return words, nil
} }


+ 47
- 1
keys/wordcodec_test.go View File

@ -5,6 +5,8 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
cmn "github.com/tendermint/tmlibs/common"
) )
func TestLengthCalc(t *testing.T) { func TestLengthCalc(t *testing.T) {
@ -80,7 +82,51 @@ func TestEncodeDecode(t *testing.T) {
} }
func TestCheckInvalidLists(t *testing.T) { func TestCheckInvalidLists(t *testing.T) {
// assert, require := assert.New(t), require.New(t)
assert := assert.New(t)
trivial := []string{"abc", "def"}
short := make([]string, 1234)
long := make([]string, BankSize+1)
right := make([]string, BankSize)
dups := make([]string, BankSize)
for _, list := range [][]string{short, long, right, dups} {
for i := range list {
list[i] = cmn.RandStr(8)
}
}
// create one single duplicate
dups[192] = dups[782]
cases := []struct {
words []string
loadable bool
valid bool
}{
{trivial, false, false},
{short, false, false},
{long, false, false},
{dups, true, false}, // we only check dups on first use...
{right, true, true},
}
for i, tc := range cases {
codec, err := NewCodec(tc.words)
if !tc.loadable {
assert.NotNil(err, "%d", i)
} else if assert.Nil(err, "%d: %+v", i, err) {
data := cmn.RandBytes(32)
w, err := codec.BytesToWords(data)
if tc.valid {
assert.Nil(err, "%d: %+v", i, err)
b, err := codec.WordsToBytes(w)
assert.Nil(err, "%d: %+v", i, err)
assert.Equal(data, b)
} else {
assert.NotNil(err, "%d", i)
}
}
}
} }


Loading…
Cancel
Save