github.com/johnathanhowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/modules/negotiate_test.go (about)

     1  package modules
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  
     7  	"github.com/NebulousLabs/Sia/crypto"
     8  	"github.com/NebulousLabs/Sia/types"
     9  )
    10  
    11  // TestAnnouncementHandling checks that CreateAnnouncement and
    12  // DecodeAnnouncement work together correctly.
    13  func TestAnnouncementHandling(t *testing.T) {
    14  	t.Parallel()
    15  
    16  	// Create the keys that will be used to generate the announcement.
    17  	sk, pk, err := crypto.GenerateKeyPair()
    18  	if err != nil {
    19  		t.Fatal(err)
    20  	}
    21  	spk := types.SiaPublicKey{
    22  		Algorithm: types.SignatureEd25519,
    23  		Key:       pk[:],
    24  	}
    25  	addr := NetAddress("f.o:1234")
    26  
    27  	// Generate the announcement.
    28  	annBytes, err := CreateAnnouncement(addr, spk, sk)
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	// Decode the announcement
    34  	decAddr, decPubKey, err := DecodeAnnouncement(annBytes)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if decPubKey.Algorithm != spk.Algorithm {
    39  		t.Error("decoded announcement has the wrong algorithm on the public key")
    40  	}
    41  	if decAddr != addr {
    42  		t.Error("decoded announcement has the wrong net address")
    43  	}
    44  	if !bytes.Equal(decPubKey.Key, spk.Key) {
    45  		t.Error("decoded announcement has the wrong public key")
    46  	}
    47  
    48  	// Corrupt the data, and see that decoding fails. Decoding should fail
    49  	// because the signature should not be valid anymore.
    50  	//
    51  	// First 16 bytes are the host announcement prefix, followed by 8 bytes
    52  	// describing the length of the net address, followed by the net address.
    53  	// Corrupt the net address.
    54  	annBytes[25]++
    55  	_, _, err = DecodeAnnouncement(annBytes)
    56  	if err != crypto.ErrInvalidSignature {
    57  		t.Error(err)
    58  	}
    59  	annBytes[25]--
    60  
    61  	// The final byte is going to be a part of the signature. Corrupt the final
    62  	// byte and verify that there's an error.
    63  	lastIndex := len(annBytes) - 1
    64  	annBytes[lastIndex]++
    65  	_, _, err = DecodeAnnouncement(annBytes)
    66  	if err != crypto.ErrInvalidSignature {
    67  		t.Error(err)
    68  	}
    69  	annBytes[lastIndex]--
    70  
    71  	// Pass in a bad specifier - change the host announcement type.
    72  	annBytes[0]++
    73  	_, _, err = DecodeAnnouncement(annBytes)
    74  	if err != ErrAnnNotAnnouncement {
    75  		t.Error(err)
    76  	}
    77  	annBytes[0]--
    78  
    79  	// Pass in a bad signature algorithm. 16 bytes to pass the specifier, 8+8 bytes to pass the net address.
    80  	annBytes[33]++
    81  	_, _, err = DecodeAnnouncement(annBytes)
    82  	if err != ErrAnnUnrecognizedSignature {
    83  		t.Error(err)
    84  	}
    85  	annBytes[33]--
    86  
    87  	// Cause the decoding to fail altogether.
    88  	_, _, err = DecodeAnnouncement(annBytes[:12])
    89  	if err == nil {
    90  		t.Error(err)
    91  	}
    92  }
    93  
    94  // TestNegotiationResponses tests the WriteNegotiationAcceptance,
    95  // WriteNegotiationRejection, and ReadNegotiationAcceptance functions.
    96  func TestNegotiationResponses(t *testing.T) {
    97  	// Write/Read acceptance
    98  	buf := new(bytes.Buffer)
    99  	err := WriteNegotiationAcceptance(buf)
   100  	if err != nil {
   101  		t.Fatal(err)
   102  	}
   103  	err = ReadNegotiationAcceptance(buf)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	// Write/Read rejection
   109  	buf.Reset()
   110  	err = WriteNegotiationRejection(buf, ErrLowBalance)
   111  	if err != ErrLowBalance {
   112  		t.Fatal(err)
   113  	}
   114  	err = ReadNegotiationAcceptance(buf)
   115  	// can't compare to ErrLowBalance directly; contents are the same, but pointer is different
   116  	if err == nil || err.Error() != ErrLowBalance.Error() {
   117  		t.Fatal(err)
   118  	}
   119  
   120  	// Write/Read StopResponse
   121  	buf.Reset()
   122  	err = WriteNegotiationStop(buf)
   123  	if err != nil {
   124  		t.Fatal(err)
   125  	}
   126  	err = ReadNegotiationAcceptance(buf)
   127  	if err != ErrStopResponse {
   128  		t.Fatal(err)
   129  	}
   130  }