1// Copyright 2013 Julien Schmidt. All rights reserved. 2// Based on the path package, Copyright 2009 The Go Authors. 3// Use of this source code is governed by a BSD-style license that can be found 4// in the LICENSE file. 5 6package httprouter 7 8import ( 9 "runtime" 10 "testing" 11) 12 13var cleanTests = []struct { 14 path, result string 15}{ 16 // Already clean 17 {"/", "/"}, 18 {"/abc", "/abc"}, 19 {"/a/b/c", "/a/b/c"}, 20 {"/abc/", "/abc/"}, 21 {"/a/b/c/", "/a/b/c/"}, 22 23 // missing root 24 {"", "/"}, 25 {"a/", "/a/"}, 26 {"abc", "/abc"}, 27 {"abc/def", "/abc/def"}, 28 {"a/b/c", "/a/b/c"}, 29 30 // Remove doubled slash 31 {"//", "/"}, 32 {"/abc//", "/abc/"}, 33 {"/abc/def//", "/abc/def/"}, 34 {"/a/b/c//", "/a/b/c/"}, 35 {"/abc//def//ghi", "/abc/def/ghi"}, 36 {"//abc", "/abc"}, 37 {"///abc", "/abc"}, 38 {"//abc//", "/abc/"}, 39 40 // Remove . elements 41 {".", "/"}, 42 {"./", "/"}, 43 {"/abc/./def", "/abc/def"}, 44 {"/./abc/def", "/abc/def"}, 45 {"/abc/.", "/abc/"}, 46 47 // Remove .. elements 48 {"..", "/"}, 49 {"../", "/"}, 50 {"../../", "/"}, 51 {"../..", "/"}, 52 {"../../abc", "/abc"}, 53 {"/abc/def/ghi/../jkl", "/abc/def/jkl"}, 54 {"/abc/def/../ghi/../jkl", "/abc/jkl"}, 55 {"/abc/def/..", "/abc"}, 56 {"/abc/def/../..", "/"}, 57 {"/abc/def/../../..", "/"}, 58 {"/abc/def/../../..", "/"}, 59 {"/abc/def/../../../ghi/jkl/../../../mno", "/mno"}, 60 61 // Combinations 62 {"abc/./../def", "/def"}, 63 {"abc//./../def", "/def"}, 64 {"abc/../../././../def", "/def"}, 65} 66 67func TestPathClean(t *testing.T) { 68 for _, test := range cleanTests { 69 if s := CleanPath(test.path); s != test.result { 70 t.Errorf("CleanPath(%q) = %q, want %q", test.path, s, test.result) 71 } 72 if s := CleanPath(test.result); s != test.result { 73 t.Errorf("CleanPath(%q) = %q, want %q", test.result, s, test.result) 74 } 75 } 76} 77 78func TestPathCleanMallocs(t *testing.T) { 79 if testing.Short() { 80 t.Skip("skipping malloc count in short mode") 81 } 82 if runtime.GOMAXPROCS(0) > 1 { 83 t.Log("skipping AllocsPerRun checks; GOMAXPROCS>1") 84 return 85 } 86 87 for _, test := range cleanTests { 88 allocs := testing.AllocsPerRun(100, func() { CleanPath(test.result) }) 89 if allocs > 0 { 90 t.Errorf("CleanPath(%q): %v allocs, want zero", test.result, allocs) 91 } 92 } 93} 94