2018-01-12 17:30:54 +01:00
|
|
|
package object_test
|
2017-12-01 16:15:44 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fs/hash"
|
|
|
|
"github.com/ncw/rclone/fs/object"
|
2017-12-01 16:15:44 +01:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStaticObject(t *testing.T) {
|
|
|
|
now := time.Now()
|
|
|
|
remote := "path/to/object"
|
|
|
|
size := int64(1024)
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
o := object.NewStaticObjectInfo(remote, now, size, true, nil, object.MemoryFs)
|
2017-12-01 16:15:44 +01:00
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
assert.Equal(t, object.MemoryFs, o.Fs())
|
2017-12-01 16:15:44 +01:00
|
|
|
assert.Equal(t, remote, o.Remote())
|
|
|
|
assert.Equal(t, remote, o.String())
|
|
|
|
assert.Equal(t, now, o.ModTime())
|
|
|
|
assert.Equal(t, size, o.Size())
|
|
|
|
assert.Equal(t, true, o.Storable())
|
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
Hash, err := o.Hash(hash.MD5)
|
2017-12-01 16:15:44 +01:00
|
|
|
assert.NoError(t, err)
|
2018-01-12 17:30:54 +01:00
|
|
|
assert.Equal(t, "", Hash)
|
2017-12-01 16:15:44 +01:00
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
o = object.NewStaticObjectInfo(remote, now, size, true, nil, nil)
|
2018-01-18 21:27:52 +01:00
|
|
|
_, err = o.Hash(hash.MD5)
|
|
|
|
assert.Equal(t, hash.ErrUnsupported, err)
|
2017-12-01 16:15:44 +01:00
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
hs := map[hash.Type]string{
|
2018-01-18 21:27:52 +01:00
|
|
|
hash.MD5: "potato",
|
2017-12-01 16:15:44 +01:00
|
|
|
}
|
2018-01-12 17:30:54 +01:00
|
|
|
o = object.NewStaticObjectInfo(remote, now, size, true, hs, nil)
|
2018-01-18 21:27:52 +01:00
|
|
|
Hash, err = o.Hash(hash.MD5)
|
2017-12-01 16:15:44 +01:00
|
|
|
assert.NoError(t, err)
|
2018-01-12 17:30:54 +01:00
|
|
|
assert.Equal(t, "potato", Hash)
|
2018-01-18 21:27:52 +01:00
|
|
|
_, err = o.Hash(hash.SHA1)
|
|
|
|
assert.Equal(t, hash.ErrUnsupported, err)
|
2017-12-01 16:15:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryFs(t *testing.T) {
|
2018-01-12 17:30:54 +01:00
|
|
|
f := object.MemoryFs
|
2017-12-01 16:15:44 +01:00
|
|
|
assert.Equal(t, "memory", f.Name())
|
|
|
|
assert.Equal(t, "", f.Root())
|
|
|
|
assert.Equal(t, "memory", f.String())
|
|
|
|
assert.Equal(t, time.Nanosecond, f.Precision())
|
2018-01-18 21:27:52 +01:00
|
|
|
assert.Equal(t, hash.Supported, f.Hashes())
|
2017-12-01 16:15:44 +01:00
|
|
|
assert.Equal(t, &fs.Features{}, f.Features())
|
|
|
|
|
|
|
|
entries, err := f.List("")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Nil(t, entries)
|
|
|
|
|
|
|
|
o, err := f.NewObject("obj")
|
|
|
|
assert.Equal(t, fs.ErrorObjectNotFound, err)
|
|
|
|
assert.Nil(t, o)
|
|
|
|
|
|
|
|
buf := bytes.NewBufferString("potato")
|
|
|
|
now := time.Now()
|
2018-01-12 17:30:54 +01:00
|
|
|
src := object.NewStaticObjectInfo("remote", now, int64(buf.Len()), true, nil, nil)
|
2017-12-01 16:15:44 +01:00
|
|
|
o, err = f.Put(buf, src)
|
|
|
|
assert.NoError(t, err)
|
2018-01-18 21:27:52 +01:00
|
|
|
hash, err := o.Hash(hash.SHA1)
|
2017-12-01 16:15:44 +01:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, "3e2e95f5ad970eadfa7e17eaf73da97024aa5359", hash)
|
|
|
|
|
|
|
|
err = f.Mkdir("dir")
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
err = f.Rmdir("dir")
|
2018-05-04 16:19:50 +02:00
|
|
|
assert.Equal(t, fs.ErrorDirNotFound, err)
|
2017-12-01 16:15:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMemoryObject(t *testing.T) {
|
|
|
|
remote := "path/to/object"
|
|
|
|
now := time.Now()
|
|
|
|
content := []byte("potatoXXXXXXXXXXXXX")
|
|
|
|
content = content[:6] // make some extra cap
|
|
|
|
|
2018-01-12 17:30:54 +01:00
|
|
|
o := object.NewMemoryObject(remote, now, content)
|
2017-12-01 16:15:44 +01:00
|
|
|
|
|
|
|
assert.Equal(t, content, o.Content())
|
2018-01-12 17:30:54 +01:00
|
|
|
assert.Equal(t, object.MemoryFs, o.Fs())
|
2017-12-01 16:15:44 +01:00
|
|
|
assert.Equal(t, remote, o.Remote())
|
|
|
|
assert.Equal(t, remote, o.String())
|
|
|
|
assert.Equal(t, now, o.ModTime())
|
|
|
|
assert.Equal(t, int64(len(content)), o.Size())
|
|
|
|
assert.Equal(t, true, o.Storable())
|
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
Hash, err := o.Hash(hash.MD5)
|
2017-12-01 16:15:44 +01:00
|
|
|
assert.NoError(t, err)
|
2018-01-12 17:30:54 +01:00
|
|
|
assert.Equal(t, "8ee2027983915ec78acc45027d874316", Hash)
|
2017-12-01 16:15:44 +01:00
|
|
|
|
2018-01-18 21:27:52 +01:00
|
|
|
Hash, err = o.Hash(hash.SHA1)
|
2017-12-01 16:15:44 +01:00
|
|
|
assert.NoError(t, err)
|
2018-01-12 17:30:54 +01:00
|
|
|
assert.Equal(t, "3e2e95f5ad970eadfa7e17eaf73da97024aa5359", Hash)
|
2017-12-01 16:15:44 +01:00
|
|
|
|
|
|
|
newNow := now.Add(time.Minute)
|
|
|
|
err = o.SetModTime(newNow)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, newNow, o.ModTime())
|
|
|
|
|
|
|
|
checkOpen := func(rc io.ReadCloser, expected string) {
|
|
|
|
actual, err := ioutil.ReadAll(rc)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
err = rc.Close()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, expected, string(actual))
|
|
|
|
}
|
|
|
|
|
|
|
|
checkContent := func(o fs.Object, expected string) {
|
|
|
|
rc, err := o.Open()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
checkOpen(rc, expected)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkContent(o, string(content))
|
|
|
|
|
|
|
|
rc, err := o.Open(&fs.RangeOption{Start: 1, End: 3})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
checkOpen(rc, "ot")
|
|
|
|
|
|
|
|
rc, err = o.Open(&fs.SeekOption{Offset: 3})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
checkOpen(rc, "ato")
|
|
|
|
|
|
|
|
// check it fits within the buffer
|
|
|
|
newNow = now.Add(2 * time.Minute)
|
|
|
|
newContent := bytes.NewBufferString("Rutabaga")
|
|
|
|
assert.True(t, newContent.Len() < cap(content)) // fits within cap(content)
|
2018-01-12 17:30:54 +01:00
|
|
|
src := object.NewStaticObjectInfo(remote, newNow, int64(newContent.Len()), true, nil, nil)
|
2017-12-01 16:15:44 +01:00
|
|
|
err = o.Update(newContent, src)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
checkContent(o, "Rutabaga")
|
|
|
|
assert.Equal(t, newNow, o.ModTime())
|
|
|
|
assert.Equal(t, "Rutaba", string(content)) // check we re-used the buffer
|
|
|
|
|
|
|
|
// not within the buffer
|
|
|
|
newStr := "0123456789"
|
|
|
|
newStr = newStr + newStr + newStr + newStr + newStr + newStr + newStr + newStr + newStr + newStr
|
|
|
|
newContent = bytes.NewBufferString(newStr)
|
|
|
|
assert.True(t, newContent.Len() > cap(content)) // does not fit within cap(content)
|
2018-01-12 17:30:54 +01:00
|
|
|
src = object.NewStaticObjectInfo(remote, newNow, int64(newContent.Len()), true, nil, nil)
|
2017-12-01 16:15:44 +01:00
|
|
|
err = o.Update(newContent, src)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
checkContent(o, newStr)
|
|
|
|
assert.Equal(t, "Rutaba", string(content)) // check we didn't re-use the buffer
|
|
|
|
|
|
|
|
// now try streaming
|
|
|
|
newStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
newContent = bytes.NewBufferString(newStr)
|
2018-01-12 17:30:54 +01:00
|
|
|
src = object.NewStaticObjectInfo(remote, newNow, -1, true, nil, nil)
|
2017-12-01 16:15:44 +01:00
|
|
|
err = o.Update(newContent, src)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
checkContent(o, newStr)
|
|
|
|
|
|
|
|
// and zero length
|
|
|
|
newStr = ""
|
|
|
|
newContent = bytes.NewBufferString(newStr)
|
2018-01-12 17:30:54 +01:00
|
|
|
src = object.NewStaticObjectInfo(remote, newNow, 0, true, nil, nil)
|
2017-12-01 16:15:44 +01:00
|
|
|
err = o.Update(newContent, src)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
checkContent(o, newStr)
|
|
|
|
|
|
|
|
err = o.Remove()
|
|
|
|
assert.Error(t, err)
|
|
|
|
}
|