2016-03-28 09:38:30 +00:00
|
|
|
package storage
|
2016-03-26 07:30:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
2016-03-28 09:38:30 +00:00
|
|
|
"github.com/anacrolix/missinggo"
|
2016-03-26 07:30:30 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/anacrolix/torrent/metainfo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShortFile(t *testing.T) {
|
|
|
|
td, err := ioutil.TempDir("", "")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(td)
|
2016-03-28 11:40:29 +00:00
|
|
|
s := NewFile(td)
|
2016-08-26 10:29:05 +00:00
|
|
|
info := &metainfo.Info{
|
|
|
|
Name: "a",
|
|
|
|
Length: 2,
|
|
|
|
PieceLength: missinggo.MiB,
|
2016-03-28 09:38:30 +00:00
|
|
|
}
|
2016-08-26 10:29:05 +00:00
|
|
|
ts, err := s.OpenTorrent(info, metainfo.Hash{})
|
2016-03-28 11:40:29 +00:00
|
|
|
assert.NoError(t, err)
|
2016-03-26 07:30:30 +00:00
|
|
|
f, err := os.Create(filepath.Join(td, "a"))
|
2017-11-07 13:12:34 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-26 07:30:30 +00:00
|
|
|
err = f.Truncate(1)
|
2017-11-07 13:12:34 +00:00
|
|
|
require.NoError(t, err)
|
2016-03-26 07:30:30 +00:00
|
|
|
f.Close()
|
|
|
|
var buf bytes.Buffer
|
2016-03-28 09:38:30 +00:00
|
|
|
p := info.Piece(0)
|
2016-03-28 11:40:29 +00:00
|
|
|
n, err := io.Copy(&buf, io.NewSectionReader(ts.Piece(p), 0, p.Length()))
|
2016-03-26 07:30:30 +00:00
|
|
|
assert.EqualValues(t, 1, n)
|
|
|
|
assert.Equal(t, io.ErrUnexpectedEOF, err)
|
|
|
|
}
|