github.com/mutagen-io/mutagen@v0.18.0-rc1/pkg/encoding/protobuf_test.go (about) 1 package encoding 2 3 import ( 4 "bytes" 5 "os" 6 "testing" 7 8 "github.com/mutagen-io/mutagen/pkg/url" 9 ) 10 11 // TestProtocolBuffersCycle tests a Protocol Buffers marshal/save/load/unmarshal 12 // cycle. 13 func TestProtocolBuffersCycle(t *testing.T) { 14 // Create an empty temporary file and defer its cleanup. 15 file, err := os.CreateTemp("", "mutagen_encoding") 16 if err != nil { 17 t.Fatal("unable to create temporary file:", err) 18 } else if err = file.Close(); err != nil { 19 t.Fatal("unable to close temporary file:", err) 20 } 21 defer os.Remove(file.Name()) 22 23 // Create a Protocol Buffers message that we can test with. 24 message := &url.URL{ 25 Protocol: url.Protocol_SSH, 26 User: "George", 27 Host: "washington", 28 Port: 1776, 29 Path: "/by/land/or/by/sea", 30 } 31 if err := MarshalAndSaveProtobuf(file.Name(), message); err != nil { 32 t.Fatal("unable to marshal and save Protocol Buffers message:", err) 33 } 34 35 // Reload the message. 36 decoded := &url.URL{} 37 if err := LoadAndUnmarshalProtobuf(file.Name(), decoded); err != nil { 38 t.Fatal("unable to load and unmarshal Protocol Buffers message:", err) 39 } 40 41 // Verify that contents were preserved. We have to explicitly compare 42 // members because the Protocol Buffers generated struct contains fields 43 // that can't be compared by value. 44 match := decoded.Protocol == message.Protocol && 45 decoded.User == message.User && 46 decoded.Host == message.Host && 47 decoded.Port == message.Port && 48 decoded.Path == message.Path 49 if !match { 50 t.Error("decoded Protocol Buffers message did not match original:", decoded, "!=", message) 51 } 52 } 53 54 const ( 55 // testProtobufEncodingNMessages is the number of messages to send/receive 56 // in TestProtobufEncoding. 57 testProtobufEncodingNMessages = 100 58 // testProtobufSingleEncodingNMessage is the number of messages to 59 // send/receive in TestProtobufSingleEncoding. 60 testProtobufSingleEncodingNMessage = 10 61 ) 62 63 func TestProtobufEncoding(t *testing.T) { 64 // Create a buffer to use as our stream. 65 stream := &bytes.Buffer{} 66 67 // Create an encoder/decoder pair. 68 encoder := NewProtobufEncoder(stream) 69 decoder := NewProtobufDecoder(stream) 70 71 // Set test message parameters. 72 protocol := url.Protocol_SSH 73 username := "George" 74 hostname := "washington" 75 path := "/by/land/or/by/sea" 76 77 // Write a sequence of SSH URL messages with increasing port values. 78 message := &url.URL{ 79 Protocol: protocol, 80 User: username, 81 Host: hostname, 82 Path: path, 83 } 84 for i := 0; i < testProtobufEncodingNMessages; i++ { 85 message.Port = uint32(i) 86 if err := encoder.Encode(message); err != nil { 87 t.Fatal("unable to encode message:", err) 88 } 89 } 90 91 // Read a sequence of URL messages and verify their port values. 92 for i := 0; i < testProtobufEncodingNMessages; i++ { 93 *message = url.URL{} 94 if err := decoder.Decode(message); err != nil { 95 t.Fatal("unable to decode message:", err) 96 } else if message.Protocol != protocol { 97 t.Error("protocol mismatch in received message") 98 } else if message.User != username { 99 t.Error("username mismatch in received message") 100 } else if message.Host != hostname { 101 t.Error("hostname mismatch in received message") 102 } else if message.Port != uint32(i) { 103 t.Error("hostname mismatch in received message") 104 } else if message.Path != path { 105 t.Error("path mismatch in received message") 106 } 107 } 108 109 // Verify that the stream is left empty. 110 if stream.Len() > 0 { 111 t.Error("stream is still populated") 112 } 113 } 114 115 func TestProtobufSingleEncoding(t *testing.T) { 116 // Create a buffer to use as our stream. 117 stream := &bytes.Buffer{} 118 119 // Set test message parameters. 120 protocol := url.Protocol_SSH 121 username := "George" 122 hostname := "washington" 123 path := "/by/land/or/by/sea" 124 125 // Write a sequence of SSH URL messages with increasing port values. 126 message := &url.URL{ 127 Protocol: protocol, 128 User: username, 129 Host: hostname, 130 Path: path, 131 } 132 for i := 0; i < testProtobufSingleEncodingNMessage; i++ { 133 message.Port = uint32(i) 134 if err := EncodeProtobuf(stream, message); err != nil { 135 t.Fatal("unable to encode message:", err) 136 } 137 } 138 139 // Read a sequence of URL messages and verify their port values. 140 for i := 0; i < testProtobufSingleEncodingNMessage; i++ { 141 *message = url.URL{} 142 if err := DecodeProtobuf(stream, message); err != nil { 143 t.Fatal("unable to decode message:", err) 144 } else if message.Protocol != protocol { 145 t.Error("protocol mismatch in received message") 146 } else if message.User != username { 147 t.Error("username mismatch in received message") 148 } else if message.Host != hostname { 149 t.Error("hostname mismatch in received message") 150 } else if message.Port != uint32(i) { 151 t.Error("hostname mismatch in received message") 152 } else if message.Path != path { 153 t.Error("path mismatch in received message") 154 } 155 } 156 157 // Verify that the stream is left empty. 158 if stream.Len() > 0 { 159 t.Error("stream is still populated") 160 } 161 }