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.
19 func TestAbsolutePath(t
*testing
.T
) {
20 tmp
, err
:= ioutil
.TempDir("", "TestAbsolutePath")
24 defer os
.RemoveAll(tmp
)
26 file
:= filepath
.Join(tmp
, "a.go")
27 err
= ioutil
.WriteFile(file
, []byte{}, 0644)
31 dir
:= filepath
.Join(tmp
, "dir")
32 err
= os
.Mkdir(dir
, 0777)
43 // Chdir so current directory and a.go reside on the same drive.
49 noVolume
:= file
[len(filepath
.VolumeName(file
)):]
50 wrongPath
:= filepath
.Join(dir
, noVolume
)
51 output
, err
:= exec
.Command(testenv
.GoToolPath(t
), "build", noVolume
).CombinedOutput()
53 t
.Fatal("build should fail")
55 if strings
.Contains(string(output
), wrongPath
) {
56 t
.Fatalf("wrong output found: %v %v", err
, string(output
))
60 func isWindowsXP(t
*testing
.T
) bool {
61 v
, err
:= syscall
.GetVersion()
63 t
.Fatalf("GetVersion failed: %v", err
)
69 func runIcacls(t
*testing
.T
, args
...string) string {
71 out
, err
:= exec
.Command("icacls", args
...).CombinedOutput()
73 t
.Fatalf("icacls failed: %v\n%v", err
, string(out
))
78 func runGetACL(t
*testing
.T
, path
string) string {
80 cmd
:= fmt
.Sprintf(`Get-Acl "%s" | Select -expand AccessToString`, path
)
81 out
, err
:= exec
.Command("powershell", "-Command", cmd
).CombinedOutput()
83 t
.Fatalf("Get-Acl failed: %v\n%v", err
, string(out
))
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
) {
93 t
.Skip("Windows XP does not have powershell command")
96 tmpdir
, err
:= ioutil
.TempDir("", "TestACL")
100 defer os
.RemoveAll(tmpdir
)
102 newtmpdir
:= filepath
.Join(tmpdir
, "tmp")
103 err
= os
.Mkdir(newtmpdir
, 0777)
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
117 src
:= filepath
.Join(tmpdir
, "main.go")
118 err
= ioutil
.WriteFile(src
, []byte("package main; func main() { }\n"), 0644)
122 exe
:= filepath
.Join(tmpdir
, "main.exe")
123 cmd
:= exec
.Command(testenv
.GoToolPath(t
), "build", "-o", exe
, src
)
124 cmd
.Env
= append(os
.Environ(),
128 out
, err
:= cmd
.CombinedOutput()
130 t
.Fatalf("go command failed: %v\n%v", err
, string(out
))
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
)