44package integration
55
66import (
7- "crypto/rand"
87 "encoding/hex"
98 "fmt"
9+ "io"
10+ mathRand "math/rand/v2"
1011 "net/http"
1112 "net/url"
1213 "os"
@@ -34,8 +35,8 @@ import (
3435)
3536
3637const (
37- littleSize = 1024 // 1K
38- bigSize = 128 * 1024 * 1024 // 128M
38+ testFileSizeSmall = 10
39+ testFileSizeLarge = 10 * 1024 * 1024 // 10M
3940)
4041
4142func TestGitGeneral (t * testing.T ) {
@@ -73,8 +74,8 @@ func testGitGeneral(t *testing.T, u *url.URL) {
7374
7475 t .Run ("Partial Clone" , doPartialGitClone (dstPath2 , u ))
7576
76- pushedFilesStandard := standardCommitAndPushTest (t , dstPath , littleSize , bigSize )
77- pushedFilesLFS := lfsCommitAndPushTest (t , dstPath , littleSize , bigSize )
77+ pushedFilesStandard := standardCommitAndPushTest (t , dstPath , testFileSizeSmall , testFileSizeLarge )
78+ pushedFilesLFS := lfsCommitAndPushTest (t , dstPath , testFileSizeSmall , testFileSizeLarge )
7879 rawTest (t , & httpContext , pushedFilesStandard [0 ], pushedFilesStandard [1 ], pushedFilesLFS [0 ], pushedFilesLFS [1 ])
7980 mediaTest (t , & httpContext , pushedFilesStandard [0 ], pushedFilesStandard [1 ], pushedFilesLFS [0 ], pushedFilesLFS [1 ])
8081
@@ -114,8 +115,8 @@ func testGitGeneral(t *testing.T, u *url.URL) {
114115
115116 t .Run ("Clone" , doGitClone (dstPath , sshURL ))
116117
117- pushedFilesStandard := standardCommitAndPushTest (t , dstPath , littleSize , bigSize )
118- pushedFilesLFS := lfsCommitAndPushTest (t , dstPath , littleSize , bigSize )
118+ pushedFilesStandard := standardCommitAndPushTest (t , dstPath , testFileSizeSmall , testFileSizeLarge )
119+ pushedFilesLFS := lfsCommitAndPushTest (t , dstPath , testFileSizeSmall , testFileSizeLarge )
119120 rawTest (t , & sshContext , pushedFilesStandard [0 ], pushedFilesStandard [1 ], pushedFilesLFS [0 ], pushedFilesLFS [1 ])
120121 mediaTest (t , & sshContext , pushedFilesStandard [0 ], pushedFilesStandard [1 ], pushedFilesLFS [0 ], pushedFilesLFS [1 ])
121122
@@ -202,28 +203,28 @@ func rawTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS s
202203 // Request raw paths
203204 req := NewRequest (t , "GET" , path .Join ("/" , username , reponame , "/raw/branch/master/" , little ))
204205 resp := session .MakeRequestNilResponseRecorder (t , req , http .StatusOK )
205- assert .Equal (t , littleSize , resp .Length )
206+ assert .Equal (t , testFileSizeSmall , resp .Length )
206207
207208 if setting .LFS .StartServer {
208209 req = NewRequest (t , "GET" , path .Join ("/" , username , reponame , "/raw/branch/master/" , littleLFS ))
209210 resp := session .MakeRequest (t , req , http .StatusOK )
210- assert .NotEqual (t , littleSize , resp .Body .Len ())
211+ assert .NotEqual (t , testFileSizeSmall , resp .Body .Len ())
211212 assert .LessOrEqual (t , resp .Body .Len (), 1024 )
212- if resp .Body .Len () != littleSize && resp .Body .Len () <= 1024 {
213+ if resp .Body .Len () != testFileSizeSmall && resp .Body .Len () <= 1024 {
213214 assert .Contains (t , resp .Body .String (), lfs .MetaFileIdentifier )
214215 }
215216 }
216217
217218 if ! testing .Short () {
218219 req = NewRequest (t , "GET" , path .Join ("/" , username , reponame , "/raw/branch/master/" , big ))
219220 resp := session .MakeRequestNilResponseRecorder (t , req , http .StatusOK )
220- assert .Equal (t , bigSize , resp .Length )
221+ assert .Equal (t , testFileSizeLarge , resp .Length )
221222
222223 if setting .LFS .StartServer {
223224 req = NewRequest (t , "GET" , path .Join ("/" , username , reponame , "/raw/branch/master/" , bigLFS ))
224225 resp := session .MakeRequest (t , req , http .StatusOK )
225- assert .NotEqual (t , bigSize , resp .Body .Len ())
226- if resp .Body .Len () != bigSize && resp .Body .Len () <= 1024 {
226+ assert .NotEqual (t , testFileSizeLarge , resp .Body .Len ())
227+ if resp .Body .Len () != testFileSizeLarge && resp .Body .Len () <= 1024 {
227228 assert .Contains (t , resp .Body .String (), lfs .MetaFileIdentifier )
228229 }
229230 }
@@ -243,21 +244,21 @@ func mediaTest(t *testing.T, ctx *APITestContext, little, big, littleLFS, bigLFS
243244 // Request media paths
244245 req := NewRequest (t , "GET" , path .Join ("/" , username , reponame , "/media/branch/master/" , little ))
245246 resp := session .MakeRequestNilResponseRecorder (t , req , http .StatusOK )
246- assert .Equal (t , littleSize , resp .Length )
247+ assert .Equal (t , testFileSizeSmall , resp .Length )
247248
248249 req = NewRequest (t , "GET" , path .Join ("/" , username , reponame , "/media/branch/master/" , littleLFS ))
249250 resp = session .MakeRequestNilResponseRecorder (t , req , http .StatusOK )
250- assert .Equal (t , littleSize , resp .Length )
251+ assert .Equal (t , testFileSizeSmall , resp .Length )
251252
252253 if ! testing .Short () {
253254 req = NewRequest (t , "GET" , path .Join ("/" , username , reponame , "/media/branch/master/" , big ))
254255 resp = session .MakeRequestNilResponseRecorder (t , req , http .StatusOK )
255- assert .Equal (t , bigSize , resp .Length )
256+ assert .Equal (t , testFileSizeLarge , resp .Length )
256257
257258 if setting .LFS .StartServer {
258259 req = NewRequest (t , "GET" , path .Join ("/" , username , reponame , "/media/branch/master/" , bigLFS ))
259260 resp = session .MakeRequestNilResponseRecorder (t , req , http .StatusOK )
260- assert .Equal (t , bigSize , resp .Length )
261+ assert .Equal (t , testFileSizeLarge , resp .Length )
261262 }
262263 }
263264 })
@@ -287,35 +288,19 @@ func doCommitAndPush(t *testing.T, size int, repoPath, prefix string) string {
287288}
288289
289290func generateCommitWithNewData (size int , repoPath , email , fullName , prefix string ) (string , error ) {
290- // Generate random file
291- bufSize := 4 * 1024
292- if bufSize > size {
293- bufSize = size
294- }
295-
296- buffer := make ([]byte , bufSize )
297-
298291 tmpFile , err := os .CreateTemp (repoPath , prefix )
299292 if err != nil {
300293 return "" , err
301294 }
302295 defer tmpFile .Close ()
303- written := 0
304- for written < size {
305- n := size - written
306- if n > bufSize {
307- n = bufSize
308- }
309- _ , err := rand .Read (buffer [:n ])
310- if err != nil {
311- return "" , err
312- }
313- n , err = tmpFile .Write (buffer [:n ])
314- if err != nil {
315- return "" , err
316- }
317- written += n
296+
297+ var seed [32 ]byte
298+ rander := mathRand .NewChaCha8 (seed ) // for testing only, no need to seed
299+ _ , err = io .CopyN (tmpFile , rander , int64 (size ))
300+ if err != nil {
301+ return "" , err
318302 }
303+ _ = tmpFile .Close ()
319304
320305 // Commit
321306 // Now here we should explicitly allow lfs filters to run
@@ -355,7 +340,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
355340
356341 // Try to push without permissions, which should fail
357342 t .Run ("TryPushWithoutPermissions" , func (t * testing.T ) {
358- _ ,
err := generateCommitWithNewData (
littleSize ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
343+ _ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
359344 assert .NoError (t , err )
360345 doGitPushTestRepositoryFail (dstPath , "origin" , "protected" )
361346 })
@@ -367,7 +352,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
367352
368353 // Normal push should work
369354 t .Run ("NormalPushWithPermissions" , func (t * testing.T ) {
370- _ ,
err := generateCommitWithNewData (
littleSize ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
355+ _ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
371356 assert .NoError (t , err )
372357 doGitPushTestRepository (dstPath , "origin" , "protected" )
373358 })
@@ -376,7 +361,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
376361 t .Run ("ForcePushWithoutForcePermissions" , func (t * testing.T ) {
377362 t .Run ("CreateDivergentHistory" , func (t * testing.T ) {
378363 git .NewCommand (git .DefaultContext , "reset" , "--hard" , "HEAD~1" ).Run (& git.RunOpts {Dir : dstPath })
379- _ ,
err := generateCommitWithNewData (
littleSize ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-new" )
364+ _ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-new" )
380365 assert .NoError (t , err )
381366 })
382367 doGitPushTestRepositoryFail (dstPath , "-f" , "origin" , "protected" )
@@ -411,7 +396,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
411396 assert .NoError (t , err )
412397 })
413398 t .Run ("GenerateCommit" , func (t * testing.T ) {
414- _ ,
err := generateCommitWithNewData (
littleSize ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
399+ _ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
415400 assert .NoError (t , err )
416401 })
417402 t .Run ("PushToUnprotectedBranch" , doGitPushTestRepository (dstPath , "origin" , "protected:unprotected-2" ))
@@ -426,7 +411,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
426411
427412 t .Run ("ProtectProtectedBranchUnprotectedFilePaths" , doProtectBranch (ctx , "protected" , "" , "" , "unprotected-file-*" ))
428413 t .Run ("GenerateCommit" , func (t * testing.T ) {
429- _ ,
err := generateCommitWithNewData (
littleSize ,
dstPath ,
"[email protected] " ,
"User Two" ,
"unprotected-file-" )
414+ _ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"unprotected-file-" )
430415 assert .NoError (t , err )
431416 })
432417 t .Run ("PushUnprotectedFilesToProtectedBranch" , doGitPushTestRepository (dstPath , "origin" , "protected" ))
@@ -436,7 +421,7 @@ func doBranchProtectPRMerge(baseCtx *APITestContext, dstPath string) func(t *tes
436421 t .Run ("CheckoutMaster" , doGitCheckoutBranch (dstPath , "master" ))
437422 t .Run ("CreateBranchForced" , doGitCreateBranch (dstPath , "toforce" ))
438423 t .Run ("GenerateCommit" , func (t * testing.T ) {
439- _ ,
err := generateCommitWithNewData (
littleSize ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
424+ _ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
440425 assert .NoError (t , err )
441426 })
442427 t .Run ("FailToForcePushToProtectedBranch" , doGitPushTestRepositoryFail (dstPath , "-f" , "origin" , "toforce:protected" ))
@@ -649,7 +634,7 @@ func doAutoPRMerge(baseCtx *APITestContext, dstPath string) func(t *testing.T) {
649634 t .Run ("CheckoutProtected" , doGitCheckoutBranch (dstPath , "protected" ))
650635 t .Run ("PullProtected" , doGitPull (dstPath , "origin" , "protected" ))
651636 t .Run ("GenerateCommit" , func (t * testing.T ) {
652- _ ,
err := generateCommitWithNewData (
littleSize ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
637+ _ ,
err := generateCommitWithNewData (
testFileSizeSmall ,
dstPath ,
"[email protected] " ,
"User Two" ,
"branch-data-file-" )
653638 assert .NoError (t , err )
654639 })
655640 t .Run ("PushToUnprotectedBranch" , doGitPushTestRepository (dstPath , "origin" , "protected:unprotected3" ))
0 commit comments