2014-04-08 14:45:33 +08:00
|
|
|
package torrent
|
|
|
|
|
|
|
|
import (
|
2014-08-28 06:03:55 +08:00
|
|
|
"sync"
|
2014-04-08 14:45:33 +08:00
|
|
|
"testing"
|
2014-08-28 06:03:55 +08:00
|
|
|
|
2015-03-20 13:37:44 +08:00
|
|
|
"github.com/anacrolix/torrent/peer_protocol"
|
2014-04-08 14:45:33 +08:00
|
|
|
)
|
|
|
|
|
2014-04-16 19:13:44 +08:00
|
|
|
func r(i, b, l peer_protocol.Integer) request {
|
|
|
|
return request{i, chunkSpec{b, l}}
|
2014-04-08 14:45:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check the given Request is correct for various torrent offsets.
|
|
|
|
func TestTorrentRequest(t *testing.T) {
|
|
|
|
const s = 472183431 // Length of torrent.
|
|
|
|
for _, _case := range []struct {
|
|
|
|
off int64 // An offset into the torrent.
|
2014-04-16 19:13:44 +08:00
|
|
|
req request // The expected Request. The zero value means !ok.
|
2014-04-08 14:45:33 +08:00
|
|
|
}{
|
|
|
|
// Invalid offset.
|
2014-04-16 19:13:44 +08:00
|
|
|
{-1, request{}},
|
2014-04-08 14:45:33 +08:00
|
|
|
{0, r(0, 0, 16384)},
|
|
|
|
// One before the end of a piece.
|
|
|
|
{1<<18 - 1, r(0, 1<<18-16384, 16384)},
|
|
|
|
// Offset beyond torrent length.
|
2014-04-16 19:13:44 +08:00
|
|
|
{472 * 1 << 20, request{}},
|
2014-04-08 14:45:33 +08:00
|
|
|
// One before the end of the torrent. Complicates the chunk length.
|
|
|
|
{s - 1, r((s-1)/(1<<18), (s-1)%(1<<18)/(16384)*(16384), 12935)},
|
|
|
|
{1, r(0, 0, 16384)},
|
|
|
|
// One before end of chunk.
|
|
|
|
{16383, r(0, 0, 16384)},
|
|
|
|
// Second chunk.
|
|
|
|
{16384, r(0, 16384, 16384)},
|
|
|
|
} {
|
|
|
|
req, ok := torrentOffsetRequest(472183431, 1<<18, 16384, _case.off)
|
2014-04-16 19:13:44 +08:00
|
|
|
if (_case.req == request{}) == ok {
|
2014-04-08 14:45:33 +08:00
|
|
|
t.Fatalf("expected %v, got %v", _case.req, req)
|
|
|
|
}
|
|
|
|
if req != _case.req {
|
|
|
|
t.Fatalf("expected %v, got %v", _case.req, req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-28 06:03:55 +08:00
|
|
|
|
|
|
|
func TestTorrentDoubleClose(t *testing.T) {
|
2015-03-18 15:28:13 +08:00
|
|
|
tt, err := newTorrent(InfoHash{})
|
2014-08-28 06:03:55 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
2015-02-09 21:12:29 +08:00
|
|
|
tt.close()
|
2014-08-28 06:03:55 +08:00
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2014-09-11 18:31:31 +08:00
|
|
|
|
|
|
|
func TestAppendToCopySlice(t *testing.T) {
|
|
|
|
orig := []int{1, 2, 3}
|
|
|
|
dupe := append([]int{}, orig...)
|
|
|
|
dupe[0] = 4
|
|
|
|
if orig[0] != 1 {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|
2014-12-02 06:34:45 +08:00
|
|
|
|
|
|
|
func TestTorrentString(t *testing.T) {
|
|
|
|
tor := &torrent{}
|
|
|
|
s := tor.InfoHash.HexString()
|
|
|
|
if s != "0000000000000000000000000000000000000000" {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
}
|