github.com/dara-project/godist@v0.0.0-20200823115410-e0c80c8f0c78/src/cmd/go/go_windows_test.go (about)

     1  // Copyright 2014 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"internal/testenv"
    10  	"io/ioutil"
    11  	"os"
    12  	"os/exec"
    13  	"path/filepath"
    14  	"strings"
    15  	"syscall"
    16  	"testing"
    17  )
    18  
    19  func TestAbsolutePath(t *testing.T) {
    20  	tmp, err := ioutil.TempDir("", "TestAbsolutePath")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer os.RemoveAll(tmp)
    25  
    26  	file := filepath.Join(tmp, "a.go")
    27  	err = ioutil.WriteFile(file, []byte{}, 0644)
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  	dir := filepath.Join(tmp, "dir")
    32  	err = os.Mkdir(dir, 0777)
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	wd, err := os.Getwd()
    38  	if err != nil {
    39  		t.Fatal(err)
    40  	}
    41  	defer os.Chdir(wd)
    42  
    43  	// Chdir so current directory and a.go reside on the same drive.
    44  	err = os.Chdir(dir)
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  
    49  	noVolume := file[len(filepath.VolumeName(file)):]
    50  	wrongPath := filepath.Join(dir, noVolume)
    51  	output, err := exec.Command(testenv.GoToolPath(t), "build", noVolume).CombinedOutput()
    52  	if err == nil {
    53  		t.Fatal("build should fail")
    54  	}
    55  	if strings.Contains(string(output), wrongPath) {
    56  		t.Fatalf("wrong output found: %v %v", err, string(output))
    57  	}
    58  }
    59  
    60  func isWindowsXP(t *testing.T) bool {
    61  	v, err := syscall.GetVersion()
    62  	if err != nil {
    63  		t.Fatalf("GetVersion failed: %v", err)
    64  	}
    65  	major := byte(v)
    66  	return major < 6
    67  }
    68  
    69  func runIcacls(t *testing.T, args ...string) string {
    70  	t.Helper()
    71  	out, err := exec.Command("icacls", args...).CombinedOutput()
    72  	if err != nil {
    73  		t.Fatalf("icacls failed: %v\n%v", err, string(out))
    74  	}
    75  	return string(out)
    76  }
    77  
    78  func runGetACL(t *testing.T, path string) string {
    79  	t.Helper()
    80  	cmd := fmt.Sprintf(`Get-Acl "%s" | Select -expand AccessToString`, path)
    81  	out, err := exec.Command("powershell", "-Command", cmd).CombinedOutput()
    82  	if err != nil {
    83  		t.Fatalf("Get-Acl failed: %v\n%v", err, string(out))
    84  	}
    85  	return string(out)
    86  }
    87  
    88  // For issue 22343: verify that executable file created by "go build" command
    89  // has discretionary access control list (DACL) set as if the file
    90  // was created in the destination directory.
    91  func TestACL(t *testing.T) {
    92  	if isWindowsXP(t) {
    93  		t.Skip("Windows XP does not have powershell command")
    94  	}
    95  
    96  	tmpdir, err := ioutil.TempDir("", "TestACL")
    97  	if err != nil {
    98  		t.Fatal(err)
    99  	}
   100  	defer os.RemoveAll(tmpdir)
   101  
   102  	newtmpdir := filepath.Join(tmpdir, "tmp")
   103  	err = os.Mkdir(newtmpdir, 0777)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  
   108  	// When TestACL/tmp directory is created, it will have
   109  	// the same security attributes as TestACL.
   110  	// Add Guest account full access to TestACL/tmp - this
   111  	// will make all files created in TestACL/tmp have different
   112  	// security attributes to the files created in TestACL.
   113  	runIcacls(t, newtmpdir,
   114  		"/grant", "guest:(oi)(ci)f", // add Guest user to have full access
   115  	)
   116  
   117  	src := filepath.Join(tmpdir, "main.go")
   118  	err = ioutil.WriteFile(src, []byte("package main; func main() { }\n"), 0644)
   119  	if err != nil {
   120  		t.Fatal(err)
   121  	}
   122  	exe := filepath.Join(tmpdir, "main.exe")
   123  	cmd := exec.Command(testenv.GoToolPath(t), "build", "-o", exe, src)
   124  	cmd.Env = append(os.Environ(),
   125  		"TMP="+newtmpdir,
   126  		"TEMP="+newtmpdir,
   127  	)
   128  	out, err := cmd.CombinedOutput()
   129  	if err != nil {
   130  		t.Fatalf("go command failed: %v\n%v", err, string(out))
   131  	}
   132  
   133  	// exe file is expected to have the same security attributes as the src.
   134  	if got, expected := runGetACL(t, exe), runGetACL(t, src); got != expected {
   135  		t.Fatalf("expected Get-Acl output of \n%v\n, got \n%v\n", expected, got)
   136  	}
   137  }