github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/whisper/whisperv6/envelope_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  // Contains the tests associated with the Whisper protocol Envelope object.
    13  
    14  package whisperv6
    15  
    16  import (
    17  	mrand "math/rand"
    18  	"testing"
    19  
    20  	"github.com/Sberex/go-sberex/crypto"
    21  )
    22  
    23  func TestEnvelopeOpenAcceptsOnlyOneKeyTypeInFilter(t *testing.T) {
    24  	symKey := make([]byte, aesKeyLength)
    25  	mrand.Read(symKey)
    26  
    27  	asymKey, err := crypto.GenerateKey()
    28  	if err != nil {
    29  		t.Fatalf("failed GenerateKey with seed %d: %s.", seed, err)
    30  	}
    31  
    32  	params := MessageParams{
    33  		PoW:      0.01,
    34  		WorkTime: 1,
    35  		TTL:      uint32(mrand.Intn(1024)),
    36  		Payload:  make([]byte, 50),
    37  		KeySym:   symKey,
    38  		Dst:      nil,
    39  	}
    40  
    41  	mrand.Read(params.Payload)
    42  
    43  	msg, err := NewSentMessage(&params)
    44  	if err != nil {
    45  		t.Fatalf("failed to create new message with seed %d: %s.", seed, err)
    46  	}
    47  
    48  	e, err := msg.Wrap(&params)
    49  	if err != nil {
    50  		t.Fatalf("Failed to Wrap the message in an envelope with seed %d: %s", seed, err)
    51  	}
    52  
    53  	f := Filter{KeySym: symKey, KeyAsym: asymKey}
    54  
    55  	decrypted := e.Open(&f)
    56  	if decrypted != nil {
    57  		t.Fatalf("Managed to decrypt a message with an invalid filter, seed %d", seed)
    58  	}
    59  }