...
Run Format

Source file src/cmd/go/go_windows_test.go

Documentation: cmd/go

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

View as plain text