lib/protocol: Use DeviceID in protocol messages, with custom marshalling
This makes the device ID a real type that can be used in the protobuf schema. That avoids the juggling back and forth from []byte in a bunch of places and simplifies the code. GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/3695
This commit is contained in:
committed by
Audrius Butkevicius
parent
1cdfef4d6a
commit
0296c23685
@@ -1,5 +1,8 @@
|
||||
// Copyright (C) 2014 The Protocol Authors.
|
||||
|
||||
//go:generate go run ../../script/protofmt.go deviceid_test.proto
|
||||
//go:generate protoc --proto_path=../../../../../:../../../../gogo/protobuf/protobuf:. --gogofast_out=. deviceid_test.proto
|
||||
|
||||
package protocol
|
||||
|
||||
import "testing"
|
||||
@@ -96,3 +99,53 @@ func TestDeviceIDFromBytes(t *testing.T) {
|
||||
t.Errorf("Wrong device ID, got %q, want %q", id1, formatted)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewDeviceIDMarshalling(t *testing.T) {
|
||||
// The new DeviceID.Unmarshal / DeviceID.MarshalTo serialization should
|
||||
// be message compatible with how we used to serialize DeviceIDs.
|
||||
|
||||
// Create a message with a device ID in old style bytes format
|
||||
|
||||
id0, _ := DeviceIDFromString(formatted)
|
||||
msg0 := TestOldDeviceID{id0[:]}
|
||||
|
||||
// Marshal it
|
||||
|
||||
bs, err := msg0.Marshal()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Unmarshal using the new DeviceID.Unmarshal
|
||||
|
||||
var msg1 TestNewDeviceID
|
||||
if err := msg1.Unmarshal(bs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Verify it's the same
|
||||
|
||||
if msg1.Test != id0 {
|
||||
t.Error("Mismatch in old -> new direction")
|
||||
}
|
||||
|
||||
// Marshal using the new DeviceID.MarshalTo
|
||||
|
||||
bs, err = msg1.Marshal()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Create an old style message and and attempt unmarshal
|
||||
|
||||
var msg2 TestOldDeviceID
|
||||
if err := msg2.Unmarshal(bs); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
// Verify it's the same
|
||||
|
||||
if DeviceIDFromBytes(msg2.Test) != id0 {
|
||||
t.Error("Mismatch in old -> new direction")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user