You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.5 KiB

  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package integrations
  5. import (
  6. "archive/zip"
  7. "bytes"
  8. "crypto/sha256"
  9. "encoding/hex"
  10. "io"
  11. "io/ioutil"
  12. "net/http"
  13. "testing"
  14. "code.gitea.io/gitea/models"
  15. "code.gitea.io/gitea/modules/gzip"
  16. "code.gitea.io/gitea/modules/lfs"
  17. "code.gitea.io/gitea/modules/setting"
  18. gzipp "github.com/klauspost/compress/gzip"
  19. "github.com/stretchr/testify/assert"
  20. )
  21. func GenerateLFSOid(content io.Reader) (string, error) {
  22. h := sha256.New()
  23. if _, err := io.Copy(h, content); err != nil {
  24. return "", err
  25. }
  26. sum := h.Sum(nil)
  27. return hex.EncodeToString(sum), nil
  28. }
  29. var lfsID = int64(20000)
  30. func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string {
  31. oid, err := GenerateLFSOid(bytes.NewReader(*content))
  32. assert.NoError(t, err)
  33. var lfsMetaObject *models.LFSMetaObject
  34. if setting.Database.UsePostgreSQL {
  35. lfsMetaObject = &models.LFSMetaObject{ID: lfsID, Oid: oid, Size: int64(len(*content)), RepositoryID: repositoryID}
  36. } else {
  37. lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(*content)), RepositoryID: repositoryID}
  38. }
  39. lfsID++
  40. lfsMetaObject, err = models.NewLFSMetaObject(lfsMetaObject)
  41. assert.NoError(t, err)
  42. contentStore := &lfs.ContentStore{BasePath: setting.LFS.ContentPath}
  43. if !contentStore.Exists(lfsMetaObject) {
  44. err := contentStore.Put(lfsMetaObject, bytes.NewReader(*content))
  45. assert.NoError(t, err)
  46. }
  47. return oid
  48. }
  49. func doLfs(t *testing.T, content *[]byte, expectGzip bool) {
  50. prepareTestEnv(t)
  51. setting.CheckLFSVersion()
  52. if !setting.LFS.StartServer {
  53. t.Skip()
  54. return
  55. }
  56. repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1")
  57. assert.NoError(t, err)
  58. oid := storeObjectInRepo(t, repo.ID, content)
  59. defer repo.RemoveLFSMetaObjectByOid(oid)
  60. session := loginUser(t, "user2")
  61. // Request OID
  62. req := NewRequest(t, "GET", "/user2/repo1.git/info/lfs/objects/"+oid+"/test")
  63. req.Header.Set("Accept-Encoding", "gzip")
  64. resp := session.MakeRequest(t, req, http.StatusOK)
  65. contentEncoding := resp.Header().Get("Content-Encoding")
  66. if !expectGzip || !setting.EnableGzip {
  67. assert.NotContains(t, contentEncoding, "gzip")
  68. result := resp.Body.Bytes()
  69. assert.Equal(t, *content, result)
  70. } else {
  71. assert.Contains(t, contentEncoding, "gzip")
  72. gzippReader, err := gzipp.NewReader(resp.Body)
  73. assert.NoError(t, err)
  74. result, err := ioutil.ReadAll(gzippReader)
  75. assert.NoError(t, err)
  76. assert.Equal(t, *content, result)
  77. }
  78. }
  79. func TestGetLFSSmall(t *testing.T) {
  80. content := []byte("A very small file\n")
  81. doLfs(t, &content, false)
  82. }
  83. func TestGetLFSLarge(t *testing.T) {
  84. content := make([]byte, gzip.MinSize*10)
  85. for i := range content {
  86. content[i] = byte(i % 256)
  87. }
  88. doLfs(t, &content, true)
  89. }
  90. func TestGetLFSGzip(t *testing.T) {
  91. b := make([]byte, gzip.MinSize*10)
  92. for i := range b {
  93. b[i] = byte(i % 256)
  94. }
  95. outputBuffer := bytes.NewBuffer([]byte{})
  96. gzippWriter := gzipp.NewWriter(outputBuffer)
  97. gzippWriter.Write(b)
  98. gzippWriter.Close()
  99. content := outputBuffer.Bytes()
  100. doLfs(t, &content, false)
  101. }
  102. func TestGetLFSZip(t *testing.T) {
  103. b := make([]byte, gzip.MinSize*10)
  104. for i := range b {
  105. b[i] = byte(i % 256)
  106. }
  107. outputBuffer := bytes.NewBuffer([]byte{})
  108. zipWriter := zip.NewWriter(outputBuffer)
  109. fileWriter, err := zipWriter.Create("default")
  110. assert.NoError(t, err)
  111. fileWriter.Write(b)
  112. zipWriter.Close()
  113. content := outputBuffer.Bytes()
  114. doLfs(t, &content, false)
  115. }