github.com/anacrolix/torrent@v1.61.0/bad-storage.go (about)

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