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.

129 lines
3.4 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. "github.com/stretchr/testify/assert"
  19. gzipp "github.com/klauspost/compress/gzip"
  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.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 = lfsID + 1
  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. repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1")
  52. assert.NoError(t, err)
  53. oid := storeObjectInRepo(t, repo.ID, content)
  54. defer repo.RemoveLFSMetaObjectByOid(oid)
  55. session := loginUser(t, "user2")
  56. // Request OID
  57. req := NewRequest(t, "GET", "/user2/repo1.git/info/lfs/objects/"+oid+"/test")
  58. req.Header.Set("Accept-Encoding", "gzip")
  59. resp := session.MakeRequest(t, req, http.StatusOK)
  60. contentEncoding := resp.Header().Get("Content-Encoding")
  61. if !expectGzip || !setting.EnableGzip {
  62. assert.NotContains(t, contentEncoding, "gzip")
  63. result := resp.Body.Bytes()
  64. assert.Equal(t, *content, result)
  65. } else {
  66. assert.Contains(t, contentEncoding, "gzip")
  67. gzippReader, err := gzipp.NewReader(resp.Body)
  68. assert.NoError(t, err)
  69. result, err := ioutil.ReadAll(gzippReader)
  70. assert.NoError(t, err)
  71. assert.Equal(t, *content, result)
  72. }
  73. }
  74. func TestGetLFSSmall(t *testing.T) {
  75. content := []byte("A very small file\n")
  76. doLfs(t, &content, false)
  77. }
  78. func TestGetLFSLarge(t *testing.T) {
  79. content := make([]byte, gzip.MinSize*10)
  80. for i := range content {
  81. content[i] = byte(i % 256)
  82. }
  83. doLfs(t, &content, true)
  84. }
  85. func TestGetLFSGzip(t *testing.T) {
  86. b := make([]byte, gzip.MinSize*10)
  87. for i := range b {
  88. b[i] = byte(i % 256)
  89. }
  90. outputBuffer := bytes.NewBuffer([]byte{})
  91. gzippWriter := gzipp.NewWriter(outputBuffer)
  92. gzippWriter.Write(b)
  93. gzippWriter.Close()
  94. content := outputBuffer.Bytes()
  95. doLfs(t, &content, false)
  96. }
  97. func TestGetLFSZip(t *testing.T) {
  98. b := make([]byte, gzip.MinSize*10)
  99. for i := range b {
  100. b[i] = byte(i % 256)
  101. }
  102. outputBuffer := bytes.NewBuffer([]byte{})
  103. zipWriter := zip.NewWriter(outputBuffer)
  104. fileWriter, err := zipWriter.Create("default")
  105. assert.NoError(t, err)
  106. fileWriter.Write(b)
  107. zipWriter.Close()
  108. content := outputBuffer.Bytes()
  109. doLfs(t, &content, false)
  110. }