Skip to content

Commit

Permalink
Merge pull request #14 from vds/013-marshalbinary
Browse files Browse the repository at this point in the history
Fix bugs in binary marshal/unmarshal code and add tests.
  • Loading branch information
rogpeppe committed Jul 10, 2014
2 parents e8568dc + 566c766 commit 7ea7f20
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 4 deletions.
6 changes: 6 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package macaroon

// Data returns the macaroon's data.
func (m *Macaroon) Data() []byte {
return m.data
}
2 changes: 1 addition & 1 deletion macaroon.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (m *Macaroon) appendCaveat(caveatId string, verificationId []byte, loc stri
}
}
if loc != "" {
cav.location, ok = m.appendPacket(fieldLocation, []byte(loc))
cav.location, ok = m.appendPacket(fieldCaveatLocation, []byte(loc))
if !ok {
return nil, fmt.Errorf("caveat location too big")
}
Expand Down
48 changes: 47 additions & 1 deletion macaroon_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package macaroon_test

import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"testing"

"github.com/rogpeppe/macaroon"
gc "gopkg.in/check.v1"
_ "net/http"
)

func TestPackage(t *testing.T) {
Expand Down Expand Up @@ -485,3 +485,49 @@ func makeMacaroons(mspecs []macaroonSpec) (
}
return []byte(mspecs[0].rootKey), primary, discharges
}

func assertEqualMacaroons(c *gc.C, m0, m1 *macaroon.Macaroon) {
m0json, err := m0.MarshalJSON()
c.Assert(err, gc.IsNil)
m1json, err := m1.MarshalJSON()
var m0val, m1val interface{}
err = json.Unmarshal(m0json, &m0val)
c.Assert(err, gc.IsNil)
err = json.Unmarshal(m1json, &m1val)
c.Assert(err, gc.IsNil)
c.Assert(m0val, gc.DeepEquals, m1val)
}

func (*macaroonSuite) TestBinaryRoundTrip(c *gc.C) {
// Test the binary marshalling and unmarshalling of a macaroon with
// first and third party caveats.
rootKey := []byte("secret")
m0 := MustNew(rootKey, "some id", "a location")
err := m0.AddFirstPartyCaveat("first caveat")
c.Assert(err, gc.IsNil)
err = m0.AddFirstPartyCaveat("second caveat")
c.Assert(err, gc.IsNil)
err = m0.AddThirdPartyCaveat([]byte("shared root key"), "3rd party caveat", "remote.com")
c.Assert(err, gc.IsNil)
data, err := m0.MarshalBinary()
c.Assert(err, gc.IsNil)
var m1 macaroon.Macaroon
err = m1.UnmarshalBinary(data)
c.Assert(err, gc.IsNil)
assertEqualMacaroons(c, m0, &m1)
}

func (*macaroonSuite) TestBinaryMarshalingAgainstLibmacaroon(c *gc.C) {
// Test that a libmacaroon marshalled macaroon can be correctly unmarshaled
data, err := base64.StdEncoding.DecodeString(
"MDAxN2xvY2F0aW9uIHNvbWV3aGVyZQowMDEyaWRlbnRpZmllciBpZAowMDEzY2lkIGlkZW50aWZpZXIKMDA1MXZpZCAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAC4i9QwCgbL/wZGFvLQpsyhLOv0v6VjIo2KJv5miz+7krqCpt5EhmrL8pYO9xrhT80KMDAxM2NsIHRoaXJkIHBhcnR5CjAwMmZzaWduYXR1cmUg3BXkIDX0giAPPrgkDLbiMGYy/zsC2qPb4jU4G/dohkAK")
c.Assert(err, gc.IsNil)
var m0 macaroon.Macaroon
err = m0.UnmarshalBinary(data)
c.Assert(err, gc.IsNil)
jsonData := []byte(`{"caveats":[{"cid":"identifier\n","vid":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAuIvUMAoGy/8GRhby0KbMoSzr9L+lYyKNiib+Zos/u5K6gqbeRIZqy/KWDvca4U/NCg==","cl":"third party\n"}],"location":"somewhere\n","identifier":"id\n","signature":"dc15e42035f482200f3eb8240cb6e2306632ff3b02daa3dbe235381bf76886400a"}`)
var m1 macaroon.Macaroon
err = m1.UnmarshalJSON(jsonData)
c.Assert(err, gc.IsNil)
assertEqualMacaroons(c, &m0, &m1)
}
6 changes: 4 additions & 2 deletions marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ func (m *Macaroon) UnmarshalBinary(data []byte) error {
if err != nil {
return err
}

var cav caveat
for {
p, err := m.parsePacket(start)
Expand All @@ -142,6 +141,8 @@ func (m *Macaroon) UnmarshalBinary(data []byte) error {
if cav.caveatId.len() != 0 {
m.caveats = append(m.caveats, cav)
}
// Remove the signature from data.
m.data = m.data[0:p.start]
m.sig = append([]byte(nil), m.dataBytes(p)...)
return nil
case fieldCaveatId:
Expand All @@ -163,10 +164,11 @@ func (m *Macaroon) UnmarshalBinary(data []byte) error {
return fmt.Errorf("unexpected field %q", field)
}
}
return nil
}

func (m *Macaroon) expectPacket(start int, kind string) (int, packet, error) {
p, err := m.parsePacket(0)
p, err := m.parsePacket(start)
if err != nil {
return 0, packet{}, err
}
Expand Down

0 comments on commit 7ea7f20

Please sign in to comment.