]> Sergey Matveev's repositories - btrtrc.git/blob - storage/file_test.go
Remove CGO build flags for pure-Go squirrel branch
[btrtrc.git] / storage / file_test.go
1 package storage
2
3 import (
4         "bytes"
5         "io"
6         "io/ioutil"
7         "os"
8         "path/filepath"
9         "testing"
10
11         "github.com/anacrolix/missinggo/v2"
12         "github.com/stretchr/testify/assert"
13         "github.com/stretchr/testify/require"
14
15         "github.com/anacrolix/torrent/metainfo"
16 )
17
18 func TestShortFile(t *testing.T) {
19         td, err := ioutil.TempDir("", "")
20         require.NoError(t, err)
21         defer os.RemoveAll(td)
22         s := NewFile(td)
23         info := &metainfo.Info{
24                 Name:        "a",
25                 Length:      2,
26                 PieceLength: missinggo.MiB,
27         }
28         ts, err := s.OpenTorrent(info, metainfo.Hash{})
29         assert.NoError(t, err)
30         f, err := os.Create(filepath.Join(td, "a"))
31         require.NoError(t, err)
32         err = f.Truncate(1)
33         require.NoError(t, err)
34         f.Close()
35         var buf bytes.Buffer
36         p := info.Piece(0)
37         n, err := io.Copy(&buf, io.NewSectionReader(ts.Piece(p), 0, p.Length()))
38         assert.EqualValues(t, 1, n)
39         switch err {
40         case nil, io.EOF:
41         default:
42                 t.Errorf("expected nil or EOF error from truncated piece, got %v", err)
43         }
44 }