]> Sergey Matveev's repositories - btrtrc.git/blob - bencode/both_test.go
Merge pull request #9 from gitter-badger/gitter-badge
[btrtrc.git] / bencode / both_test.go
1 package bencode
2
3 import "testing"
4 import "bytes"
5 import "io/ioutil"
6
7 func load_file(name string, t *testing.T) []byte {
8         data, err := ioutil.ReadFile(name)
9         if err != nil {
10                 t.Fatal(err)
11         }
12         return data
13 }
14
15 func test_file_interface(t *testing.T, filename string) {
16         data1 := load_file(filename, t)
17         var iface interface{}
18
19         err := Unmarshal(data1, &iface)
20         if err != nil {
21                 t.Fatal(err)
22         }
23
24         data2, err := Marshal(iface)
25         if err != nil {
26                 t.Fatal(err)
27         }
28
29         if !bytes.Equal(data1, data2) {
30                 t.Fatalf("equality expected\n")
31         }
32
33 }
34
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")
38 }
39
40 type torrent_file struct {
41         Info 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"`
48         } `bencode:"info"`
49
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"`
56 }
57
58 func test_file(t *testing.T, filename string) {
59         data1 := load_file(filename, t)
60         var f torrent_file
61
62         err := Unmarshal(data1, &f)
63         if err != nil {
64                 t.Fatal(err)
65         }
66
67         data2, err := Marshal(&f)
68         if err != nil {
69                 t.Fatal(err)
70         }
71
72         if !bytes.Equal(data1, data2) {
73                 println(string(data2))
74                 t.Fatalf("equality expected")
75         }
76 }
77
78 func TestBoth(t *testing.T) {
79         test_file(t, "_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
80 }