]> Sergey Matveev's repositories - btrtrc.git/blobdiff - bencode/encode_test.go
Handle torrentfs failure in test.sh
[btrtrc.git] / bencode / encode_test.go
index de89542b3352d38adf6894b74b303b77dd36fbfc..b0fabc45b4947a2ea4524b5be94950acf20e2eae 100644 (file)
@@ -1,7 +1,13 @@
 package bencode
 
-import "testing"
-import "bytes"
+import (
+       "bytes"
+       "fmt"
+       "math/big"
+       "testing"
+
+       "github.com/stretchr/testify/assert"
+)
 
 type random_encode_test struct {
        value    interface{}
@@ -14,6 +20,19 @@ type random_struct struct {
        CDE         string
 }
 
+type dummy struct {
+       a, b, c int
+}
+
+func (d *dummy) MarshalBencode() ([]byte, error) {
+       var b bytes.Buffer
+       _, err := fmt.Fprintf(&b, "i%dei%dei%de", d.a+1, d.b+1, d.c+1)
+       if err != nil {
+               return nil, err
+       }
+       return b.Bytes(), nil
+}
+
 var random_encode_tests = []random_encode_test{
        {int(10), "i10e"},
        {uint(10), "i10e"},
@@ -31,23 +50,42 @@ var random_encode_tests = []random_encode_test{
        {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"},
+       {&[4]byte{1, 2, 3, 4}, "4:\x01\x02\x03\x04"},
        {nil, ""},
        {[]byte{}, "0:"},
+       {[]byte(nil), "0:"},
        {"", "0:"},
        {[]int{}, "le"},
        {map[string]int{}, "de"},
+       {&dummy{1, 2, 3}, "i2ei3ei4e"},
+       {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"},
+       {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
 }
 
 func TestRandomEncode(t *testing.T) {
        for _, test := range random_encode_tests {
                data, err := Marshal(test.value)
-               if err != nil {
-                       t.Fatal(err)
-               }
-               if !bytes.Equal(data, []byte(test.expected)) {
-                       t.Errorf("got: %s, expected: %s\n",
-                               string(data), string(test.expected))
-               }
+               assert.NoError(t, err, "%s", test)
+               assert.EqualValues(t, test.expected, string(data))
        }
 }