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.

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