some more tests

This commit is contained in:
Shreyas Goenka 2023-06-19 18:32:29 +02:00
parent 32786e009a
commit 404652b95a
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
2 changed files with 24 additions and 2 deletions

View File

@ -26,7 +26,7 @@ func (p WindowsRootPath) Join(name string) (string, error) {
// Don't allow escaping the specified root using relative paths.
if !strings.HasPrefix(absPath, p.rootPath) {
return "", fmt.Errorf("relative path %s escapes root %s", name, p.rootPath)
return "", fmt.Errorf("relative path escapes root: %s", name)
}
return absPath, nil

View File

@ -2,6 +2,7 @@ package filer
import (
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
@ -42,6 +43,14 @@ func TestWindowsRootPathForRoot(t *testing.T) {
assert.Equal(t, `C:a/b`, path)
}
func notThisVolume(name string) string {
if "c" != strings.ToLower(name) {
return "c"
} else {
return "d"
}
}
func TestWindowsRootPath(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("this test is meant for windows")
@ -50,14 +59,27 @@ func TestWindowsRootPath(t *testing.T) {
tmpDir := t.TempDir()
rp := NewWindowsRootPath(t.TempDir())
parts := strings.SplitN(tmpDir, ":", 2)
volume := parts[0]
// Assert root value returned
assert.Equal(t, tmpDir, rp.Root())
// relative windows paths
path, err := rp.Join(`a\b\c`)
assert.NoError(t, err)
assert.Equal(t, tmpDir+`\a\b`, path)
path, err = rp.Join("a/b")
assert.NoError(t, err)
assert.Equal(t, tmpDir + `\a/b`, path)
assert.Equal(t, tmpDir+`\a/b`, path)
// relative path with drive specified
path, err = rp.Join(volume + ":a\b")
assert.NoError(t, err)
assert.Equal(t, tmpDir+`\a\b`, path)
// path in a different volume
_, err = rp.Join(notThisVolume(volume) + ":a\b")
assert.Contains(t, err, "relative path escapes root")
}