7 func load_file(name string, t *testing.T) []byte {
8 data, err := ioutil.ReadFile(name)
15 func test_file_interface(t *testing.T, filename string) {
16 data1 := load_file(filename, t)
19 err := Unmarshal(data1, &iface)
24 data2, err := Marshal(iface)
29 if !bytes.Equal(data1, data2) {
30 t.Fatalf("equality expected\n")
35 func TestBothInterface(t *testing.T) {
36 test_file_interface(t, "_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
37 test_file_interface(t, "_testdata/continuum.torrent")
40 type torrent_file struct {
42 Name string `bencode:"name"`
43 Length int64 `bencode:"length"`
44 MD5Sum string `bencode:"md5sum,omitempty"`
45 PieceLength int64 `bencode:"piece length"`
46 Pieces string `bencode:"pieces"`
47 Private bool `bencode:"private,omitempty"`
50 Announce string `bencode:"announce"`
51 AnnounceList [][]string `bencode:"announce-list,omitempty"`
52 CreationDate int64 `bencode:"creation date,omitempty"`
53 Comment string `bencode:"comment,omitempty"`
54 CreatedBy string `bencode:"created by,omitempty"`
55 URLList interface{} `bencode:"url-list,omitempty"`
58 func test_file(t *testing.T, filename string) {
59 data1 := load_file(filename, t)
62 err := Unmarshal(data1, &f)
67 data2, err := Marshal(&f)
72 if !bytes.Equal(data1, data2) {
73 println(string(data2))
74 t.Fatalf("equality expected")
78 func TestBoth(t *testing.T) {
79 test_file(t, "_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")