| 1 |
|
| 2 |
|
| 3 |
package osfs |
| 4 |
|
| 5 |
import ( |
| 6 |
"os" |
| 7 |
"path/filepath" |
| 8 |
"reflect" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/go-git/go-billy/v6" |
| 12 |
"github.com/go-git/go-billy/v6/helper/chroot" |
| 13 |
"github.com/go-git/go-billy/v6/util" |
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
) |
| 17 |
|
| 18 |
func TestOpenDoesNotCreateDir(t *testing.T) { |
| 19 |
path := t.TempDir() |
| 20 |
fs := New(path) |
| 21 |
_, err := fs.Open("dir/non-existent") |
| 22 |
assert.Error(t, err) |
| 23 |
|
| 24 |
_, err = fs.Stat(filepath.Join(path, "dir")) |
| 25 |
assert.ErrorIs(t, err, os.ErrNotExist) |
| 26 |
} |
| 27 |
|
| 28 |
func TestCapabilities(t *testing.T) { |
| 29 |
fs := New(t.TempDir()) |
| 30 |
_, ok := fs.(billy.Capable) |
| 31 |
assert.True(t, ok) |
| 32 |
|
| 33 |
caps := billy.Capabilities(fs) |
| 34 |
assert.Equal(t, billy.DefaultCapabilities&^billy.LockCapability, caps) |
| 35 |
} |
| 36 |
|
| 37 |
func TestDefault(t *testing.T) { |
| 38 |
want := &chroot.ChrootHelper{} |
| 39 |
got := Default |
| 40 |
|
| 41 |
if reflect.TypeOf(got) != reflect.TypeOf(want) { |
| 42 |
t.Errorf("wanted Default to be %T got %T", want, got) |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
func TestWithBoundOSReturnsBoundOS(t *testing.T) { |
| 47 |
got := New(t.TempDir(), WithBoundOS()) |
| 48 |
assert.IsType(t, &BoundOS{}, got) |
| 49 |
} |
| 50 |
|
| 51 |
func TestDefaultTypeIsBoundOS(t *testing.T) { |
| 52 |
got := New(t.TempDir()) |
| 53 |
assert.IsType(t, &BoundOS{}, got) |
| 54 |
} |
| 55 |
|
| 56 |
func TestBoundOSRoot(t *testing.T) { |
| 57 |
dir := t.TempDir() |
| 58 |
fs := New(dir, WithBoundOS()).(*BoundOS) |
| 59 |
assert.Equal(t, filepath.Join("/", dir), fs.Root()) |
| 60 |
} |
| 61 |
|
| 62 |
func TestBoundOSReadWrite(t *testing.T) { |
| 63 |
fs := New(t.TempDir(), WithBoundOS()) |
| 64 |
|
| 65 |
err := util.WriteFile(fs, "hello.txt", []byte("world"), 0o600) |
| 66 |
require.NoError(t, err) |
| 67 |
|
| 68 |
got, err := util.ReadFile(fs, "hello.txt") |
| 69 |
require.NoError(t, err) |
| 70 |
assert.Equal(t, []byte("world"), got) |
| 71 |
} |
| 72 |
|
| 73 |
func TestBoundOSChrootScopesPaths(t *testing.T) { |
| 74 |
fs := New(t.TempDir(), WithBoundOS()) |
| 75 |
|
| 76 |
sub, err := fs.Chroot("sub") |
| 77 |
require.NoError(t, err) |
| 78 |
assert.IsType(t, &BoundOS{}, sub) |
| 79 |
|
| 80 |
err = util.WriteFile(sub, "in-sub.txt", []byte("scoped"), 0o600) |
| 81 |
require.NoError(t, err) |
| 82 |
|
| 83 |
got, err := util.ReadFile(fs, filepath.Join("sub", "in-sub.txt")) |
| 84 |
require.NoError(t, err) |
| 85 |
assert.Equal(t, []byte("scoped"), got) |
| 86 |
} |
| 87 |
|
| 88 |
func TestBoundOSRejectsParentTraversal(t *testing.T) { |
| 89 |
fs := New(t.TempDir(), WithBoundOS()).(*BoundOS) |
| 90 |
_, err := fs.Chroot("..") |
| 91 |
assert.ErrorIs(t, err, billy.ErrCrossedBoundary) |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
var _ = New("/") |
| 97 |
var _ = New("/", WithBoundOS()) |
| 98 |
|
| 99 |
|
| 100 |
var ( |
| 101 |
_ Type = BoundOSFS |
| 102 |
) |