trie, whisper/whisperv5: use math/rand Read function

pull/3809/head
Felix Lange 8 years ago
parent 9a2720fb35
commit f1534f5797
  1. 18
      trie/trie_test.go
  2. 20
      whisper/whisperv5/filter_test.go
  3. 18
      whisper/whisperv5/message.go
  4. 16
      whisper/whisperv5/message_test.go
  5. 5
      whisper/whisperv5/whisper_test.go

@ -377,7 +377,7 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
if len(allKeys) < 2 || r.Intn(100) < 10 { if len(allKeys) < 2 || r.Intn(100) < 10 {
// new key // new key
key := make([]byte, r.Intn(50)) key := make([]byte, r.Intn(50))
randRead(r, key) r.Read(key)
allKeys = append(allKeys, key) allKeys = append(allKeys, key)
return key return key
} }
@ -401,22 +401,6 @@ func (randTest) Generate(r *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(steps) return reflect.ValueOf(steps)
} }
// rand.Rand provides a Read method in Go 1.7 and later, but
// we can't use it yet.
func randRead(r *rand.Rand, b []byte) {
pos := 0
val := 0
for n := 0; n < len(b); n++ {
if pos == 0 {
val = r.Int()
pos = 7
}
b[n] = byte(val)
val >>= 8
pos--
}
}
func runRandTest(rt randTest) bool { func runRandTest(rt randTest) bool {
db, _ := ethdb.NewMemDatabase() db, _ := ethdb.NewMemDatabase()
tr, _ := New(common.Hash{}, db) tr, _ := New(common.Hash{}, db)

@ -18,7 +18,7 @@ package whisperv5
import ( import (
"math/big" "math/big"
"math/rand" mrand "math/rand"
"testing" "testing"
"time" "time"
@ -33,12 +33,12 @@ var seed int64
// reproduciblity independent of their sequence. // reproduciblity independent of their sequence.
func InitSingleTest() { func InitSingleTest() {
seed = time.Now().Unix() seed = time.Now().Unix()
rand.Seed(seed) mrand.Seed(seed)
} }
func InitDebugTest(i int64) { func InitDebugTest(i int64) {
seed = i seed = i
rand.Seed(seed) mrand.Seed(seed)
} }
type FilterTestCase struct { type FilterTestCase struct {
@ -55,7 +55,7 @@ func generateFilter(t *testing.T, symmetric bool) (*Filter, error) {
const topicNum = 8 const topicNum = 8
f.Topics = make([]TopicType, topicNum) f.Topics = make([]TopicType, topicNum)
for i := 0; i < topicNum; i++ { for i := 0; i < topicNum; i++ {
randomize(f.Topics[i][:]) mrand.Read(f.Topics[i][:])
f.Topics[i][0] = 0x01 f.Topics[i][0] = 0x01
} }
@ -68,7 +68,7 @@ func generateFilter(t *testing.T, symmetric bool) (*Filter, error) {
if symmetric { if symmetric {
f.KeySym = make([]byte, 12) f.KeySym = make([]byte, 12)
randomize(f.KeySym) mrand.Read(f.KeySym)
f.SymKeyHash = crypto.Keccak256Hash(f.KeySym) f.SymKeyHash = crypto.Keccak256Hash(f.KeySym)
} else { } else {
f.KeyAsym, err = crypto.GenerateKey() f.KeyAsym, err = crypto.GenerateKey()
@ -87,7 +87,7 @@ func generateTestCases(t *testing.T, SizeTestFilters int) []FilterTestCase {
for i := 0; i < SizeTestFilters; i++ { for i := 0; i < SizeTestFilters; i++ {
f, _ := generateFilter(t, true) f, _ := generateFilter(t, true)
cases[i].f = f cases[i].f = f
cases[i].alive = (rand.Int()&int(1) == 0) cases[i].alive = (mrand.Int()&int(1) == 0)
} }
return cases return cases
} }
@ -147,7 +147,7 @@ func TestComparePubKey(t *testing.T) {
} }
// generate key3 == key1 // generate key3 == key1
rand.Seed(seed) mrand.Seed(seed)
key3, err := crypto.GenerateKey() key3, err := crypto.GenerateKey()
if err != nil { if err != nil {
t.Fatalf("failed to generate third key with seed %d: %s.", seed, err) t.Fatalf("failed to generate third key with seed %d: %s.", seed, err)
@ -193,7 +193,7 @@ func TestMatchEnvelope(t *testing.T) {
} }
// encrypt symmetrically // encrypt symmetrically
i := rand.Int() % 4 i := mrand.Int() % 4
fsym.Topics[i] = params.Topic fsym.Topics[i] = params.Topic
fasym.Topics[i] = params.Topic fasym.Topics[i] = params.Topic
msg = NewSentMessage(params) msg = NewSentMessage(params)
@ -544,7 +544,7 @@ func TestWatchers(t *testing.T) {
var envelopes [NumMessages]*Envelope var envelopes [NumMessages]*Envelope
for i = 0; i < NumMessages; i++ { for i = 0; i < NumMessages; i++ {
j = rand.Uint32() % NumFilters j = mrand.Uint32() % NumFilters
e = generateCompatibeEnvelope(t, tst[j].f) e = generateCompatibeEnvelope(t, tst[j].f)
envelopes[i] = e envelopes[i] = e
tst[j].msgCnt++ tst[j].msgCnt++
@ -597,7 +597,7 @@ func TestWatchers(t *testing.T) {
envelopes[0] = e envelopes[0] = e
tst[0].msgCnt++ tst[0].msgCnt++
for i = 1; i < NumMessages; i++ { for i = 1; i < NumMessages; i++ {
j = rand.Uint32() % NumFilters j = mrand.Uint32() % NumFilters
e = generateCompatibeEnvelope(t, tst[j].f) e = generateCompatibeEnvelope(t, tst[j].f)
envelopes[i] = e envelopes[i] = e
tst[j].msgCnt++ tst[j].msgCnt++

@ -128,7 +128,7 @@ func (msg *SentMessage) appendPadding(params *MessageParams) {
panic("please fix the padding algorithm before releasing new version") panic("please fix the padding algorithm before releasing new version")
} }
buf := make([]byte, padSize) buf := make([]byte, padSize)
randomize(buf[1:]) mrand.Read(buf[1:])
buf[0] = byte(padSize) buf[0] = byte(padSize)
if params.Padding != nil { if params.Padding != nil {
copy(buf[1:], params.Padding) copy(buf[1:], params.Padding)
@ -365,19 +365,3 @@ func (msg *ReceivedMessage) hash() []byte {
} }
return crypto.Keccak256(msg.Raw) return crypto.Keccak256(msg.Raw)
} }
// rand.Rand provides a Read method in Go 1.7 and later,
// but we can't use it yet.
func randomize(b []byte) {
cnt := 0
val := mrand.Int63()
for n := 0; n < len(b); n++ {
b[n] = byte(val)
val >>= 8
cnt++
if cnt >= 7 {
cnt = 0
val = mrand.Int63()
}
}
}

@ -18,7 +18,7 @@ package whisperv5
import ( import (
"bytes" "bytes"
"math/rand" mrand "math/rand"
"testing" "testing"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -34,13 +34,13 @@ func generateMessageParams() (*MessageParams, error) {
// set all the parameters except p.Dst // set all the parameters except p.Dst
buf := make([]byte, 1024) buf := make([]byte, 1024)
randomize(buf) mrand.Read(buf)
sz := rand.Intn(400) sz := mrand.Intn(400)
var p MessageParams var p MessageParams
p.PoW = 0.01 p.PoW = 0.01
p.WorkTime = 1 p.WorkTime = 1
p.TTL = uint32(rand.Intn(1024)) p.TTL = uint32(mrand.Intn(1024))
p.Payload = make([]byte, sz) p.Payload = make([]byte, sz)
p.Padding = make([]byte, padSizeLimitUpper) p.Padding = make([]byte, padSizeLimitUpper)
p.KeySym = make([]byte, aesKeyLength) p.KeySym = make([]byte, aesKeyLength)
@ -132,7 +132,7 @@ func TestMessageEncryption(t *testing.T) {
func TestMessageWrap(t *testing.T) { func TestMessageWrap(t *testing.T) {
seed = int64(1777444222) seed = int64(1777444222)
rand.Seed(seed) mrand.Seed(seed)
target := 128.0 target := 128.0
params, err := generateMessageParams() params, err := generateMessageParams()
@ -168,7 +168,7 @@ func TestMessageWrap(t *testing.T) {
func TestMessageSeal(t *testing.T) { func TestMessageSeal(t *testing.T) {
// this test depends on deterministic choice of seed (1976726903) // this test depends on deterministic choice of seed (1976726903)
seed = int64(1976726903) seed = int64(1976726903)
rand.Seed(seed) mrand.Seed(seed)
params, err := generateMessageParams() params, err := generateMessageParams()
if err != nil { if err != nil {
@ -179,8 +179,8 @@ func TestMessageSeal(t *testing.T) {
params.TTL = 1 params.TTL = 1
aesnonce := make([]byte, 12) aesnonce := make([]byte, 12)
salt := make([]byte, 12) salt := make([]byte, 12)
randomize(aesnonce) mrand.Read(aesnonce)
randomize(salt) mrand.Read(salt)
env := NewEnvelope(params.TTL, params.Topic, salt, aesnonce, msg) env := NewEnvelope(params.TTL, params.Topic, salt, aesnonce, msg)
if err != nil { if err != nil {

@ -18,6 +18,7 @@ package whisperv5
import ( import (
"bytes" "bytes"
mrand "math/rand"
"testing" "testing"
"time" "time"
@ -49,7 +50,7 @@ func TestWhisperBasic(t *testing.T) {
} }
peerID := make([]byte, 64) peerID := make([]byte, 64)
randomize(peerID) mrand.Read(peerID)
peer, _ := w.getPeer(peerID) peer, _ := w.getPeer(peerID)
if peer != nil { if peer != nil {
t.Fatal("found peer for random key.") t.Fatal("found peer for random key.")
@ -212,7 +213,7 @@ func TestWhisperSymKeyManagement(t *testing.T) {
// add existing id, nothing should change // add existing id, nothing should change
randomKey := make([]byte, 16) randomKey := make([]byte, 16)
randomize(randomKey) mrand.Read(randomKey)
err = w.AddSymKey(id1, randomKey) err = w.AddSymKey(id1, randomKey)
if err == nil { if err == nil {
t.Fatalf("failed AddSymKey with seed %d.", seed) t.Fatalf("failed AddSymKey with seed %d.", seed)

Loading…
Cancel
Save