-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix JoinPathSegments to disallow dot segments (#221)
JoinPathSegments does not fail for dot segments but should. This PR also exports ValidatePathSegment for convenience. Signed-off-by: Andrew Harding <[email protected]>
- Loading branch information
Showing
2 changed files
with
67 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package spiffeid | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestJoinPathSegments(t *testing.T) { | ||
assertBad := func(t *testing.T, expectErr error, segments ...string) { | ||
_, err := JoinPathSegments(segments...) | ||
assert.ErrorIs(t, err, expectErr) | ||
} | ||
assertOK := func(t *testing.T, expectPath string, segments ...string) { | ||
path, err := JoinPathSegments(segments...) | ||
if assert.NoError(t, err) { | ||
assert.Equal(t, expectPath, path) | ||
} | ||
} | ||
|
||
t.Run("empty", func(t *testing.T) { | ||
assertBad(t, errEmptySegment, "") | ||
}) | ||
t.Run("single dot", func(t *testing.T) { | ||
assertBad(t, errDotSegment, ".") | ||
}) | ||
t.Run("double dot", func(t *testing.T) { | ||
assertBad(t, errDotSegment, "..") | ||
}) | ||
t.Run("invalid char", func(t *testing.T) { | ||
assertBad(t, errBadPathSegmentChar, "/") | ||
}) | ||
t.Run("valid segment", func(t *testing.T) { | ||
assertOK(t, "/a", "a") | ||
}) | ||
t.Run("valid segments", func(t *testing.T) { | ||
assertOK(t, "/a/b", "a", "b") | ||
}) | ||
} | ||
|
||
func TestValidatePathSegment(t *testing.T) { | ||
t.Run("empty", func(t *testing.T) { | ||
require.ErrorIs(t, ValidatePathSegment(""), errEmptySegment) | ||
}) | ||
t.Run("single dot", func(t *testing.T) { | ||
require.ErrorIs(t, ValidatePathSegment("."), errDotSegment) | ||
}) | ||
t.Run("double dot", func(t *testing.T) { | ||
require.ErrorIs(t, ValidatePathSegment(".."), errDotSegment) | ||
}) | ||
t.Run("invalid char", func(t *testing.T) { | ||
require.ErrorIs(t, ValidatePathSegment("/"), errBadPathSegmentChar) | ||
}) | ||
t.Run("valid segment", func(t *testing.T) { | ||
require.NoError(t, ValidatePathSegment("a")) | ||
}) | ||
} |