forked from anacrolix/torrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
torrent_test.go
69 lines (62 loc) · 1.78 KB
/
torrent_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package torrent_test
import (
"os"
"path/filepath"
"testing"
"github.com/anacrolix/missinggo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/james-lawrence/torrent"
"github.com/james-lawrence/torrent/bencode"
"github.com/james-lawrence/torrent/metainfo"
"github.com/james-lawrence/torrent/storage"
)
func TestAppendToCopySlice(t *testing.T) {
orig := []int{1, 2, 3}
dupe := append([]int{}, orig...)
dupe[0] = 4
if orig[0] != 1 {
t.FailNow()
}
}
// Check that a torrent containing zero-length file(s) will start, and that
// they're created in the filesystem. The client storage is assumed to be
// file-based on the native filesystem based.
func testEmptyFilesAndZeroPieceLength(t *testing.T, cfg *torrent.ClientConfig) {
cl, err := torrent.NewClient(cfg)
require.NoError(t, err)
defer cl.Close()
ib, err := bencode.Marshal(metainfo.Info{
Name: "empty",
Length: 0,
PieceLength: 0,
})
require.NoError(t, err)
fp := filepath.Join(cfg.DataDir, "empty")
os.Remove(fp)
assert.False(t, missinggo.FilePathExists(fp))
ts, err := torrent.NewFromMetaInfo(&metainfo.MetaInfo{
InfoBytes: ib,
})
require.NoError(t, err)
tt, _, err := cl.Start(ts)
require.NoError(t, err)
defer cl.Stop(ts)
tt.DownloadAll()
require.True(t, cl.WaitAll())
assert.True(t, missinggo.FilePathExists(fp))
}
func TestEmptyFilesAndZeroPieceLengthWithFileStorage(t *testing.T) {
cfg := torrent.TestingConfig(t)
ci := storage.NewFile(cfg.DataDir)
defer ci.Close()
cfg.DefaultStorage = ci
testEmptyFilesAndZeroPieceLength(t, cfg)
}
func TestEmptyFilesAndZeroPieceLengthWithMMapStorage(t *testing.T) {
cfg := torrent.TestingConfig(t)
ci := storage.NewMMap(cfg.DataDir)
defer ci.Close()
cfg.DefaultStorage = ci
testEmptyFilesAndZeroPieceLength(t, cfg)
}