2017-06-27 20:54:42 +00:00
|
|
|
// Copyright 2013 Julien Schmidt. All rights reserved.
|
|
|
|
// Based on the path package, Copyright 2009 The Go Authors.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be found
|
2017-06-27 20:58:49 +00:00
|
|
|
// at https://github.com/julienschmidt/httprouter/blob/master/LICENSE
|
2015-04-09 10:15:02 +00:00
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
2024-03-14 03:22:54 +00:00
|
|
|
"runtime"
|
2020-01-07 09:37:18 +00:00
|
|
|
"strings"
|
2015-04-09 10:15:02 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2020-01-07 09:37:18 +00:00
|
|
|
type cleanPathTest struct {
|
2015-04-09 10:15:02 +00:00
|
|
|
path, result string
|
2020-01-07 09:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var cleanTests = []cleanPathTest{
|
2015-04-09 10:15:02 +00:00
|
|
|
// Already clean
|
|
|
|
{"/", "/"},
|
|
|
|
{"/abc", "/abc"},
|
|
|
|
{"/a/b/c", "/a/b/c"},
|
|
|
|
{"/abc/", "/abc/"},
|
|
|
|
{"/a/b/c/", "/a/b/c/"},
|
|
|
|
|
|
|
|
// missing root
|
|
|
|
{"", "/"},
|
2018-06-23 03:06:27 +00:00
|
|
|
{"a/", "/a/"},
|
2015-04-09 10:15:02 +00:00
|
|
|
{"abc", "/abc"},
|
|
|
|
{"abc/def", "/abc/def"},
|
|
|
|
{"a/b/c", "/a/b/c"},
|
|
|
|
|
|
|
|
// Remove doubled slash
|
|
|
|
{"//", "/"},
|
|
|
|
{"/abc//", "/abc/"},
|
|
|
|
{"/abc/def//", "/abc/def/"},
|
|
|
|
{"/a/b/c//", "/a/b/c/"},
|
|
|
|
{"/abc//def//ghi", "/abc/def/ghi"},
|
|
|
|
{"//abc", "/abc"},
|
|
|
|
{"///abc", "/abc"},
|
|
|
|
{"//abc//", "/abc/"},
|
|
|
|
|
|
|
|
// Remove . elements
|
|
|
|
{".", "/"},
|
|
|
|
{"./", "/"},
|
|
|
|
{"/abc/./def", "/abc/def"},
|
|
|
|
{"/./abc/def", "/abc/def"},
|
|
|
|
{"/abc/.", "/abc/"},
|
|
|
|
|
|
|
|
// Remove .. elements
|
|
|
|
{"..", "/"},
|
|
|
|
{"../", "/"},
|
|
|
|
{"../../", "/"},
|
|
|
|
{"../..", "/"},
|
|
|
|
{"../../abc", "/abc"},
|
|
|
|
{"/abc/def/ghi/../jkl", "/abc/def/jkl"},
|
|
|
|
{"/abc/def/../ghi/../jkl", "/abc/jkl"},
|
|
|
|
{"/abc/def/..", "/abc"},
|
|
|
|
{"/abc/def/../..", "/"},
|
|
|
|
{"/abc/def/../../..", "/"},
|
|
|
|
{"/abc/def/../../..", "/"},
|
|
|
|
{"/abc/def/../../../ghi/jkl/../../../mno", "/mno"},
|
|
|
|
|
|
|
|
// Combinations
|
|
|
|
{"abc/./../def", "/def"},
|
|
|
|
{"abc//./../def", "/def"},
|
|
|
|
{"abc/../../././../def", "/def"},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathClean(t *testing.T) {
|
|
|
|
for _, test := range cleanTests {
|
2017-11-21 13:18:45 +00:00
|
|
|
assert.Equal(t, test.result, cleanPath(test.path))
|
|
|
|
assert.Equal(t, test.result, cleanPath(test.result))
|
2015-04-09 10:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathCleanMallocs(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping malloc count in short mode")
|
|
|
|
}
|
|
|
|
|
2024-03-14 03:22:54 +00:00
|
|
|
if runtime.GOMAXPROCS(0) > 1 {
|
|
|
|
t.Skip("skipping malloc count; GOMAXPROCS>1")
|
|
|
|
}
|
|
|
|
|
2015-04-09 10:15:02 +00:00
|
|
|
for _, test := range cleanTests {
|
2015-05-30 12:45:13 +00:00
|
|
|
allocs := testing.AllocsPerRun(100, func() { cleanPath(test.result) })
|
2024-07-14 12:33:08 +00:00
|
|
|
assert.InDelta(t, 0, allocs, 0.01)
|
2015-04-09 10:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-07 09:37:18 +00:00
|
|
|
|
|
|
|
func BenchmarkPathClean(b *testing.B) {
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for _, test := range cleanTests {
|
|
|
|
cleanPath(test.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func genLongPaths() (testPaths []cleanPathTest) {
|
|
|
|
for i := 1; i <= 1234; i++ {
|
|
|
|
ss := strings.Repeat("a", i)
|
|
|
|
|
|
|
|
correctPath := "/" + ss
|
|
|
|
testPaths = append(testPaths, cleanPathTest{
|
|
|
|
path: correctPath,
|
|
|
|
result: correctPath,
|
|
|
|
}, cleanPathTest{
|
|
|
|
path: ss,
|
|
|
|
result: correctPath,
|
|
|
|
}, cleanPathTest{
|
|
|
|
path: "//" + ss,
|
|
|
|
result: correctPath,
|
|
|
|
}, cleanPathTest{
|
|
|
|
path: "/" + ss + "/b/..",
|
|
|
|
result: correctPath,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathCleanLong(t *testing.T) {
|
|
|
|
cleanTests := genLongPaths()
|
|
|
|
|
|
|
|
for _, test := range cleanTests {
|
|
|
|
assert.Equal(t, test.result, cleanPath(test.path))
|
|
|
|
assert.Equal(t, test.result, cleanPath(test.result))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkPathCleanLong(b *testing.B) {
|
|
|
|
cleanTests := genLongPaths()
|
|
|
|
b.ResetTimer()
|
|
|
|
b.ReportAllocs()
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
for _, test := range cleanTests {
|
|
|
|
cleanPath(test.path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|