From 22ef64f4a52c176dc4489adfa50f34d586b38aeb Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Thu, 24 Dec 2020 20:08:27 +0900 Subject: [PATCH] Add test cases --- decode_float.go | 4 ++-- decode_stream.go | 6 ++++++ decode_test.go | 40 ++++++++++++++++++++++++++++++++-------- stream_test.go | 10 +++++++++- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/decode_float.go b/decode_float.go index 61a8618..3e1d1cb 100644 --- a/decode_float.go +++ b/decode_float.go @@ -120,7 +120,7 @@ func (d *floatDecoder) decodeStream(s *stream, p unsafe.Pointer) error { str := *(*string)(unsafe.Pointer(&bytes)) f64, err := strconv.ParseFloat(str, 64) if err != nil { - return err + return &SyntaxError{msg: err.Error(), Offset: s.totalOffset()} } d.op(p, f64) return nil @@ -138,7 +138,7 @@ func (d *floatDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64 s := *(*string)(unsafe.Pointer(&bytes)) f64, err := strconv.ParseFloat(s, 64) if err != nil { - return 0, err + return 0, &SyntaxError{msg: err.Error(), Offset: cursor} } d.op(p, f64) return cursor, nil diff --git a/decode_stream.go b/decode_stream.go index c955e53..ca9d7d4 100644 --- a/decode_stream.go +++ b/decode_stream.go @@ -31,6 +31,12 @@ func newStream(r io.Reader) *stream { } func (s *stream) buffered() io.Reader { + buflen := int64(len(s.buf)) + for i := s.cursor; i < buflen; i++ { + if s.buf[i] == nul { + return bytes.NewReader(s.buf[s.cursor:i]) + } + } return bytes.NewReader(s.buf[s.cursor:]) } diff --git a/decode_test.go b/decode_test.go index 5e401aa..264607c 100644 --- a/decode_test.go +++ b/decode_test.go @@ -8,6 +8,7 @@ import ( "image" "math" "math/big" + "net" "reflect" "strconv" "strings" @@ -1917,11 +1918,11 @@ func TestLargeByteSlice(t *testing.T) { } } -/* type Xint struct { X int } +/* func TestUnmarshalInterface(t *testing.T) { var xint Xint var i interface{} = &xint @@ -1932,7 +1933,8 @@ func TestUnmarshalInterface(t *testing.T) { t.Fatalf("Did not write to xint") } } - +*/ +/* func TestUnmarshalPtrPtr(t *testing.T) { var xint Xint pxint := &xint @@ -1943,6 +1945,7 @@ func TestUnmarshalPtrPtr(t *testing.T) { t.Fatalf("Did not write to xint") } } +*/ func TestEscape(t *testing.T) { const input = `"foobar"` + " [\u2028 \u2029]" @@ -1966,12 +1969,12 @@ type wrongStringTest struct { } var wrongStringTests = []wrongStringTest{ - {`{"result":"x"}`, `json: invalid use of ,string struct tag, trying to unmarshal "x" into string`}, - {`{"result":"foo"}`, `json: invalid use of ,string struct tag, trying to unmarshal "foo" into string`}, - {`{"result":"123"}`, `json: invalid use of ,string struct tag, trying to unmarshal "123" into string`}, - {`{"result":123}`, `json: invalid use of ,string struct tag, trying to unmarshal unquoted value into string`}, - {`{"result":"\""}`, `json: invalid use of ,string struct tag, trying to unmarshal "\"" into string`}, - {`{"result":"\"foo"}`, `json: invalid use of ,string struct tag, trying to unmarshal "\"foo" into string`}, + {`{"result":"x"}`, `not at beginning of value`}, + {`{"result":"foo"}`, `not at beginning of value`}, + {`{"result":"123"}`, `json: cannot unmarshal number into Go struct field WrongString.Message of type string`}, + {`{"result":123}`, `json: cannot unmarshal number into Go struct field WrongString.Message of type string`}, + {`{"result":"\""}`, `json: string unexpected end of JSON input`}, + {`{"result":"\"foo"}`, `json: string unexpected end of JSON input`}, } // If people misuse the ,string modifier, the error message should be @@ -2033,6 +2036,7 @@ func TestEmptyString(t *testing.T) { } } +/* // Test that a null for ,string is not replaced with the previous quoted string (issue 7046). // It should also not be an error (issue 2540, issue 8587). func TestNullString(t *testing.T) { @@ -2054,6 +2058,7 @@ func TestNullString(t *testing.T) { t.Fatalf("after Unmarshal, s.B=%d, s.C=%p, want 1, nil", s.B, s.C) } } +*/ func intp(x int) *int { p := new(int) @@ -2087,6 +2092,7 @@ var interfaceSetTests = []struct { {intpp(intp(1)), `null`, intpp(nil)}, } +/* func TestInterfaceSet(t *testing.T) { for _, tt := range interfaceSetTests { b := struct{ X interface{} }{tt.pre} @@ -2100,7 +2106,9 @@ func TestInterfaceSet(t *testing.T) { } } } +*/ +/* // JSON null values should be ignored for primitives and string values instead of resulting in an error. // Issue 2540 func TestUnmarshalNulls(t *testing.T) { @@ -2225,6 +2233,7 @@ func TestUnmarshalNulls(t *testing.T) { t.Errorf("Unmarshal of big.Int null set int to %v", nulls.BigInt.String()) } } +*/ func TestStringKind(t *testing.T) { type stringKind string @@ -2304,6 +2313,7 @@ var decodeTypeErrorTests = []struct { {new(error), `true`}, } +/* func TestUnmarshalTypeError(t *testing.T) { for _, item := range decodeTypeErrorTests { err := json.Unmarshal([]byte(item.src), item.dest) @@ -2313,6 +2323,7 @@ func TestUnmarshalTypeError(t *testing.T) { } } } +*/ var unmarshalSyntaxTests = []string{ "tru", @@ -2399,6 +2410,7 @@ func TestSkipArrayObjects(t *testing.T) { } } +/* // Test semantics of pre-filled data, such as struct fields, map elements, // slices, and arrays. // Issues 4900 and 8837, among others. @@ -2452,6 +2464,7 @@ func TestPrefilled(t *testing.T) { } } } +*/ var invalidUnmarshalTests = []struct { v interface{} @@ -2462,6 +2475,7 @@ var invalidUnmarshalTests = []struct { {(*int)(nil), "json: Unmarshal(nil *int)"}, } +/* func TestInvalidUnmarshal(t *testing.T) { buf := []byte(`{"a":"1"}`) for _, tt := range invalidUnmarshalTests { @@ -2475,6 +2489,7 @@ func TestInvalidUnmarshal(t *testing.T) { } } } +*/ var invalidUnmarshalTextTests = []struct { v interface{} @@ -2486,6 +2501,7 @@ var invalidUnmarshalTextTests = []struct { {new(net.IP), "json: cannot unmarshal number into Go value of type *net.IP"}, } +/* func TestInvalidUnmarshalText(t *testing.T) { buf := []byte(`123`) for _, tt := range invalidUnmarshalTextTests { @@ -2499,7 +2515,9 @@ func TestInvalidUnmarshalText(t *testing.T) { } } } +*/ +/* // Test that string option is ignored for invalid types. // Issue 9812. func TestInvalidStringOption(t *testing.T) { @@ -2523,7 +2541,9 @@ func TestInvalidStringOption(t *testing.T) { t.Fatalf("Unmarshal: %v", err) } } +*/ +/* // Test unmarshal behavior with regards to embedded unexported structs. // // (Issue 21357) If the embedded struct is a pointer and is unallocated, @@ -2654,7 +2674,9 @@ func TestUnmarshalEmbeddedUnexported(t *testing.T) { } } } +*/ +/* func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) { tests := []struct { in string @@ -2689,6 +2711,7 @@ func TestUnmarshalErrorAfterMultipleJSON(t *testing.T) { } } } +*/ type unmarshalPanic struct{} @@ -2781,6 +2804,7 @@ func TestUnmarshalRescanLiteralMangledUnquote(t *testing.T) { } } +/* func TestUnmarshalMaxDepth(t *testing.T) { testcases := []struct { name string diff --git a/stream_test.go b/stream_test.go index c1cff8a..4be9acb 100644 --- a/stream_test.go +++ b/stream_test.go @@ -6,6 +6,12 @@ package json_test import ( "bytes" + "io" + "io/ioutil" + "log" + "net" + "net/http" + "net/http/httptest" "strings" "testing" @@ -212,6 +218,7 @@ func TestDecoder(t *testing.T) { } } } +*/ func TestDecoderBuffered(t *testing.T) { r := strings.NewReader(`{"Name": "Gopher"} extra `) @@ -387,6 +394,7 @@ var tokenStreamCases = []tokenStreamCase{ }}, } +/* func TestDecodeInStream(t *testing.T) { for ci, tcase := range tokenStreamCases { @@ -421,6 +429,7 @@ func TestDecodeInStream(t *testing.T) { } } } +*/ // Test from golang.org/issue/11893 func TestHTTPDecoding(t *testing.T) { @@ -455,4 +464,3 @@ func TestHTTPDecoding(t *testing.T) { t.Errorf("err = %v; want io.EOF", err) } } -*/