From 62cff26edfb2a6010a615a9b4840db3bd9e1e688 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Tue, 28 Jul 2020 12:07:54 +0200 Subject: [PATCH] lib/config: Repair versioning XML serialization (fixes #6859, ref #6693) --- lib/config/config_test.go | 34 +++++++++++++++++++++++++++ lib/config/versioningconfiguration.go | 6 ++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/config/config_test.go b/lib/config/config_test.go index 51c2b29f6..7b0dce22c 100644 --- a/lib/config/config_test.go +++ b/lib/config/config_test.go @@ -9,6 +9,7 @@ package config import ( "bytes" "encoding/json" + "encoding/xml" "fmt" "io/ioutil" "os" @@ -1173,3 +1174,36 @@ func load(path string, myID protocol.DeviceID) (Wrapper, error) { func wrap(path string, cfg Configuration) Wrapper { return Wrap(path, cfg, events.NoopLogger) } + +func TestInternalVersioningConfiguration(t *testing.T) { + // Verify that the versioning configuration XML seralizes to something + // reasonable. + + cfg := New(device1) + cfg.Folders = append(cfg.Folders, NewFolderConfiguration(device1, "default", "default", fs.FilesystemTypeBasic, "/tmp")) + cfg.Folders[0].Versioning = VersioningConfiguration{ + Type: "foo", + Params: map[string]string{"bar": "baz"}, + CleanupIntervalS: 42, + } + + // These things should all be present in the serialized version. + expected := []string{ + ``, + `42<`, + ``, + } + + bs, err := xml.MarshalIndent(cfg, "", " ") + if err != nil { + t.Fatal(err) + } + + for _, exp := range expected { + if !strings.Contains(string(bs), exp) { + t.Logf("%s", bs) + t.Fatal("bad serializion of versioning parameters") + } + } +} diff --git a/lib/config/versioningconfiguration.go b/lib/config/versioningconfiguration.go index 90cd496db..a399fa3d0 100644 --- a/lib/config/versioningconfiguration.go +++ b/lib/config/versioningconfiguration.go @@ -60,7 +60,11 @@ func (c *VersioningConfiguration) UnmarshalXML(d *xml.Decoder, start xml.StartEl } func (c *VersioningConfiguration) MarshalXML(e *xml.Encoder, start xml.StartElement) error { - return e.Encode(c.toInternal()) + // Using EncodeElement instead of plain Encode ensures that we use the + // outer tag name from the VersioningConfiguration (i.e., + // ``) rather than whatever the internal representation + // would otherwise be. + return e.EncodeElement(c.toInternal(), start) } func (c *VersioningConfiguration) toInternal() internalVersioningConfiguration {