whisper: new test added (rlp encode/decode)

This commit is contained in:
Vlad 2017-01-11 12:36:37 +01:00
parent 5b682d6485
commit 03ef121919
3 changed files with 42 additions and 0 deletions

View file

@ -172,6 +172,10 @@ func (e *Envelope) DecodeRLP(s *rlp.Stream) error {
if err != nil { if err != nil {
return err return err
} }
return e.DecodeBytes(raw)
}
func (e *Envelope) DecodeBytes(raw []byte) error {
// The decoding of Envelope uses the struct fields but also needs // The decoding of Envelope uses the struct fields but also needs
// to compute the hash of the whole RLP-encoded envelope. This // to compute the hash of the whole RLP-encoded envelope. This
// type has the same structure as Envelope but is not an // type has the same structure as Envelope but is not an

View file

@ -22,6 +22,7 @@ import (
"testing" "testing"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
) )
func copyFromBuf(dst []byte, src []byte, beg int) int { func copyFromBuf(dst []byte, src []byte, beg int) int {
@ -315,3 +316,35 @@ func TestEncryptWithZeroKey(t *testing.T) {
t.Fatalf("wrapped with nil key, seed: %d.", seed) t.Fatalf("wrapped with nil key, seed: %d.", seed)
} }
} }
func TestRlpEncode(t *testing.T) {
InitSingleTest()
params, err := generateMessageParams()
if err != nil {
t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err)
}
msg := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
t.Fatalf("wrapped with zero key, seed: %d.", seed)
}
rlpEncoded, err := rlp.EncodeToBytes(env)
if err != nil {
t.Fatalf("RLP encode failed: %s.", err)
}
var decoded Envelope
err = decoded.DecodeBytes(rlpEncoded)
if err != nil {
t.Fatalf("RLP decode failed: %s.", err)
}
he := env.Hash()
hd := decoded.Hash()
if he != hd {
t.Fatalf("Hashes are not equal: %x vs. %x", he, hd)
}
}

View file

@ -175,3 +175,8 @@ func (p *Peer) broadcast() error {
glog.V(logger.Detail).Infoln(p.peer, "broadcasted", len(transmit), "message(s)") glog.V(logger.Detail).Infoln(p.peer, "broadcasted", len(transmit), "message(s)")
return nil return nil
} }
func (p *Peer) ID() []byte {
id := p.peer.ID()
return id[:]
}