Skip to content

Commit

Permalink
Add test to account for symlinks as sources
Browse files Browse the repository at this point in the history
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
  • Loading branch information
gabriel-samfira committed Feb 13, 2023
1 parent d05c10d commit 33c45b1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/bindfilter/bind_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ func getFinalPath(pth string) (string, error) {
buf := make([]uint16, 100)
var flags uint32 = 0x0
for {
n, err := windows.GetFinalPathNameByHandle(windows.Handle(han), &buf[0], uint32(len(buf)), flags)
n, err := windows.GetFinalPathNameByHandle(han, &buf[0], uint32(len(buf)), flags)
if err != nil {
// if we mounted a volume that does not also have a drive letter assigned, attempting to
// fetch the VOLUME_NAME_DOS will fail with os.ErrNotExist. Attempt to get the VOLUME_NAME_GUID.
Expand Down
53 changes: 53 additions & 0 deletions pkg/bindfilter/bind_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,56 @@ func getWindowsBuildNumber() (int, error) {
}
return buildNum, nil
}

func TestGetBindMappingsSymlinks(t *testing.T) {
version, err := getWindowsBuildNumber()
if err != nil {
t.Fatalf("couldn't get version number: %s", err)
}

if version <= 17763 {
t.Skip("not supported on RS5 or earlier")
}

srcShort := t.TempDir()
sourceNested := filepath.Join(srcShort, "source")
if err := os.MkdirAll(sourceNested, 0600); err != nil {
t.Fatalf("failed to create folder: %s", err)
}
simlinkSource := filepath.Join(srcShort, "symlink")
if err := os.Symlink(sourceNested, simlinkSource); err != nil {
t.Fatalf("failed to create symlink: %s", err)
}

// We'll need the long form of the source folder, as we expect bfSetupFilter()
// to resolve the symlink and create a mapping to the actual source the symlink
// points to.
source, err := getFinalPath(sourceNested)
if err != nil {
t.Fatalf("failed to get long path")
}

dstShort := t.TempDir()
destination, err := getFinalPath(dstShort)
if err != nil {
t.Fatalf("failed to get long path")
}

// Use the symlink as a source for the mapping.
err = ApplyFileBinding(destination, simlinkSource, false)
if err != nil {
t.Fatal(err)
}
defer removeFileBinding(t, destination)

// We expect the mapping to point to the folder the symlink points to, not to the
// actual symlink.
hasMapping, err := checkSourceIsMountedOnDestination(source, destination)
if err != nil {
t.Fatal(err)
}

if !hasMapping {
t.Fatalf("expected to find %s mounted on %s, but could not", source, destination)
}
}

0 comments on commit 33c45b1

Please sign in to comment.