Skip to content

Commit

Permalink
Negative tests for LineString/MultiLineString
Browse files Browse the repository at this point in the history
  • Loading branch information
shaxbee committed Apr 20, 2016
1 parent 70a0aab commit bd254ba
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 13 deletions.
5 changes: 4 additions & 1 deletion wkb/geometry.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ func readGeometry(b []byte) ([]byte, Geometry, error) {

switch kind {
case GeomPoint:
b, g.Value, err = readPoint(b, dec)
if len(b) < PointSize {
return nil, g, ErrInvalidStorage
}
b, g.Value = readPoint(b, dec)
case GeomLineString:
b, g.Value, err = readLineString(b, dec)
case GeomPolygon:
Expand Down
89 changes: 89 additions & 0 deletions wkb/linestring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,41 @@ import (
)

func TestLineString(t *testing.T) {
invalid := []struct {
err error
b []byte
}{
{
// invalid type
ErrUnsupportedValue,
[]byte{
0x01, 0x42, 0x00, 0x00, 0x00,
},
},
{
// no payload
ErrInvalidStorage,
[]byte{
0x01, 0x02, 0x00, 0x00, 0x00, // header
},
},
{
// no points
ErrInvalidStorage,
[]byte{
0x01, 0x02, 0x00, 0x00, 0x00, // header
0x01, 0x00, 0x00, 0x00, // numpoints - 1
},
},
}

for _, e := range invalid {
ls := LineString{}
if err := ls.Scan(e.b); assert.Error(t, err) {
assert.Exactly(t, e.err, err)
}
}

valid := []byte{
0x01, 0x02, 0x00, 0x00, 0x00, // header
0x03, 0x00, 0x00, 0x00, // numpoints - 3
Expand All @@ -29,6 +64,60 @@ func TestLineString(t *testing.T) {
}

func TestMultiLineString(t *testing.T) {
invalid := []struct {
err error
b []byte
}{
{
// invalid type
ErrUnsupportedValue,
[]byte{
0x01, 0x42, 0x00, 0x00, 0x00,
},
},
{
// no payload
ErrInvalidStorage,
[]byte{
0x01, 0x05, 0x00, 0x00, 0x00, // header
},
},
{
// no elements
ErrInvalidStorage,
[]byte{
0x01, 0x05, 0x00, 0x00, 0x00, // header
0x01, 0x00, 0x00, 0x00, // numlinestring - 1
},
},
{
//invalid element type
ErrUnsupportedValue,
[]byte{
0x01, 0x05, 0x00, 0x00, 0x00, // header
0x01, 0x00, 0x00, 0x00, // numlinestring - 2
0x01, 0x42, 0x00, 0x00, 0x00, // header - invalid type
0x00, 0x00, 0x00, 0x00, // numpoints - 0
},
},
{
// no payload in element
ErrInvalidStorage,
[]byte{
0x01, 0x05, 0x00, 0x00, 0x00, // header
0x01, 0x00, 0x00, 0x00, // numlinestring - 2
0x01, 0x02, 0x00, 0x00, 0x00, // header - invalid type
},
},
}

for _, e := range invalid {
mls := MultiLineString{}
if err := mls.Scan(e.b); assert.Error(t, err) {
assert.Exactly(t, e.err, err)
}
}

valid := []byte{
0x01, 0x05, 0x00, 0x00, 0x00, // header
0x02, 0x00, 0x00, 0x00, // numlinestring - 2
Expand Down
25 changes: 13 additions & 12 deletions wkb/point.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ func (p *Point) Scan(src interface{}) error {
return err
}

_, *p, err = readPoint(b, dec)
return err
if len(b) < PointSize {
return ErrInvalidStorage
}

_, *p = readPoint(b, dec)
return nil
}

func (mp *MultiPoint) Scan(src interface{}) error {
Expand All @@ -30,15 +34,11 @@ func (mp *MultiPoint) Scan(src interface{}) error {
return err
}

func readPoint(b []byte, dec binary.ByteOrder) ([]byte, Point, error) {
func readPoint(b []byte, dec binary.ByteOrder) ([]byte, Point) {
p := Point{}
if len(b) < PointSize {
return nil, p, ErrInvalidStorage
}

b, p.X = readFloat64(b, dec)
b, p.Y = readFloat64(b, dec)
return b, p, nil
return b, p
}

func readMultiPoint(b []byte, dec binary.ByteOrder) ([]byte, MultiPoint, error) {
Expand All @@ -47,17 +47,18 @@ func readMultiPoint(b []byte, dec binary.ByteOrder) ([]byte, MultiPoint, error)
return nil, nil, err
}

if len(b) < (HeaderSize+PointSize)*n {
return nil, nil, ErrInvalidStorage
}

mp := make([]Point, n)
for i := 0; i < n; i++ {
b, dec, err = byteHeader(b, GeomPoint)
if err != nil {
return nil, nil, err
}

b, mp[i], err = readPoint(b, dec)
if err != nil {
return nil, nil, err
}
b, mp[i] = readPoint(b, dec)
}

return b, mp, nil
Expand Down

0 comments on commit bd254ba

Please sign in to comment.