]> Sergey Matveev's repositories - btrtrc.git/blob - bencode/both_test.go
Initial commit.
[btrtrc.git] / bencode / both_test.go
1 package bencode
2
3 import "testing"
4 import "bytes"
5 import "io/ioutil"
6 import "time"
7
8 func load_file(name string, t *testing.T) []byte {
9         data, err := ioutil.ReadFile(name)
10         if err != nil {
11                 t.Fatal(err)
12         }
13         return data
14 }
15
16 func TestBothInterface(t *testing.T) {
17         data1 := load_file("_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent", t)
18         var iface interface{}
19
20         err := Unmarshal(data1, &iface)
21         if err != nil {
22                 t.Fatal(err)
23         }
24
25         data2, err := Marshal(iface)
26         if err != nil {
27                 t.Fatal(err)
28         }
29
30         if !bytes.Equal(data1, data2) {
31                 t.Fatalf("equality expected\n")
32         }
33 }
34
35 type torrent_file struct {
36         Info struct {
37                 Name        string `bencode:"name"`
38                 Length      int64  `bencode:"length"`
39                 MD5Sum      string `bencode:"md5sum,omitempty"`
40                 PieceLength int64  `bencode:"piece length"`
41                 Pieces      string `bencode:"pieces"`
42                 Private     bool   `bencode:"private,omitempty"`
43         } `bencode:"info"`
44
45         Announce     string      `bencode:"announce"`
46         AnnounceList [][]string  `bencode:"announce-list,omitempty"`
47         CreationDate int64       `bencode:"creation date,omitempty"`
48         Comment      string      `bencode:"comment,omitempty"`
49         CreatedBy    string      `bencode:"created by,omitempty"`
50         URLList      interface{} `bencode:"url-list,omitempty"`
51 }
52
53 func TestBoth(t *testing.T) {
54         data1 := load_file("_testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent", t)
55         var f torrent_file
56
57         err := Unmarshal(data1, &f)
58         if err != nil {
59                 t.Fatal(err)
60         }
61
62         t.Logf("Name: %s\n", f.Info.Name)
63         t.Logf("Length: %v bytes\n", f.Info.Length)
64         t.Logf("Announce: %s\n", f.Announce)
65         t.Logf("CreationDate: %s\n", time.Unix(f.CreationDate, 0).String())
66         t.Logf("CreatedBy: %s\n", f.CreatedBy)
67         t.Logf("Comment: %s\n", f.Comment)
68
69         data2, err := Marshal(&f)
70         if err != nil {
71                 t.Fatal(err)
72         }
73
74         if !bytes.Equal(data1, data2) {
75                 t.Fatalf("equality expected")
76         }
77 }