]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/metainfo_test.go
Allow encoding private=0 in metainfo, which fixes test for not using InfoEx.Bytes...
[btrtrc.git] / metainfo / metainfo_test.go
1 package metainfo
2
3 import (
4         "io"
5         "io/ioutil"
6         "path"
7         "testing"
8
9         "github.com/anacrolix/missinggo"
10         "github.com/stretchr/testify/assert"
11         "github.com/stretchr/testify/require"
12
13         "github.com/anacrolix/torrent/bencode"
14 )
15
16 func testFile(t *testing.T, filename string) {
17         mi, err := LoadFromFile(filename)
18         if err != nil {
19                 t.Fatal(err)
20         }
21
22         if len(mi.Info.Files) == 1 {
23                 t.Logf("Single file: %s (length: %d)\n", mi.Info.Name, mi.Info.Files[0].Length)
24         } else {
25                 t.Logf("Multiple files: %s\n", mi.Info.Name)
26                 for _, f := range mi.Info.Files {
27                         t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
28                 }
29         }
30
31         for _, group := range mi.AnnounceList {
32                 for _, tracker := range group {
33                         t.Logf("Tracker: %s\n", tracker)
34                 }
35         }
36         // for _, url := range mi.WebSeedURLs {
37         //      t.Logf("URL: %s\n", url)
38         // }
39
40         b, err := bencode.Marshal(mi.Info)
41         require.NoError(t, err)
42         assert.EqualValues(t, b, mi.Info.Bytes)
43 }
44
45 func TestFile(t *testing.T) {
46         testFile(t, "testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
47         testFile(t, "testdata/continuum.torrent")
48         testFile(t, "testdata/23516C72685E8DB0C8F15553382A927F185C4F01.torrent")
49         testFile(t, "testdata/trackerless.torrent")
50 }
51
52 // Ensure that the correct number of pieces are generated when hashing files.
53 func TestNumPieces(t *testing.T) {
54         for _, _case := range []struct {
55                 PieceLength int64
56                 Files       []FileInfo
57                 NumPieces   int
58         }{
59                 {256 * 1024, []FileInfo{{Length: 1024*1024 + -1}}, 4},
60                 {256 * 1024, []FileInfo{{Length: 1024 * 1024}}, 4},
61                 {256 * 1024, []FileInfo{{Length: 1024*1024 + 1}}, 5},
62                 {5, []FileInfo{{Length: 1}, {Length: 12}}, 3},
63                 {5, []FileInfo{{Length: 4}, {Length: 12}}, 4},
64         } {
65                 info := Info{
66                         Files:       _case.Files,
67                         PieceLength: _case.PieceLength,
68                 }
69                 err := info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
70                         return ioutil.NopCloser(missinggo.ZeroReader), nil
71                 })
72                 assert.NoError(t, err)
73                 assert.EqualValues(t, _case.NumPieces, info.NumPieces())
74         }
75 }