Skip to content

Commit

Permalink
Merge branch 'add-bind-filter' of https://github.com/gabriel-samfira/…
Browse files Browse the repository at this point in the history
…go-winio into add-bind-filter

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
  • Loading branch information
gabriel-samfira committed Feb 7, 2023
2 parents 64bab15 + 258cf20 commit 707b9e5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
7 changes: 1 addition & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@ jobs:
- uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}
- name: Run tests on ltsc 2019
if: matrix.os == 'windows-2019'
run: go test -gcflags=all=-d=checkptr -v --test.run="[^TestEnsureOnlyOneTargetCanBeMounted|^TestGetBindMappings]" ./...
- name: Run tests
if: matrix.os != 'windows-2019'
run: go test -gcflags=all=-d=checkptr -v ./...
- run: go test -gcflags=all=-d=checkptr -v ./...

build:
name: Build Repo
Expand Down
4 changes: 3 additions & 1 deletion pkg/bindfilter/bind_filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ func getFinalPath(pth string) (string, error) {
if err != nil {
return "", fmt.Errorf("fetching file handle: %w", err)
}
defer syscall.CloseHandle(han)
defer func() {
_ = syscall.CloseHandle(han)
}()

buf := make([]uint16, 100)
var flags uint32 = 0x0
Expand Down
42 changes: 39 additions & 3 deletions pkg/bindfilter/bind_filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"testing"

"golang.org/x/sys/windows/registry"
)

func TestApplyFileBinding(t *testing.T) {
Expand Down Expand Up @@ -96,11 +99,19 @@ func TestApplyFileBindingReadOnly(t *testing.T) {
}

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

if version <= 17763 {
t.Skip("not supported on RS5 or earlier")
}
source := t.TempDir()
secondarySource := t.TempDir()
destination := t.TempDir()

err := ApplyFileBinding(destination, source, false)
err = ApplyFileBinding(destination, source, false)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -139,6 +150,14 @@ func checkSourceIsMountedOnDestination(src, dst string) (bool, error) {
}

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

if version <= 17763 {
t.Skip("not supported on RS5 or earlier")
}
// GetBindMappings will exoand short paths like ADMINI~1 and PROGRA~1 to their
// full names. In order to properly match the names later, we expand them here.
srcShort := t.TempDir()
Expand Down Expand Up @@ -198,14 +217,31 @@ func TestRemoveFileBinding(t *testing.T) {

if _, err := os.Stat(dstFile); err != nil {
removeFileBinding(t, destination)
t.Fatalf("expected to find %s, but did not", dstFile)
t.Fatalf("expected to find %s, but could not", dstFile)
}

if err := RemoveFileBinding(destination); err != nil {
t.Fatal(err)
}

if _, err := os.Stat(dstFile); err == nil {
t.Fatalf("expected %s to be gone, but it not", dstFile)
t.Fatalf("expected %s to be gone, but it is not", dstFile)
}
}

func getWindowsBuildNumber() (int, error) {
k, err := registry.OpenKey(registry.LOCAL_MACHINE, `SOFTWARE\Microsoft\Windows NT\CurrentVersion`, registry.QUERY_VALUE)
if err != nil {
return 0, fmt.Errorf("read CurrentVersion reg key: %s", err)
}
defer k.Close()
buildNumStr, _, err := k.GetStringValue("CurrentBuild")
if err != nil {
return 0, fmt.Errorf("read CurrentBuild reg value: %s", err)
}
buildNum, err := strconv.Atoi(buildNumStr)
if err != nil {
return 0, err
}
return buildNum, nil
}

0 comments on commit 707b9e5

Please sign in to comment.