diff options
author | obscuren <geffobscura@gmail.com> | 2015-04-13 23:35:46 +0800 |
---|---|---|
committer | obscuren <geffobscura@gmail.com> | 2015-04-13 23:35:46 +0800 |
commit | 333e539ce2143e9f416cef45053c1c21ce0312d4 (patch) | |
tree | da21c5ca613615f78e7475f67e743c6d9464d1cb /whisper/message_test.go | |
parent | a8a2b2a488f7433abc09c51b751556875c9107a9 (diff) | |
parent | 1fa844aaf51beae9129b52a52f51b6602c52ccdb (diff) | |
download | go-tangerine-333e539ce2143e9f416cef45053c1c21ce0312d4.tar go-tangerine-333e539ce2143e9f416cef45053c1c21ce0312d4.tar.gz go-tangerine-333e539ce2143e9f416cef45053c1c21ce0312d4.tar.bz2 go-tangerine-333e539ce2143e9f416cef45053c1c21ce0312d4.tar.lz go-tangerine-333e539ce2143e9f416cef45053c1c21ce0312d4.tar.xz go-tangerine-333e539ce2143e9f416cef45053c1c21ce0312d4.tar.zst go-tangerine-333e539ce2143e9f416cef45053c1c21ce0312d4.zip |
Merge branch 'develop' of github.com-obscure:ethereum/go-ethereum into develop
Diffstat (limited to 'whisper/message_test.go')
-rw-r--r-- | whisper/message_test.go | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/whisper/message_test.go b/whisper/message_test.go new file mode 100644 index 000000000..8d4c5e990 --- /dev/null +++ b/whisper/message_test.go @@ -0,0 +1,138 @@ +package whisper + +import ( + "bytes" + "crypto/elliptic" + "testing" + + "github.com/ethereum/go-ethereum/crypto" +) + +// Tests whether a message can be wrapped without any identity or encryption. +func TestMessageSimpleWrap(t *testing.T) { + payload := []byte("hello world") + + msg := NewMessage(payload) + if _, err := msg.Wrap(DefaultProofOfWork, Options{}); err != nil { + t.Fatalf("failed to wrap message: %v", err) + } + if msg.Flags&128 != 0 { + t.Fatalf("signature flag mismatch: have %d, want %d", (msg.Flags&128)>>7, 0) + } + if len(msg.Signature) != 0 { + t.Fatalf("signature found for simple wrapping: 0x%x", msg.Signature) + } + if bytes.Compare(msg.Payload, payload) != 0 { + t.Fatalf("payload mismatch after wrapping: have 0x%x, want 0x%x", msg.Payload, payload) + } +} + +// Tests whether a message can be signed, and wrapped in plain-text. +func TestMessageCleartextSignRecover(t *testing.T) { + key, err := crypto.GenerateKey() + if err != nil { + t.Fatalf("failed to create crypto key: %v", err) + } + payload := []byte("hello world") + + msg := NewMessage(payload) + if _, err := msg.Wrap(DefaultProofOfWork, Options{ + From: key, + }); err != nil { + t.Fatalf("failed to sign message: %v", err) + } + if msg.Flags&128 != 128 { + t.Fatalf("signature flag mismatch: have %d, want %d", (msg.Flags&128)>>7, 1) + } + if bytes.Compare(msg.Payload, payload) != 0 { + t.Fatalf("payload mismatch after signing: have 0x%x, want 0x%x", msg.Payload, payload) + } + + pubKey := msg.Recover() + if pubKey == nil { + t.Fatalf("failed to recover public key") + } + p1 := elliptic.Marshal(crypto.S256(), key.PublicKey.X, key.PublicKey.Y) + p2 := elliptic.Marshal(crypto.S256(), pubKey.X, pubKey.Y) + if !bytes.Equal(p1, p2) { + t.Fatalf("public key mismatch: have 0x%x, want 0x%x", p2, p1) + } +} + +// Tests whether a message can be encrypted and decrypted using an anonymous +// sender (i.e. no signature). +func TestMessageAnonymousEncryptDecrypt(t *testing.T) { + key, err := crypto.GenerateKey() + if err != nil { + t.Fatalf("failed to create recipient crypto key: %v", err) + } + payload := []byte("hello world") + + msg := NewMessage(payload) + envelope, err := msg.Wrap(DefaultProofOfWork, Options{ + To: &key.PublicKey, + }) + if err != nil { + t.Fatalf("failed to encrypt message: %v", err) + } + if msg.Flags&128 != 0 { + t.Fatalf("signature flag mismatch: have %d, want %d", (msg.Flags&128)>>7, 0) + } + if len(msg.Signature) != 0 { + t.Fatalf("signature found for anonymous message: 0x%x", msg.Signature) + } + + out, err := envelope.Open(key) + if err != nil { + t.Fatalf("failed to open encrypted message: %v", err) + } + if !bytes.Equal(out.Payload, payload) { + t.Error("payload mismatch: have 0x%x, want 0x%x", out.Payload, payload) + } +} + +// Tests whether a message can be properly signed and encrypted. +func TestMessageFullCrypto(t *testing.T) { + fromKey, err := crypto.GenerateKey() + if err != nil { + t.Fatalf("failed to create sender crypto key: %v", err) + } + toKey, err := crypto.GenerateKey() + if err != nil { + t.Fatalf("failed to create recipient crypto key: %v", err) + } + + payload := []byte("hello world") + msg := NewMessage(payload) + envelope, err := msg.Wrap(DefaultProofOfWork, Options{ + From: fromKey, + To: &toKey.PublicKey, + }) + if err != nil { + t.Fatalf("failed to encrypt message: %v", err) + } + if msg.Flags&128 != 128 { + t.Fatalf("signature flag mismatch: have %d, want %d", (msg.Flags&128)>>7, 1) + } + if len(msg.Signature) == 0 { + t.Fatalf("no signature found for signed message") + } + + out, err := envelope.Open(toKey) + if err != nil { + t.Fatalf("failed to open encrypted message: %v", err) + } + if !bytes.Equal(out.Payload, payload) { + t.Error("payload mismatch: have 0x%x, want 0x%x", out.Payload, payload) + } + + pubKey := out.Recover() + if pubKey == nil { + t.Fatalf("failed to recover public key") + } + p1 := elliptic.Marshal(crypto.S256(), fromKey.PublicKey.X, fromKey.PublicKey.Y) + p2 := elliptic.Marshal(crypto.S256(), pubKey.X, pubKey.Y) + if !bytes.Equal(p1, p2) { + t.Fatalf("public key mismatch: have 0x%x, want 0x%x", p2, p1) + } +} |