github.com/anacrolix/torrent@v1.61.0/storage/file_test.go (about) 1 package storage 2 3 import ( 4 "bytes" 5 "context" 6 "io" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/anacrolix/missinggo/v2" 12 "github.com/go-quicktest/qt" 13 14 "github.com/anacrolix/torrent/metainfo" 15 ) 16 17 func TestShortFile(t *testing.T) { 18 td := t.TempDir() 19 s := NewFile(td) 20 defer s.Close() 21 info := &metainfo.Info{ 22 Name: "a", 23 Length: 2, 24 PieceLength: missinggo.MiB, 25 Pieces: make([]byte, 20), 26 } 27 ts, err := s.OpenTorrent(context.Background(), info, metainfo.Hash{}) 28 qt.Assert(t, qt.IsNil(err)) 29 f, err := os.Create(filepath.Join(td, "a")) 30 qt.Assert(t, qt.IsNil(err)) 31 err = f.Truncate(1) 32 qt.Assert(t, qt.IsNil(err)) 33 f.Close() 34 var buf bytes.Buffer 35 p := info.Piece(0) 36 n, err := io.Copy(&buf, io.NewSectionReader(ts.Piece(p), 0, p.Length())) 37 qt.Check(t, qt.Equals(n, int64(1))) 38 switch err { 39 case nil, io.EOF: 40 default: 41 t.Fatalf("unexpected error: %v", err) 42 } 43 }