From b591735919e9aa3e387c529093baecb5ffa479ab Mon Sep 17 00:00:00 2001 From: Adrian-George Bostan Date: Wed, 8 Sep 2021 13:17:02 +0300 Subject: [PATCH 1/2] Minor internal refactor --- utils.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/utils.go b/utils.go index 8cd8104..cf3e174 100644 --- a/utils.go +++ b/utils.go @@ -101,20 +101,21 @@ func xdgPaths(name string, defaultPaths ...string) []string { } func uniquePaths(paths []string) []string { - var uniq []string - registry := map[string]struct{}{} + var ( + uniq []string + registry = map[string]struct{}{} + ) for _, p := range paths { dir := expandPath(p, Home) - if dir == "" || !filepath.IsAbs(dir) { - continue - } - if _, ok := registry[dir]; ok { - continue - } + if dir != "" && filepath.IsAbs(dir) { + if _, ok := registry[dir]; ok { + continue + } - registry[dir] = struct{}{} - uniq = append(uniq, dir) + registry[dir] = struct{}{} + uniq = append(uniq, dir) + } } return uniq From 5a48d5cb40d3638337ce59120791766f50faef37 Mon Sep 17 00:00:00 2001 From: Adrian-George Bostan Date: Wed, 8 Sep 2021 13:17:23 +0300 Subject: [PATCH 2/2] Add invalid paths test case --- xdg_test.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/xdg_test.go b/xdg_test.go index ef8fa19..e1f6290 100644 --- a/xdg_test.go +++ b/xdg_test.go @@ -60,6 +60,11 @@ func TestBaseDirFuncs(t *testing.T) { pathFunc: xdg.ConfigFile, searchFunc: xdg.SearchConfigFile, }, + { + relPaths: []string{"app.state", "appname/app.state"}, + pathFunc: xdg.StateFile, + searchFunc: xdg.SearchStateFile, + }, { relPaths: []string{"app.cache", "appname/app.cache"}, pathFunc: xdg.CacheFile, @@ -70,11 +75,6 @@ func TestBaseDirFuncs(t *testing.T) { pathFunc: xdg.RuntimeFile, searchFunc: xdg.SearchRuntimeFile, }, - { - relPaths: []string{"app.state", "appname/app.state"}, - pathFunc: xdg.StateFile, - searchFunc: xdg.SearchStateFile, - }, } // Test base directories for regular files. @@ -167,3 +167,18 @@ func testBaseDirsSymlinks(t *testing.T, inputs []*testInputData) { } } } + +func TestInvalidPaths(t *testing.T) { + inputs := map[string]func(string) (string, error){ + "\000/app.data": xdg.DataFile, + "appname\000/app.yaml": xdg.ConfigFile, + "appname/\000/app.state": xdg.StateFile, + "\000appname/app.cache": xdg.CacheFile, + "\000/appname/app.pid": xdg.RuntimeFile, + } + + for inputPath, xdgFunc := range inputs { + _, err := xdgFunc(inputPath) + require.Error(t, err) + } +}