2021-05-15 23:32:09 +08:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Copyright 2019 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-05-15 23:32:09 +08:00
|
|
|
|
2021-06-10 01:53:16 +08:00
|
|
|
package auth
|
2021-05-15 23:32:09 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
2021-05-28 02:46:11 +08:00
|
|
|
func Test_isGitRawOrLFSPath(t *testing.T) {
|
2021-05-15 23:32:09 +08:00
|
|
|
tests := []struct {
|
|
|
|
path string
|
|
|
|
|
|
|
|
want bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"/owner/repo/git-upload-pack",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/git-receive-pack",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/info/refs",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/HEAD",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/objects/info/alternates",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/objects/info/http-alternates",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/objects/info/packs",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/objects/info/blahahsdhsdkla",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/objects/01/23456789abcdef0123456789abcdef01234567",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/objects/pack/pack-123456789012345678921234567893124567894.pack",
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/objects/pack/pack-0123456789abcdef0123456789abcdef0123456.idx",
|
|
|
|
true,
|
|
|
|
},
|
2021-05-28 02:46:11 +08:00
|
|
|
{
|
|
|
|
"/owner/repo/raw/branch/foo/fanaso",
|
|
|
|
true,
|
|
|
|
},
|
2021-05-15 23:32:09 +08:00
|
|
|
{
|
|
|
|
"/owner/repo/stars",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/notowner",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo",
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"/owner/repo/commit/123456789012345678921234567893124567894",
|
|
|
|
false,
|
|
|
|
},
|
2021-09-02 23:48:48 +08:00
|
|
|
{
|
|
|
|
"/owner/repo/releases/download/tag/repo.tar.gz",
|
|
|
|
true,
|
|
|
|
},
|
2021-05-15 23:32:09 +08:00
|
|
|
}
|
|
|
|
lfsTests := []string{
|
|
|
|
"/owner/repo/info/lfs/",
|
|
|
|
"/owner/repo/info/lfs/objects/batch",
|
|
|
|
"/owner/repo/info/lfs/objects/oid/filename",
|
|
|
|
"/owner/repo/info/lfs/objects/oid",
|
|
|
|
"/owner/repo/info/lfs/objects",
|
|
|
|
"/owner/repo/info/lfs/verify",
|
|
|
|
"/owner/repo/info/lfs/locks",
|
|
|
|
"/owner/repo/info/lfs/locks/verify",
|
|
|
|
"/owner/repo/info/lfs/locks/123/unlock",
|
|
|
|
}
|
|
|
|
|
|
|
|
origLFSStartServer := setting.LFS.StartServer
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.path, func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("POST", "http://localhost"+tt.path, nil)
|
|
|
|
setting.LFS.StartServer = false
|
2021-09-02 23:48:48 +08:00
|
|
|
if got := isGitRawReleaseOrLFSPath(req); got != tt.want {
|
2021-05-15 23:32:09 +08:00
|
|
|
t.Errorf("isGitOrLFSPath() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
setting.LFS.StartServer = true
|
2021-09-02 23:48:48 +08:00
|
|
|
if got := isGitRawReleaseOrLFSPath(req); got != tt.want {
|
2021-05-15 23:32:09 +08:00
|
|
|
t.Errorf("isGitOrLFSPath() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
for _, tt := range lfsTests {
|
|
|
|
t.Run(tt, func(t *testing.T) {
|
|
|
|
req, _ := http.NewRequest("POST", tt, nil)
|
|
|
|
setting.LFS.StartServer = false
|
2021-09-02 23:48:48 +08:00
|
|
|
if got := isGitRawReleaseOrLFSPath(req); got != setting.LFS.StartServer {
|
|
|
|
t.Errorf("isGitOrLFSPath(%q) = %v, want %v, %v", tt, got, setting.LFS.StartServer, gitRawReleasePathRe.MatchString(tt))
|
2021-05-15 23:32:09 +08:00
|
|
|
}
|
|
|
|
setting.LFS.StartServer = true
|
2021-09-02 23:48:48 +08:00
|
|
|
if got := isGitRawReleaseOrLFSPath(req); got != setting.LFS.StartServer {
|
2021-05-15 23:32:09 +08:00
|
|
|
t.Errorf("isGitOrLFSPath(%q) = %v, want %v", tt, got, setting.LFS.StartServer)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
setting.LFS.StartServer = origLFSStartServer
|
|
|
|
}
|