summaryrefslogtreecommitdiffstats
path: root/integrations/lfs_getobject_test.go
blob: e9f0c580224006bfde947306fa8ca97d3ff260c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147pre { line-height: 125%; }
td.linenos .normal { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
span.linenos { color: inherit; background-color: transparent; padding-left: 5px; padding-right: 5px; }
td.linenos .special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
span.linenos.special { color: #000000; background-color: #ffffc0; padding-left: 5px; padding-right: 5px; }
.highlight .hll { background-color: #ffffcc }
.highlight .c { color: #888888 } /* Comment */
.highlight .err { color: #a61717; background-color: #e3d2d2 } /* Error */
.highlight .k { color: #008800; font-weight: bold } /* Keyword */
.highlight .ch { color: #888888 } /* Comment.Hashbang */
.highlight .cm { color: #888888 } /* Comment.Multiline */
.highlight .cp { color: #cc0000; font-weight: bold } /* Comment.Preproc */
.highlight .cpf { color: #888888 } /* Comment.PreprocFile */
.highlight .c1 { color: #888888 } /* Comment.Single */
.highlight .cs { color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */
.highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #aa0000 } /* Generic.Error */
.highlight .gh { color: #333333 } /* Generic.Heading */
.highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #555555 } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #666666 } /* Generic.Subheading */
.highlight .gt { color: #aa0000 } /* Generic.Traceback */
.highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008800 } /* Keyword.Pseudo */
.highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */
.highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */
.highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */
.highlight .na { color: #336699 } /* Name.Attribute */
.highlight .nb { color: #00
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package integrations

import (
	"archive/zip"
	"bytes"
	"io"
	"net/http"
	"net/http/httptest"
	"testing"

	"code.gitea.io/gitea/models"
	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/modules/git"
	"code.gitea.io/gitea/modules/json"
	"code.gitea.io/gitea/modules/lfs"
	"code.gitea.io/gitea/modules/setting"
	"code.gitea.io/gitea/routers/web"

	gzipp "github.com/klauspost/compress/gzip"
	"github.com/stretchr/testify/assert"
)

func storeObjectInRepo(t *testing.T, repositoryID int64, content *[]byte) string {
	pointer, err := lfs.GeneratePointer(bytes.NewReader(*content))
	assert.NoError(t, err)

	_, err = models.NewLFSMetaObject(&models.LFSMetaObject{Pointer: pointer, RepositoryID: repositoryID})
	assert.NoError(t, err)
	contentStore := lfs.NewContentStore()
	exist, err := contentStore.Exists(pointer)
	assert.NoError(t, err)
	if !exist {
		err := contentStore.Put(pointer, bytes.NewReader(*content))
		assert.NoError(t, err)
	}
	return pointer.Oid
}

func storeAndGetLfs(t *testing.T, content *[]byte, extraHeader *http.Header, expectedStatus int) *httptest.ResponseRecorder {
	repo, err := repo_model.GetRepositoryByOwnerAndName("user2", "repo1")
	assert.NoError(t, err)
	oid := storeObjectInRepo(t, repo.ID, content)
	defer models.RemoveLFSMetaObjectByOid(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 *[]byte, resp *httptest.ResponseRecorder, expectGzip bool) {
	contentEncoding := resp.Header().Get("Content-Encoding")
	if !expectGzip || !setting.EnableGzip {
		assert.NotContains(t, contentEncoding, "gzip")

		result := resp.Body.Bytes()
		assert.Equal(t, *content, result)
	} else {
		assert.Contains(t, contentEncoding, "gzip")
		gzippReader, err := gzipp.NewReader(resp.Body)
		assert.NoError(t, err)
		result, err := io.ReadAll(gzippReader)
		assert.NoError(t, err)
		assert.Equal(t, *content, result)
	}
}

func TestGetLFSSmall(t *testing.T) {
	defer prepareTestEnv(t)()
	git.CheckLFSVersion()
	if !setting.LFS.StartServer {
		t.Skip()
		return
	}
	content := []byte("A very small file\n")

	resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
	checkResponseTestContentEncoding(t, &content, resp, false)
}

func TestGetLFSLarge(t *testing.T) {
	defer prepareTestEnv(t)()
	git.CheckLFSVersion()
	if !setting.LFS.StartServer {
		t.Skip()
		return
	}
	content := make([]byte, web.GzipMinSize*10)
	for i := range content {
		content[i] = byte(i % 256)
	}

	resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
	checkResponseTestContentEncoding(t, &content, resp, true)
}

func TestGetLFSGzip(t *testing.T) {
	defer prepareTestEnv(t)()
	git.CheckLFSVersion()
	if !setting.LFS.StartServer {
		t.Skip()
		return
	}
	b := make([]byte, web.GzipMinSize*10)
	for i := range b {
		b[i] = byte(i % 256)
	}
	outputBuffer := bytes.NewBuffer([]byte{})
	gzippWriter := gzipp.NewWriter(outputBuffer)
	gzippWriter.Write(b)
	gzippWriter.Close()
	content := outputBuffer.Bytes()

	resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
	checkResponseTestContentEncoding(t, &content, resp, false)
}

func TestGetLFSZip(t *testing.T) {
	defer prepareTestEnv(t)()
	git.CheckLFSVersion()
	if !setting.LFS.StartServer {
		t.Skip()
		return
	}
	b := make([]byte, web.GzipMinSize*10)
	for i := range b {
		b[i] = byte(i % 256)
	}
	outputBuffer := bytes.NewBuffer([]byte{})
	zipWriter := zip.NewWriter(outputBuffer)
	fileWriter, err := zipWriter.Create("default")
	assert.NoError(t, err)
	fileWriter.Write(b)
	zipWriter.Close()
	content := outputBuffer.Bytes()

	resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
	checkResponseTestContentEncoding(t, &content, resp, false)
}

func TestGetLFSRangeNo(t *testing.T) {
	defer prepareTestEnv(t)()
	git.CheckLFSVersion()
	if !setting.LFS.StartServer {
		t.Skip()
		return
	}
	content := []byte("123456789\n")

	resp := storeAndGetLfs(t, &content, nil, http.StatusOK)
	assert.Equal(t, content, resp.Body.Bytes())
}

func TestGetLFSRange(t *testing.T) {
	defer prepareTestEnv(t)()
	git.CheckLFSVersion()
	if !setting.LFS.StartServer {
		t.Skip()
		return
	}
	content := []byte("123456789\n")

	tests := []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 tests {
		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)
			}
		})
	}
}