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.
18 func TestAbsolutePath(t
*testing
.T
) {
19 tmp
, err
:= ioutil
.TempDir("", "TestAbsolutePath")
23 defer os
.RemoveAll(tmp
)
25 file
:= filepath
.Join(tmp
, "a.go")
26 err
= ioutil
.WriteFile(file
, []byte{}, 0644)
30 dir
:= filepath
.Join(tmp
, "dir")
31 err
= os
.Mkdir(dir
, 0777)
42 // Chdir so current directory and a.go reside on the same drive.
48 noVolume
:= file
[len(filepath
.VolumeName(file
)):]
49 wrongPath
:= filepath
.Join(dir
, noVolume
)
50 output
, err
:= exec
.Command(testenv
.GoToolPath(t
), "build", noVolume
).CombinedOutput()
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 {
61 out
, err
:= exec
.Command("icacls", args
...).CombinedOutput()
63 t
.Fatalf("icacls failed: %v\n%v", err
, string(out
))
68 func runGetACL(t
*testing
.T
, path
string) string {
70 cmd
:= fmt
.Sprintf(`Get-Acl "%s" | Select -expand AccessToString`, path
)
71 out
, err
:= exec
.Command("powershell", "-Command", cmd
).CombinedOutput()
73 t
.Fatalf("Get-Acl failed: %v\n%v", err
, 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")
86 defer os
.RemoveAll(tmpdir
)
88 newtmpdir
:= filepath
.Join(tmpdir
, "tmp")
89 err
= os
.Mkdir(newtmpdir
, 0777)
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)
108 exe
:= filepath
.Join(tmpdir
, "main.exe")
109 cmd
:= exec
.Command(testenv
.GoToolPath(t
), "build", "-o", exe
, src
)
110 cmd
.Env
= append(os
.Environ(),
114 out
, err
:= cmd
.CombinedOutput()
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
)