github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/whisper/whisperv2/message_test.go (about)

     1  // Copyright 2014 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 whisperv2
    18  
    19  import (
    20  	"bytes"
    21  	"crypto/elliptic"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/atheioschain/go-atheios/crypto"
    26  	"github.com/atheioschain/go-atheios/crypto/secp256k1"
    27  )
    28  
    29  // Tests whether a message can be wrapped without any identity or encryption.
    30  func TestMessageSimpleWrap(t *testing.T) {
    31  	payload := []byte("hello world")
    32  
    33  	msg := NewMessage(payload)
    34  	if _, err := msg.Wrap(DefaultPoW, Options{}); err != nil {
    35  		t.Fatalf("failed to wrap message: %v", err)
    36  	}
    37  	if msg.Flags&signatureFlag != 0 {
    38  		t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, 0)
    39  	}
    40  	if len(msg.Signature) != 0 {
    41  		t.Fatalf("signature found for simple wrapping: 0x%x", msg.Signature)
    42  	}
    43  	if !bytes.Equal(msg.Payload, payload) {
    44  		t.Fatalf("payload mismatch after wrapping: have 0x%x, want 0x%x", msg.Payload, payload)
    45  	}
    46  	if msg.TTL/time.Second != DefaultTTL/time.Second {
    47  		t.Fatalf("message TTL mismatch: have %v, want %v", msg.TTL, DefaultTTL)
    48  	}
    49  }
    50  
    51  // Tests whether a message can be signed, and wrapped in plain-text.
    52  func TestMessageCleartextSignRecover(t *testing.T) {
    53  	key, err := crypto.GenerateKey()
    54  	if err != nil {
    55  		t.Fatalf("failed to create crypto key: %v", err)
    56  	}
    57  	payload := []byte("hello world")
    58  
    59  	msg := NewMessage(payload)
    60  	if _, err := msg.Wrap(DefaultPoW, Options{
    61  		From: key,
    62  	}); err != nil {
    63  		t.Fatalf("failed to sign message: %v", err)
    64  	}
    65  	if msg.Flags&signatureFlag != signatureFlag {
    66  		t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, signatureFlag)
    67  	}
    68  	if !bytes.Equal(msg.Payload, payload) {
    69  		t.Fatalf("payload mismatch after signing: have 0x%x, want 0x%x", msg.Payload, payload)
    70  	}
    71  
    72  	pubKey := msg.Recover()
    73  	if pubKey == nil {
    74  		t.Fatalf("failed to recover public key")
    75  	}
    76  	p1 := elliptic.Marshal(secp256k1.S256(), key.PublicKey.X, key.PublicKey.Y)
    77  	p2 := elliptic.Marshal(secp256k1.S256(), pubKey.X, pubKey.Y)
    78  	if !bytes.Equal(p1, p2) {
    79  		t.Fatalf("public key mismatch: have 0x%x, want 0x%x", p2, p1)
    80  	}
    81  }
    82  
    83  // Tests whether a message can be encrypted and decrypted using an anonymous
    84  // sender (i.e. no signature).
    85  func TestMessageAnonymousEncryptDecrypt(t *testing.T) {
    86  	key, err := crypto.GenerateKey()
    87  	if err != nil {
    88  		t.Fatalf("failed to create recipient crypto key: %v", err)
    89  	}
    90  	payload := []byte("hello world")
    91  
    92  	msg := NewMessage(payload)
    93  	envelope, err := msg.Wrap(DefaultPoW, Options{
    94  		To: &key.PublicKey,
    95  	})
    96  	if err != nil {
    97  		t.Fatalf("failed to encrypt message: %v", err)
    98  	}
    99  	if msg.Flags&signatureFlag != 0 {
   100  		t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, 0)
   101  	}
   102  	if len(msg.Signature) != 0 {
   103  		t.Fatalf("signature found for anonymous message: 0x%x", msg.Signature)
   104  	}
   105  
   106  	out, err := envelope.Open(key)
   107  	if err != nil {
   108  		t.Fatalf("failed to open encrypted message: %v", err)
   109  	}
   110  	if !bytes.Equal(out.Payload, payload) {
   111  		t.Errorf("payload mismatch: have 0x%x, want 0x%x", out.Payload, payload)
   112  	}
   113  }
   114  
   115  // Tests whether a message can be properly signed and encrypted.
   116  func TestMessageFullCrypto(t *testing.T) {
   117  	fromKey, err := crypto.GenerateKey()
   118  	if err != nil {
   119  		t.Fatalf("failed to create sender crypto key: %v", err)
   120  	}
   121  	toKey, err := crypto.GenerateKey()
   122  	if err != nil {
   123  		t.Fatalf("failed to create recipient crypto key: %v", err)
   124  	}
   125  
   126  	payload := []byte("hello world")
   127  	msg := NewMessage(payload)
   128  	envelope, err := msg.Wrap(DefaultPoW, Options{
   129  		From: fromKey,
   130  		To:   &toKey.PublicKey,
   131  	})
   132  	if err != nil {
   133  		t.Fatalf("failed to encrypt message: %v", err)
   134  	}
   135  	if msg.Flags&signatureFlag != signatureFlag {
   136  		t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, signatureFlag)
   137  	}
   138  	if len(msg.Signature) == 0 {
   139  		t.Fatalf("no signature found for signed message")
   140  	}
   141  
   142  	out, err := envelope.Open(toKey)
   143  	if err != nil {
   144  		t.Fatalf("failed to open encrypted message: %v", err)
   145  	}
   146  	if !bytes.Equal(out.Payload, payload) {
   147  		t.Errorf("payload mismatch: have 0x%x, want 0x%x", out.Payload, payload)
   148  	}
   149  
   150  	pubKey := out.Recover()
   151  	if pubKey == nil {
   152  		t.Fatalf("failed to recover public key")
   153  	}
   154  	p1 := elliptic.Marshal(secp256k1.S256(), fromKey.PublicKey.X, fromKey.PublicKey.Y)
   155  	p2 := elliptic.Marshal(secp256k1.S256(), pubKey.X, pubKey.Y)
   156  	if !bytes.Equal(p1, p2) {
   157  		t.Fatalf("public key mismatch: have 0x%x, want 0x%x", p2, p1)
   158  	}
   159  }