2018-01-10 07:31:54 +08:00
|
|
|
package sftp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
2019-12-27 17:56:42 +08:00
|
|
|
"testing"
|
2018-01-10 07:31:54 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-01-19 09:23:22 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-01-10 07:31:54 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRequestPflags(t *testing.T) {
|
2019-08-30 23:04:37 +08:00
|
|
|
pflags := newFileOpenFlags(sshFxfRead | sshFxfWrite | sshFxfAppend)
|
2018-01-10 07:31:54 +08:00
|
|
|
assert.True(t, pflags.Read)
|
|
|
|
assert.True(t, pflags.Write)
|
|
|
|
assert.True(t, pflags.Append)
|
|
|
|
assert.False(t, pflags.Creat)
|
|
|
|
assert.False(t, pflags.Trunc)
|
|
|
|
assert.False(t, pflags.Excl)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestAflags(t *testing.T) {
|
2018-05-12 05:15:09 +08:00
|
|
|
aflags := newFileAttrFlags(
|
2019-08-30 23:04:37 +08:00
|
|
|
sshFileXferAttrSize | sshFileXferAttrUIDGID)
|
2018-01-10 07:31:54 +08:00
|
|
|
assert.True(t, aflags.Size)
|
|
|
|
assert.True(t, aflags.UidGid)
|
|
|
|
assert.False(t, aflags.Acmodtime)
|
|
|
|
assert.False(t, aflags.Permissions)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequestAttributes(t *testing.T) {
|
|
|
|
// UID/GID
|
2018-05-12 05:15:09 +08:00
|
|
|
fa := FileStat{UID: 1, GID: 2}
|
2019-08-30 23:04:37 +08:00
|
|
|
fl := uint32(sshFileXferAttrUIDGID)
|
2018-01-10 07:31:54 +08:00
|
|
|
at := []byte{}
|
|
|
|
at = marshalUint32(at, 1)
|
|
|
|
at = marshalUint32(at, 2)
|
2024-01-19 09:23:22 +08:00
|
|
|
testFs, _, err := unmarshalFileStat(fl, at)
|
|
|
|
require.NoError(t, err)
|
2019-08-30 23:04:37 +08:00
|
|
|
assert.Equal(t, fa, *testFs)
|
2018-01-10 07:31:54 +08:00
|
|
|
// Size and Mode
|
2022-10-16 22:05:40 +08:00
|
|
|
fa = FileStat{Mode: 0700, Size: 99}
|
2019-08-30 23:04:37 +08:00
|
|
|
fl = uint32(sshFileXferAttrSize | sshFileXferAttrPermissions)
|
2018-01-10 07:31:54 +08:00
|
|
|
at = []byte{}
|
|
|
|
at = marshalUint64(at, 99)
|
2022-10-16 22:05:40 +08:00
|
|
|
at = marshalUint32(at, 0700)
|
2024-01-19 09:23:22 +08:00
|
|
|
testFs, _, err = unmarshalFileStat(fl, at)
|
|
|
|
require.NoError(t, err)
|
2019-08-30 23:04:37 +08:00
|
|
|
assert.Equal(t, fa, *testFs)
|
2018-01-10 07:31:54 +08:00
|
|
|
// FileMode
|
2019-08-30 23:04:37 +08:00
|
|
|
assert.True(t, testFs.FileMode().IsRegular())
|
|
|
|
assert.False(t, testFs.FileMode().IsDir())
|
2022-10-16 22:05:40 +08:00
|
|
|
assert.Equal(t, testFs.FileMode().Perm(), os.FileMode(0700).Perm())
|
2018-01-10 07:31:54 +08:00
|
|
|
}
|
2019-12-24 16:37:32 +08:00
|
|
|
|
|
|
|
func TestRequestAttributesEmpty(t *testing.T) {
|
2024-01-19 09:23:22 +08:00
|
|
|
fs, b, err := unmarshalFileStat(sshFileXferAttrAll, []byte{
|
|
|
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // size
|
|
|
|
0x00, 0x00, 0x00, 0x00, // mode
|
|
|
|
0x00, 0x00, 0x00, 0x00, // mtime
|
|
|
|
0x00, 0x00, 0x00, 0x00, // atime
|
|
|
|
0x00, 0x00, 0x00, 0x00, // uid
|
|
|
|
0x00, 0x00, 0x00, 0x00, // gid
|
|
|
|
0x00, 0x00, 0x00, 0x00, // extended_count
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
2019-12-24 16:37:32 +08:00
|
|
|
assert.Equal(t, &FileStat{
|
|
|
|
Extended: []StatExtended{},
|
|
|
|
}, fs)
|
|
|
|
assert.Empty(t, b)
|
2019-12-27 17:56:42 +08:00
|
|
|
}
|