Files
forge/tests/integration/lfs_getobject_test.go
zeekayandHanzo Dev 9a07b4d089 legal: Hanzo AI, Inc. copyright above the upstream notices
2978 files. The fork is 2026-07-15 (rebrand: gitea.dev -> github.com/hanzoai/git),
so 2026 is the year, and "Copyright 2026 Hanzo AI, Inc." was already the form
used by the handful of files that had ours.

Shape is now:

	// Copyright 2026 Hanzo AI, Inc. All rights reserved.
	// Copyright 2015 The Gogs Authors. All rights reserved.
	// Copyright 2019 The Gitea Authors. All rights reserved.
	// SPDX-License-Identifier: MIT

Ours on top, theirs retained verbatim. Retained because MIT requires it, and
separately because they are true — those authors did write that code.

Not renamed to "The Git Authors": Git is a different project under a different
license, and putting its name here would attribute this code to people who have
nothing to do with it. Adding our line is the fix; overwriting theirs would have
been both a licence violation and a false statement.

gofmt still reports import-order drift in some of these files. It predates this
change (verified against a stash) and is left alone rather than burying a
one-line-per-file legal change under 3000 files of unrelated reordering.

Build clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-27 13:49:52 -07:00

209 lines
6.6 KiB
Go

// Copyright 2026 Hanzo AI, Inc. All rights reserved.
// Copyright 2019 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"bytes"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/hanzoai/git/models/auth"
git_model "github.com/hanzoai/git/models/git"
repo_model "github.com/hanzoai/git/models/repo"
"github.com/hanzoai/git/modules/json"
"github.com/hanzoai/git/modules/lfs"
"github.com/hanzoai/git/modules/setting"
"github.com/hanzoai/git/modules/test"
"github.com/hanzoai/git/routers/web"
"github.com/hanzoai/git/tests"
gzipp "github.com/klauspost/compress/gzip"
"github.com/stretchr/testify/assert"
)
func storeObjectInRepo(t *testing.T, repositoryID int64, content string) string {
pointer, err := lfs.GeneratePointer(strings.NewReader(content))
assert.NoError(t, err)
_, err = git_model.NewLFSMetaObject(t.Context(), repositoryID, pointer)
assert.NoError(t, err)
contentStore := lfs.NewContentStore()
exist, err := contentStore.Exists(pointer)
assert.NoError(t, err)
if !exist {
err := contentStore.Put(pointer, strings.NewReader(content))
assert.NoError(t, err)
}
return pointer.Oid
}
func storeAndGetLfsToken(t *testing.T, content string, extraHeader *http.Header, expectedStatus int, ts ...auth.AccessTokenScope) *httptest.ResponseRecorder {
repo, err := repo_model.GetRepositoryByOwnerAndName(t.Context(), "user2", "repo1")
assert.NoError(t, err)
oid := storeObjectInRepo(t, repo.ID, content)
defer git_model.RemoveLFSMetaObjectByOid(t.Context(), repo.ID, oid)
token := getUserToken(t, "user2", ts...)
// Request OID
req := NewRequest(t, "GET", "/user2/repo1.git/info/lfs/objects/"+oid+"/test")
req.Header.Set("Accept-Encoding", "gzip")
req.SetBasicAuth("user2", token)
if extraHeader != nil {
for key, values := range *extraHeader {
for _, value := range values {
req.Header.Add(key, value)
}
}
}
resp := MakeRequest(t, req, expectedStatus)
return resp
}
func storeAndGetLfs(t *testing.T, content string, extraHeader *http.Header, expectedStatus int) *httptest.ResponseRecorder {
repo, err := repo_model.GetRepositoryByOwnerAndName(t.Context(), "user2", "repo1")
assert.NoError(t, err)
oid := storeObjectInRepo(t, repo.ID, content)
defer git_model.RemoveLFSMetaObjectByOid(t.Context(), repo.ID, oid)
session := loginUser(t, "user2")
// Request OID
req := NewRequest(t, "GET", "/user2/repo1.git/info/lfs/objects/"+oid+"/test")
req.Header.Set("Accept-Encoding", "gzip")
if extraHeader != nil {
for key, values := range *extraHeader {
for _, value := range values {
req.Header.Add(key, value)
}
}
}
resp := session.MakeRequest(t, req, expectedStatus)
return resp
}
func checkResponseTestContentEncoding(t *testing.T, content string, resp *httptest.ResponseRecorder, expectGzip bool) {
contentEncoding := resp.Header().Get("Content-Encoding")
if !expectGzip || !setting.EnableGzip {
assert.NotContains(t, contentEncoding, "gzip")
assert.Equal(t, content, resp.Body.String())
} else {
assert.Contains(t, contentEncoding, "gzip")
gzipReader, err := gzipp.NewReader(resp.Body)
assert.NoError(t, err)
result, err := io.ReadAll(gzipReader)
assert.NoError(t, err)
assert.Equal(t, content, string(result))
}
}
func TestLFSGetObject(t *testing.T) {
defer tests.PrepareTestEnv(t)()
t.Run("GetLFSSmall", testGetLFSSmall)
t.Run("GetLFSSmallToken", testGetLFSSmallToken)
t.Run("GetLFSSmallTokenFail", testGetLFSSmallTokenFail)
t.Run("GetLFSLarge", testGetLFSLarge)
t.Run("GetLFSGzip", testGetLFSGzip)
t.Run("GetLFSZip", testGetLFSZip)
t.Run("GetLFSRangeNo", testGetLFSRangeNo)
t.Run("GetLFSRange", testGetLFSRange)
}
func testGetLFSSmall(t *testing.T) {
content := "A very small file\n"
resp := storeAndGetLfs(t, content, nil, http.StatusOK)
checkResponseTestContentEncoding(t, content, resp, false)
}
func testGetLFSSmallToken(t *testing.T) {
content := "A very small file\n"
resp := storeAndGetLfsToken(t, content, nil, http.StatusOK, auth.AccessTokenScopePublicOnly, auth.AccessTokenScopeReadRepository)
checkResponseTestContentEncoding(t, content, resp, false)
}
func testGetLFSSmallTokenFail(t *testing.T) {
content := "A very small file\n"
storeAndGetLfsToken(t, content, nil, http.StatusForbidden, auth.AccessTokenScopeReadNotification)
}
func testGetLFSLarge(t *testing.T) {
content := strings.Repeat("a", web.GzipMinSize*10)
resp := storeAndGetLfs(t, content, nil, http.StatusOK)
checkResponseTestContentEncoding(t, content, resp, true)
}
func testGetLFSGzip(t *testing.T) {
s := strings.Repeat("a", web.GzipMinSize*10)
outputBuffer := &bytes.Buffer{}
gzipWriter := gzipp.NewWriter(outputBuffer)
_, _ = gzipWriter.Write([]byte(s))
_ = gzipWriter.Close()
content := outputBuffer.String()
resp := storeAndGetLfs(t, content, nil, http.StatusOK)
checkResponseTestContentEncoding(t, content, resp, false)
}
func testGetLFSZip(t *testing.T) {
b := strings.Repeat("a", web.GzipMinSize*10)
content := test.WriteZipArchive(map[string]string{"default": b}).String()
resp := storeAndGetLfs(t, content, nil, http.StatusOK)
checkResponseTestContentEncoding(t, content, resp, false)
}
func testGetLFSRangeNo(t *testing.T) {
content := "123456789\n"
resp := storeAndGetLfs(t, content, nil, http.StatusOK)
assert.Equal(t, content, resp.Body.String())
}
func testGetLFSRange(t *testing.T) {
content := "123456789\n"
cases := []struct {
in string
out string
status int
}{
{"bytes=0-0", "1", http.StatusPartialContent},
{"bytes=0-1", "12", http.StatusPartialContent},
{"bytes=1-1", "2", http.StatusPartialContent},
{"bytes=1-3", "234", http.StatusPartialContent},
{"bytes=1-", "23456789\n", http.StatusPartialContent},
// end-range smaller than start-range is ignored
{"bytes=1-0", "23456789\n", http.StatusPartialContent},
{"bytes=0-10", "123456789\n", http.StatusPartialContent},
// end-range bigger than length-1 is ignored
{"bytes=0-11", "123456789\n", http.StatusPartialContent},
{"bytes=11-", "Requested Range Not Satisfiable", http.StatusRequestedRangeNotSatisfiable},
// incorrect header value cause whole header to be ignored
{"bytes=-", "123456789\n", http.StatusOK},
{"foobar", "123456789\n", http.StatusOK},
}
for _, tt := range cases {
t.Run(tt.in, func(t *testing.T) {
h := http.Header{
"Range": []string{tt.in},
}
resp := storeAndGetLfs(t, content, &h, tt.status)
if tt.status == http.StatusPartialContent || tt.status == http.StatusOK {
assert.Equal(t, tt.out, resp.Body.String())
} else {
var er lfs.ErrorResponse
err := json.Unmarshal(resp.Body.Bytes(), &er)
assert.NoError(t, err)
assert.Equal(t, tt.out, er.Message)
}
})
}
}