github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/kbfs/libdokan/noexec_test.go (about)

     1  // Copyright 2019 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build windows
     6  // +build windows
     7  
     8  package libdokan
     9  
    10  import (
    11  	"errors"
    12  	"io"
    13  	"os"
    14  	"os/exec"
    15  	"path/filepath"
    16  	"strings"
    17  	"testing"
    18  
    19  	"github.com/keybase/client/go/kbfs/libcontext"
    20  	"github.com/keybase/client/go/kbfs/libkbfs"
    21  	"github.com/keybase/client/go/kbun"
    22  )
    23  
    24  func findGoExe() (string, error) {
    25  	for _, path := range []string{"go.exe",
    26  		os.Getenv("GOBIN") + "/go.exe", os.Getenv("GOROOT") + "/bin/go.exe"} {
    27  		exe, err := exec.LookPath(path)
    28  		if err == nil {
    29  			return exe, err
    30  		}
    31  
    32  	}
    33  	return "", errors.New("Cannot find go.exe")
    34  }
    35  
    36  func copyFile(from, to string) error {
    37  	src, err := Open(from)
    38  	if err != nil {
    39  		return err
    40  	}
    41  	defer src.Close()
    42  	tgt, err := Create(to)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	defer tgt.Close()
    47  	_, err = io.Copy(tgt, src)
    48  	if err != nil {
    49  		return err
    50  	}
    51  	err = tgt.Sync()
    52  	if err != nil {
    53  		return err
    54  	}
    55  	return nil
    56  }
    57  
    58  func unames(uns []string) []kbun.NormalizedUsername {
    59  	res := make([]kbun.NormalizedUsername, len(uns))
    60  	for i, v := range uns {
    61  		res[i] = kbun.NormalizedUsername(v)
    62  	}
    63  	return res
    64  }
    65  
    66  func testNoExec(t *testing.T, users []string) error {
    67  	ctx := libcontext.BackgroundContextWithCancellationDelayer()
    68  	defer testCleanupDelayer(ctx, t)
    69  	config := libkbfs.MakeTestConfigOrBust(t, unames(users)...)
    70  	// Background flushed needed for large files.
    71  	config.SetDoBackgroundFlushes(true)
    72  	defer libkbfs.CheckConfigAndShutdown(ctx, t, config)
    73  	mnt, _, cancelFn := makeFS(ctx, t, config)
    74  	defer mnt.Close()
    75  	defer cancelFn()
    76  
    77  	exe, err := findGoExe()
    78  	if err != nil {
    79  		t.Fatal("Error finding go.exe: ", err)
    80  	}
    81  
    82  	tlfName := strings.Join(users, ",")
    83  	targetPath := filepath.Join(mnt.Dir, PrivateName, tlfName, "test.exe")
    84  
    85  	err = copyFile(exe, targetPath)
    86  	if err != nil {
    87  		t.Fatal("Error copying go.exe to kbfs: ", err)
    88  	}
    89  
    90  	return exec.Command(targetPath, "version").Run()
    91  
    92  }
    93  func TestNoExec(t *testing.T) {
    94  	err := testNoExec(t, []string{"jdoe", "janedoe"})
    95  	if err == nil {
    96  		t.Fatal("Unexpected success executing go on kbfs, expected fail (noexec)")
    97  	}
    98  	if !os.IsPermission(err) {
    99  		t.Fatal("Wrong error trying to execute go on kbfs: ", err)
   100  	}
   101  }
   102  
   103  func TestNoExecWhitelist(t *testing.T) {
   104  	err := testNoExec(t, []string{"jdoe"})
   105  	if err != nil {
   106  		t.Fatal("Unexpected failure executing go on kbfs, expected success (whitelist): ", err)
   107  	}
   108  }