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.

228 lines
6.0 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. "net/http/httptest"
  14. "testing"
  15. "code.gitea.io/gitea/models"
  16. "code.gitea.io/gitea/modules/lfs"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/storage"
  19. "gitea.com/macaron/gzip"
  20. gzipp "github.com/klauspost/compress/gzip"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. func GenerateLFSOid(content io.Reader) (string, error) {
  24. h := sha256.New()
  25. if _, err := io.Copy(h, content); err != nil {
  26. return "", err
  27. }
  28. sum := h.Sum(nil)
  29. return hex.EncodeToString(sum), nil
  30. }
  31. var lfsID = int64(20000)
  32. func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string {
  33. oid, err := GenerateLFSOid(bytes.NewReader(*content))
  34. assert.NoError(t, err)
  35. var lfsMetaObject *models.LFSMetaObject
  36. if setting.Database.UsePostgreSQL {
  37. lfsMetaObject = &models.LFSMetaObject{ID: lfsID, Oid: oid, Size: int64(len(*content)), RepositoryID: repositoryID}
  38. } else {
  39. lfsMetaObject = &models.LFSMetaObject{Oid: oid, Size: int64(len(*content)), RepositoryID: repositoryID}
  40. }
  41. lfsID++
  42. lfsMetaObject, err = models.NewLFSMetaObject(lfsMetaObject)
  43. assert.NoError(t, err)
  44. contentStore := &lfs.ContentStore{ObjectStorage: storage.LFS}
  45. exist, err := contentStore.Exists(lfsMetaObject)
  46. assert.NoError(t, err)
  47. if !exist {
  48. err := contentStore.Put(lfsMetaObject, bytes.NewReader(*content))
  49. assert.NoError(t, err)
  50. }
  51. return oid
  52. }
  53. func storeAndGetLfs(t *testing.T, content *[]byte, extraHeader *http.Header, expectedStatus int) *httptest.ResponseRecorder {
  54. repo, err := models.GetRepositoryByOwnerAndName("user2", "repo1")
  55. assert.NoError(t, err)
  56. oid := storeObjectInRepo(t, repo.ID, content)
  57. defer repo.RemoveLFSMetaObjectByOid(oid)
  58. session := loginUser(t, "user2")
  59. // Request OID
  60. req := NewRequest(t, "GET", "/user2/repo1.git/info/lfs/objects/"+oid+"/test")
  61. req.Header.Set("Accept-Encoding", "gzip")
  62. if extraHeader != nil {
  63. for key, values := range *extraHeader {
  64. for _, value := range values {
  65. req.Header.Add(key, value)
  66. }
  67. }
  68. }
  69. resp := session.MakeRequest(t, req, expectedStatus)
  70. return resp
  71. }
  72. func checkResponseTestContentEncoding(t *testing.T, content *[]byte, resp *httptest.ResponseRecorder, expectGzip bool) {
  73. contentEncoding := resp.Header().Get("Content-Encoding")
  74. if !expectGzip || !setting.EnableGzip {
  75. assert.NotContains(t, contentEncoding, "gzip")
  76. result := resp.Body.Bytes()
  77. assert.Equal(t, *content, result)
  78. } else {
  79. assert.Contains(t, contentEncoding, "gzip")
  80. gzippReader, err := gzipp.NewReader(resp.Body)
  81. assert.NoError(t, err)
  82. result, err := ioutil.ReadAll(gzippReader)
  83. assert.NoError(t, err)
  84. assert.Equal(t, *content, result)
  85. }
  86. }
  87. func TestGetLFSSmall(t *testing.T) {
  88. defer prepareTestEnv(t)()
  89. setting.CheckLFSVersion()
  90. if !setting.LFS.StartServer {
  91. t.Skip()
  92. return
  93. }
  94. content := []byte("A very small file\n")
  95. resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
  96. checkResponseTestContentEncoding(t, &content, resp, false)
  97. }
  98. func TestGetLFSLarge(t *testing.T) {
  99. defer prepareTestEnv(t)()
  100. setting.CheckLFSVersion()
  101. if !setting.LFS.StartServer {
  102. t.Skip()
  103. return
  104. }
  105. content := make([]byte, gzip.MinSize*10)
  106. for i := range content {
  107. content[i] = byte(i % 256)
  108. }
  109. resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
  110. checkResponseTestContentEncoding(t, &content, resp, true)
  111. }
  112. func TestGetLFSGzip(t *testing.T) {
  113. defer prepareTestEnv(t)()
  114. setting.CheckLFSVersion()
  115. if !setting.LFS.StartServer {
  116. t.Skip()
  117. return
  118. }
  119. b := make([]byte, gzip.MinSize*10)
  120. for i := range b {
  121. b[i] = byte(i % 256)
  122. }
  123. outputBuffer := bytes.NewBuffer([]byte{})
  124. gzippWriter := gzipp.NewWriter(outputBuffer)
  125. gzippWriter.Write(b)
  126. gzippWriter.Close()
  127. content := outputBuffer.Bytes()
  128. resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
  129. checkResponseTestContentEncoding(t, &content, resp, false)
  130. }
  131. func TestGetLFSZip(t *testing.T) {
  132. defer prepareTestEnv(t)()
  133. setting.CheckLFSVersion()
  134. if !setting.LFS.StartServer {
  135. t.Skip()
  136. return
  137. }
  138. b := make([]byte, gzip.MinSize*10)
  139. for i := range b {
  140. b[i] = byte(i % 256)
  141. }
  142. outputBuffer := bytes.NewBuffer([]byte{})
  143. zipWriter := zip.NewWriter(outputBuffer)
  144. fileWriter, err := zipWriter.Create("default")
  145. assert.NoError(t, err)
  146. fileWriter.Write(b)
  147. zipWriter.Close()
  148. content := outputBuffer.Bytes()
  149. resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
  150. checkResponseTestContentEncoding(t, &content, resp, false)
  151. }
  152. func TestGetLFSRangeNo(t *testing.T) {
  153. defer prepareTestEnv(t)()
  154. setting.CheckLFSVersion()
  155. if !setting.LFS.StartServer {
  156. t.Skip()
  157. return
  158. }
  159. content := []byte("123456789\n")
  160. resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
  161. assert.Equal(t, content, resp.Body.Bytes())
  162. }
  163. func TestGetLFSRange(t *testing.T) {
  164. defer prepareTestEnv(t)()
  165. setting.CheckLFSVersion()
  166. if !setting.LFS.StartServer {
  167. t.Skip()
  168. return
  169. }
  170. content := []byte("123456789\n")
  171. tests := []struct {
  172. in string
  173. out string
  174. status int
  175. }{
  176. {"bytes=0-0", "1", http.StatusPartialContent},
  177. {"bytes=0-1", "12", http.StatusPartialContent},
  178. {"bytes=1-1", "2", http.StatusPartialContent},
  179. {"bytes=1-3", "234", http.StatusPartialContent},
  180. {"bytes=1-", "23456789\n", http.StatusPartialContent},
  181. // end-range smaller than start-range is ignored
  182. {"bytes=1-0", "23456789\n", http.StatusPartialContent},
  183. {"bytes=0-10", "123456789\n", http.StatusPartialContent},
  184. // end-range bigger than length-1 is ignored
  185. {"bytes=0-11", "123456789\n", http.StatusPartialContent},
  186. {"bytes=11-", "", http.StatusPartialContent},
  187. // incorrect header value cause whole header to be ignored
  188. {"bytes=-", "123456789\n", http.StatusOK},
  189. {"foobar", "123456789\n", http.StatusOK},
  190. }
  191. for _, tt := range tests {
  192. t.Run(tt.in, func(t *testing.T) {
  193. h := http.Header{
  194. "Range": []string{tt.in},
  195. }
  196. resp := storeAndGetLfs(t, &content, &h, tt.status)
  197. assert.Equal(t, tt.out, resp.Body.String())
  198. })
  199. }
  200. }