github.com/fozzysec/SiaPrime@v0.0.0-20190612043147-66c8e8d11fe3/modules/renter/proto/negotiate_test.go (about)

     1  package proto
     2  
     3  import (
     4  	"errors"
     5  	"net"
     6  	"testing"
     7  
     8  	"SiaPrime/crypto"
     9  	"SiaPrime/encoding"
    10  	"SiaPrime/modules"
    11  	"SiaPrime/types"
    12  )
    13  
    14  // TestNegotiateRevisionStopResponse tests that when the host sends
    15  // StopResponse, the renter continues processing the revision instead of
    16  // immediately terminating.
    17  func TestNegotiateRevisionStopResponse(t *testing.T) {
    18  	// simulate a renter-host connection
    19  	rConn, hConn := net.Pipe()
    20  
    21  	// handle the host's half of the pipe
    22  	go func() {
    23  		defer hConn.Close()
    24  		// read revision
    25  		encoding.ReadObject(hConn, new(types.FileContractRevision), 1<<22)
    26  		// write acceptance
    27  		modules.WriteNegotiationAcceptance(hConn)
    28  		// read txn signature
    29  		encoding.ReadObject(hConn, new(types.TransactionSignature), 1<<22)
    30  		// write StopResponse
    31  		modules.WriteNegotiationStop(hConn)
    32  		// write txn signature
    33  		encoding.WriteObject(hConn, types.TransactionSignature{})
    34  	}()
    35  
    36  	// since the host wrote StopResponse, we should proceed to validating the
    37  	// transaction. This will return a known error because we are supplying an
    38  	// empty revision.
    39  	_, err := negotiateRevision(rConn, types.FileContractRevision{}, crypto.SecretKey{}, 0)
    40  	if err != types.ErrFileContractWindowStartViolation {
    41  		t.Fatalf("expected %q, got \"%v\"", types.ErrFileContractWindowStartViolation, err)
    42  	}
    43  	rConn.Close()
    44  
    45  	// same as above, but send an error instead of StopResponse. The error
    46  	// should be returned by negotiateRevision immediately (if it is not, we
    47  	// should expect to see a transaction validation error instead).
    48  	rConn, hConn = net.Pipe()
    49  	go func() {
    50  		defer hConn.Close()
    51  		encoding.ReadObject(hConn, new(types.FileContractRevision), 1<<22)
    52  		modules.WriteNegotiationAcceptance(hConn)
    53  		encoding.ReadObject(hConn, new(types.TransactionSignature), 1<<22)
    54  		// write a sentinel error
    55  		modules.WriteNegotiationRejection(hConn, errors.New("sentinel"))
    56  		encoding.WriteObject(hConn, types.TransactionSignature{})
    57  	}()
    58  	expectedErr := "host did not accept transaction signature: sentinel"
    59  	_, err = negotiateRevision(rConn, types.FileContractRevision{}, crypto.SecretKey{}, 0)
    60  	if err == nil || err.Error() != expectedErr {
    61  		t.Fatalf("expected %q, got \"%v\"", expectedErr, err)
    62  	}
    63  	rConn.Close()
    64  }