-
-
Notifications
You must be signed in to change notification settings - Fork 628
/
bad_storage.go
61 lines (47 loc) · 1.28 KB
/
bad_storage.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
package torrent
import (
"context"
"errors"
"math/rand"
"strings"
"github.com/anacrolix/torrent/internal/testutil"
"github.com/anacrolix/torrent/metainfo"
"github.com/anacrolix/torrent/storage"
)
type badStorage struct{}
var _ storage.ClientImpl = badStorage{}
func (bs badStorage) OpenTorrent(
context.Context,
*metainfo.Info,
metainfo.Hash,
) (storage.TorrentImpl, error) {
return storage.TorrentImpl{
Piece: bs.Piece,
}, nil
}
func (bs badStorage) Piece(p metainfo.Piece) storage.PieceImpl {
return badStoragePiece{p}
}
type badStoragePiece struct {
p metainfo.Piece
}
var _ storage.PieceImpl = badStoragePiece{}
func (p badStoragePiece) WriteAt(b []byte, off int64) (int, error) {
return 0, nil
}
func (p badStoragePiece) Completion() storage.Completion {
return storage.Completion{Complete: true, Ok: true}
}
func (p badStoragePiece) MarkComplete() error {
return errors.New("psyyyyyyyche")
}
func (p badStoragePiece) MarkNotComplete() error {
return errors.New("psyyyyyyyche")
}
func (p badStoragePiece) randomlyTruncatedDataString() string {
return testutil.GreetingFileContents[:rand.Intn(14)]
}
func (p badStoragePiece) ReadAt(b []byte, off int64) (n int, err error) {
r := strings.NewReader(p.randomlyTruncatedDataString())
return r.ReadAt(b, off+p.p.Offset())
}