From 0ee7f82d681048df00a73494caebed6d0ecb6ff5 Mon Sep 17 00:00:00 2001 From: Daniel Skinner Date: Fri, 26 Feb 2016 18:54:50 -0600 Subject: [PATCH] internal/binres: move test-related code out of package source Change-Id: I5989c9395ff1aa40869ccd123cb277eea992a64c Reviewed-on: https://go-review.googlesource.com/19991 Reviewed-by: Hyang-Ah Hana Kim --- internal/binres/binres.go | 38 ++++++++++++++++------------------ internal/binres/binres_test.go | 25 ++++++++++++++++++++-- 2 files changed, 41 insertions(+), 22 deletions(-) diff --git a/internal/binres/binres.go b/internal/binres/binres.go index a796efa..3fca41c 100644 --- a/internal/binres/binres.go +++ b/internal/binres/binres.go @@ -159,11 +159,6 @@ type XML struct { stack []*Element } -// TODO this is used strictly for querying in tests and dependent on -// current XML.UnmarshalBinary implementation. Look into moving directly -// into tests. -var debugIndices = make(map[encoding.BinaryMarshaler]int) - const ( androidSchema = "http://schemas.android.com/apk/res/android" toolsSchema = "http://schemas.android.com/tools" @@ -568,33 +563,36 @@ func UnmarshalXML(r io.Reader) (*XML, error) { return bx, nil } -func (bx *XML) UnmarshalBinary(bin []byte) error { - buf := bin - if err := (&bx.chunkHeader).UnmarshalBinary(bin); err != nil { +// UnmarshalBinary decodes all resource chunks in buf returning any error encountered. +func (bx *XML) UnmarshalBinary(buf []byte) error { + if err := (&bx.chunkHeader).UnmarshalBinary(buf); err != nil { return err } buf = buf[8:] - - // TODO this is tracked strictly for querying in tests; look into moving this - // functionality directly into tests if possible. - debugIndex := 8 - for len(buf) > 0 { - t := ResType(btou16(buf)) - k, err := bx.kind(t) + k, err := bx.unmarshalBinaryKind(buf) if err != nil { return err } - if err := k.UnmarshalBinary(buf); err != nil { - return err - } - debugIndices[k.(encoding.BinaryMarshaler)] = debugIndex - debugIndex += int(k.size()) buf = buf[k.size():] } return nil } +// unmarshalBinaryKind decodes and stores the first resource chunk of bin. +// It returns the unmarshaler interface and any error encountered. +// If k.size() < len(bin), subsequent chunks can be decoded at bin[k.size():]. +func (bx *XML) unmarshalBinaryKind(bin []byte) (k unmarshaler, err error) { + k, err = bx.kind(ResType(btou16(bin))) + if err != nil { + return nil, err + } + if err = k.UnmarshalBinary(bin); err != nil { + return nil, err + } + return k, nil +} + func (bx *XML) kind(t ResType) (unmarshaler, error) { switch t { case ResStringPool: diff --git a/internal/binres/binres_test.go b/internal/binres/binres_test.go index bfd2b51..f7c5069 100644 --- a/internal/binres/binres_test.go +++ b/internal/binres/binres_test.go @@ -105,6 +105,27 @@ func TestBootstrap(t *testing.T) { log.Fatal(err) } + // unmarshal binary xml and store byte indices of decoded resources. + debugIndices := make(map[encoding.BinaryMarshaler]int) + trackUnmarshal := func(buf []byte) (*XML, error) { + bx := new(XML) + if err := (&bx.chunkHeader).UnmarshalBinary(buf); err != nil { + return nil, err + } + buf = buf[8:] + debugIndex := 8 + for len(buf) > 0 { + k, err := bx.unmarshalBinaryKind(buf) + if err != nil { + return nil, err + } + debugIndices[k.(encoding.BinaryMarshaler)] = debugIndex + debugIndex += k.size() + buf = buf[k.size():] + } + return bx, nil + } + checkMarshal := func(res encoding.BinaryMarshaler, bsize int) { b, err := res.MarshalBinary() if err != nil { @@ -151,8 +172,8 @@ func TestBootstrap(t *testing.T) { } } - bxml := new(XML) - if err := bxml.UnmarshalBinary(bin); err != nil { + bxml, err := trackUnmarshal(bin) + if err != nil { t.Fatal(err) }