2017-06-12 12:01:09 +03:00
|
|
|
// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
2015-04-09 13:15:02 +03:00
|
|
|
|
|
|
|
package gin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
var cleanTests = []struct {
|
|
|
|
path, result string
|
|
|
|
}{
|
|
|
|
// Already clean
|
|
|
|
{"/", "/"},
|
|
|
|
{"/abc", "/abc"},
|
|
|
|
{"/a/b/c", "/a/b/c"},
|
|
|
|
{"/abc/", "/abc/"},
|
|
|
|
{"/a/b/c/", "/a/b/c/"},
|
|
|
|
|
|
|
|
// missing root
|
|
|
|
{"", "/"},
|
|
|
|
{"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 {
|
2015-05-30 15:45:13 +03:00
|
|
|
assert.Equal(t, cleanPath(test.path), test.result)
|
|
|
|
assert.Equal(t, cleanPath(test.result), test.result)
|
2015-04-09 13:15:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPathCleanMallocs(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("skipping malloc count in short mode")
|
|
|
|
}
|
|
|
|
if runtime.GOMAXPROCS(0) > 1 {
|
|
|
|
t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range cleanTests {
|
2015-05-30 15:45:13 +03:00
|
|
|
allocs := testing.AllocsPerRun(100, func() { cleanPath(test.result) })
|
2015-05-31 23:35:49 +03:00
|
|
|
assert.EqualValues(t, allocs, 0)
|
2015-04-09 13:15:02 +03:00
|
|
|
}
|
|
|
|
}
|