]> Sergey Matveev's repositories - btrtrc.git/blobdiff - bencode/decode_test.go
Drop support for go 1.20
[btrtrc.git] / bencode / decode_test.go
index 056a399a43ed1211151f3695f58e79d66e780ba1..4d05d2b332d77bedf6fc043076c62d306150dded 100644 (file)
@@ -2,6 +2,7 @@ package bencode
 
 import (
        "bytes"
+       "fmt"
        "io"
        "math/big"
        "reflect"
@@ -23,20 +24,23 @@ var random_decode_tests = []random_decode_test{
        {"5:hello", "hello"},
        {"29:unicode test проверка", "unicode test проверка"},
        {"d1:ai5e1:b5:helloe", map[string]interface{}{"a": int64(5), "b": "hello"}},
-       {"li5ei10ei15ei20e7:bencodee",
-               []interface{}{int64(5), int64(10), int64(15), int64(20), "bencode"}},
+       {
+               "li5ei10ei15ei20e7:bencodee",
+               []interface{}{int64(5), int64(10), int64(15), int64(20), "bencode"},
+       },
        {"ldedee", []interface{}{map[string]interface{}{}, map[string]interface{}{}}},
        {"le", []interface{}{}},
        {"i604919719469385652980544193299329427705624352086e", func() *big.Int {
                ret, _ := big.NewInt(-1).SetString("604919719469385652980544193299329427705624352086", 10)
                return ret
        }()},
-       {"d1:rd6:\xd4/\xe2F\x00\x01e1:t3:\x9a\x87\x011:v4:TR%=1:y1:re", map[string]interface{}{
-               "r": map[string]interface{}{},
+       {"d1:rd6:\xd4/\xe2F\x00\x01i42ee1:t3:\x9a\x87\x011:v4:TR%=1:y1:re", map[string]interface{}{
+               "r": map[string]interface{}{"\xd4/\xe2F\x00\x01": int64(42)},
                "t": "\x9a\x87\x01",
                "v": "TR%=",
                "y": "r",
        }},
+       {"d0:i420ee", map[string]interface{}{"": int64(420)}},
 }
 
 func TestRandomDecode(t *testing.T) {
@@ -135,7 +139,6 @@ func TestUnmarshalerBencode(t *testing.T) {
        assert_equal(t, ss[0].x, "5:hello")
        assert_equal(t, ss[1].x, "5:fruit")
        assert_equal(t, ss[2].x, "3:way")
-
 }
 
 func TestIgnoreUnmarshalTypeError(t *testing.T) {
@@ -174,3 +177,91 @@ func TestUnmarshalByteArray(t *testing.T) {
        assert.NoError(t, Unmarshal([]byte("2:hi"), &ba))
        assert.EqualValues(t, "hi", ba[:])
 }
+
+func TestDecodeDictIntoUnsupported(t *testing.T) {
+       // Any type that a dict shouldn't be unmarshallable into.
+       var i int
+       c := qt.New(t)
+       err := Unmarshal([]byte("d1:a1:be"), &i)
+       t.Log(err)
+       c.Check(err, qt.Not(qt.IsNil))
+}
+
+func TestUnmarshalDictKeyNotString(t *testing.T) {
+       // Any type that a dict shouldn't be unmarshallable into.
+       var i int
+       c := qt.New(t)
+       err := Unmarshal([]byte("di42e3:yese"), &i)
+       t.Log(err)
+       c.Check(err, qt.Not(qt.IsNil))
+}
+
+type arbitraryReader struct{}
+
+func (arbitraryReader) Read(b []byte) (int, error) {
+       return len(b), nil
+}
+
+func decodeHugeString(t *testing.T, strLen int64, header, tail string, v interface{}, maxStrLen MaxStrLen) error {
+       r, w := io.Pipe()
+       go func() {
+               fmt.Fprintf(w, header, strLen)
+               io.CopyN(w, arbitraryReader{}, strLen)
+               w.Write([]byte(tail))
+               w.Close()
+       }()
+       d := NewDecoder(r)
+       d.MaxStrLen = maxStrLen
+       return d.Decode(v)
+}
+
+// Ensure that bencode strings in various places obey the Decoder.MaxStrLen field.
+func TestDecodeMaxStrLen(t *testing.T) {
+       t.Parallel()
+       c := qt.New(t)
+       test := func(header, tail string, v interface{}, maxStrLen MaxStrLen) {
+               strLen := maxStrLen
+               if strLen == 0 {
+                       strLen = DefaultDecodeMaxStrLen
+               }
+               c.Assert(decodeHugeString(t, strLen, header, tail, v, maxStrLen), qt.IsNil)
+               c.Assert(decodeHugeString(t, strLen+1, header, tail, v, maxStrLen), qt.IsNotNil)
+       }
+       test("d%d:", "i0ee", new(interface{}), 0)
+       test("%d:", "", new(interface{}), DefaultDecodeMaxStrLen)
+       test("%d:", "", new([]byte), 1)
+       test("d3:420%d:", "e", new(struct {
+               Hi []byte `bencode:"420"`
+       }), 69)
+}
+
+// This is for the "github.com/anacrolix/torrent/metainfo".Info.Private field.
+func TestDecodeStringIntoBoolPtr(t *testing.T) {
+       var m struct {
+               Private *bool `bencode:"private,omitempty"`
+       }
+       c := qt.New(t)
+       check := func(msg string, expectNil, expectTrue bool) {
+               m.Private = nil
+               c.Check(Unmarshal([]byte(msg), &m), qt.IsNil, qt.Commentf("%q", msg))
+               if expectNil {
+                       c.Check(m.Private, qt.IsNil)
+               } else {
+                       if c.Check(m.Private, qt.IsNotNil, qt.Commentf("%q", msg)) {
+                               c.Check(*m.Private, qt.Equals, expectTrue, qt.Commentf("%q", msg))
+                       }
+               }
+       }
+       check("d7:privatei1ee", false, true)
+       check("d7:privatei0ee", false, false)
+       check("d7:privatei42ee", false, true)
+       // This is a weird case. We could not allocate the bool to indicate it was bad (maybe a bad
+       // serializer which isn't uncommon), but that requires reworking the decoder to handle
+       // automatically. I think if we cared enough we'd create a custom Unmarshaler. Also if we were
+       // worried enough about performance I'd completely rewrite this package.
+       check("d7:private0:e", false, false)
+       check("d7:private1:te", false, true)
+       check("d7:private5:falsee", false, false)
+       check("d7:private1:Fe", false, false)
+       check("d7:private11:bunnyfoofooe", false, true)
+}