github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/mutagen/version_test.go (about)

     1  package mutagen
     2  
     3  import (
     4  	"bytes"
     5  	"io"
     6  	"testing"
     7  )
     8  
     9  // receiveAndCompareVersion is a test helper function that reads version
    10  // information from the specified reader and ensures that it matches the current
    11  // version. Version tag components are neither transmitted nor received, so they
    12  // do not enter into this comparison.
    13  func receiveAndCompareVersion(reader io.Reader) (bool, error) {
    14  	// Receive the version.
    15  	major, minor, patch, err := receiveVersion(reader)
    16  	if err != nil {
    17  		return false, err
    18  	}
    19  
    20  	// Compare the version.
    21  	return major == VersionMajor &&
    22  		minor == VersionMinor &&
    23  		patch == VersionPatch, nil
    24  }
    25  
    26  // TestVersionSendReceiveAndCompare tests a version send/receive cycle.
    27  func TestVersionSendReceiveAndCompare(t *testing.T) {
    28  	// Create an intermediate buffer.
    29  	buffer := &bytes.Buffer{}
    30  
    31  	// Send the version.
    32  	if err := sendVersion(buffer); err != nil {
    33  		t.Fatal("unable to send version:", err)
    34  	}
    35  
    36  	// Ensure that the buffer is non-empty.
    37  	if buffer.Len() != 12 {
    38  		t.Fatal("buffer does not contain expected byte count")
    39  	}
    40  
    41  	// Receive the version.
    42  	if match, err := receiveAndCompareVersion(buffer); err != nil {
    43  		t.Fatal("unable to receive version:", err)
    44  	} else if !match {
    45  		t.Error("version mismatch on receive")
    46  	}
    47  }
    48  
    49  // TestVersionReceiveAndCompareEmptyBuffer tests that receiving a version fails
    50  // when reading from an empty buffer.
    51  func TestVersionReceiveAndCompareEmptyBuffer(t *testing.T) {
    52  	// Create an empty buffer.
    53  	buffer := &bytes.Buffer{}
    54  
    55  	// Attempt to receive the version.
    56  	match, err := receiveAndCompareVersion(buffer)
    57  	if err == nil {
    58  		t.Error("version received from empty buffer")
    59  	}
    60  	if match {
    61  		t.Error("version match on empty buffer")
    62  	}
    63  }
    64  
    65  // TODO: Add tests for ClientVersionHandshake and ServerVersionHandshake.