diff options
author | gluk256 <gluk256@users.noreply.github.com> | 2017-04-27 03:05:48 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-04-27 03:05:48 +0800 |
commit | 95f0bd0acf301bf8415747c4ff050e8a4dfdc864 (patch) | |
tree | 13edad970fb30a8e12151b2ec1a443b0ad970d2b /whisper/whisperv5/message_test.go | |
parent | 8dce4c283dda3a8e10aa30dadab05a8c0dd9e19d (diff) | |
download | go-tangerine-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar go-tangerine-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar.gz go-tangerine-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar.bz2 go-tangerine-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar.lz go-tangerine-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar.xz go-tangerine-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.tar.zst go-tangerine-95f0bd0acf301bf8415747c4ff050e8a4dfdc864.zip |
whisper: message format refactoring (#14335)
* whisper: salt removed from AES encryption
* whisper: padding format updated
* whisper: padding test added
* whisper: padding refactored, tests fixed
* whisper: padding test updated
* whisper: wnode bugfix
* whisper: send/receive protocol updated
* whisper: minor update
* whisper: bugfix in test
* whisper: updated parameter names and comments
* whisper: functions renamed
* whisper: minor refactoring
Diffstat (limited to 'whisper/whisperv5/message_test.go')
-rw-r--r-- | whisper/whisperv5/message_test.go | 140 |
1 files changed, 107 insertions, 33 deletions
diff --git a/whisper/whisperv5/message_test.go b/whisper/whisperv5/message_test.go index 1ed7250d3..aa82a02f3 100644 --- a/whisper/whisperv5/message_test.go +++ b/whisper/whisperv5/message_test.go @@ -31,9 +31,9 @@ func copyFromBuf(dst []byte, src []byte, beg int) int { } func generateMessageParams() (*MessageParams, error) { - // set all the parameters except p.Dst + // set all the parameters except p.Dst and p.Padding - buf := make([]byte, 1024) + buf := make([]byte, 4) mrand.Read(buf) sz := mrand.Intn(400) @@ -42,14 +42,10 @@ func generateMessageParams() (*MessageParams, error) { p.WorkTime = 1 p.TTL = uint32(mrand.Intn(1024)) p.Payload = make([]byte, sz) - p.Padding = make([]byte, padSizeLimitUpper) p.KeySym = make([]byte, aesKeyLength) - - var b int - b = copyFromBuf(p.Payload, buf, b) - b = copyFromBuf(p.Padding, buf, b) - b = copyFromBuf(p.KeySym, buf, b) - p.Topic = BytesToTopic(buf[b:]) + mrand.Read(p.Payload) + mrand.Read(p.KeySym) + p.Topic = BytesToTopic(buf) var err error p.Src, err = crypto.GenerateKey() @@ -77,11 +73,12 @@ func singleMessageTest(t *testing.T, symmetric bool) { } text := make([]byte, 0, 512) - steg := make([]byte, 0, 512) text = append(text, params.Payload...) - steg = append(steg, params.Padding...) - msg := NewSentMessage(params) + msg, err := NewSentMessage(params) + if err != nil { + t.Fatalf("failed to create new message with seed %d: %s.", seed, err) + } env, err := msg.Wrap(params) if err != nil { t.Fatalf("failed Wrap with seed %d: %s.", seed, err) @@ -102,10 +99,6 @@ func singleMessageTest(t *testing.T, symmetric bool) { t.Fatalf("failed to validate with seed %d.", seed) } - padsz := len(decrypted.Padding) - if !bytes.Equal(steg[:padsz], decrypted.Padding) { - t.Fatalf("failed with seed %d: compare padding.", seed) - } if !bytes.Equal(text, decrypted.Payload) { t.Fatalf("failed with seed %d: compare payload.", seed) } @@ -140,7 +133,10 @@ func TestMessageWrap(t *testing.T) { t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) } - msg := NewSentMessage(params) + msg, err := NewSentMessage(params) + if err != nil { + t.Fatalf("failed to create new message with seed %d: %s.", seed, err) + } params.TTL = 1 params.WorkTime = 12 params.PoW = target @@ -155,7 +151,10 @@ func TestMessageWrap(t *testing.T) { } // set PoW target too high, expect error - msg2 := NewSentMessage(params) + msg2, err := NewSentMessage(params) + if err != nil { + t.Fatalf("failed to create new message with seed %d: %s.", seed, err) + } params.TTL = 1000000 params.WorkTime = 1 params.PoW = 10000000.0 @@ -175,14 +174,15 @@ func TestMessageSeal(t *testing.T) { t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) } - msg := NewSentMessage(params) + msg, err := NewSentMessage(params) + if err != nil { + t.Fatalf("failed to create new message with seed %d: %s.", seed, err) + } params.TTL = 1 aesnonce := make([]byte, 12) - salt := make([]byte, 12) mrand.Read(aesnonce) - mrand.Read(salt) - env := NewEnvelope(params.TTL, params.Topic, salt, aesnonce, msg) + env := NewEnvelope(params.TTL, params.Topic, aesnonce, msg) if err != nil { t.Fatalf("failed Wrap with seed %d: %s.", seed, err) } @@ -236,11 +236,12 @@ func singleEnvelopeOpenTest(t *testing.T, symmetric bool) { } text := make([]byte, 0, 512) - steg := make([]byte, 0, 512) text = append(text, params.Payload...) - steg = append(steg, params.Padding...) - msg := NewSentMessage(params) + msg, err := NewSentMessage(params) + if err != nil { + t.Fatalf("failed to create new message with seed %d: %s.", seed, err) + } env, err := msg.Wrap(params) if err != nil { t.Fatalf("failed Wrap with seed %d: %s.", seed, err) @@ -252,10 +253,6 @@ func singleEnvelopeOpenTest(t *testing.T, symmetric bool) { t.Fatalf("failed to open with seed %d.", seed) } - padsz := len(decrypted.Padding) - if !bytes.Equal(steg[:padsz], decrypted.Padding) { - t.Fatalf("failed with seed %d: compare padding.", seed) - } if !bytes.Equal(text, decrypted.Payload) { t.Fatalf("failed with seed %d: compare payload.", seed) } @@ -291,21 +288,38 @@ func TestEncryptWithZeroKey(t *testing.T) { if err != nil { t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) } - - msg := NewSentMessage(params) - + msg, err := NewSentMessage(params) + if err != nil { + t.Fatalf("failed to create new message with seed %d: %s.", seed, err) + } params.KeySym = make([]byte, aesKeyLength) _, err = msg.Wrap(params) if err == nil { t.Fatalf("wrapped with zero key, seed: %d.", seed) } + params, err = generateMessageParams() + if err != nil { + t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) + } + msg, err = NewSentMessage(params) + if err != nil { + t.Fatalf("failed to create new message with seed %d: %s.", seed, err) + } params.KeySym = make([]byte, 0) _, err = msg.Wrap(params) if err == nil { t.Fatalf("wrapped with empty key, seed: %d.", seed) } + params, err = generateMessageParams() + if err != nil { + t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) + } + msg, err = NewSentMessage(params) + if err != nil { + t.Fatalf("failed to create new message with seed %d: %s.", seed, err) + } params.KeySym = nil _, err = msg.Wrap(params) if err == nil { @@ -320,7 +334,10 @@ func TestRlpEncode(t *testing.T) { if err != nil { t.Fatalf("failed generateMessageParams with seed %d: %s.", seed, err) } - msg := NewSentMessage(params) + msg, err := NewSentMessage(params) + if err != nil { + t.Fatalf("failed to create new message with seed %d: %s.", seed, err) + } env, err := msg.Wrap(params) if err != nil { t.Fatalf("wrapped with zero key, seed: %d.", seed) @@ -344,3 +361,60 @@ func TestRlpEncode(t *testing.T) { t.Fatalf("Hashes are not equal: %x vs. %x", he, hd) } } + +func singlePaddingTest(t *testing.T, padSize int) { + params, err := generateMessageParams() + if err != nil { + t.Fatalf("failed generateMessageParams with seed %d and sz=%d: %s.", seed, padSize, err) + } + params.Padding = make([]byte, padSize) + params.PoW = 0.0000000001 + pad := make([]byte, padSize) + _, err = mrand.Read(pad) + if err != nil { + t.Fatalf("padding is not generated (seed %d): %s", seed, err) + } + n := copy(params.Padding, pad) + if n != padSize { + t.Fatalf("padding is not copied (seed %d): %s", seed, err) + } + msg, err := NewSentMessage(params) + if err != nil { + t.Fatalf("failed to create new message with seed %d: %s.", seed, err) + } + env, err := msg.Wrap(params) + if err != nil { + t.Fatalf("failed to wrap, seed: %d and sz=%d.", seed, padSize) + } + f := Filter{KeySym: params.KeySym} + decrypted := env.Open(&f) + if decrypted == nil { + t.Fatalf("failed to open, seed and sz=%d: %d.", seed, padSize) + } + if !bytes.Equal(pad, decrypted.Padding) { + t.Fatalf("padding is not retireved as expected with seed %d and sz=%d:\n[%x]\n[%x].", seed, padSize, pad, decrypted.Padding) + } +} + +func TestPadding(t *testing.T) { + InitSingleTest() + + for i := 1; i < 260; i++ { + singlePaddingTest(t, i) + } + + lim := 256 * 256 + for i := lim - 5; i < lim+2; i++ { + singlePaddingTest(t, i) + } + + for i := 0; i < 256; i++ { + n := mrand.Intn(256*254) + 256 + singlePaddingTest(t, n) + } + + for i := 0; i < 256; i++ { + n := mrand.Intn(256*1024) + 256*256 + singlePaddingTest(t, n) + } +} |