github.com/status-im/status-go@v1.1.0/waku/common/envelope_test.go (about)

     1  // Copyright 2019 The Waku Library Authors.
     2  //
     3  // The Waku library is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Lesser General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // (at your option) any later version.
     7  //
     8  // The Waku library is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty off
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    11  // GNU Lesser General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Lesser General Public License
    14  // along with the Waku library. If not, see <http://www.gnu.org/licenses/>.
    15  //
    16  // This software uses the go-ethereum library, which is licensed
    17  // under the GNU Lesser General Public Library, version 3 or any later.
    18  
    19  package common
    20  
    21  import (
    22  	mrand "math/rand"
    23  	"testing"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/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) //nolint: gosec
    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)), // nolint: gosec
    69  		Payload:  make([]byte, 50),
    70  		KeySym:   symKey,
    71  		Dst:      nil,
    72  	}
    73  
    74  	mrand.Read(params.Payload) // nolint: gosec
    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, time.Now())
    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  }