]> Sergey Matveev's repositories - btrtrc.git/blob - storage/file_test.go
Fix TestSeedAfterDownloading when cgo is disabled
[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         info := &metainfo.Info{
21                 Name:        "a",
22                 Length:      2,
23                 PieceLength: missinggo.MiB,
24         }
25         ts, err := s.OpenTorrent(info, metainfo.Hash{})
26         assert.NoError(t, err)
27         f, err := os.Create(filepath.Join(td, "a"))
28         require.NoError(t, err)
29         err = f.Truncate(1)
30         require.NoError(t, err)
31         f.Close()
32         var buf bytes.Buffer
33         p := info.Piece(0)
34         n, err := io.Copy(&buf, io.NewSectionReader(ts.Piece(p), 0, p.Length()))
35         assert.EqualValues(t, 1, n)
36         switch err {
37         case nil, io.EOF:
38         default:
39                 t.Errorf("expected nil or EOF error from truncated piece, got %v", err)
40         }
41 }