File: path_test.go

package info (click to toggle)
golang-github-containers-storage 1.59.1%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,184 kB
  • sloc: sh: 630; ansic: 389; makefile: 143; awk: 12
file content (63 lines) | stat: -rw-r--r-- 1,697 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package path

import (
	"fmt"
	"testing"

	"github.com/opencontainers/go-digest"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestCleanAbsPath(t *testing.T) {
	tests := []struct {
		path     string
		expected string
	}{
		{"", "/"},
		{".", "/"},
		{"..", "/"},
		{"foo/../..", "/"},
		{"/foo/../..", "/"},
		{"./", "/"},
		{"../", "/"},
		{"/../", "/"},
		{"/./", "/"},
		{"foo", "/foo"},
		{"foo/bar", "/foo/bar"},
		{"/foo/bar/../baz", "/foo/baz"},
		{"/foo/./bar", "/foo/bar"},
		{"/foo/bar/../../baz", "/baz"},
		{"/././foo", "/foo"},
		{"../foo", "/foo"},
		{"./foo/bar/../..", "/"},
		{"foo/..", "/"},
		{"foo/../bar", "/bar"},
		{"//foo//bar", "/foo/bar"},
		{"foo/bar//baz/..", "/foo/bar"},
		{"../..", "/"},
		{".././..", "/"},
		{"../../.", "/"},
		{"/../../foo", "/foo"},
		{"../foo/bar/../baz", "/foo/baz"},
		{"../.././/.//../foo/./../bar/..", "/"},
		{"a/../.././/.//../foo/./../bar/..", "/"},
	}

	for _, test := range tests {
		assert.Equal(t, test.expected, CleanAbsPath(test.path), fmt.Sprintf("path %q failed", test.path))
	}
}

func TestRegularFilePathForValidatedDigest(t *testing.T) {
	d, err := digest.Parse("sha256:0123456789abcdef1123456789abcdef2123456789abcdef3123456789abcdef")
	require.NoError(t, err)
	res, err := RegularFilePathForValidatedDigest(d)
	require.NoError(t, err)
	assert.Equal(t, "01/23456789abcdef1123456789abcdef2123456789abcdef3123456789abcdef", res)

	d, err = digest.Parse("sha512:0123456789abcdef1123456789abcdef2123456789abcdef3123456789abcdef0123456789abcdef1123456789abcdef2123456789abcdef3123456789abcdef")
	require.NoError(t, err)
	_, err = RegularFilePathForValidatedDigest(d)
	assert.Error(t, err)
}