]> Sergey Matveev's repositories - btrtrc.git/blob - storage/file_test.go
go1.19 compat
[btrtrc.git] / storage / file_test.go
1 package storage
2
3 import (
4         "bytes"
5         "io"
6         "os"
7         "path/filepath"
8         "testing"
9
10         "github.com/anacrolix/missinggo/v2"
11         "github.com/stretchr/testify/assert"
12         "github.com/stretchr/testify/require"
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         }
26         ts, err := s.OpenTorrent(info, metainfo.Hash{})
27         assert.NoError(t, err)
28         f, err := os.Create(filepath.Join(td, "a"))
29         require.NoError(t, err)
30         err = f.Truncate(1)
31         require.NoError(t, err)
32         f.Close()
33         var buf bytes.Buffer
34         p := info.Piece(0)
35         n, err := io.Copy(&buf, io.NewSectionReader(ts.Piece(p), 0, p.Length()))
36         assert.EqualValues(t, 1, n)
37         switch err {
38         case nil, io.EOF:
39         default:
40                 t.Errorf("expected nil or EOF error from truncated piece, got %v", err)
41         }
42 }