2
0
mirror of synced 2025-02-23 23:08:14 +00:00

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 <hyangah@gmail.com>
This commit is contained in:
Daniel Skinner 2016-02-26 18:54:50 -06:00
parent 39fb9d6163
commit 0ee7f82d68
2 changed files with 41 additions and 22 deletions

View File

@ -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:

View File

@ -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)
}