]> Sergey Matveev's repositories - btrtrc.git/blob - bad_storage.go
Use iotest.TestReader
[btrtrc.git] / bad_storage.go
1 package torrent
2
3 import (
4         "errors"
5         "math/rand"
6         "strings"
7
8         "github.com/anacrolix/torrent/internal/testutil"
9         "github.com/anacrolix/torrent/metainfo"
10         "github.com/anacrolix/torrent/storage"
11 )
12
13 type badStorage struct{}
14
15 var _ storage.ClientImpl = badStorage{}
16
17 func (bs badStorage) OpenTorrent(*metainfo.Info, metainfo.Hash) (storage.TorrentImpl, error) {
18         return bs, nil
19 }
20
21 func (bs badStorage) Close() error {
22         return nil
23 }
24
25 func (bs badStorage) Piece(p metainfo.Piece) storage.PieceImpl {
26         return badStoragePiece{p}
27 }
28
29 type badStoragePiece struct {
30         p metainfo.Piece
31 }
32
33 var _ storage.PieceImpl = badStoragePiece{}
34
35 func (p badStoragePiece) WriteAt(b []byte, off int64) (int, error) {
36         return 0, nil
37 }
38
39 func (p badStoragePiece) Completion() storage.Completion {
40         return storage.Completion{Complete: true, Ok: true}
41 }
42
43 func (p badStoragePiece) MarkComplete() error {
44         return errors.New("psyyyyyyyche")
45 }
46
47 func (p badStoragePiece) MarkNotComplete() error {
48         return errors.New("psyyyyyyyche")
49 }
50
51 func (p badStoragePiece) randomlyTruncatedDataString() string {
52         return testutil.GreetingFileContents[:rand.Intn(14)]
53 }
54
55 func (p badStoragePiece) ReadAt(b []byte, off int64) (n int, err error) {
56         r := strings.NewReader(p.randomlyTruncatedDataString())
57         return r.ReadAt(b, off+p.p.Offset())
58 }