torrent/storage/file_test.go

45 lines
945 B
Go
Raw Normal View History

2016-03-28 09:38:30 +00:00
package storage
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
2016-03-28 09:38:30 +00:00
"github.com/anacrolix/missinggo"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
2019-08-21 10:58:40 +00:00
"github.com/anacrolix/torrent/metainfo"
)
func TestShortFile(t *testing.T) {
td, err := ioutil.TempDir("", "")
require.NoError(t, err)
defer os.RemoveAll(td)
s := NewFile(td)
info := &metainfo.Info{
Name: "a",
Length: 2,
PieceLength: missinggo.MiB,
2016-03-28 09:38:30 +00:00
}
ts, err := s.OpenTorrent(info, metainfo.Hash{})
assert.NoError(t, err)
f, err := os.Create(filepath.Join(td, "a"))
2017-11-07 13:12:34 +00:00
require.NoError(t, err)
err = f.Truncate(1)
2017-11-07 13:12:34 +00:00
require.NoError(t, err)
f.Close()
var buf bytes.Buffer
2016-03-28 09:38:30 +00:00
p := info.Piece(0)
n, err := io.Copy(&buf, io.NewSectionReader(ts.Piece(p), 0, p.Length()))
assert.EqualValues(t, 1, n)
switch err {
case nil, io.EOF:
default:
t.Errorf("expected nil or EOF error from truncated piece, got %v", err)
}
}