FedP2P/bencode/both_test.go

77 lines
1.8 KiB
Go
Raw Normal View History

2012-06-20 21:21:32 +08:00
package bencode
2016-02-23 18:47:00 +08:00
import (
"bytes"
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2012-06-20 21:21:32 +08:00
func loadFile(name string, t *testing.T) []byte {
2012-06-20 21:21:32 +08:00
data, err := ioutil.ReadFile(name)
2016-02-23 18:47:00 +08:00
require.NoError(t, err)
2012-06-20 21:21:32 +08:00
return data
}
func testFileInterface(t *testing.T, filename string) {
data1 := loadFile(filename, t)
2012-06-20 21:21:32 +08:00
2016-02-23 18:47:00 +08:00
var iface interface{}
2012-06-20 21:21:32 +08:00
err := Unmarshal(data1, &iface)
2016-02-23 18:47:00 +08:00
require.NoError(t, err)
2012-06-20 21:21:32 +08:00
data2, err := Marshal(iface)
2016-02-23 18:47:00 +08:00
require.NoError(t, err)
2016-02-23 18:47:00 +08:00
assert.EqualValues(t, data1, data2)
}
func TestBothInterface(t *testing.T) {
testFileInterface(t, "testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
testFileInterface(t, "testdata/continuum.torrent")
2012-06-20 21:21:32 +08:00
}
type torrentFile struct {
2012-06-20 21:21:32 +08:00
Info struct {
Name string `bencode:"name"`
Length int64 `bencode:"length"`
MD5Sum string `bencode:"md5sum,omitempty"`
PieceLength int64 `bencode:"piece length"`
Pieces string `bencode:"pieces"`
Private bool `bencode:"private,omitempty"`
} `bencode:"info"`
Announce string `bencode:"announce"`
AnnounceList [][]string `bencode:"announce-list,omitempty"`
CreationDate int64 `bencode:"creation date,omitempty"`
Comment string `bencode:"comment,omitempty"`
CreatedBy string `bencode:"created by,omitempty"`
URLList interface{} `bencode:"url-list,omitempty"`
}
func testFile(t *testing.T, filename string) {
data1 := loadFile(filename, t)
var f torrentFile
2012-06-20 21:21:32 +08:00
err := Unmarshal(data1, &f)
if err != nil {
t.Fatal(err)
}
data2, err := Marshal(&f)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(data1, data2) {
println(string(data2))
2012-06-20 21:21:32 +08:00
t.Fatalf("equality expected")
}
}
func TestBoth(t *testing.T) {
testFile(t, "testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
}