]> Sergey Matveev's repositories - btrtrc.git/blob - metainfo/metainfo_test.go
metainfo.InfoEx.Hash becomes a function, UpdateBytes is added, and Bytes used in...
[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         require.NoError(t, err)
19
20         if len(mi.Info.Files) == 1 {
21                 t.Logf("Single file: %s (length: %d)\n", mi.Info.Name, mi.Info.Files[0].Length)
22         } else {
23                 t.Logf("Multiple files: %s\n", mi.Info.Name)
24                 for _, f := range mi.Info.Files {
25                         t.Logf(" - %s (length: %d)\n", path.Join(f.Path...), f.Length)
26                 }
27         }
28
29         for _, group := range mi.AnnounceList {
30                 for _, tracker := range group {
31                         t.Logf("Tracker: %s\n", tracker)
32                 }
33         }
34
35         b, err := bencode.Marshal(&mi.Info.Info)
36         require.NoError(t, err)
37         assert.EqualValues(t, string(b), string(mi.Info.Bytes))
38 }
39
40 func TestFile(t *testing.T) {
41         testFile(t, "testdata/archlinux-2011.08.19-netinstall-i686.iso.torrent")
42         testFile(t, "testdata/continuum.torrent")
43         testFile(t, "testdata/23516C72685E8DB0C8F15553382A927F185C4F01.torrent")
44         testFile(t, "testdata/trackerless.torrent")
45 }
46
47 // Ensure that the correct number of pieces are generated when hashing files.
48 func TestNumPieces(t *testing.T) {
49         for _, _case := range []struct {
50                 PieceLength int64
51                 Files       []FileInfo
52                 NumPieces   int
53         }{
54                 {256 * 1024, []FileInfo{{Length: 1024*1024 + -1}}, 4},
55                 {256 * 1024, []FileInfo{{Length: 1024 * 1024}}, 4},
56                 {256 * 1024, []FileInfo{{Length: 1024*1024 + 1}}, 5},
57                 {5, []FileInfo{{Length: 1}, {Length: 12}}, 3},
58                 {5, []FileInfo{{Length: 4}, {Length: 12}}, 4},
59         } {
60                 info := Info{
61                         Files:       _case.Files,
62                         PieceLength: _case.PieceLength,
63                 }
64                 err := info.GeneratePieces(func(fi FileInfo) (io.ReadCloser, error) {
65                         return ioutil.NopCloser(missinggo.ZeroReader), nil
66                 })
67                 assert.NoError(t, err)
68                 assert.EqualValues(t, _case.NumPieces, info.NumPieces())
69         }
70 }