| 1 |
// Copyright 2015 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 net |
| 6 |
|
| 7 |
import ( |
| 8 |
"internal/poll" |
| 9 |
"os/exec" |
| 10 |
"syscall" |
| 11 |
) |
| 12 |
|
| 13 |
var ( |
| 14 |
// Placeholders for saving original socket system calls. |
| 15 |
origWSASocket = wsaSocketFunc |
| 16 |
origClosesocket = poll.CloseFunc |
| 17 |
origConnect = connectFunc |
| 18 |
origConnectEx = poll.ConnectExFunc |
| 19 |
origListen = listenFunc |
| 20 |
origAccept = poll.AcceptFunc |
| 21 |
) |
| 22 |
|
| 23 |
func installTestHooks() { |
| 24 |
wsaSocketFunc = sw.WSASocket |
| 25 |
poll.CloseFunc = sw.Closesocket |
| 26 |
connectFunc = sw.Connect |
| 27 |
poll.ConnectExFunc = sw.ConnectEx |
| 28 |
listenFunc = sw.Listen |
| 29 |
poll.AcceptFunc = sw.AcceptEx |
| 30 |
} |
| 31 |
|
| 32 |
func uninstallTestHooks() { |
| 33 |
wsaSocketFunc = origWSASocket |
| 34 |
poll.CloseFunc = origClosesocket |
| 35 |
connectFunc = origConnect |
| 36 |
poll.ConnectExFunc = origConnectEx |
| 37 |
listenFunc = origListen |
| 38 |
poll.AcceptFunc = origAccept |
| 39 |
} |
| 40 |
|
| 41 |
// forceCloseSockets must be called only from TestMain. |
| 42 |
func forceCloseSockets() { |
| 43 |
for s := range sw.Sockets() { |
| 44 |
poll.CloseFunc(s) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func addCmdInheritedHandle(cmd *exec.Cmd, fd uintptr) { |
| 49 |
// Inherited handles are not inherited by default in Windows. |
| 50 |
// We need to set the handle inheritance flag explicitly. |
| 51 |
// See https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#parameters |
| 52 |
// for more details. |
| 53 |
if cmd.SysProcAttr == nil { |
| 54 |
cmd.SysProcAttr = &syscall.SysProcAttr{} |
| 55 |
} |
| 56 |
cmd.SysProcAttr.AdditionalInheritedHandles = append(cmd.SysProcAttr.AdditionalInheritedHandles, syscall.Handle(fd)) |
| 57 |
} |