github.com/code-reading/golang@v0.0.0-20220303082512-ba5bc0e589a3/go/src/net/main_unix_test.go (about) 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 //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris 6 // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris 7 8 package net 9 10 import "internal/poll" 11 12 var ( 13 // Placeholders for saving original socket system calls. 14 origSocket = socketFunc 15 origClose = poll.CloseFunc 16 origConnect = connectFunc 17 origListen = listenFunc 18 origAccept = poll.AcceptFunc 19 origGetsockoptInt = getsockoptIntFunc 20 21 extraTestHookInstallers []func() 22 extraTestHookUninstallers []func() 23 ) 24 25 func installTestHooks() { 26 socketFunc = sw.Socket 27 poll.CloseFunc = sw.Close 28 connectFunc = sw.Connect 29 listenFunc = sw.Listen 30 poll.AcceptFunc = sw.Accept 31 getsockoptIntFunc = sw.GetsockoptInt 32 33 for _, fn := range extraTestHookInstallers { 34 fn() 35 } 36 } 37 38 func uninstallTestHooks() { 39 socketFunc = origSocket 40 poll.CloseFunc = origClose 41 connectFunc = origConnect 42 listenFunc = origListen 43 poll.AcceptFunc = origAccept 44 getsockoptIntFunc = origGetsockoptInt 45 46 for _, fn := range extraTestHookUninstallers { 47 fn() 48 } 49 } 50 51 // forceCloseSockets must be called only from TestMain. 52 func forceCloseSockets() { 53 for s := range sw.Sockets() { 54 poll.CloseFunc(s) 55 } 56 }