2019-01-23 01:56:51 -07:00
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package integrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bytes"
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2020-05-11 02:37:59 -06:00
|
|
|
"net/http/httptest"
|
2019-01-23 01:56:51 -07:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/models"
|
|
|
|
"code.gitea.io/gitea/modules/lfs"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2020-09-08 09:45:10 -06:00
|
|
|
"code.gitea.io/gitea/modules/storage"
|
2019-01-23 01:56:51 -07:00
|
|
|
|
2019-11-17 22:18:33 -07:00
|
|
|
"gitea.com/macaron/gzip"
|
2019-01-23 01:56:51 -07:00
|
|
|
gzipp "github.com/klauspost/compress/gzip"
|
2019-08-23 10:40:30 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
2019-01-23 01:56:51 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func GenerateLFSOid(content io.Reader) (string, error) {
|
|
|
|
h := sha256.New()
|
|
|
|
if _, err := io.Copy(h, content); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
sum := h.Sum(nil)
|
|
|
|
return hex.EncodeToString(sum), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var lfsID = int64(20000)
|
|
|
|
|
|
|
|
func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string {
|
|
|
|
oid, err := GenerateLFSOid(bytes.NewReader(*content))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
var lfsMetaObject *models.LFSMetaObject
|
|
|
|
|
2019-08-24 03:24:45 -06:00
|
|
|
if setting.Database.UsePostgreSQL {
|
2019-01-23 01:56:51 -07:00
|
|
|
lfsMetaObject = &models.LFSMetaObject{ID: lfsID, Oid: oid, Size: int64(len(*content)), RepositoryID: repositoryID}
|
|
|
|
} else {
|
|
|
|
lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(*content)), RepositoryID: repositoryID}
|
|
|
|
}
|
|
|
|
|
2019-06-12 13:41:28 -06:00
|
|
|
lfsID++
|
2019-01-23 01:56:51 -07:00
|
|
|
lfsMetaObject, err = models.NewLFSMetaObject(lfsMetaObject)
|
|
|
|
assert.NoError(t, err)
|
2020-09-08 09:45:10 -06:00
|
|
|
contentStore := &lfs.ContentStore{ObjectStorage: storage.LFS}
|
|
|
|
exist, err := contentStore.Exists(lfsMetaObject)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
if !exist {
|
2019-01-23 01:56:51 -07:00
|
|
|
err := contentStore.Put(lfsMetaObject, bytes.NewReader(*content))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
return oid
|
|
|
|
}
|
|
|
|
|
2020-05-11 02:37:59 -06:00
|
|
|
func storeAndGetLfs(t *testing.T, content *[]byte, extraHeader *http.Header, expectedStatus int) *httptest.ResponseRecorder {
|
2019-01-23 01:56:51 -07:00
|
|
|
repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
oid := storeObjectInRepo(t, repo.ID, content)
|
|
|
|
defer repo.RemoveLFSMetaObjectByOid(oid)
|
|
|
|
|
|
|
|
session := loginUser(t, "user2")
|
|
|
|
|
|
|
|
// Request OID
|
|
|
|
req := NewRequest(t, "GET", "/user2/repo1.git/info/lfs/objects/"+oid+"/test")
|
|
|
|
req.Header.Set("Accept-Encoding", "gzip")
|
2020-05-11 02:37:59 -06:00
|
|
|
if extraHeader != nil {
|
|
|
|
for key, values := range *extraHeader {
|
|
|
|
for _, value := range values {
|
|
|
|
req.Header.Add(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-01 08:12:50 -07:00
|
|
|
|
2020-05-11 02:37:59 -06:00
|
|
|
resp := session.MakeRequest(t, req, expectedStatus)
|
2019-01-23 01:56:51 -07:00
|
|
|
|
2020-05-11 02:37:59 -06:00
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkResponseTestContentEncoding(t *testing.T, content *[]byte, resp *httptest.ResponseRecorder, expectGzip bool) {
|
2019-01-23 01:56:51 -07:00
|
|
|
contentEncoding := resp.Header().Get("Content-Encoding")
|
|
|
|
if !expectGzip || !setting.EnableGzip {
|
|
|
|
assert.NotContains(t, contentEncoding, "gzip")
|
|
|
|
|
|
|
|
result := resp.Body.Bytes()
|
|
|
|
assert.Equal(t, *content, result)
|
|
|
|
} else {
|
|
|
|
assert.Contains(t, contentEncoding, "gzip")
|
|
|
|
gzippReader, err := gzipp.NewReader(resp.Body)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
result, err := ioutil.ReadAll(gzippReader)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, *content, result)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSSmall(t *testing.T) {
|
2020-05-11 02:37:59 -06:00
|
|
|
defer prepareTestEnv(t)()
|
|
|
|
setting.CheckLFSVersion()
|
|
|
|
if !setting.LFS.StartServer {
|
|
|
|
t.Skip()
|
|
|
|
return
|
|
|
|
}
|
2019-01-23 01:56:51 -07:00
|
|
|
content := []byte("A very small file\n")
|
2020-05-11 02:37:59 -06:00
|
|
|
|
|
|
|
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
|
|
|
|
checkResponseTestContentEncoding(t, &content, resp, false)
|
2019-01-23 01:56:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSLarge(t *testing.T) {
|
2020-05-11 02:37:59 -06:00
|
|
|
defer prepareTestEnv(t)()
|
|
|
|
setting.CheckLFSVersion()
|
|
|
|
if !setting.LFS.StartServer {
|
|
|
|
t.Skip()
|
|
|
|
return
|
|
|
|
}
|
2019-01-23 01:56:51 -07:00
|
|
|
content := make([]byte, gzip.MinSize*10)
|
|
|
|
for i := range content {
|
|
|
|
content[i] = byte(i % 256)
|
|
|
|
}
|
2020-05-11 02:37:59 -06:00
|
|
|
|
|
|
|
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
|
|
|
|
checkResponseTestContentEncoding(t, &content, resp, true)
|
2019-01-23 01:56:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSGzip(t *testing.T) {
|
2020-05-11 02:37:59 -06:00
|
|
|
defer prepareTestEnv(t)()
|
|
|
|
setting.CheckLFSVersion()
|
|
|
|
if !setting.LFS.StartServer {
|
|
|
|
t.Skip()
|
|
|
|
return
|
|
|
|
}
|
2019-01-23 01:56:51 -07:00
|
|
|
b := make([]byte, gzip.MinSize*10)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = byte(i % 256)
|
|
|
|
}
|
|
|
|
outputBuffer := bytes.NewBuffer([]byte{})
|
|
|
|
gzippWriter := gzipp.NewWriter(outputBuffer)
|
|
|
|
gzippWriter.Write(b)
|
|
|
|
gzippWriter.Close()
|
|
|
|
content := outputBuffer.Bytes()
|
2020-05-11 02:37:59 -06:00
|
|
|
|
|
|
|
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
|
|
|
|
checkResponseTestContentEncoding(t, &content, resp, false)
|
2019-01-23 01:56:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSZip(t *testing.T) {
|
2020-05-11 02:37:59 -06:00
|
|
|
defer prepareTestEnv(t)()
|
|
|
|
setting.CheckLFSVersion()
|
|
|
|
if !setting.LFS.StartServer {
|
|
|
|
t.Skip()
|
|
|
|
return
|
|
|
|
}
|
2019-01-23 01:56:51 -07:00
|
|
|
b := make([]byte, gzip.MinSize*10)
|
|
|
|
for i := range b {
|
|
|
|
b[i] = byte(i % 256)
|
|
|
|
}
|
|
|
|
outputBuffer := bytes.NewBuffer([]byte{})
|
|
|
|
zipWriter := zip.NewWriter(outputBuffer)
|
|
|
|
fileWriter, err := zipWriter.Create("default")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
fileWriter.Write(b)
|
|
|
|
zipWriter.Close()
|
|
|
|
content := outputBuffer.Bytes()
|
2020-05-11 02:37:59 -06:00
|
|
|
|
|
|
|
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
|
|
|
|
checkResponseTestContentEncoding(t, &content, resp, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSRangeNo(t *testing.T) {
|
|
|
|
defer prepareTestEnv(t)()
|
|
|
|
setting.CheckLFSVersion()
|
|
|
|
if !setting.LFS.StartServer {
|
|
|
|
t.Skip()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
content := []byte("123456789\n")
|
|
|
|
|
|
|
|
resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
|
|
|
|
assert.Equal(t, content, resp.Body.Bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetLFSRange(t *testing.T) {
|
|
|
|
defer prepareTestEnv(t)()
|
|
|
|
setting.CheckLFSVersion()
|
|
|
|
if !setting.LFS.StartServer {
|
|
|
|
t.Skip()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
content := []byte("123456789\n")
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
in string
|
|
|
|
out string
|
|
|
|
status int
|
|
|
|
}{
|
|
|
|
{"bytes=0-0", "1", http.StatusPartialContent},
|
|
|
|
{"bytes=0-1", "12", http.StatusPartialContent},
|
|
|
|
{"bytes=1-1", "2", http.StatusPartialContent},
|
|
|
|
{"bytes=1-3", "234", http.StatusPartialContent},
|
|
|
|
{"bytes=1-", "23456789\n", http.StatusPartialContent},
|
|
|
|
// end-range smaller than start-range is ignored
|
|
|
|
{"bytes=1-0", "23456789\n", http.StatusPartialContent},
|
|
|
|
{"bytes=0-10", "123456789\n", http.StatusPartialContent},
|
|
|
|
// end-range bigger than length-1 is ignored
|
|
|
|
{"bytes=0-11", "123456789\n", http.StatusPartialContent},
|
2020-11-01 08:12:50 -07:00
|
|
|
{"bytes=11-", "Requested Range Not Satisfiable", http.StatusRequestedRangeNotSatisfiable},
|
2020-05-11 02:37:59 -06:00
|
|
|
// incorrect header value cause whole header to be ignored
|
|
|
|
{"bytes=-", "123456789\n", http.StatusOK},
|
|
|
|
{"foobar", "123456789\n", http.StatusOK},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.in, func(t *testing.T) {
|
|
|
|
h := http.Header{
|
|
|
|
"Range": []string{tt.in},
|
|
|
|
}
|
|
|
|
resp := storeAndGetLfs(t, &content, &h, tt.status)
|
|
|
|
assert.Equal(t, tt.out, resp.Body.String())
|
|
|
|
})
|
|
|
|
}
|
2019-01-23 01:56:51 -07:00
|
|
|
}
|