1
2
3
4
5 package windows_test
6
7 import (
8 "internal/syscall/windows"
9 "os"
10 "path/filepath"
11 "syscall"
12 "testing"
13 )
14
15 func TestOpen(t *testing.T) {
16 t.Parallel()
17
18 dir := t.TempDir()
19 file := filepath.Join(dir, "a")
20 f, err := os.Create(file)
21 if err != nil {
22 t.Fatal(err)
23 }
24 f.Close()
25
26 tests := []struct {
27 path string
28 flag int
29 err error
30 }{
31 {dir, syscall.O_RDONLY, nil},
32 {dir, syscall.O_CREAT, nil},
33 {dir, syscall.O_RDONLY | syscall.O_CREAT, nil},
34 {file, syscall.O_APPEND | syscall.O_WRONLY | os.O_CREATE, nil},
35 {file, syscall.O_APPEND | syscall.O_WRONLY | os.O_CREATE | os.O_TRUNC, nil},
36 {dir, syscall.O_RDONLY | syscall.O_TRUNC, syscall.ERROR_ACCESS_DENIED},
37 {dir, syscall.O_WRONLY | syscall.O_RDWR, nil},
38 {dir, syscall.O_WRONLY, syscall.EISDIR},
39 {dir, syscall.O_RDWR, syscall.EISDIR},
40 }
41 for i, tt := range tests {
42 dir := filepath.Dir(tt.path)
43 dirfd, err := syscall.Open(dir, syscall.O_RDONLY, 0)
44 if err != nil {
45 t.Error(err)
46 continue
47 }
48 base := filepath.Base(tt.path)
49 h, err := windows.Openat(dirfd, base, tt.flag, 0o660)
50 syscall.CloseHandle(dirfd)
51 if err == nil {
52 syscall.CloseHandle(h)
53 }
54 if err != tt.err {
55 t.Errorf("%d: Open got %q, want %q", i, err, tt.err)
56 }
57 }
58 }
59
View as plain text