2012-06-20 21:21:32 +08:00
|
|
|
package bencode
|
|
|
|
|
2015-10-18 22:25:56 +08:00
|
|
|
import (
|
2017-11-08 17:58:35 +08:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2017-11-08 18:34:24 +08:00
|
|
|
"math/big"
|
2015-10-18 22:25:56 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
2012-06-20 21:21:32 +08:00
|
|
|
|
|
|
|
type random_encode_test struct {
|
|
|
|
value interface{}
|
|
|
|
expected string
|
|
|
|
}
|
|
|
|
|
|
|
|
type random_struct struct {
|
|
|
|
ABC int `bencode:"abc"`
|
|
|
|
SkipThisOne string `bencode:"-"`
|
|
|
|
CDE string
|
|
|
|
}
|
|
|
|
|
2012-06-24 19:10:27 +08:00
|
|
|
type dummy struct {
|
|
|
|
a, b, c int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *dummy) MarshalBencode() ([]byte, error) {
|
|
|
|
var b bytes.Buffer
|
2015-04-29 22:32:01 +08:00
|
|
|
_, err := fmt.Fprintf(&b, "i%dei%dei%de", d.a+1, d.b+1, d.c+1)
|
2012-06-24 19:10:27 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2012-06-20 21:21:32 +08:00
|
|
|
var random_encode_tests = []random_encode_test{
|
|
|
|
{int(10), "i10e"},
|
|
|
|
{uint(10), "i10e"},
|
|
|
|
{"hello, world", "12:hello, world"},
|
|
|
|
{true, "i1e"},
|
|
|
|
{false, "i0e"},
|
|
|
|
{int8(-8), "i-8e"},
|
|
|
|
{int16(-16), "i-16e"},
|
|
|
|
{int32(32), "i32e"},
|
|
|
|
{int64(-64), "i-64e"},
|
|
|
|
{uint8(8), "i8e"},
|
|
|
|
{uint16(16), "i16e"},
|
|
|
|
{uint32(32), "i32e"},
|
|
|
|
{uint64(64), "i64e"},
|
|
|
|
{random_struct{123, "nono", "hello"}, "d3:CDE5:hello3:abci123ee"},
|
|
|
|
{map[string]string{"a": "b", "c": "d"}, "d1:a1:b1:c1:de"},
|
|
|
|
{[]byte{1, 2, 3, 4}, "4:\x01\x02\x03\x04"},
|
|
|
|
{[4]byte{1, 2, 3, 4}, "li1ei2ei3ei4ee"},
|
|
|
|
{nil, ""},
|
|
|
|
{[]byte{}, "0:"},
|
2018-11-27 05:57:17 +08:00
|
|
|
{[]byte(nil), "0:"},
|
2012-06-20 21:21:32 +08:00
|
|
|
{"", "0:"},
|
|
|
|
{[]int{}, "le"},
|
|
|
|
{map[string]int{}, "de"},
|
2012-06-24 19:10:27 +08:00
|
|
|
{&dummy{1, 2, 3}, "i2ei3ei4e"},
|
2015-10-18 22:28:36 +08:00
|
|
|
{struct {
|
|
|
|
A *string
|
|
|
|
}{nil}, "d1:A0:e"},
|
|
|
|
{struct {
|
|
|
|
A *string
|
|
|
|
}{new(string)}, "d1:A0:e"},
|
|
|
|
{struct {
|
|
|
|
A *string `bencode:",omitempty"`
|
|
|
|
}{nil}, "de"},
|
|
|
|
{struct {
|
|
|
|
A *string `bencode:",omitempty"`
|
|
|
|
}{new(string)}, "d1:A0:e"},
|
2017-11-08 18:34:24 +08:00
|
|
|
{bigIntFromString("62208002200000000000"), "i62208002200000000000e"},
|
|
|
|
{*bigIntFromString("62208002200000000000"), "i62208002200000000000e"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func bigIntFromString(s string) *big.Int {
|
|
|
|
bi, ok := new(big.Int).SetString(s, 10)
|
|
|
|
if !ok {
|
|
|
|
panic(s)
|
|
|
|
}
|
|
|
|
return bi
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRandomEncode(t *testing.T) {
|
|
|
|
for _, test := range random_encode_tests {
|
|
|
|
data, err := Marshal(test.value)
|
2015-10-23 09:35:38 +08:00
|
|
|
assert.NoError(t, err, "%s", test)
|
2015-10-18 22:25:56 +08:00
|
|
|
assert.EqualValues(t, test.expected, string(data))
|
2012-06-20 21:21:32 +08:00
|
|
|
}
|
|
|
|
}
|