Skip to content

Commit

Permalink
GODRIVER-246 Add stack trace to ErrTooSmall
Browse files Browse the repository at this point in the history
Change-Id: I319c17b94791c4e8426e63c59eb31f9d4c0ec4a7
  • Loading branch information
saghm committed Feb 22, 2018
1 parent 1c1456b commit f61505c
Show file tree
Hide file tree
Showing 16 changed files with 601 additions and 153 deletions.
27 changes: 27 additions & 0 deletions THIRD-PARTY-NOTICES
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,30 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

----------------------------------------------------------------------
License notice for github.com/go-stack/stack
----------------------------------------------------------------------

The MIT License (MIT)

Copyright (c) 2014 Chris Hines

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

2 changes: 1 addition & 1 deletion bson/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func (a *Array) writeByteSlice(start uint, size uint32, b []byte) (int64, error)
var pos = start

if len(b) < int(start)+int(size) {
return 0, ErrTooSmall
return 0, NewErrTooSmall()
}
n, err := elements.Int32.Encode(start, b, int32(size))
total += int64(n)
Expand Down
6 changes: 3 additions & 3 deletions bson/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (d *Decoder) Decode(v interface{}) error {
}

if len(t) < int(length) {
return ErrTooSmall
return NewErrTooSmall()
}

_, err = io.ReadFull(d.pReader, t)
Expand All @@ -153,7 +153,7 @@ func (d *Decoder) Decode(v interface{}) error {
}

if len(t) < int(length) {
return ErrTooSmall
return NewErrTooSmall()
}

_, err = io.ReadAtLeast(d.pReader, t, int(length))
Expand Down Expand Up @@ -674,7 +674,7 @@ func (d *Decoder) decodeIntoElementSlice(sliceVal reflect.Value) error {
i := 0
for itr.Next() {
if i >= sliceLength {
return ErrTooSmall
return NewErrTooSmall()
}

elem := reflect.ValueOf(itr.Element().Clone())
Expand Down
52 changes: 35 additions & 17 deletions bson/decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,24 @@ import (
"github.com/stretchr/testify/require"
)

func requireErrEqual(t *testing.T, err1 error, err2 error) {
switch e := err1.(type) {
case ErrTooSmall:
require.True(t, e.Equals(err2))

return
}

switch e := err2.(type) {
case ErrTooSmall:
require.True(t, e.Equals(e))

return
}

require.Equal(t, err1, err2)
}

func TestDecoder(t *testing.T) {
t.Run("byte slice", func(t *testing.T) {
testCases := []struct {
Expand All @@ -29,14 +47,14 @@ func TestDecoder(t *testing.T) {
bytes.NewBuffer([]byte{0x5, 0x0, 0x0, 0x0, 0x0}),
nil,
nil,
ErrTooSmall,
NewErrTooSmall(),
},
{
"empty slice",
bytes.NewBuffer([]byte{0x5, 0x0, 0x0, 0x0}),
nil,
[]byte{},
ErrTooSmall,
NewErrTooSmall(),
},
{
"too small",
Expand All @@ -45,7 +63,7 @@ func TestDecoder(t *testing.T) {
}),
nil,
make([]byte, 0x4),
ErrTooSmall,
NewErrTooSmall(),
},
{
"empty doc",
Expand Down Expand Up @@ -110,7 +128,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand All @@ -133,14 +151,14 @@ func TestDecoder(t *testing.T) {
bytes.NewBuffer([]byte{0x5, 0x0, 0x0, 0x0, 0x0}),
nil,
nil,
ErrTooSmall,
NewErrTooSmall(),
},
{
"empty slice",
bytes.NewBuffer([]byte{0x5, 0x0, 0x0, 0x0}),
nil,
[]byte{},
ErrTooSmall,
NewErrTooSmall(),
},
{
"too small",
Expand All @@ -149,7 +167,7 @@ func TestDecoder(t *testing.T) {
}),
nil,
make([]byte, 0x4),
ErrTooSmall,
NewErrTooSmall(),
},
{
"empty doc",
Expand Down Expand Up @@ -214,7 +232,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand Down Expand Up @@ -297,7 +315,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand Down Expand Up @@ -410,7 +428,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand Down Expand Up @@ -525,7 +543,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand Down Expand Up @@ -591,7 +609,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand Down Expand Up @@ -770,7 +788,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand Down Expand Up @@ -970,7 +988,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand Down Expand Up @@ -1195,7 +1213,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand Down Expand Up @@ -1498,7 +1516,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand Down Expand Up @@ -1767,7 +1785,7 @@ func TestDecoder(t *testing.T) {
d := NewDecoder(tc.reader)

err := d.Decode(tc.actual)
require.Equal(t, tc.err, err)
requireErrEqual(t, tc.err, err)
if err != nil {
return
}
Expand Down
2 changes: 1 addition & 1 deletion bson/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ func (d *Document) writeByteSlice(start uint, size uint32, b []byte) (int64, err
var total int64
var pos = start
if len(b) < int(start)+int(size) {
return 0, ErrTooSmall
return 0, NewErrTooSmall()
}
n, err := elements.Int32.Encode(start, b, int32(size))
total += int64(n)
Expand Down
26 changes: 13 additions & 13 deletions bson/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
func TestDocument(t *testing.T) {
t.Run("NewDocument", func(t *testing.T) {
t.Run("TooShort", func(t *testing.T) {
want := ErrTooSmall
want := NewErrTooSmall()
_, got := ReadDocument([]byte{'\x00', '\x00'})
if got != want {
if !want.Equals(got) {
t.Errorf("Did not get expected error. got %#v; want %#v", got, want)
}
})
Expand Down Expand Up @@ -57,12 +57,12 @@ func TestDocument(t *testing.T) {
}
})
t.Run("validateValue-error", func(t *testing.T) {
want := ErrTooSmall
want := NewErrTooSmall()
b := make([]byte, 11)
binary.LittleEndian.PutUint32(b[0:4], 11)
b[4], b[5], b[6], b[7], b[8], b[9], b[10] = '\x01', 'f', 'o', 'o', '\x00', '\x01', '\x02'
_, got := ReadDocument(b)
if got != want {
if !want.Equals(got) {
t.Errorf("Did not get expected error. got %#v; want %#v", got, want)
}
})
Expand Down Expand Up @@ -735,24 +735,24 @@ func TestDocument(t *testing.T) {
d.elems[0].value.data = d.elems[0].value.data[:3]
b := make([]byte, 15)
_, err := d.WriteDocument(0, b)
if err != ErrTooSmall {
t.Errorf("Expected error not returned. got %s; want %s", err, ErrTooSmall)
if !NewErrTooSmall().Equals(err) {
t.Errorf("Expected error not returned. got %s; want %s", err, NewErrTooSmall())
}
})
t.Run("[]byte-too-small", func(t *testing.T) {
d := NewDocument(EC.Double("", 3.14159))
b := make([]byte, 5)
_, err := d.WriteDocument(0, b)
if err != ErrTooSmall {
t.Errorf("Expected error not returned. got %s; want %s", err, ErrTooSmall)
if !NewErrTooSmall().Equals(err) {
t.Errorf("Expected error not returned. got %s; want %s", err, NewErrTooSmall())
}
})
t.Run("invalid-writer", func(t *testing.T) {
d := NewDocument(EC.Double("", 3.14159))
var buf bytes.Buffer
_, err := d.WriteDocument(0, buf)
if err != ErrInvalidWriter {
t.Errorf("Expected error not returned. got %s; want %s", err, ErrTooSmall)
t.Errorf("Expected error not returned. got %s; want %s", err, NewErrTooSmall())
}
})

Expand Down Expand Up @@ -840,8 +840,8 @@ func TestDocument(t *testing.T) {
t.Errorf("Unexpected error while writing document to buffer: %s", err)
}
_, err = NewDocument().ReadFrom(&buf)
if err != ErrTooSmall {
t.Errorf("Expected error not returned. got %s; want %s", err, ErrTooSmall)
if !NewErrTooSmall().Equals(err) {
t.Errorf("Expected error not returned. got %s; want %s", err, NewErrTooSmall())
}
})
testCases := []struct {
Expand Down Expand Up @@ -902,7 +902,7 @@ func testDocumentKeys(t *testing.T) {
// '\x0B', '\x00', '\x00', '\x00', '\x01', '1', '\x00',
// '\x0A', '2', '\x00', '\x00', '\x00',
// },
// nil, ErrTooSmall, true,
// nil, NewErrTooSmall(), true,
// },
// {"invalid-array",
// Reader{
Expand All @@ -912,7 +912,7 @@ func testDocumentKeys(t *testing.T) {
// '\x0B', '\x00', '\x00', '\x00', '\x01', '1', '\x00',
// '\x0A', '2', '\x00', '\x00', '\x00',
// },
// nil, ErrTooSmall, true,
// nil, NewErrTooSmall(), true,
// },
}

Expand Down
Loading

0 comments on commit f61505c

Please sign in to comment.