github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/whisper/envelope_test.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package whisper 18 19 import ( 20 "bytes" 21 "testing" 22 "time" 23 24 "github.com/ethereumproject/go-ethereum/crypto" 25 "github.com/ethereumproject/go-ethereum/crypto/ecies" 26 ) 27 28 func TestEnvelopeOpen(t *testing.T) { 29 payload := []byte("hello world") 30 message := NewMessage(payload) 31 32 envelope, err := message.Wrap(DefaultPoW, Options{}) 33 if err != nil { 34 t.Fatalf("failed to wrap message: %v", err) 35 } 36 opened, err := envelope.Open(nil) 37 if err != nil { 38 t.Fatalf("failed to open envelope: %v", err) 39 } 40 if opened.Flags != message.Flags { 41 t.Fatalf("flags mismatch: have %d, want %d", opened.Flags, message.Flags) 42 } 43 if bytes.Compare(opened.Signature, message.Signature) != 0 { 44 t.Fatalf("signature mismatch: have 0x%x, want 0x%x", opened.Signature, message.Signature) 45 } 46 if bytes.Compare(opened.Payload, message.Payload) != 0 { 47 t.Fatalf("payload mismatch: have 0x%x, want 0x%x", opened.Payload, message.Payload) 48 } 49 if opened.Sent.Unix() != message.Sent.Unix() { 50 t.Fatalf("send time mismatch: have %s, want %s", opened.Sent.String(), message.Sent.String()) 51 } 52 if opened.TTL/time.Second != DefaultTTL/time.Second { 53 t.Fatalf("message TTL mismatch: have %v, want %v", opened.TTL, DefaultTTL) 54 } 55 56 if opened.Hash != envelope.Hash() { 57 t.Fatalf("message hash mismatch: have 0x%x, want 0x%x", opened.Hash, envelope.Hash()) 58 } 59 } 60 61 func TestEnvelopeAnonymousOpenUntargeted(t *testing.T) { 62 payload := []byte("hello envelope") 63 envelope, err := NewMessage(payload).Wrap(DefaultPoW, Options{}) 64 if err != nil { 65 t.Fatalf("failed to wrap message: %v", err) 66 } 67 opened, err := envelope.Open(nil) 68 if err != nil { 69 t.Fatalf("failed to open envelope: %v", err) 70 } 71 if opened.To != nil { 72 t.Fatalf("recipient mismatch: have 0x%x, want nil", opened.To) 73 } 74 if bytes.Compare(opened.Payload, payload) != 0 { 75 t.Fatalf("payload mismatch: have 0x%x, want 0x%x", opened.Payload, payload) 76 } 77 } 78 79 func TestEnvelopeAnonymousOpenTargeted(t *testing.T) { 80 key, err := crypto.GenerateKey() 81 if err != nil { 82 t.Fatalf("failed to generate test identity: %v", err) 83 } 84 85 payload := []byte("hello envelope") 86 envelope, err := NewMessage(payload).Wrap(DefaultPoW, Options{ 87 To: &key.PublicKey, 88 }) 89 if err != nil { 90 t.Fatalf("failed to wrap message: %v", err) 91 } 92 opened, err := envelope.Open(nil) 93 if err != nil { 94 t.Fatalf("failed to open envelope: %v", err) 95 } 96 if opened.To != nil { 97 t.Fatalf("recipient mismatch: have 0x%x, want nil", opened.To) 98 } 99 if bytes.Compare(opened.Payload, payload) == 0 { 100 t.Fatalf("payload match, should have been encrypted: 0x%x", opened.Payload) 101 } 102 } 103 104 func TestEnvelopeIdentifiedOpenUntargeted(t *testing.T) { 105 key, err := crypto.GenerateKey() 106 if err != nil { 107 t.Fatalf("failed to generate test identity: %v", err) 108 } 109 110 payload := []byte("hello envelope") 111 envelope, err := NewMessage(payload).Wrap(DefaultPoW, Options{}) 112 if err != nil { 113 t.Fatalf("failed to wrap message: %v", err) 114 } 115 opened, err := envelope.Open(key) 116 switch err { 117 case nil: 118 t.Fatalf("envelope opened with bad key: %v", opened) 119 120 case ecies.ErrInvalidPublicKey: 121 // Ok, key mismatch but opened 122 123 default: 124 t.Fatalf("failed to open envelope: %v", err) 125 } 126 127 if opened.To != nil { 128 t.Fatalf("recipient mismatch: have 0x%x, want nil", opened.To) 129 } 130 if bytes.Compare(opened.Payload, payload) != 0 { 131 t.Fatalf("payload mismatch: have 0x%x, want 0x%x", opened.Payload, payload) 132 } 133 } 134 135 func TestEnvelopeIdentifiedOpenTargeted(t *testing.T) { 136 key, err := crypto.GenerateKey() 137 if err != nil { 138 t.Fatalf("failed to generate test identity: %v", err) 139 } 140 141 payload := []byte("hello envelope") 142 envelope, err := NewMessage(payload).Wrap(DefaultPoW, Options{ 143 To: &key.PublicKey, 144 }) 145 if err != nil { 146 t.Fatalf("failed to wrap message: %v", err) 147 } 148 opened, err := envelope.Open(key) 149 if err != nil { 150 t.Fatalf("failed to open envelope: %v", err) 151 } 152 if opened.To != nil { 153 t.Fatalf("recipient mismatch: have 0x%x, want nil", opened.To) 154 } 155 if bytes.Compare(opened.Payload, payload) != 0 { 156 t.Fatalf("payload mismatch: have 0x%x, want 0x%x", opened.Payload, payload) 157 } 158 }