Skip to content

Commit

Permalink
Merge de79595 into 89c894f
Browse files Browse the repository at this point in the history
  • Loading branch information
qmuntal committed Apr 30, 2019
2 parents 89c894f + de79595 commit a45caac
Showing 1 changed file with 52 additions and 15 deletions.
67 changes: 52 additions & 15 deletions decoder_test.go
@@ -1,6 +1,7 @@
package gltf

import (
"reflect"
"bytes"
"errors"
"io"
Expand Down Expand Up @@ -139,8 +140,24 @@ func TestOpen(t *testing.T) {
}
}

func readCallback(name string) (io.ReadCloser, error) {
return ioutil.NopCloser(bytes.NewBufferString("abcdefg")), nil
type chunkedReader struct {
s []byte
n int
}

func (c *chunkedReader) Read(p []byte) (n int, err error) {
c.n++
if c.n == len(c.s) + 1 {
return 0, io.EOF
}
p[0] = c.s[c.n-1:c.n][0]
return 1, nil
}

func readCallback(payload string) func(string) (io.ReadCloser, error) {
return func(name string) (io.ReadCloser, error) {
return ioutil.NopCloser(&chunkedReader{s: []byte(payload)}), nil
}
}

func TestDecoder_decodeBuffer(t *testing.T) {
Expand All @@ -151,19 +168,25 @@ func TestDecoder_decodeBuffer(t *testing.T) {
name string
d *Decoder
args args
want []byte
wantErr bool
}{
{"byteLength_0", &Decoder{quotas: ReadQuotas{MaxMemoryAllocation: 2}}, args{&Buffer{ByteLength: 0, URI: "a.bin"}}, true},
{"noURI", &Decoder{quotas: ReadQuotas{MaxMemoryAllocation: 2}}, args{&Buffer{ByteLength: 1, URI: ""}}, true},
{"invalidURI", &Decoder{quotas: ReadQuotas{MaxMemoryAllocation: 2}}, args{&Buffer{ByteLength: 1, URI: "../a.bin"}}, true},
{"maxQuota", &Decoder{quotas: ReadQuotas{MaxMemoryAllocation: 2}}, args{&Buffer{ByteLength: 3, URI: "a.bin"}}, true},
{"cbErr", NewDecoder(nil, func(name string) (io.ReadCloser, error) { return nil, errors.New("") }), args{&Buffer{ByteLength: 3, URI: "a.bin"}}, true},
{"base", NewDecoder(nil, readCallback), args{&Buffer{ByteLength: 3, URI: "a.bin"}}, false},
{"byteLength_0", &Decoder{quotas: ReadQuotas{MaxMemoryAllocation: 2}}, args{&Buffer{ByteLength: 0, URI: "a.bin"}}, nil, true},
{"noURI", &Decoder{quotas: ReadQuotas{MaxMemoryAllocation: 2}}, args{&Buffer{ByteLength: 1, URI: ""}}, nil, true},
{"invalidURI", &Decoder{quotas: ReadQuotas{MaxMemoryAllocation: 2}}, args{&Buffer{ByteLength: 1, URI: "../a.bin"}}, nil, true},
{"maxQuota", &Decoder{quotas: ReadQuotas{MaxMemoryAllocation: 2}}, args{&Buffer{ByteLength: 3, URI: "a.bin"}}, nil, true},
{"cbErr", NewDecoder(nil, func(name string) (io.ReadCloser, error) { return nil, errors.New("") }), args{&Buffer{ByteLength: 3, URI: "a.bin"}}, nil, true},
{"noFilBuf", NewDecoder(nil, readCallback("")), args{&Buffer{ByteLength: 30, URI: "a.bin"}}, make([]byte, 30), true},
{"base", NewDecoder(nil, readCallback("abcdfg")), args{&Buffer{ByteLength: 6, URI: "a.bin"}}, []byte("abcdfg"), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.d.decodeBuffer(tt.args.buffer); (err != nil) != tt.wantErr {
t.Errorf("Decoder.decodeBuffer() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(tt.args.buffer.Data, tt.want) {
t.Errorf("Decoder.decodeBuffer() buffer = %v, want %v", tt.args.buffer.Data, tt.want)
}
})
}
Expand All @@ -177,16 +200,30 @@ func TestDecoder_decodeBinaryBuffer(t *testing.T) {
name string
d *Decoder
args args
want []byte
wantErr bool
}{
{"invalidBuffer", new(Decoder), args{&Buffer{ByteLength: 0, URI: "a.bin"}}, true},
{"readErr", NewDecoder(bytes.NewBufferString(""), nil), args{&Buffer{ByteLength: 1, URI: "a.bin"}}, true},
{"invalidHeader", NewDecoder(bytes.NewBufferString("aaaaaaaa"), nil), args{&Buffer{ByteLength: 1, URI: "a.bin"}}, true},
{"base", NewDecoder(bytes.NewBuffer([]byte{0x06, 0x00, 0x00, 0x00, 0x42, 0x49, 0x4e, 0x00, 1, 2, 3, 4, 5, 6}),
nil), args{&Buffer{ByteLength: 6}},
[]byte{1, 2, 3, 4, 5, 6}, false},
{"smallbuffer", NewDecoder(bytes.NewBuffer([]byte{0x6, 0x00, 0x00, 0x00, 0x42, 0x49, 0x4e, 0x00, 1, 2, 3, 4, 5, 6}),
nil), args{&Buffer{ByteLength: 5}},
[]byte{1, 2, 3, 4, 5}, false},
{"bigbuffer", NewDecoder(bytes.NewBuffer([]byte{0x6, 0x00, 0x00, 0x00, 0x42, 0x49, 0x4e, 0x00, 1, 2, 3, 4, 5, 6}),
nil), args{&Buffer{ByteLength: 7}},
nil, true},
{"invalidBuffer", new(Decoder), args{&Buffer{ByteLength: 0}}, nil, true},
{"readErr", NewDecoder(bytes.NewBufferString(""), nil), args{&Buffer{ByteLength: 1}}, nil, true},
{"invalidHeader", NewDecoder(bytes.NewBufferString("aaaaaaaa"), nil), args{&Buffer{ByteLength: 1}}, nil, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.d.decodeBinaryBuffer(tt.args.buffer); (err != nil) != tt.wantErr {
t.Errorf("Decoder.decodeBinaryBuffer() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(tt.args.buffer.Data, tt.want) {
t.Errorf("Decoder.decodeBinaryBuffer() buffer = %v, want %v", tt.args.buffer.Data, tt.want)
}
})
}
Expand All @@ -202,10 +239,10 @@ func TestDecoder_Decode(t *testing.T) {
args args
wantErr bool
}{
{"baseJSON", NewDecoder(bytes.NewBufferString("{\"buffers\": [{\"byteLength\": 1, \"URI\": \"a.bin\"}]}"), readCallback), args{new(Document)}, false},
{"onlyGLBHeader", NewDecoder(bytes.NewBuffer([]byte{0x67, 0x6c, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00, 0x40, 0x0b, 0x00, 0x00, 0x5c, 0x06, 0x00, 0x00, 0x4a, 0x53, 0x4f, 0x4e}), readCallback), args{new(Document)}, true},
{"glbMaxMemory", NewDecoder(bytes.NewBuffer([]byte{0x67, 0x6c, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00, 0x40, 0x0b, 0x00, 0x00, 0x5c, 0x06, 0x00, 0x00, 0x4a, 0x53, 0x4f, 0x4e}), readCallback).SetQuotas(ReadQuotas{MaxMemoryAllocation: 0}), args{new(Document)}, true},
{"glbNoJSONChunk", NewDecoder(bytes.NewBuffer([]byte{0x67, 0x6c, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00, 0x40, 0x0b, 0x00, 0x00, 0x5c, 0x06, 0x00, 0x00, 0x4a, 0x52, 0x4f, 0x4e}), readCallback), args{new(Document)}, true},
{"baseJSON", NewDecoder(bytes.NewBufferString("{\"buffers\": [{\"byteLength\": 1, \"URI\": \"a.bin\"}]}"), readCallback("abcdfg")), args{new(Document)}, false},
{"onlyGLBHeader", NewDecoder(bytes.NewBuffer([]byte{0x67, 0x6c, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00, 0x40, 0x0b, 0x00, 0x00, 0x5c, 0x06, 0x00, 0x00, 0x4a, 0x53, 0x4f, 0x4e}), readCallback("abcdfg")), args{new(Document)}, true},
{"glbMaxMemory", NewDecoder(bytes.NewBuffer([]byte{0x67, 0x6c, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00, 0x40, 0x0b, 0x00, 0x00, 0x5c, 0x06, 0x00, 0x00, 0x4a, 0x53, 0x4f, 0x4e}), readCallback("abcdfg")).SetQuotas(ReadQuotas{MaxMemoryAllocation: 0}), args{new(Document)}, true},
{"glbNoJSONChunk", NewDecoder(bytes.NewBuffer([]byte{0x67, 0x6c, 0x54, 0x46, 0x02, 0x00, 0x00, 0x00, 0x40, 0x0b, 0x00, 0x00, 0x5c, 0x06, 0x00, 0x00, 0x4a, 0x52, 0x4f, 0x4e}), readCallback("abcdfg")), args{new(Document)}, true},
{"empty", NewDecoder(bytes.NewBufferString(""), nil), args{new(Document)}, true},
{"invalidJSON", NewDecoder(bytes.NewBufferString("{asset: {}}"), nil), args{new(Document)}, true},
{"invalidBuffer", NewDecoder(bytes.NewBufferString("{\"buffers\": [{\"byteLength\": 0}]}"), nil), args{new(Document)}, true},
Expand Down

0 comments on commit a45caac

Please sign in to comment.