]> Sergey Matveev's repositories - btrtrc.git/blob - storage/issue95_test.go
Storages that use piece hashes would panic with pure v2 torrents
[btrtrc.git] / storage / issue95_test.go
1 package storage
2
3 import (
4         "testing"
5
6         "github.com/anacrolix/missinggo/v2/resource"
7         "github.com/stretchr/testify/assert"
8         "github.com/stretchr/testify/require"
9
10         "github.com/anacrolix/torrent/metainfo"
11 )
12
13 // Two different torrents opened from the same storage. Closing one should not
14 // break the piece completion on the other.
15 func testIssue95(t *testing.T, ci ClientImpl) {
16         i1 := &metainfo.Info{
17                 Files:  []metainfo.FileInfo{{Path: []string{"a"}}},
18                 Pieces: make([]byte, 20),
19         }
20         c := NewClient(ci)
21         t1, err := c.OpenTorrent(i1, metainfo.HashBytes([]byte("a")))
22         require.NoError(t, err)
23         defer t1.Close()
24         i2 := &metainfo.Info{
25                 Files:  []metainfo.FileInfo{{Path: []string{"a"}}},
26                 Pieces: make([]byte, 20),
27         }
28         t2, err := c.OpenTorrent(i2, metainfo.HashBytes([]byte("b")))
29         require.NoError(t, err)
30         defer t2.Close()
31         t2p := t2.Piece(i2.Piece(0))
32         assert.NoError(t, t1.Close())
33         assert.NotPanics(t, func() { t2p.Completion() })
34 }
35
36 func TestIssue95File(t *testing.T) {
37         td := t.TempDir()
38         cs := NewFile(td)
39         defer cs.Close()
40         testIssue95(t, cs)
41 }
42
43 func TestIssue95MMap(t *testing.T) {
44         td := t.TempDir()
45         cs := NewMMap(td)
46         defer cs.Close()
47         testIssue95(t, cs)
48 }
49
50 func TestIssue95ResourcePieces(t *testing.T) {
51         testIssue95(t, NewResourcePieces(resource.OSFileProvider{}))
52 }