libgo: update to Go 1.11
[official-gcc.git] / libgo / go / cmd / go / go_windows_test.go
blob99af3d43dccb431a2828a4f086454e3656b910f6
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.
5 package main
7 import (
8 "fmt"
9 "internal/testenv"
10 "io/ioutil"
11 "os"
12 "os/exec"
13 "path/filepath"
14 "strings"
15 "testing"
18 func TestAbsolutePath(t *testing.T) {
19 tmp, err := ioutil.TempDir("", "TestAbsolutePath")
20 if err != nil {
21 t.Fatal(err)
23 defer os.RemoveAll(tmp)
25 file := filepath.Join(tmp, "a.go")
26 err = ioutil.WriteFile(file, []byte{}, 0644)
27 if err != nil {
28 t.Fatal(err)
30 dir := filepath.Join(tmp, "dir")
31 err = os.Mkdir(dir, 0777)
32 if err != nil {
33 t.Fatal(err)
36 wd, err := os.Getwd()
37 if err != nil {
38 t.Fatal(err)
40 defer os.Chdir(wd)
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)
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")
54 if strings.Contains(string(output), wrongPath) {
55 t.Fatalf("wrong output found: %v %v", err, string(output))
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))
65 return string(out)
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))
75 return string(out)
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)
86 defer os.RemoveAll(tmpdir)
88 newtmpdir := filepath.Join(tmpdir, "tmp")
89 err = os.Mkdir(newtmpdir, 0777)
90 if err != nil {
91 t.Fatal(err)
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
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)
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,
114 out, err := cmd.CombinedOutput()
115 if err != nil {
116 t.Fatalf("go command failed: %v\n%v", err, string(out))
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)