1
0
mirror of https://github.com/golang/go synced 2024-10-05 07:21:25 -06:00
go/src/net/main_posix_test.go
Mikio Hara 57bc7a0434 net: fix TestDialGoogle with -ipv6 when CGO_ENABLED=0
Under some dial tests that require external network connectivity, we
must prevent application traffic but must not interfere with control
plane traffic such as DNS message exchange. But test helper function
disableSocketConnect prevents both application and control plane traffic
unconditionally and makes some dial tests with -ipv6 fail when
CGO_ENABLED=0.

This change makes disableSocketConnect take a look at not only address
family but socket type for fixing some dial tests with -ipv6 when
CGO_ENBALED=0.

Change-Id: I32241d9592d31483424bb5e69cb4d56f3fc20312
Reviewed-on: https://go-review.googlesource.com/8743
Reviewed-by: Ian Lance Taylor <iant@golang.org>
2015-04-16 00:44:52 +00:00

51 lines
1.4 KiB
Go

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build !plan9
package net
import (
"net/internal/socktest"
"strings"
"syscall"
)
func enableSocketConnect() {
sw.Set(socktest.FilterConnect, nil)
}
func disableSocketConnect(network string) {
ss := strings.Split(network, ":")
sw.Set(socktest.FilterConnect, func(so *socktest.Status) (socktest.AfterFilter, error) {
switch ss[0] {
case "tcp4":
if so.Cookie.Family() == syscall.AF_INET && so.Cookie.Type() == syscall.SOCK_STREAM {
return nil, syscall.EHOSTUNREACH
}
case "udp4":
if so.Cookie.Family() == syscall.AF_INET && so.Cookie.Type() == syscall.SOCK_DGRAM {
return nil, syscall.EHOSTUNREACH
}
case "ip4":
if so.Cookie.Family() == syscall.AF_INET && so.Cookie.Type() == syscall.SOCK_RAW {
return nil, syscall.EHOSTUNREACH
}
case "tcp6":
if so.Cookie.Family() == syscall.AF_INET6 && so.Cookie.Type() == syscall.SOCK_STREAM {
return nil, syscall.EHOSTUNREACH
}
case "udp6":
if so.Cookie.Family() == syscall.AF_INET6 && so.Cookie.Type() == syscall.SOCK_DGRAM {
return nil, syscall.EHOSTUNREACH
}
case "ip6":
if so.Cookie.Family() == syscall.AF_INET6 && so.Cookie.Type() == syscall.SOCK_RAW {
return nil, syscall.EHOSTUNREACH
}
}
return nil, nil
})
}