gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/internal/poll/fd_windows_test.go (about) 1 // Copyright 2017 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 poll_test 6 7 import ( 8 "fmt" 9 "gitee.com/ks-custle/core-gm/internal/poll" 10 "os" 11 "sync" 12 "syscall" 13 "testing" 14 ) 15 16 type loggedFD struct { 17 Net string 18 FD *poll.FD 19 Err error 20 } 21 22 var ( 23 logMu sync.Mutex 24 loggedFDs map[syscall.Handle]*loggedFD 25 ) 26 27 func logFD(net string, fd *poll.FD, err error) { 28 logMu.Lock() 29 defer logMu.Unlock() 30 31 loggedFDs[fd.Sysfd] = &loggedFD{ 32 Net: net, 33 FD: fd, 34 Err: err, 35 } 36 } 37 38 func init() { 39 loggedFDs = make(map[syscall.Handle]*loggedFD) 40 *poll.LogInitFD = logFD 41 } 42 43 func findLoggedFD(h syscall.Handle) (lfd *loggedFD, found bool) { 44 logMu.Lock() 45 defer logMu.Unlock() 46 47 lfd, found = loggedFDs[h] 48 return lfd, found 49 } 50 51 // checkFileIsNotPartOfNetpoll verifies that f is not managed by netpoll. 52 // It returns error, if check fails. 53 func checkFileIsNotPartOfNetpoll(f *os.File) error { 54 lfd, found := findLoggedFD(syscall.Handle(f.Fd())) 55 if !found { 56 return fmt.Errorf("%v fd=%v: is not found in the log", f.Name(), f.Fd()) 57 } 58 if lfd.FD.IsPartOfNetpoll() { 59 return fmt.Errorf("%v fd=%v: is part of netpoll, but should not be (logged: net=%v err=%v)", f.Name(), f.Fd(), lfd.Net, lfd.Err) 60 } 61 return nil 62 } 63 64 func TestFileFdsAreInitialised(t *testing.T) { 65 exe, err := os.Executable() 66 if err != nil { 67 t.Fatal(err) 68 } 69 f, err := os.Open(exe) 70 if err != nil { 71 t.Fatal(err) 72 } 73 defer f.Close() 74 75 err = checkFileIsNotPartOfNetpoll(f) 76 if err != nil { 77 t.Fatal(err) 78 } 79 } 80 81 func TestSerialFdsAreInitialised(t *testing.T) { 82 for _, name := range []string{"COM1", "COM2", "COM3", "COM4"} { 83 t.Run(name, func(t *testing.T) { 84 h, err := syscall.CreateFile(syscall.StringToUTF16Ptr(name), 85 syscall.GENERIC_READ|syscall.GENERIC_WRITE, 86 0, 87 nil, 88 syscall.OPEN_EXISTING, 89 syscall.FILE_ATTRIBUTE_NORMAL|syscall.FILE_FLAG_OVERLAPPED, 90 0) 91 if err != nil { 92 if errno, ok := err.(syscall.Errno); ok { 93 switch errno { 94 case syscall.ERROR_FILE_NOT_FOUND, 95 syscall.ERROR_ACCESS_DENIED: 96 t.Log("Skipping: ", err) 97 return 98 } 99 } 100 t.Fatal(err) 101 } 102 f := os.NewFile(uintptr(h), name) 103 defer f.Close() 104 105 err = checkFileIsNotPartOfNetpoll(f) 106 if err != nil { 107 t.Fatal(err) 108 } 109 }) 110 } 111 }