From 2a3c9de1e0a805b04f4dfaa944798edda4283ba0 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Wed, 17 Jun 2020 15:19:18 +0800 Subject: [PATCH] fix clone not copying caveats --- go.mod | 2 ++ go.sum | 1 + macaroon.go | 2 +- macaroon_test.go | 30 ++++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 98da5e2..0420426 100644 --- a/go.mod +++ b/go.mod @@ -5,3 +5,5 @@ require ( github.com/google/go-cmp v0.2.0 golang.org/x/crypto v0.0.0-20180723164146-c126467f60eb ) + +go 1.13 diff --git a/go.sum b/go.sum index f32e9ae..10008f9 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,6 @@ github.com/frankban/quicktest v1.0.0 h1:QgmxFbprE29UG4oL88tGiiL/7VuiBl5xCcz+wJcJhc0= github.com/frankban/quicktest v1.0.0/go.mod h1:R98jIehRai+d1/3Hv2//jOVCTJhW1VBavT6B6CuGq2k= +github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= diff --git a/macaroon.go b/macaroon.go index d730f89..d833ef6 100644 --- a/macaroon.go +++ b/macaroon.go @@ -120,7 +120,7 @@ func (m *Macaroon) Clone() *Macaroon { m1 := *m // Ensure that if any caveats are appended to the new // macaroon, it will copy the caveats. - m1.caveats = m1.caveats[0:len(m1.caveats):len(m1.caveats)] + m1.caveats = append([]Caveat{}, m1.caveats...) return &m1 } diff --git a/macaroon_test.go b/macaroon_test.go index 7e1d702..da58d19 100644 --- a/macaroon_test.go +++ b/macaroon_test.go @@ -97,6 +97,36 @@ func TestSetLocation(t *testing.T) { c.Assert(m.Location(), qt.Equals, "another location") } +func TestClone(t *testing.T) { + c := qt.New(t) + rootKey := []byte("secret") + m := MustNew(rootKey, []byte("some id"), "a location", macaroon.LatestVersion) + + caveats := map[string]bool{ + "a caveat": true, + } + for cav := range caveats { + m.AddFirstPartyCaveat([]byte(cav)) + } + + m1 := m.Clone() + + c.Assert(m.Location(), qt.Equals, "a location") + c.Assert(m.Caveats()[0].Location, qt.Equals, "") + c.Assert(m1.Location(), qt.Equals, "a location") + c.Assert(m1.Caveats()[0].Location, qt.Equals, "") + + // Modify the caveat, causes a change to m, but not m1 + m.SetLocation("new location") + m.Caveats()[0].Location = "mars" + + c.Assert(m.Location(), qt.Equals, "new location") + c.Assert(m.Caveats()[0].Location, qt.Equals, "mars") + c.Assert(m1.Location(), qt.Equals, "a location") + c.Assert(m1.Caveats()[0].Location, qt.Equals, "") + +} + var equalTests = []struct { about string m1, m2 macaroonSpec