2016-03-28 17:38:30 +08:00
|
|
|
package storage
|
2016-03-26 15:30:30 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2021-06-23 15:24:50 +08:00
|
|
|
"github.com/anacrolix/missinggo/v2"
|
2016-03-26 15:30:30 +08:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2019-08-21 18:58:40 +08:00
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
2016-03-26 15:30:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShortFile(t *testing.T) {
|
2022-02-02 15:26:46 +08:00
|
|
|
td := t.TempDir()
|
2016-03-28 19:40:29 +08:00
|
|
|
s := NewFile(td)
|
2016-08-26 18:29:05 +08:00
|
|
|
info := &metainfo.Info{
|
|
|
|
Name: "a",
|
|
|
|
Length: 2,
|
|
|
|
PieceLength: missinggo.MiB,
|
2016-03-28 17:38:30 +08:00
|
|
|
}
|
2016-08-26 18:29:05 +08:00
|
|
|
ts, err := s.OpenTorrent(info, metainfo.Hash{})
|
2016-03-28 19:40:29 +08:00
|
|
|
assert.NoError(t, err)
|
2016-03-26 15:30:30 +08:00
|
|
|
f, err := os.Create(filepath.Join(td, "a"))
|
2017-11-07 21:12:34 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-26 15:30:30 +08:00
|
|
|
err = f.Truncate(1)
|
2017-11-07 21:12:34 +08:00
|
|
|
require.NoError(t, err)
|
2016-03-26 15:30:30 +08:00
|
|
|
f.Close()
|
|
|
|
var buf bytes.Buffer
|
2016-03-28 17:38:30 +08:00
|
|
|
p := info.Piece(0)
|
2016-03-28 19:40:29 +08:00
|
|
|
n, err := io.Copy(&buf, io.NewSectionReader(ts.Piece(p), 0, p.Length()))
|
2016-03-26 15:30:30 +08:00
|
|
|
assert.EqualValues(t, 1, n)
|
2020-02-27 13:45:57 +08:00
|
|
|
switch err {
|
|
|
|
case nil, io.EOF:
|
|
|
|
default:
|
|
|
|
t.Errorf("expected nil or EOF error from truncated piece, got %v", err)
|
|
|
|
}
|
2016-03-26 15:30:30 +08:00
|
|
|
}
|