github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/whisper/whisperv6/envelope_test.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  // Contains the tests associated with the Whisper protocol Envelope object.
    19  
    20  package whisperv6
    21  
    22  import (
    23  	mrand "math/rand"
    24  	"testing"
    25  
    26  	"github.com/AigarNetwork/aigar/crypto"
    27  )
    28  
    29  func TestPoWCalculationsWithNoLeadingZeros(t *testing.T) {
    30  	e := Envelope{
    31  		TTL:   1,
    32  		Data:  []byte{0xde, 0xad, 0xbe, 0xef},
    33  		Nonce: 100000,
    34  	}
    35  
    36  	e.calculatePoW(0)
    37  
    38  	if e.pow != 0.07692307692307693 {
    39  		t.Fatalf("invalid PoW calculation. Expected 0.07692307692307693, got %v", e.pow)
    40  	}
    41  }
    42  
    43  func TestPoWCalculationsWith8LeadingZeros(t *testing.T) {
    44  	e := Envelope{
    45  		TTL:   1,
    46  		Data:  []byte{0xde, 0xad, 0xbe, 0xef},
    47  		Nonce: 276,
    48  	}
    49  	e.calculatePoW(0)
    50  
    51  	if e.pow != 19.692307692307693 {
    52  		t.Fatalf("invalid PoW calculation. Expected 19.692307692307693, got %v", e.pow)
    53  	}
    54  }
    55  
    56  func TestEnvelopeOpenAcceptsOnlyOneKeyTypeInFilter(t *testing.T) {
    57  	symKey := make([]byte, aesKeyLength)
    58  	mrand.Read(symKey)
    59  
    60  	asymKey, err := crypto.GenerateKey()
    61  	if err != nil {
    62  		t.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
    63  	}
    64  
    65  	params := MessageParams{
    66  		PoW:      0.01,
    67  		WorkTime: 1,
    68  		TTL:      uint32(mrand.Intn(1024)),
    69  		Payload:  make([]byte, 50),
    70  		KeySym:   symKey,
    71  		Dst:      nil,
    72  	}
    73  
    74  	mrand.Read(params.Payload)
    75  
    76  	msg, err := NewSentMessage(&params)
    77  	if err != nil {
    78  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
    79  	}
    80  
    81  	e, err := msg.Wrap(&params)
    82  	if err != nil {
    83  		t.Fatalf("Failed to Wrap the message in an envelope with seed %d: %s", seed, err)
    84  	}
    85  
    86  	f := Filter{KeySym: symKey, KeyAsym: asymKey}
    87  
    88  	decrypted := e.Open(&f)
    89  	if decrypted != nil {
    90  		t.Fatalf("Managed to decrypt a message with an invalid filter, seed %d", seed)
    91  	}
    92  }