|
1 |
| -package main |
2 |
| - |
3 |
| -import ( |
4 |
| - "fmt" |
5 |
| - "io/ioutil" |
6 |
| - "os" |
7 |
| - "os/exec" |
8 |
| - "path/filepath" |
9 |
| - "runtime" |
10 |
| - "strings" |
11 |
| - "syscall" |
12 |
| -) |
13 |
| - |
14 |
| -// https://github.com/golang/go/issues/22874 |
15 |
| -// os: Symlink creation should work on Windows without elevation |
16 |
| - |
17 |
| -func main() { |
18 |
| - // run on windows, only |
19 |
| - if runtime.GOOS != "windows" { |
20 |
| - fmt.Println("Skipping test on non-Windows system") |
21 |
| - return |
22 |
| - } |
23 |
| - |
24 |
| - // run test |
25 |
| - err := test() |
26 |
| - |
27 |
| - if err != nil { |
28 |
| - panic(err) |
29 |
| - } |
30 |
| -} |
31 |
| - |
32 |
| -func test() error { |
33 |
| - // is developer mode active? |
34 |
| - // the expected result depends on it |
35 |
| - devMode, _ := isDeveloperModeActive() |
36 |
| - |
37 |
| - fmt.Printf("Windows developer mode active: %v\n", devMode) |
38 |
| - |
39 |
| - // create dummy file to symlink |
40 |
| - dummyFile := filepath.Join(os.TempDir(), "issue22874.test") |
41 |
| - |
42 |
| - err := ioutil.WriteFile(dummyFile, []byte(""), 0644) |
43 |
| - |
44 |
| - if err != nil { |
45 |
| - return fmt.Errorf("Failed to create dummy file: %v", err) |
46 |
| - } |
47 |
| - |
48 |
| - defer os.Remove(dummyFile) |
49 |
| - |
50 |
| - // create the symlink |
51 |
| - linkFile := fmt.Sprintf("%v.link", dummyFile) |
52 |
| - |
53 |
| - err = os.Symlink(dummyFile, linkFile) |
54 |
| - |
55 |
| - if err != nil { |
56 |
| - // only the ERROR_PRIVILEGE_NOT_HELD error is allowed |
57 |
| - if x, ok := err.(*os.LinkError); ok { |
58 |
| - if xx, ok := x.Err.(syscall.Errno); ok { |
59 |
| - |
60 |
| - if xx == syscall.ERROR_PRIVILEGE_NOT_HELD { |
61 |
| - // is developer mode active? |
62 |
| - if devMode { |
63 |
| - return fmt.Errorf("Windows developer mode is active, but creating symlink failed with ERROR_PRIVILEGE_NOT_HELD anyway: %v", err) |
64 |
| - } |
65 |
| - |
66 |
| - // developer mode is disabled, and the error is expected |
67 |
| - fmt.Printf("Success: Creating symlink failed with expected ERROR_PRIVILEGE_NOT_HELD error\n") |
68 |
| - |
69 |
| - return nil |
70 |
| - } |
71 |
| - } |
72 |
| - } |
73 |
| - |
74 |
| - return fmt.Errorf("Failed to create symlink: %v", err) |
75 |
| - } |
76 |
| - |
77 |
| - // remove the link. don't care for any errors |
78 |
| - os.Remove(linkFile) |
79 |
| - |
80 |
| - fmt.Printf("Success: Creating symlink succeeded\n") |
81 |
| - |
82 |
| - return nil |
83 |
| -} |
84 |
| - |
85 |
| -func isDeveloperModeActive() (bool, error) { |
86 |
| - result, err := exec.Command("reg.exe", "query", "HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AppModelUnlock", "/v", "AllowDevelopmentWithoutDevLicense").Output() |
87 |
| - |
88 |
| - if err != nil { |
89 |
| - return false, err |
90 |
| - } |
91 |
| - |
92 |
| - return strings.Contains(string(result), "0x1"), nil |
93 |
| -} |
| 1 | +// Copyright 2016 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 os |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "internal/syscall/windows/registry" |
| 10 | + "io/ioutil" |
| 11 | + "os" |
| 12 | + "path/filepath" |
| 13 | + "syscall" |
| 14 | + "testing" |
| 15 | +) |
| 16 | + |
| 17 | +// see https://github.com/golang/go/issues/22874 |
| 18 | +// os: Symlink creation should work on Windows without elevation |
| 19 | + |
| 20 | +func TestSymlink(t *testing.T) { |
| 21 | + // is developer mode active? |
| 22 | + // the expected result depends on it |
| 23 | + devMode, _ := isDeveloperModeActive() |
| 24 | + |
| 25 | + t.Logf("Windows developer mode active: %v\n", devMode) |
| 26 | + |
| 27 | + // create dummy file to symlink |
| 28 | + dummyFile := filepath.Join(os.TempDir(), "issue22874.test") |
| 29 | + |
| 30 | + err := ioutil.WriteFile(dummyFile, []byte(""), 0644) |
| 31 | + |
| 32 | + if err != nil { |
| 33 | + t.Fatalf("Failed to create dummy file: %v", err) |
| 34 | + } |
| 35 | + |
| 36 | + defer os.Remove(dummyFile) |
| 37 | + |
| 38 | + // create the symlink |
| 39 | + linkFile := fmt.Sprintf("%v.link", dummyFile) |
| 40 | + |
| 41 | + err = os.Symlink(dummyFile, linkFile) |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + // only the ERROR_PRIVILEGE_NOT_HELD error is allowed |
| 45 | + if x, ok := err.(*os.LinkError); ok { |
| 46 | + if xx, ok := x.Err.(syscall.Errno); ok { |
| 47 | + |
| 48 | + if xx == syscall.ERROR_PRIVILEGE_NOT_HELD { |
| 49 | + // is developer mode active? |
| 50 | + if devMode { |
| 51 | + t.Fatalf("Windows developer mode is active, but creating symlink failed with ERROR_PRIVILEGE_NOT_HELD anyway: %v", err) |
| 52 | + } |
| 53 | + |
| 54 | + // developer mode is disabled, and the error is expected |
| 55 | + fmt.Printf("Success: Creating symlink failed with expected ERROR_PRIVILEGE_NOT_HELD error\n") |
| 56 | + |
| 57 | + return nil |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + t.Fatalf("Failed to create symlink: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + // remove the link. don't care for any errors |
| 66 | + os.Remove(linkFile) |
| 67 | + |
| 68 | + t.Logf("Success: Creating symlink succeeded\n") |
| 69 | + |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func isDeveloperModeActive() (bool, error) { |
| 74 | + key, err := registry.OpenKey(registry.LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\AppModelUnlock", registry.READ) |
| 75 | + |
| 76 | + if err != nil { |
| 77 | + return false, err |
| 78 | + } |
| 79 | + |
| 80 | + val, _, err := key.GetIntegerValue("AllowDevelopmentWithoutDevLicense") |
| 81 | + |
| 82 | + if err != nil { |
| 83 | + return false, err |
| 84 | + } |
| 85 | + |
| 86 | + return val != 0, nil |
| 87 | +} |
0 commit comments