github.com/moreal/bencodex-go@v0.0.0-20231021172012-18277a477d15/encoder_test.go (about)

     1  package bencode
     2  
     3  import (
     4  	"runtime"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  var marshalTestData = map[string]interface{}{
    11  	"announce": []byte("udp://tracker.publicbt.com:80/announce"),
    12  	"announce-list": []interface{}{
    13  		[]interface{}{[]byte("udp://tracker.publicbt.com:80/announce")},
    14  		[]interface{}{[]byte("udp://tracker.openbittorrent.com:80/announce")},
    15  	},
    16  	"comment": []byte("Debian CD from cdimage.debian.org"),
    17  	"info": map[string]interface{}{
    18  		"name":         []byte("debian-8.8.0-arm64-netinst.iso"),
    19  		"length":       170917888,
    20  		"piece length": 262144,
    21  	},
    22  }
    23  
    24  func TestMarshal(t *testing.T) {
    25  	assert := assert.New(t)
    26  
    27  	result, err := Marshal(int64(38))
    28  	if !assert.NoError(err) || !assert.Equal([]byte("i38e"), result) {
    29  		return
    30  	}
    31  
    32  	result, err = Marshal([]byte("udp://tracker.publicbt.com:80/announce"))
    33  	if !assert.NoError(err) || !assert.Equal([]byte("38:udp://tracker.publicbt.com:80/announce"), result) {
    34  		return
    35  	}
    36  
    37  	result, err = Marshal([]interface{}{
    38  		[]interface{}{[]byte("udp://tracker.publicbt.com:80/announce")},
    39  		[]interface{}{[]byte("udp://tracker.openbittorrent.com:80/announce")},
    40  	})
    41  	if !assert.NoError(err) || !assert.Equal([]byte("ll38:udp://tracker.publicbt.com:80/announceel44:udp://tracker.openbittorrent.com:80/announceee"), result) {
    42  		return
    43  	}
    44  
    45  	result, err = Marshal(map[string]interface{}{
    46  		"announce": []byte("udp://tracker.publicbt.com:80/announce"),
    47  		"announce-list": []interface{}{
    48  			[]interface{}{[]byte("udp://tracker.publicbt.com:80/announce")},
    49  			[]interface{}{[]byte("udp://tracker.openbittorrent.com:80/announce")},
    50  		},
    51  	})
    52  	if !assert.NoError(err) || !assert.Equal([]byte("d8:announce38:udp://tracker.publicbt.com:80/announce13:announce-listll38:udp://tracker.publicbt.com:80/announceel44:udp://tracker.openbittorrent.com:80/announceeee"), result) {
    53  		return
    54  	}
    55  
    56  	result, err = Marshal(marshalTestData)
    57  	if !assert.NoError(err) || !assert.Equal(unmarshalTestData, result) {
    58  		return
    59  	}
    60  
    61  	result, err = MarshalTo(make([]byte, 1), marshalTestData)
    62  	if !assert.NoError(err) || !assert.Equal(unmarshalTestData, result) {
    63  		return
    64  	}
    65  
    66  	result, err = Marshal(nil)
    67  	if !assert.Error(err) || !assert.Nil(result) || !assert.Equal("bencode: unsupported type: <nil>", err.Error()) {
    68  		return
    69  	}
    70  }
    71  
    72  func TestMarshalUnOrderedDict(t *testing.T) {
    73  	assert := assert.New(t)
    74  	unorderedEncodedData := []byte(`d7:ttg_tag32:8032a74ec22927a5bd6367537eafd87e7:privatei1e6:source20:[ttys3.bencodex.test]e`)
    75  	expectedReencodedData := []byte(`d7:privatei1e6:source20:[ttys3.bencodex.test]7:ttg_tag32:8032a74ec22927a5bd6367537eafd87ee`)
    76  	data, err := Unmarshal(unorderedEncodedData)
    77  	assert.NoError(err)
    78  	//t.Logf("decoded data: %#v", data)
    79  	reencoded, err := Marshal(data)
    80  	assert.NoError(err)
    81  	assert.Equal(expectedReencodedData, reencoded)
    82  }
    83  
    84  func BenchmarkMarshal(b *testing.B) {
    85  	b.ReportAllocs()
    86  	var result []byte
    87  	b.Run("Marshal", func(b *testing.B) {
    88  		for n := 0; n < b.N; n++ {
    89  			result, _ = Marshal(marshalTestData)
    90  		}
    91  	})
    92  	b.Run("MarshalTo", func(b *testing.B) {
    93  		for n := 0; n < b.N; n++ {
    94  			result, _ = MarshalTo(result, marshalTestData)
    95  		}
    96  	})
    97  	runtime.KeepAlive(result)
    98  }