commit
321090bff4
@ -0,0 +1,61 @@
|
||||
# Configuration for probot-stale - https://github.com/probot/stale
|
||||
|
||||
# Number of days of inactivity before an Issue or Pull Request becomes stale
|
||||
daysUntilStale: 360
|
||||
|
||||
# Number of days of inactivity before an Issue or Pull Request with the stale label is closed.
|
||||
# Set to false to disable. If disabled, issues still need to be closed manually, but will remain marked as stale.
|
||||
daysUntilClose: 30
|
||||
|
||||
# Only issues or pull requests with all of these labels are check if stale. Defaults to `[]` (disabled)
|
||||
onlyLabels: []
|
||||
|
||||
# Issues or Pull Requests with these labels will never be considered stale. Set to `[]` to disable
|
||||
exemptLabels:
|
||||
- pinned
|
||||
- security
|
||||
- "[Status] Maybe Later"
|
||||
|
||||
# Set to true to ignore issues in a project (defaults to false)
|
||||
exemptProjects: true
|
||||
|
||||
# Set to true to ignore issues in a milestone (defaults to false)
|
||||
exemptMilestones: true
|
||||
|
||||
# Set to true to ignore issues with an assignee (defaults to false)
|
||||
exemptAssignees: true
|
||||
|
||||
# Label to use when marking as stale
|
||||
staleLabel: wontfix
|
||||
|
||||
# Comment to post when marking as stale. Set to `false` to disable
|
||||
markComment: >
|
||||
This issue has been automatically marked as stale because it has not had
|
||||
recent activity. It will be closed if no further activity occurs. Thank you
|
||||
for your contributions.
|
||||
|
||||
# Comment to post when removing the stale label.
|
||||
# unmarkComment: >
|
||||
# Your comment here.
|
||||
|
||||
# Comment to post when closing a stale Issue or Pull Request.
|
||||
# closeComment: >
|
||||
# Your comment here.
|
||||
|
||||
# Limit the number of actions per hour, from 1-30. Default is 30
|
||||
limitPerRun: 30
|
||||
|
||||
# Limit to only `issues` or `pulls`
|
||||
# only: issues
|
||||
|
||||
# Optionally, specify configuration settings that are specific to just 'issues' or 'pulls':
|
||||
# pulls:
|
||||
# daysUntilStale: 30
|
||||
# markComment: >
|
||||
# This pull request has been automatically marked as stale because it has not had
|
||||
# recent activity. It will be closed if no further activity occurs. Thank you
|
||||
# for your contributions.
|
||||
|
||||
# issues:
|
||||
# exemptLabels:
|
||||
# - confirmed
|
@ -1,47 +1,90 @@
|
||||
module github.com/cloudreve/Cloudreve/v3
|
||||
|
||||
go 1.13
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/DATA-DOG/go-sqlmock v1.3.3
|
||||
github.com/aliyun/aliyun-oss-go-sdk v2.0.5+incompatible
|
||||
github.com/HFO4/aliyun-oss-go-sdk v2.2.3+incompatible
|
||||
github.com/aws/aws-sdk-go v1.31.5
|
||||
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
|
||||
github.com/duo-labs/webauthn v0.0.0-20191119193225-4bf9a0f776d4
|
||||
github.com/fatih/color v1.7.0
|
||||
github.com/gin-contrib/cors v1.3.0
|
||||
github.com/gin-contrib/gzip v0.0.2-0.20200226035851-25bef2ef21e8
|
||||
github.com/gin-contrib/sessions v0.0.1
|
||||
github.com/gin-contrib/static v0.0.0-20191128031702-f81c604d8ac2
|
||||
github.com/gin-gonic/gin v1.5.0
|
||||
github.com/gin-gonic/gin v1.7.0
|
||||
github.com/go-ini/ini v1.50.0
|
||||
github.com/go-mail/mail v2.3.1+incompatible
|
||||
github.com/gofrs/uuid v4.0.0+incompatible
|
||||
github.com/gomodule/redigo v2.0.0+incompatible
|
||||
github.com/google/go-querystring v1.0.0
|
||||
github.com/gorilla/websocket v1.4.1
|
||||
github.com/hashicorp/go-version v1.2.0
|
||||
github.com/hashicorp/go-version v1.3.0
|
||||
github.com/jinzhu/gorm v1.9.11
|
||||
github.com/juju/ratelimit v1.0.1
|
||||
github.com/mattn/go-colorable v0.1.4 // indirect
|
||||
github.com/mojocn/base64Captcha v0.0.0-20190801020520-752b1cd608b2
|
||||
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/pquerna/otp v1.2.0
|
||||
github.com/qiniu/api.v7/v7 v7.4.0
|
||||
github.com/qiniu/go-sdk/v7 v7.11.1
|
||||
github.com/rafaeljusto/redigomock v0.0.0-20191117212112-00b2509252a1
|
||||
github.com/rakyll/statik v0.1.7
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/smartystreets/goconvey v1.6.4 // indirect
|
||||
github.com/speps/go-hashids v2.0.0+incompatible
|
||||
github.com/stretchr/testify v1.5.1
|
||||
github.com/stretchr/testify v1.6.1
|
||||
github.com/tencentcloud/tencentcloud-sdk-go v3.0.125+incompatible
|
||||
github.com/tencentyun/cos-go-sdk-v5 v0.0.0-20200120023323-87ff3bc489ac
|
||||
github.com/upyun/go-sdk v2.1.0+incompatible
|
||||
golang.org/x/image v0.0.0-20211028202545-6944b10bf410
|
||||
golang.org/x/text v0.3.6
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||
golang.org/x/text v0.3.7
|
||||
gopkg.in/go-playground/validator.v9 v9.29.1
|
||||
)
|
||||
|
||||
require (
|
||||
cloud.google.com/go v0.37.4 // indirect
|
||||
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect
|
||||
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f // indirect
|
||||
github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect
|
||||
github.com/boombuler/barcode v1.0.1-0.20190219062509-6c824513bacc // indirect
|
||||
github.com/cloudflare/cfssl v0.0.0-20190726000631-633726f6bcb7 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/denisenkom/go-mssqldb v0.0.0-20190515213511-eb9f6a1743f3 // indirect
|
||||
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-playground/locales v0.14.0 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.0 // indirect
|
||||
github.com/go-playground/validator/v10 v10.8.0 // indirect
|
||||
github.com/go-sql-driver/mysql v1.5.0 // indirect
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||
github.com/golang/protobuf v1.3.3 // indirect
|
||||
github.com/google/certificate-transparency-go v1.0.21 // indirect
|
||||
github.com/gorilla/context v1.1.1 // indirect
|
||||
github.com/gorilla/securecookie v1.1.1 // indirect
|
||||
github.com/gorilla/sessions v1.1.3 // indirect
|
||||
github.com/jinzhu/inflection v1.0.0 // indirect
|
||||
github.com/jmespath/go-jmespath v0.3.0 // indirect
|
||||
github.com/json-iterator/go v1.1.9 // indirect
|
||||
github.com/katzenpost/core v0.0.7 // indirect
|
||||
github.com/leodido/go-urn v1.2.1 // indirect
|
||||
github.com/lib/pq v1.1.1 // indirect
|
||||
github.com/mattn/go-colorable v0.1.4 // indirect
|
||||
github.com/mattn/go-isatty v0.0.12 // indirect
|
||||
github.com/mattn/go-sqlite3 v1.11.0 // indirect
|
||||
github.com/mitchellh/mapstructure v1.1.2 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.1 // indirect
|
||||
github.com/mozillazg/go-httpheader v0.2.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/quasoft/memstore v0.0.0-20180925164028-84a050167438 // indirect
|
||||
github.com/satori/go.uuid v1.2.0 // indirect
|
||||
github.com/smartystreets/goconvey v1.6.4 // indirect
|
||||
github.com/stretchr/objx v0.2.0 // indirect
|
||||
github.com/ugorji/go/codec v1.1.7 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
|
||||
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a // indirect
|
||||
golang.org/x/sys v0.0.0-20211020174200-9d6173849985 // indirect
|
||||
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect
|
||||
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
|
||||
gopkg.in/ini.v1 v1.51.0 // indirect
|
||||
gopkg.in/mail.v2 v2.3.1 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.8 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
|
||||
)
|
||||
|
@ -0,0 +1,22 @@
|
||||
package backoff
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestConstantBackoff_Next(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
|
||||
b := &ConstantBackoff{Sleep: time.Duration(0), Max: 3}
|
||||
a.True(b.Next())
|
||||
a.True(b.Next())
|
||||
a.True(b.Next())
|
||||
a.False(b.Next())
|
||||
b.Reset()
|
||||
a.True(b.Next())
|
||||
a.True(b.Next())
|
||||
a.True(b.Next())
|
||||
a.False(b.Next())
|
||||
}
|
@ -0,0 +1,250 @@
|
||||
package chunk
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/chunk/backoff"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewChunkGroup(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
|
||||
testCases := []struct {
|
||||
fileSize uint64
|
||||
chunkSize uint64
|
||||
expectedInnerChunkSize uint64
|
||||
expectedChunkNum uint64
|
||||
expectedInfo [][2]int //Start, Index,Length
|
||||
}{
|
||||
{10, 0, 10, 1, [][2]int{{0, 10}}},
|
||||
{0, 0, 0, 1, [][2]int{{0, 0}}},
|
||||
{0, 10, 10, 1, [][2]int{{0, 0}}},
|
||||
{50, 10, 10, 5, [][2]int{
|
||||
{0, 10},
|
||||
{10, 10},
|
||||
{20, 10},
|
||||
{30, 10},
|
||||
{40, 10},
|
||||
}},
|
||||
{50, 50, 50, 1, [][2]int{
|
||||
{0, 50},
|
||||
}},
|
||||
|
||||
{50, 15, 15, 4, [][2]int{
|
||||
{0, 15},
|
||||
{15, 15},
|
||||
{30, 15},
|
||||
{45, 5},
|
||||
}},
|
||||
}
|
||||
|
||||
for index, testCase := range testCases {
|
||||
file := &fsctx.FileStream{Size: testCase.fileSize}
|
||||
chunkGroup := NewChunkGroup(file, testCase.chunkSize, &backoff.ConstantBackoff{}, true)
|
||||
a.EqualValues(testCase.expectedChunkNum, chunkGroup.Num(),
|
||||
"TestCase:%d,ChunkNum()", index)
|
||||
a.EqualValues(testCase.expectedInnerChunkSize, chunkGroup.chunkSize,
|
||||
"TestCase:%d,InnerChunkSize()", index)
|
||||
a.EqualValues(testCase.expectedChunkNum, chunkGroup.Num(),
|
||||
"TestCase:%d,len(Chunks)", index)
|
||||
a.EqualValues(testCase.fileSize, chunkGroup.Total())
|
||||
|
||||
for cIndex, info := range testCase.expectedInfo {
|
||||
a.True(chunkGroup.Next())
|
||||
a.EqualValues(info[1], chunkGroup.Length(),
|
||||
"TestCase:%d,Chunks[%d].Length()", index, cIndex)
|
||||
a.EqualValues(info[0], chunkGroup.Start(),
|
||||
"TestCase:%d,Chunks[%d].Start()", index, cIndex)
|
||||
|
||||
a.Equal(cIndex == len(testCase.expectedInfo)-1, chunkGroup.IsLast(),
|
||||
"TestCase:%d,Chunks[%d].IsLast()", index, cIndex)
|
||||
|
||||
a.NotEmpty(chunkGroup.RangeHeader())
|
||||
}
|
||||
a.False(chunkGroup.Next())
|
||||
}
|
||||
}
|
||||
|
||||
func TestChunkGroup_TempAvailablet(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
|
||||
file := &fsctx.FileStream{Size: 1}
|
||||
c := NewChunkGroup(file, 0, &backoff.ConstantBackoff{}, true)
|
||||
a.False(c.TempAvailable())
|
||||
|
||||
f, err := os.CreateTemp("", "TestChunkGroup_TempAvailablet.*")
|
||||
defer func() {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
}()
|
||||
a.NoError(err)
|
||||
c.bufferTemp = f
|
||||
|
||||
a.False(c.TempAvailable())
|
||||
f.Write([]byte("1"))
|
||||
a.True(c.TempAvailable())
|
||||
|
||||
}
|
||||
|
||||
func TestChunkGroup_Process(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
file := &fsctx.FileStream{Size: 10}
|
||||
|
||||
// success
|
||||
{
|
||||
file.File = io.NopCloser(strings.NewReader("1234567890"))
|
||||
c := NewChunkGroup(file, 5, &backoff.ConstantBackoff{}, true)
|
||||
count := 0
|
||||
a.True(c.Next())
|
||||
a.NoError(c.Process(func(c *ChunkGroup, chunk io.Reader) error {
|
||||
count++
|
||||
res, err := io.ReadAll(chunk)
|
||||
a.NoError(err)
|
||||
a.EqualValues("12345", string(res))
|
||||
return nil
|
||||
}))
|
||||
a.True(c.Next())
|
||||
a.NoError(c.Process(func(c *ChunkGroup, chunk io.Reader) error {
|
||||
count++
|
||||
res, err := io.ReadAll(chunk)
|
||||
a.NoError(err)
|
||||
a.EqualValues("67890", string(res))
|
||||
return nil
|
||||
}))
|
||||
a.False(c.Next())
|
||||
a.Equal(2, count)
|
||||
}
|
||||
|
||||
// retry, read from buffer file
|
||||
{
|
||||
file.File = io.NopCloser(strings.NewReader("1234567890"))
|
||||
c := NewChunkGroup(file, 5, &backoff.ConstantBackoff{Max: 2}, true)
|
||||
count := 0
|
||||
a.True(c.Next())
|
||||
a.NoError(c.Process(func(c *ChunkGroup, chunk io.Reader) error {
|
||||
count++
|
||||
res, err := io.ReadAll(chunk)
|
||||
a.NoError(err)
|
||||
a.EqualValues("12345", string(res))
|
||||
return nil
|
||||
}))
|
||||
a.True(c.Next())
|
||||
a.NoError(c.Process(func(c *ChunkGroup, chunk io.Reader) error {
|
||||
count++
|
||||
res, err := io.ReadAll(chunk)
|
||||
a.NoError(err)
|
||||
a.EqualValues("67890", string(res))
|
||||
if count == 2 {
|
||||
return errors.New("error")
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
a.False(c.Next())
|
||||
a.Equal(3, count)
|
||||
}
|
||||
|
||||
// retry, read from seeker
|
||||
{
|
||||
f, _ := os.CreateTemp("", "TestChunkGroup_Process.*")
|
||||
f.Write([]byte("1234567890"))
|
||||
f.Seek(0, 0)
|
||||
defer func() {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
}()
|
||||
file.File = f
|
||||
file.Seeker = f
|
||||
c := NewChunkGroup(file, 5, &backoff.ConstantBackoff{Max: 2}, false)
|
||||
count := 0
|
||||
a.True(c.Next())
|
||||
a.NoError(c.Process(func(c *ChunkGroup, chunk io.Reader) error {
|
||||
count++
|
||||
res, err := io.ReadAll(chunk)
|
||||
a.NoError(err)
|
||||
a.EqualValues("12345", string(res))
|
||||
return nil
|
||||
}))
|
||||
a.True(c.Next())
|
||||
a.NoError(c.Process(func(c *ChunkGroup, chunk io.Reader) error {
|
||||
count++
|
||||
res, err := io.ReadAll(chunk)
|
||||
a.NoError(err)
|
||||
a.EqualValues("67890", string(res))
|
||||
if count == 2 {
|
||||
return errors.New("error")
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
a.False(c.Next())
|
||||
a.Equal(3, count)
|
||||
}
|
||||
|
||||
// retry, seek error
|
||||
{
|
||||
f, _ := os.CreateTemp("", "TestChunkGroup_Process.*")
|
||||
f.Write([]byte("1234567890"))
|
||||
f.Seek(0, 0)
|
||||
defer func() {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
}()
|
||||
file.File = f
|
||||
file.Seeker = f
|
||||
c := NewChunkGroup(file, 5, &backoff.ConstantBackoff{Max: 2}, false)
|
||||
count := 0
|
||||
a.True(c.Next())
|
||||
a.NoError(c.Process(func(c *ChunkGroup, chunk io.Reader) error {
|
||||
count++
|
||||
res, err := io.ReadAll(chunk)
|
||||
a.NoError(err)
|
||||
a.EqualValues("12345", string(res))
|
||||
return nil
|
||||
}))
|
||||
a.True(c.Next())
|
||||
f.Close()
|
||||
a.Error(c.Process(func(c *ChunkGroup, chunk io.Reader) error {
|
||||
count++
|
||||
if count == 2 {
|
||||
return errors.New("error")
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
a.False(c.Next())
|
||||
a.Equal(2, count)
|
||||
}
|
||||
|
||||
// retry, finally error
|
||||
{
|
||||
f, _ := os.CreateTemp("", "TestChunkGroup_Process.*")
|
||||
f.Write([]byte("1234567890"))
|
||||
f.Seek(0, 0)
|
||||
defer func() {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
}()
|
||||
file.File = f
|
||||
file.Seeker = f
|
||||
c := NewChunkGroup(file, 5, &backoff.ConstantBackoff{Max: 2}, false)
|
||||
count := 0
|
||||
a.True(c.Next())
|
||||
a.NoError(c.Process(func(c *ChunkGroup, chunk io.Reader) error {
|
||||
count++
|
||||
res, err := io.ReadAll(chunk)
|
||||
a.NoError(err)
|
||||
a.EqualValues("12345", string(res))
|
||||
return nil
|
||||
}))
|
||||
a.True(c.Next())
|
||||
a.Error(c.Process(func(c *ChunkGroup, chunk io.Reader) error {
|
||||
count++
|
||||
return errors.New("error")
|
||||
}))
|
||||
a.False(c.Next())
|
||||
a.Equal(4, count)
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package onedrive
|
||||
|
||||
import (
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDriver_replaceSourceHost(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
origin string
|
||||
cdn string
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{"TestNoReplace", "http://1dr.ms/download.aspx?123456", "", "http://1dr.ms/download.aspx?123456", false},
|
||||
{"TestReplaceCorrect", "http://1dr.ms/download.aspx?123456", "https://test.com:8080", "https://test.com:8080/download.aspx?123456", false},
|
||||
{"TestCdnFormatError", "http://1dr.ms/download.aspx?123456", string([]byte{0x7f}), "", true},
|
||||
{"TestSrcFormatError", string([]byte{0x7f}), "https://test.com:8080", "", true},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
policy := &model.Policy{}
|
||||
policy.OptionsSerialized.OdProxy = tt.cdn
|
||||
handler := Driver{
|
||||
Policy: policy,
|
||||
}
|
||||
got, err := handler.replaceSourceHost(tt.origin)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("replaceSourceHost() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("replaceSourceHost() got = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
@ -1,188 +0,0 @@
|
||||
package oss
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetPublicKey(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
testCases := []struct {
|
||||
Request http.Request
|
||||
ResNil bool
|
||||
Error bool
|
||||
}{
|
||||
// Header解码失败
|
||||
{
|
||||
Request: http.Request{
|
||||
Header: http.Header{
|
||||
"X-Oss-Pub-Key-Url": {"中文"},
|
||||
},
|
||||
},
|
||||
ResNil: true,
|
||||
Error: true,
|
||||
},
|
||||
// 公钥URL无效
|
||||
{
|
||||
Request: http.Request{
|
||||
Header: http.Header{
|
||||
"X-Oss-Pub-Key-Url": {"aHR0cHM6Ly9wb3JuaHViLmNvbQ=="},
|
||||
},
|
||||
},
|
||||
ResNil: true,
|
||||
Error: true,
|
||||
},
|
||||
// 请求失败
|
||||
{
|
||||
Request: http.Request{
|
||||
Header: http.Header{
|
||||
"X-Oss-Pub-Key-Url": {"aHR0cDovL2dvc3NwdWJsaWMuYWxpY2RuLmNvbS8yMzQyMzQ="},
|
||||
},
|
||||
},
|
||||
ResNil: true,
|
||||
Error: true,
|
||||
},
|
||||
// 成功
|
||||
{
|
||||
Request: http.Request{
|
||||
Header: http.Header{
|
||||
"X-Oss-Pub-Key-Url": {"aHR0cDovL2dvc3NwdWJsaWMuYWxpY2RuLmNvbS9jYWxsYmFja19wdWJfa2V5X3YxLnBlbQ=="},
|
||||
},
|
||||
},
|
||||
ResNil: false,
|
||||
Error: false,
|
||||
},
|
||||
}
|
||||
|
||||
for i, testCase := range testCases {
|
||||
asserts.NoError(cache.Deletes([]string{"oss_public_key"}, ""))
|
||||
res, err := GetPublicKey(&testCase.Request)
|
||||
if testCase.Error {
|
||||
asserts.Error(err, "Test Case #%d", i)
|
||||
} else {
|
||||
asserts.NoError(err, "Test Case #%d", i)
|
||||
}
|
||||
if testCase.ResNil {
|
||||
asserts.Empty(res, "Test Case #%d", i)
|
||||
} else {
|
||||
asserts.NotEmpty(res, "Test Case #%d", i)
|
||||
}
|
||||
}
|
||||
|
||||
// 测试缓存
|
||||
asserts.NoError(cache.Set("oss_public_key", []byte("123"), 0))
|
||||
res, err := GetPublicKey(nil)
|
||||
asserts.NoError(err)
|
||||
asserts.Equal([]byte("123"), res)
|
||||
}
|
||||
|
||||
func TestVerifyCallbackSignature(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
testPubKey := `-----BEGIN PUBLIC KEY-----
|
||||
MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKs/JBGzwUB2aVht4crBx3oIPBLNsjGs
|
||||
C0fTXv+nvlmklvkcolvpvXLTjaxUHR3W9LXxQ2EHXAJfCB+6H2YF1k8CAwEAAQ==
|
||||
-----END PUBLIC KEY-----
|
||||
`
|
||||
|
||||
// 成功
|
||||
{
|
||||
asserts.NoError(cache.Set("oss_public_key", []byte(testPubKey), 0))
|
||||
r := http.Request{
|
||||
URL: &url.URL{Path: "/api/v3/callback/oss/TnXx5E5VyfJUyM1UdkdDu1rtnJ34EbmH"},
|
||||
Header: map[string][]string{
|
||||
"Authorization": {"e5LwzwTkP9AFAItT4YzvdJOHd0Y0wqTMWhsV/h5SG90JYGAmMd+8LQyj96R+9qUfJWjMt6suuUh7LaOryR87Dw=="},
|
||||
"X-Oss-Pub-Key-Url": {"aHR0cHM6Ly9nb3NzcHVibGljLmFsaWNkbi5jb20vY2FsbGJhY2tfcHViX2tleV92MS5wZW0="},
|
||||
},
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"name":"2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","source_name":"1/1_hFRtDLgM_2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","size":114020,"pic_info":"810,539"}`)),
|
||||
}
|
||||
asserts.NoError(VerifyCallbackSignature(&r))
|
||||
}
|
||||
|
||||
// 签名错误
|
||||
{
|
||||
asserts.NoError(cache.Set("oss_public_key", []byte(testPubKey), 0))
|
||||
r := http.Request{
|
||||
URL: &url.URL{Path: "/api/v3/callback/oss/TnXx5E5VyfJUyM1UdkdDu1rtnJ34EbmH"},
|
||||
Header: map[string][]string{
|
||||
"Authorization": {"e3LwzwTkP9AFAItT4YzvdJOHd0Y0wqTMWhsV/h5SG90JYGAmMd+8LQyj96R+9qUfJWjMt6suuUh7LaOryR87Dw=="},
|
||||
"X-Oss-Pub-Key-Url": {"aHR0cHM6Ly9nb3NzcHVibGljLmFsaWNkbi5jb20vY2FsbGJhY2tfcHViX2tleV92MS5wZW0="},
|
||||
},
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"name":"2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","source_name":"1/1_hFRtDLgM_2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","size":114020,"pic_info":"810,539"}`)),
|
||||
}
|
||||
asserts.Error(VerifyCallbackSignature(&r))
|
||||
}
|
||||
|
||||
// GetPubKey 失败
|
||||
{
|
||||
asserts.NoError(cache.Deletes([]string{"oss_public_key"}, ""))
|
||||
r := http.Request{
|
||||
URL: &url.URL{Path: "/api/v3/callback/oss/TnXx5E5VyfJUyM1UdkdDu1rtnJ34EbmH"},
|
||||
Header: map[string][]string{
|
||||
"Authorization": {"e5LwzwTkP9AFAItT4YzvdJOHd0Y0wqTMWhsV/h5SG90JYGAmMd+8LQyj96R+9qUfJWjMt6suuUh7LaOryR87Dw=="},
|
||||
},
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"name":"2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","source_name":"1/1_hFRtDLgM_2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","size":114020,"pic_info":"810,539"}`)),
|
||||
}
|
||||
asserts.Error(VerifyCallbackSignature(&r))
|
||||
}
|
||||
|
||||
// getRequestMD5 失败
|
||||
{
|
||||
asserts.NoError(cache.Set("oss_public_key", []byte(testPubKey), 0))
|
||||
r := http.Request{
|
||||
URL: &url.URL{Path: "%测试"},
|
||||
Header: map[string][]string{
|
||||
"Authorization": {"e5LwzwTkP9AFAItT4YzvdJOHd0Y0wqTMWhsV/h5SG90JYGAmMd+8LQyj96R+9qUfJWjMt6suuUh7LaOryR87Dw=="},
|
||||
"X-Oss-Pub-Key-Url": {"aHR0cHM6Ly9nb3NzcHVibGljLmFsaWNkbi5jb20vY2FsbGJhY2tfcHViX2tleV92MS5wZW0="},
|
||||
},
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"name":"2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","source_name":"1/1_hFRtDLgM_2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","size":114020,"pic_info":"810,539"}`)),
|
||||
}
|
||||
asserts.Error(VerifyCallbackSignature(&r))
|
||||
}
|
||||
|
||||
// 无 Authorization 头
|
||||
{
|
||||
asserts.NoError(cache.Set("oss_public_key", []byte(testPubKey), 0))
|
||||
r := http.Request{
|
||||
URL: &url.URL{Path: "/api/v3/callback/oss/TnXx5E5VyfJUyM1UdkdDu1rtnJ34EbmH"},
|
||||
Header: map[string][]string{
|
||||
"X-Oss-Pub-Key-Url": {"aHR0cHM6Ly9nb3NzcHVibGljLmFsaWNkbi5jb20vY2FsbGJhY2tfcHViX2tleV92MS5wZW0="},
|
||||
},
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"name":"2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","source_name":"1/1_hFRtDLgM_2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","size":114020,"pic_info":"810,539"}`)),
|
||||
}
|
||||
asserts.Error(VerifyCallbackSignature(&r))
|
||||
}
|
||||
|
||||
// pub block 不存在
|
||||
{
|
||||
asserts.NoError(cache.Set("oss_public_key", []byte(""), 0))
|
||||
r := http.Request{
|
||||
URL: &url.URL{Path: "/api/v3/callback/oss/TnXx5E5VyfJUyM1UdkdDu1rtnJ34EbmH"},
|
||||
Header: map[string][]string{
|
||||
"Authorization": {"e5LwzwTkP9AFAItT4YzvdJOHd0Y0wqTMWhsV/h5SG90JYGAmMd+8LQyj96R+9qUfJWjMt6suuUh7LaOryR87Dw=="},
|
||||
"X-Oss-Pub-Key-Url": {"aHR0cHM6Ly9nb3NzcHVibGljLmFsaWNkbi5jb20vY2FsbGJhY2tfcHViX2tleV92MS5wZW0="},
|
||||
},
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"name":"2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","source_name":"1/1_hFRtDLgM_2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","size":114020,"pic_info":"810,539"}`)),
|
||||
}
|
||||
asserts.Error(VerifyCallbackSignature(&r))
|
||||
}
|
||||
|
||||
// ParsePKIXPublicKey出错
|
||||
{
|
||||
asserts.NoError(cache.Set("oss_public_key", []byte("-----BEGIN PUBLIC KEY-----\n-----END PUBLIC KEY-----"), 0))
|
||||
r := http.Request{
|
||||
URL: &url.URL{Path: "/api/v3/callback/oss/TnXx5E5VyfJUyM1UdkdDu1rtnJ34EbmH"},
|
||||
Header: map[string][]string{
|
||||
"Authorization": {"e5LwzwTkP9AFAItT4YzvdJOHd0Y0wqTMWhsV/h5SG90JYGAmMd+8LQyj96R+9qUfJWjMt6suuUh7LaOryR87Dw=="},
|
||||
"X-Oss-Pub-Key-Url": {"aHR0cHM6Ly9nb3NzcHVibGljLmFsaWNkbi5jb20vY2FsbGJhY2tfcHViX2tleV92MS5wZW0="},
|
||||
},
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"name":"2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","source_name":"1/1_hFRtDLgM_2f7b2ccf30e9270ea920f1ab8a4037a546a2f0d5.jpg","size":114020,"pic_info":"810,539"}`)),
|
||||
}
|
||||
asserts.Error(VerifyCallbackSignature(&r))
|
||||
}
|
||||
}
|
@ -1,354 +0,0 @@
|
||||
package oss
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
||||
"github.com/stretchr/testify/assert"
|
||||
testMock "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestDriver_InitOSSClient(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
handler := Driver{
|
||||
Policy: &model.Policy{
|
||||
AccessKey: "ak",
|
||||
SecretKey: "sk",
|
||||
BucketName: "test",
|
||||
Server: "test.com",
|
||||
},
|
||||
}
|
||||
|
||||
// 成功
|
||||
{
|
||||
asserts.NoError(handler.InitOSSClient(false))
|
||||
}
|
||||
|
||||
// 使用内网Endpoint
|
||||
{
|
||||
handler.Policy.OptionsSerialized.ServerSideEndpoint = "endpoint2"
|
||||
asserts.NoError(handler.InitOSSClient(false))
|
||||
}
|
||||
|
||||
// 未指定存储策略
|
||||
{
|
||||
handler := Driver{}
|
||||
asserts.Error(handler.InitOSSClient(false))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_CORS(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
handler := Driver{
|
||||
Policy: &model.Policy{
|
||||
AccessKey: "ak",
|
||||
SecretKey: "sk",
|
||||
BucketName: "test",
|
||||
Server: "test.com",
|
||||
},
|
||||
}
|
||||
|
||||
// 失败
|
||||
{
|
||||
asserts.NotPanics(func() {
|
||||
handler.CORS()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_Token(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
handler := Driver{
|
||||
Policy: &model.Policy{
|
||||
AccessKey: "ak",
|
||||
SecretKey: "sk",
|
||||
BucketName: "test",
|
||||
Server: "test.com",
|
||||
},
|
||||
}
|
||||
|
||||
// 成功
|
||||
{
|
||||
ctx := context.WithValue(context.Background(), fsctx.SavePathCtx, "/123")
|
||||
cache.Set("setting_siteURL", "http://test.cloudreve.org", 0)
|
||||
res, err := handler.Token(ctx, 10, "key", nil)
|
||||
asserts.NoError(err)
|
||||
asserts.NotEmpty(res.Policy)
|
||||
asserts.NotEmpty(res.Token)
|
||||
asserts.Equal(handler.Policy.AccessKey, res.AccessKey)
|
||||
asserts.Equal("/123", res.Path)
|
||||
}
|
||||
|
||||
// 上下文错误
|
||||
{
|
||||
ctx := context.Background()
|
||||
_, err := handler.Token(ctx, 10, "key", nil)
|
||||
asserts.Error(err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestDriver_Source(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
handler := Driver{
|
||||
Policy: &model.Policy{
|
||||
AccessKey: "ak",
|
||||
SecretKey: "sk",
|
||||
BucketName: "test",
|
||||
Server: "test.com",
|
||||
IsPrivate: true,
|
||||
},
|
||||
}
|
||||
|
||||
// 正常 非下载 无限速
|
||||
{
|
||||
res, err := handler.Source(context.Background(), "/123", url.URL{}, 10, false, 0)
|
||||
asserts.NoError(err)
|
||||
resURL, err := url.Parse(res)
|
||||
asserts.NoError(err)
|
||||
query := resURL.Query()
|
||||
asserts.NotEmpty(query.Get("Signature"))
|
||||
asserts.NotEmpty(query.Get("Expires"))
|
||||
asserts.Equal("ak", query.Get("OSSAccessKeyId"))
|
||||
}
|
||||
|
||||
// 限速 + 下载
|
||||
{
|
||||
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, model.File{Name: "123.txt"})
|
||||
res, err := handler.Source(ctx, "/123", url.URL{}, 10, true, 102401)
|
||||
asserts.NoError(err)
|
||||
resURL, err := url.Parse(res)
|
||||
asserts.NoError(err)
|
||||
query := resURL.Query()
|
||||
asserts.NotEmpty(query.Get("Signature"))
|
||||
asserts.NotEmpty(query.Get("Expires"))
|
||||
asserts.Equal("ak", query.Get("OSSAccessKeyId"))
|
||||
asserts.EqualValues("819208", query.Get("x-oss-traffic-limit"))
|
||||
asserts.NotEmpty(query.Get("response-content-disposition"))
|
||||
}
|
||||
|
||||
// 限速超出范围 + 下载
|
||||
{
|
||||
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, model.File{Name: "123.txt"})
|
||||
res, err := handler.Source(ctx, "/123", url.URL{}, 10, true, 10)
|
||||
asserts.NoError(err)
|
||||
resURL, err := url.Parse(res)
|
||||
asserts.NoError(err)
|
||||
query := resURL.Query()
|
||||
asserts.NotEmpty(query.Get("Signature"))
|
||||
asserts.NotEmpty(query.Get("Expires"))
|
||||
asserts.Equal("ak", query.Get("OSSAccessKeyId"))
|
||||
asserts.EqualValues("819200", query.Get("x-oss-traffic-limit"))
|
||||
asserts.NotEmpty(query.Get("response-content-disposition"))
|
||||
}
|
||||
|
||||
// 限速超出范围 + 下载
|
||||
{
|
||||
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, model.File{Name: "123.txt"})
|
||||
res, err := handler.Source(ctx, "/123", url.URL{}, 10, true, 838860801)
|
||||
asserts.NoError(err)
|
||||
resURL, err := url.Parse(res)
|
||||
asserts.NoError(err)
|
||||
query := resURL.Query()
|
||||
asserts.NotEmpty(query.Get("Signature"))
|
||||
asserts.NotEmpty(query.Get("Expires"))
|
||||
asserts.Equal("ak", query.Get("OSSAccessKeyId"))
|
||||
asserts.EqualValues("838860800", query.Get("x-oss-traffic-limit"))
|
||||
asserts.NotEmpty(query.Get("response-content-disposition"))
|
||||
}
|
||||
|
||||
// 公共空间
|
||||
{
|
||||
handler.Policy.IsPrivate = false
|
||||
res, err := handler.Source(context.Background(), "/123", url.URL{}, 10, false, 0)
|
||||
asserts.NoError(err)
|
||||
resURL, err := url.Parse(res)
|
||||
asserts.NoError(err)
|
||||
query := resURL.Query()
|
||||
asserts.Empty(query.Get("Signature"))
|
||||
}
|
||||
|
||||
// 正常 指定了CDN域名
|
||||
{
|
||||
handler.Policy.BaseURL = "https://cqu.edu.cn"
|
||||
res, err := handler.Source(context.Background(), "/123", url.URL{}, 10, false, 0)
|
||||
asserts.NoError(err)
|
||||
resURL, err := url.Parse(res)
|
||||
asserts.NoError(err)
|
||||
query := resURL.Query()
|
||||
asserts.Empty(query.Get("Signature"))
|
||||
asserts.Contains(resURL.String(), handler.Policy.BaseURL)
|
||||
}
|
||||
|
||||
// 强制使用公网 Endpoint
|
||||
{
|
||||
handler.Policy.BaseURL = ""
|
||||
handler.Policy.OptionsSerialized.ServerSideEndpoint = "endpoint.com"
|
||||
res, err := handler.Source(context.WithValue(context.Background(), fsctx.ForceUsePublicEndpointCtx, false), "/123", url.URL{}, 10, false, 0)
|
||||
asserts.NoError(err)
|
||||
resURL, err := url.Parse(res)
|
||||
asserts.NoError(err)
|
||||
query := resURL.Query()
|
||||
asserts.Empty(query.Get("Signature"))
|
||||
asserts.Contains(resURL.String(), "endpoint.com")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_Thumb(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
handler := Driver{
|
||||
Policy: &model.Policy{
|
||||
AccessKey: "ak",
|
||||
SecretKey: "sk",
|
||||
BucketName: "test",
|
||||
Server: "test.com",
|
||||
},
|
||||
}
|
||||
|
||||
// 上下文不存在
|
||||
{
|
||||
ctx := context.Background()
|
||||
res, err := handler.Thumb(ctx, "/123.txt")
|
||||
asserts.Error(err)
|
||||
asserts.Nil(res)
|
||||
}
|
||||
|
||||
// 成功
|
||||
{
|
||||
cache.Set("setting_preview_timeout", "60", 0)
|
||||
ctx := context.WithValue(context.Background(), fsctx.ThumbSizeCtx, [2]uint{10, 20})
|
||||
res, err := handler.Thumb(ctx, "/123.jpg")
|
||||
asserts.NoError(err)
|
||||
resURL, err := url.Parse(res.URL)
|
||||
asserts.NoError(err)
|
||||
urlQuery := resURL.Query()
|
||||
asserts.Equal("image/resize,m_lfit,h_20,w_10", urlQuery.Get("x-oss-process"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_Delete(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
handler := Driver{
|
||||
Policy: &model.Policy{
|
||||
AccessKey: "ak",
|
||||
SecretKey: "sk",
|
||||
BucketName: "test",
|
||||
Server: "oss-cn-shanghai.aliyuncs.com",
|
||||
},
|
||||
}
|
||||
|
||||
// 失败
|
||||
{
|
||||
res, err := handler.Delete(context.Background(), []string{"1", "2", "3"})
|
||||
asserts.Error(err)
|
||||
asserts.Equal([]string{"1", "2", "3"}, res)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_Put(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
handler := Driver{
|
||||
Policy: &model.Policy{
|
||||
AccessKey: "ak",
|
||||
SecretKey: "sk",
|
||||
BucketName: "test",
|
||||
Server: "oss-cn-shanghai.aliyuncs.com",
|
||||
},
|
||||
}
|
||||
cache.Set("setting_upload_credential_timeout", "3600", 0)
|
||||
ctx := context.WithValue(context.Background(), fsctx.DisableOverwrite, true)
|
||||
|
||||
// 失败
|
||||
{
|
||||
err := handler.Put(ctx, ioutil.NopCloser(strings.NewReader("123")), "/123.txt", 3)
|
||||
asserts.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
type ClientMock struct {
|
||||
testMock.Mock
|
||||
}
|
||||
|
||||
func (m ClientMock) Request(method, target string, body io.Reader, opts ...request.Option) *request.Response {
|
||||
args := m.Called(method, target, body, opts)
|
||||
return args.Get(0).(*request.Response)
|
||||
}
|
||||
|
||||
func TestDriver_Get(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
handler := Driver{
|
||||
Policy: &model.Policy{
|
||||
AccessKey: "ak",
|
||||
SecretKey: "sk",
|
||||
BucketName: "test",
|
||||
Server: "oss-cn-shanghai.aliyuncs.com",
|
||||
},
|
||||
HTTPClient: request.NewClient(),
|
||||
}
|
||||
cache.Set("setting_preview_timeout", "3600", 0)
|
||||
|
||||
// 响应失败
|
||||
{
|
||||
res, err := handler.Get(context.Background(), "123.txt")
|
||||
asserts.Error(err)
|
||||
asserts.Nil(res)
|
||||
}
|
||||
|
||||
// 响应成功
|
||||
{
|
||||
ctx := context.WithValue(context.Background(), fsctx.FileModelCtx, model.File{Size: 3})
|
||||
clientMock := ClientMock{}
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"GET",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: nil,
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader(`123`)),
|
||||
},
|
||||
})
|
||||
handler.HTTPClient = clientMock
|
||||
res, err := handler.Get(ctx, "123.txt")
|
||||
clientMock.AssertExpectations(t)
|
||||
asserts.NoError(err)
|
||||
n, err := res.Seek(0, io.SeekEnd)
|
||||
asserts.NoError(err)
|
||||
asserts.EqualValues(3, n)
|
||||
content, err := ioutil.ReadAll(res)
|
||||
asserts.NoError(err)
|
||||
asserts.Equal("123", string(content))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDriver_List(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
handler := Driver{
|
||||
Policy: &model.Policy{
|
||||
AccessKey: "ak",
|
||||
SecretKey: "sk",
|
||||
BucketName: "test",
|
||||
Server: "test.com",
|
||||
IsPrivate: true,
|
||||
},
|
||||
}
|
||||
|
||||
// 连接失败
|
||||
{
|
||||
res, err := handler.List(context.Background(), "/", true)
|
||||
asserts.Error(err)
|
||||
asserts.Empty(res)
|
||||
}
|
||||
}
|
@ -0,0 +1,262 @@
|
||||
package remote
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/cache"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/mocks/requestmock"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
||||
"github.com/stretchr/testify/assert"
|
||||
testMock "github.com/stretchr/testify/mock"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewClient(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
policy := &model.Policy{}
|
||||
|
||||
// 无法解析服务端url
|
||||
{
|
||||
policy.Server = string([]byte{0x7f})
|
||||
c, err := NewClient(policy)
|
||||
a.Error(err)
|
||||
a.Nil(c)
|
||||
}
|
||||
|
||||
// 成功
|
||||
{
|
||||
policy.Server = ""
|
||||
c, err := NewClient(policy)
|
||||
a.NoError(err)
|
||||
a.NotNil(c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteClient_Upload(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
c, _ := NewClient(&model.Policy{})
|
||||
|
||||
// 无法创建上传会话
|
||||
{
|
||||
clientMock := requestmock.RequestMock{}
|
||||
c.(*remoteClient).httpClient = &clientMock
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"PUT",
|
||||
"upload",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: errors.New("error"),
|
||||
})
|
||||
err := c.Upload(context.Background(), &fsctx.FileStream{})
|
||||
a.Error(err)
|
||||
a.Contains(err.Error(), "error")
|
||||
clientMock.AssertExpectations(t)
|
||||
}
|
||||
|
||||
// 分片上传失败,成功删除上传会话
|
||||
{
|
||||
cache.Set("setting_chunk_retries", "1", 0)
|
||||
clientMock := requestmock.RequestMock{}
|
||||
c.(*remoteClient).httpClient = &clientMock
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"PUT",
|
||||
"upload",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: nil,
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)),
|
||||
},
|
||||
})
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"POST",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: errors.New("error"),
|
||||
})
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"DELETE",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: nil,
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)),
|
||||
},
|
||||
})
|
||||
err := c.Upload(context.Background(), &fsctx.FileStream{})
|
||||
a.Error(err)
|
||||
a.Contains(err.Error(), "error")
|
||||
clientMock.AssertExpectations(t)
|
||||
}
|
||||
|
||||
// 分片上传失败,无法删除上传会话
|
||||
{
|
||||
cache.Set("setting_chunk_retries", "1", 0)
|
||||
clientMock := requestmock.RequestMock{}
|
||||
c.(*remoteClient).httpClient = &clientMock
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"PUT",
|
||||
"upload",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: nil,
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)),
|
||||
},
|
||||
})
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"POST",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: errors.New("error"),
|
||||
})
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"DELETE",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: errors.New("error2"),
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)),
|
||||
},
|
||||
})
|
||||
err := c.Upload(context.Background(), &fsctx.FileStream{})
|
||||
a.Error(err)
|
||||
a.Contains(err.Error(), "error")
|
||||
clientMock.AssertExpectations(t)
|
||||
}
|
||||
|
||||
// 成功
|
||||
{
|
||||
cache.Set("setting_chunk_retries", "1", 0)
|
||||
clientMock := requestmock.RequestMock{}
|
||||
c.(*remoteClient).httpClient = &clientMock
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"PUT",
|
||||
"upload",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: nil,
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)),
|
||||
},
|
||||
})
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"POST",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"code":0}`)),
|
||||
},
|
||||
})
|
||||
err := c.Upload(context.Background(), &fsctx.FileStream{})
|
||||
a.NoError(err)
|
||||
clientMock.AssertExpectations(t)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRemoteClient_CreateUploadSessionFailed(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
c, _ := NewClient(&model.Policy{})
|
||||
|
||||
clientMock := requestmock.RequestMock{}
|
||||
c.(*remoteClient).httpClient = &clientMock
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"PUT",
|
||||
"upload",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: nil,
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"code":500,"msg":"error"}`)),
|
||||
},
|
||||
})
|
||||
err := c.Upload(context.Background(), &fsctx.FileStream{})
|
||||
a.Error(err)
|
||||
a.Contains(err.Error(), "error")
|
||||
clientMock.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestRemoteClient_UploadChunkFailed(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
c, _ := NewClient(&model.Policy{})
|
||||
|
||||
clientMock := requestmock.RequestMock{}
|
||||
c.(*remoteClient).httpClient = &clientMock
|
||||
clientMock.On(
|
||||
"Request",
|
||||
"POST",
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
testMock.Anything,
|
||||
).Return(&request.Response{
|
||||
Err: nil,
|
||||
Response: &http.Response{
|
||||
StatusCode: 200,
|
||||
Body: ioutil.NopCloser(strings.NewReader(`{"code":500,"msg":"error"}`)),
|
||||
},
|
||||
})
|
||||
err := c.(*remoteClient).uploadChunk(context.Background(), "", 0, strings.NewReader(""), false, 0)
|
||||
a.Error(err)
|
||||
a.Contains(err.Error(), "error")
|
||||
clientMock.AssertExpectations(t)
|
||||
}
|
||||
|
||||
func TestRemoteClient_GetUploadURL(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
c, _ := NewClient(&model.Policy{})
|
||||
|
||||
// url 解析失败
|
||||
{
|
||||
c.(*remoteClient).policy.Server = string([]byte{0x7f})
|
||||
res, sign, err := c.GetUploadURL(0, "")
|
||||
a.Error(err)
|
||||
a.Empty(res)
|
||||
a.Empty(sign)
|
||||
}
|
||||
|
||||
// 成功
|
||||
{
|
||||
c.(*remoteClient).policy.Server = ""
|
||||
res, sign, err := c.GetUploadURL(0, "")
|
||||
a.NoError(err)
|
||||
a.NotEmpty(res)
|
||||
a.NotEmpty(sign)
|
||||
}
|
||||
}
|
@ -1 +0,0 @@
|
||||
package taskinfo
|
@ -0,0 +1,32 @@
|
||||
package remoteclientmock
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/filesystem/fsctx"
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/serializer"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
type RemoteClientMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (r *RemoteClientMock) CreateUploadSession(ctx context.Context, session *serializer.UploadSession, ttl int64) error {
|
||||
return r.Called(ctx, session, ttl).Error(0)
|
||||
}
|
||||
|
||||
func (r *RemoteClientMock) GetUploadURL(ttl int64, sessionID string) (string, string, error) {
|
||||
args := r.Called(ttl, sessionID)
|
||||
|
||||
return args.String(0), args.String(1), args.Error(2)
|
||||
}
|
||||
|
||||
func (r *RemoteClientMock) Upload(ctx context.Context, file fsctx.FileHeader) error {
|
||||
args := r.Called(ctx, file)
|
||||
return args.Error(0)
|
||||
}
|
||||
|
||||
func (r *RemoteClientMock) DeleteUploadSession(ctx context.Context, sessionID string) error {
|
||||
args := r.Called(ctx, sessionID)
|
||||
return args.Error(0)
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package requestmock
|
||||
|
||||
import (
|
||||
"github.com/cloudreve/Cloudreve/v3/pkg/request"
|
||||
"github.com/stretchr/testify/mock"
|
||||
"io"
|
||||
)
|
||||
|
||||
type RequestMock struct {
|
||||
mock.Mock
|
||||
}
|
||||
|
||||
func (r RequestMock) Request(method, target string, body io.Reader, opts ...request.Option) *request.Response {
|
||||
return r.Called(method, target, body, opts).Get(0).(*request.Response)
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package serializer
|
||||
|
||||
import (
|
||||
model "github.com/cloudreve/Cloudreve/v3/models"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildObjectList(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
res := BuildObjectList(1, []Object{{}, {}}, &model.Policy{})
|
||||
a.NotEmpty(res.Parent)
|
||||
a.NotNil(res.Policy)
|
||||
a.Len(res.Objects, 2)
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
package serializer
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDecodeUploadPolicy(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
|
||||
testCases := []struct {
|
||||
input string
|
||||
expectError bool
|
||||
expectNil bool
|
||||
expectRes *UploadPolicy
|
||||
}{
|
||||
{
|
||||
"错误的base64字符",
|
||||
true,
|
||||
true,
|
||||
&UploadPolicy{},
|
||||
},
|
||||
{
|
||||
"6ZSZ6K+v55qESlNPTuWtl+espg==",
|
||||
true,
|
||||
true,
|
||||
&UploadPolicy{},
|
||||
},
|
||||
{
|
||||
"e30=",
|
||||
false,
|
||||
false,
|
||||
&UploadPolicy{},
|
||||
},
|
||||
{
|
||||
"eyJjYWxsYmFja191cmwiOiJ0ZXN0In0=",
|
||||
false,
|
||||
false,
|
||||
&UploadPolicy{CallbackURL: "test"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
res, err := DecodeUploadPolicy(testCase.input)
|
||||
if testCase.expectError {
|
||||
asserts.Error(err)
|
||||
}
|
||||
if testCase.expectNil {
|
||||
asserts.Nil(res)
|
||||
}
|
||||
if !testCase.expectNil {
|
||||
asserts.Equal(testCase.expectRes, res)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadPolicy_EncodeUploadPolicy(t *testing.T) {
|
||||
asserts := assert.New(t)
|
||||
testPolicy := UploadPolicy{}
|
||||
res, err := testPolicy.EncodeUploadPolicy()
|
||||
asserts.NoError(err)
|
||||
asserts.NotEmpty(res)
|
||||
}
|
Loading…
Reference in new issue