-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
130 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,106 @@ | ||
package tcpreuse | ||
|
||
import "testing" | ||
import ( | ||
"context" | ||
"crypto/ecdsa" | ||
"crypto/elliptic" | ||
"crypto/rand" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"crypto/x509/pkix" | ||
"fmt" | ||
"math/big" | ||
"testing" | ||
"time" | ||
|
||
func TestListenerClose(t *testing.T) { | ||
// cm := NewConnMgr(false) | ||
// | ||
// cm.DemultiplexedListen("/") | ||
ma "github.com/multiformats/go-multiaddr" | ||
manet "github.com/multiformats/go-multiaddr/net" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func selfSignedTLSConfig(t *testing.T) *tls.Config { | ||
t.Helper() | ||
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) | ||
require.NoError(t, err) | ||
|
||
notBefore := time.Now() | ||
notAfter := notBefore.Add(365 * 24 * time.Hour) | ||
|
||
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) | ||
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) | ||
require.NoError(t, err) | ||
|
||
certTemplate := x509.Certificate{ | ||
SerialNumber: serialNumber, | ||
Subject: pkix.Name{ | ||
Organization: []string{"Test"}, | ||
}, | ||
NotBefore: notBefore, | ||
NotAfter: notAfter, | ||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
BasicConstraintsValid: true, | ||
} | ||
|
||
derBytes, err := x509.CreateCertificate(rand.Reader, &certTemplate, &certTemplate, &priv.PublicKey, priv) | ||
require.NoError(t, err) | ||
|
||
cert := tls.Certificate{ | ||
Certificate: [][]byte{derBytes}, | ||
PrivateKey: priv, | ||
} | ||
|
||
tlsConfig := &tls.Config{ | ||
Certificates: []tls.Certificate{cert}, | ||
} | ||
return tlsConfig | ||
} | ||
|
||
func getTLSConn(t *testing.T, c manet.Conn) (manet.Conn, error) { | ||
t.Helper() | ||
return manet.WrapNetConn(tls.Server(c, selfSignedTLSConfig(t))) | ||
} | ||
|
||
func TestListenerSingle(t *testing.T) { | ||
listenAddr := ma.StringCast("/ip4/0.0.0.0/tcp/0") | ||
for disableReuseport := range []bool{true, false} { | ||
t.Run(fmt.Sprintf("TLS-reuseport:%v", disableReuseport), func(t *testing.T) { | ||
cm := NewConnMgr(false) | ||
l, err := cm.DemultiplexedListen(listenAddr, DemultiplexedConnType_TLS) | ||
require.NoError(t, err) | ||
|
||
go func() { | ||
d := tls.Dialer{Config: &tls.Config{InsecureSkipVerify: true}} | ||
for i := 0; i < 100; i++ { | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) | ||
defer cancel() | ||
conn, err := d.DialContext(ctx, l.Addr().Network(), l.Addr().String()) | ||
if err != nil { | ||
t.Error("failed to dial", err, i) | ||
return | ||
} | ||
buf := make([]byte, 10) | ||
_, err = conn.Write([]byte("hello")) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
_, err = conn.Read(buf) | ||
if err == nil { | ||
t.Error("expected EOF got nil") | ||
} | ||
} | ||
}() | ||
for i := 0; i < 100; i++ { | ||
c, err := l.Accept() | ||
require.NoError(t, err) | ||
c, err = getTLSConn(t, c) | ||
require.NoError(t, err) | ||
buf := make([]byte, 10) | ||
n, err := c.Read(buf) | ||
require.NoError(t, err) | ||
require.Equal(t, "hello", string(buf[:n])) | ||
c.Close() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters