2012-06-20 21:21:32 +08:00
|
|
|
package bencode
|
|
|
|
|
2015-08-23 14:13:30 +08:00
|
|
|
import (
|
|
|
|
"bytes"
|
2022-01-07 16:05:03 +08:00
|
|
|
"fmt"
|
2015-08-23 14:13:30 +08:00
|
|
|
"io"
|
2015-09-20 19:08:42 +08:00
|
|
|
"math/big"
|
2015-08-23 14:13:30 +08:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2021-05-21 21:50:29 +08:00
|
|
|
qt "github.com/frankban/quicktest"
|
2015-10-01 22:13:43 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-08-23 14:13:30 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
2012-06-20 21:21:32 +08:00
|
|
|
|
|
|
|
type random_decode_test struct {
|
|
|
|
data string
|
|
|
|
expected interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var random_decode_tests = []random_decode_test{
|
|
|
|
{"i57e", int64(57)},
|
|
|
|
{"i-9223372036854775808e", int64(-9223372036854775808)},
|
|
|
|
{"5:hello", "hello"},
|
|
|
|
{"29:unicode test проверка", "unicode test проверка"},
|
|
|
|
{"d1:ai5e1:b5:helloe", map[string]interface{}{"a": int64(5), "b": "hello"}},
|
2021-11-08 11:47:01 +08:00
|
|
|
{
|
|
|
|
"li5ei10ei15ei20e7:bencodee",
|
|
|
|
[]interface{}{int64(5), int64(10), int64(15), int64(20), "bencode"},
|
|
|
|
},
|
2012-06-22 21:34:26 +08:00
|
|
|
{"ldedee", []interface{}{map[string]interface{}{}, map[string]interface{}{}}},
|
|
|
|
{"le", []interface{}{}},
|
2015-09-20 19:08:42 +08:00
|
|
|
{"i604919719469385652980544193299329427705624352086e", func() *big.Int {
|
|
|
|
ret, _ := big.NewInt(-1).SetString("604919719469385652980544193299329427705624352086", 10)
|
|
|
|
return ret
|
|
|
|
}()},
|
2021-12-10 11:05:00 +08:00
|
|
|
{"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)},
|
2015-10-01 22:13:43 +08:00
|
|
|
"t": "\x9a\x87\x01",
|
|
|
|
"v": "TR%=",
|
|
|
|
"y": "r",
|
|
|
|
}},
|
2022-01-07 16:11:37 +08:00
|
|
|
{"d0:i420ee", map[string]interface{}{"": int64(420)}},
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRandomDecode(t *testing.T) {
|
|
|
|
for _, test := range random_decode_tests {
|
|
|
|
var value interface{}
|
|
|
|
err := Unmarshal([]byte(test.data), &value)
|
|
|
|
if err != nil {
|
2015-08-23 14:13:30 +08:00
|
|
|
t.Error(err, test.data)
|
2012-06-20 21:21:32 +08:00
|
|
|
continue
|
|
|
|
}
|
2015-10-01 22:13:43 +08:00
|
|
|
assert.EqualValues(t, test.expected, value)
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-28 04:21:26 +08:00
|
|
|
|
2015-08-23 14:13:30 +08:00
|
|
|
func TestLoneE(t *testing.T) {
|
|
|
|
var v int
|
|
|
|
err := Unmarshal([]byte("e"), &v)
|
|
|
|
se := err.(*SyntaxError)
|
|
|
|
require.EqualValues(t, 0, se.Offset)
|
|
|
|
}
|
|
|
|
|
2015-08-23 17:24:29 +08:00
|
|
|
func TestDecoderConsecutive(t *testing.T) {
|
2015-08-23 14:13:30 +08:00
|
|
|
d := NewDecoder(bytes.NewReader([]byte("i1ei2e")))
|
2015-08-23 17:24:29 +08:00
|
|
|
var i int
|
|
|
|
err := d.Decode(&i)
|
2015-08-23 14:13:30 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 1, i)
|
|
|
|
err = d.Decode(&i)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 2, i)
|
|
|
|
err = d.Decode(&i)
|
|
|
|
require.Equal(t, io.EOF, err)
|
|
|
|
}
|
|
|
|
|
2017-10-26 11:25:59 +08:00
|
|
|
func TestDecoderConsecutiveDicts(t *testing.T) {
|
|
|
|
bb := bytes.NewBufferString("d4:herp4:derped3:wat1:ke17:oh baby a triple!")
|
2017-11-05 12:38:09 +08:00
|
|
|
|
2017-10-26 11:25:59 +08:00
|
|
|
d := NewDecoder(bb)
|
2017-11-05 12:38:09 +08:00
|
|
|
assert.EqualValues(t, "d4:herp4:derped3:wat1:ke17:oh baby a triple!", bb.Bytes())
|
2017-11-05 12:42:37 +08:00
|
|
|
assert.EqualValues(t, 0, d.Offset)
|
2017-11-05 12:38:09 +08:00
|
|
|
|
2017-10-26 11:25:59 +08:00
|
|
|
var m map[string]interface{}
|
2017-11-05 12:38:09 +08:00
|
|
|
|
2017-10-26 11:25:59 +08:00
|
|
|
require.NoError(t, d.Decode(&m))
|
|
|
|
assert.Len(t, m, 1)
|
|
|
|
assert.Equal(t, "derp", m["herp"])
|
2017-11-05 12:38:09 +08:00
|
|
|
assert.Equal(t, "d3:wat1:ke17:oh baby a triple!", bb.String())
|
2017-11-05 12:42:37 +08:00
|
|
|
assert.EqualValues(t, 14, d.Offset)
|
2017-11-05 12:38:09 +08:00
|
|
|
|
2017-10-26 11:25:59 +08:00
|
|
|
require.NoError(t, d.Decode(&m))
|
|
|
|
assert.Equal(t, "k", m["wat"])
|
2017-11-05 12:38:09 +08:00
|
|
|
assert.Equal(t, "17:oh baby a triple!", bb.String())
|
2017-11-05 12:42:37 +08:00
|
|
|
assert.EqualValues(t, 24, d.Offset)
|
2017-11-05 12:38:09 +08:00
|
|
|
|
2017-10-26 11:25:59 +08:00
|
|
|
var s string
|
|
|
|
require.NoError(t, d.Decode(&s))
|
|
|
|
assert.Equal(t, "oh baby a triple!", s)
|
2017-11-05 12:42:37 +08:00
|
|
|
assert.EqualValues(t, 44, d.Offset)
|
2017-10-26 11:25:59 +08:00
|
|
|
}
|
|
|
|
|
2012-06-28 04:21:26 +08:00
|
|
|
func check_error(t *testing.T, err error) {
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func assert_equal(t *testing.T, x, y interface{}) {
|
|
|
|
if !reflect.DeepEqual(x, y) {
|
|
|
|
t.Errorf("got: %v (%T), expected: %v (%T)\n", x, x, y, y)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 08:09:57 +08:00
|
|
|
type unmarshalerInt struct {
|
2012-06-28 04:21:26 +08:00
|
|
|
x int
|
|
|
|
}
|
|
|
|
|
2020-02-20 08:09:57 +08:00
|
|
|
func (me *unmarshalerInt) UnmarshalBencode(data []byte) error {
|
|
|
|
return Unmarshal(data, &me.x)
|
2012-06-28 04:21:26 +08:00
|
|
|
}
|
|
|
|
|
2020-02-20 08:09:57 +08:00
|
|
|
type unmarshalerString struct {
|
2012-06-28 04:21:26 +08:00
|
|
|
x string
|
|
|
|
}
|
|
|
|
|
2020-02-20 08:09:57 +08:00
|
|
|
func (me *unmarshalerString) UnmarshalBencode(data []byte) error {
|
|
|
|
me.x = string(data)
|
2012-06-28 04:21:26 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalerBencode(t *testing.T) {
|
2020-02-20 08:09:57 +08:00
|
|
|
var i unmarshalerInt
|
|
|
|
var ss []unmarshalerString
|
2012-06-28 04:21:26 +08:00
|
|
|
check_error(t, Unmarshal([]byte("i71e"), &i))
|
|
|
|
assert_equal(t, i.x, 71)
|
|
|
|
check_error(t, Unmarshal([]byte("l5:hello5:fruit3:waye"), &ss))
|
|
|
|
assert_equal(t, ss[0].x, "5:hello")
|
|
|
|
assert_equal(t, ss[1].x, "5:fruit")
|
|
|
|
assert_equal(t, ss[2].x, "3:way")
|
|
|
|
}
|
2018-01-27 11:31:12 +08:00
|
|
|
|
|
|
|
func TestIgnoreUnmarshalTypeError(t *testing.T) {
|
|
|
|
s := struct {
|
|
|
|
Ignore int `bencode:",ignore_unmarshal_type_error"`
|
|
|
|
Normal int
|
|
|
|
}{}
|
|
|
|
require.Error(t, Unmarshal([]byte("d6:Normal5:helloe"), &s))
|
2018-07-23 11:12:14 +08:00
|
|
|
assert.NoError(t, Unmarshal([]byte("d6:Ignore5:helloe"), &s))
|
2021-05-21 21:50:29 +08:00
|
|
|
qt.Assert(t, Unmarshal([]byte("d6:Ignorei42ee"), &s), qt.IsNil)
|
2018-01-27 11:31:12 +08:00
|
|
|
assert.EqualValues(t, 42, s.Ignore)
|
|
|
|
}
|
2018-02-12 21:44:05 +08:00
|
|
|
|
|
|
|
// Test unmarshalling []byte into something that has the same kind but
|
|
|
|
// different type.
|
|
|
|
func TestDecodeCustomSlice(t *testing.T) {
|
|
|
|
type flag byte
|
|
|
|
var fs3, fs2 []flag
|
|
|
|
// We do a longer slice then a shorter slice to see if the buffers are
|
|
|
|
// shared.
|
|
|
|
d := NewDecoder(bytes.NewBufferString("3:\x01\x10\xff2:\x04\x0f"))
|
|
|
|
require.NoError(t, d.Decode(&fs3))
|
|
|
|
require.NoError(t, d.Decode(&fs2))
|
|
|
|
assert.EqualValues(t, []flag{1, 16, 255}, fs3)
|
|
|
|
assert.EqualValues(t, []flag{4, 15}, fs2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalUnusedBytes(t *testing.T) {
|
|
|
|
var i int
|
|
|
|
require.EqualValues(t, ErrUnusedTrailingBytes{1}, Unmarshal([]byte("i42ee"), &i))
|
|
|
|
assert.EqualValues(t, 42, i)
|
|
|
|
}
|
2018-07-11 13:22:29 +08:00
|
|
|
|
|
|
|
func TestUnmarshalByteArray(t *testing.T) {
|
|
|
|
var ba [2]byte
|
|
|
|
assert.NoError(t, Unmarshal([]byte("2:hi"), &ba))
|
|
|
|
assert.EqualValues(t, "hi", ba[:])
|
|
|
|
}
|
2021-08-12 10:16:53 +08:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
2021-08-12 11:46:02 +08:00
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
2022-01-07 16:05:03 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|