2021-09-05 14:49:38 +02:00
|
|
|
package netxlite
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-09-05 21:23:47 +02:00
|
|
|
"errors"
|
2021-09-05 14:49:38 +02:00
|
|
|
"net"
|
2021-09-05 21:23:47 +02:00
|
|
|
"sync"
|
2021-09-05 14:49:38 +02:00
|
|
|
"testing"
|
2021-09-05 21:23:47 +02:00
|
|
|
"time"
|
2021-09-05 14:49:38 +02:00
|
|
|
|
2021-09-05 20:59:42 +02:00
|
|
|
"github.com/apex/log"
|
2021-09-05 14:49:38 +02:00
|
|
|
utls "gitlab.com/yawning/utls.git"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestUTLSHandshakerChrome(t *testing.T) {
|
|
|
|
h := &tlsHandshakerConfigurable{
|
2021-09-05 20:59:42 +02:00
|
|
|
NewConn: newConnUTLS(&utls.HelloChrome_Auto),
|
2021-09-05 14:49:38 +02:00
|
|
|
}
|
|
|
|
cfg := &tls.Config{ServerName: "google.com"}
|
|
|
|
conn, err := net.Dial("tcp", "google.com:443")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
|
|
|
conn, _, err = h.Handshake(context.Background(), conn, cfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error", err)
|
|
|
|
}
|
|
|
|
if conn == nil {
|
|
|
|
t.Fatal("nil connection")
|
|
|
|
}
|
|
|
|
}
|
2021-09-05 20:59:42 +02:00
|
|
|
|
|
|
|
func TestNewTLSHandshakerUTLSTypes(t *testing.T) {
|
|
|
|
th := NewTLSHandshakerUTLS(log.Log, &utls.HelloChrome_83)
|
|
|
|
thl, okay := th.(*tlsHandshakerLogger)
|
|
|
|
if !okay {
|
|
|
|
t.Fatal("invalid type")
|
|
|
|
}
|
|
|
|
if thl.Logger != log.Log {
|
|
|
|
t.Fatal("invalid logger")
|
|
|
|
}
|
2021-09-07 19:56:42 +02:00
|
|
|
ew, okay := thl.TLSHandshaker.(*tlsHandshakerErrWrapper)
|
|
|
|
if !okay {
|
|
|
|
t.Fatal("invalid type")
|
|
|
|
}
|
|
|
|
thc, okay := ew.TLSHandshaker.(*tlsHandshakerConfigurable)
|
2021-09-05 20:59:42 +02:00
|
|
|
if !okay {
|
|
|
|
t.Fatal("invalid type")
|
|
|
|
}
|
|
|
|
if thc.NewConn == nil {
|
|
|
|
t.Fatal("expected non-nil NewConn")
|
|
|
|
}
|
|
|
|
}
|
2021-09-05 21:23:47 +02:00
|
|
|
|
2021-09-05 21:41:49 +02:00
|
|
|
func TestUTLSConnHandshakeNotInterruptedSuccess(t *testing.T) {
|
2021-09-05 21:23:47 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
conn := &utlsConn{
|
|
|
|
testableHandshake: func() error {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := conn.HandshakeContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:41:49 +02:00
|
|
|
func TestUTLSConnHandshakeNotInterruptedFailure(t *testing.T) {
|
|
|
|
expected := errors.New("mocked error")
|
|
|
|
ctx := context.Background()
|
|
|
|
conn := &utlsConn{
|
|
|
|
testableHandshake: func() error {
|
|
|
|
return expected
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := conn.HandshakeContext(ctx)
|
|
|
|
if !errors.Is(err, expected) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 21:23:47 +02:00
|
|
|
func TestUTLSConnHandshakeInterrupted(t *testing.T) {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
sigch := make(chan interface{})
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
|
|
|
|
defer cancel()
|
|
|
|
conn := &utlsConn{
|
|
|
|
testableHandshake: func() error {
|
|
|
|
defer wg.Done()
|
|
|
|
<-sigch
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := conn.HandshakeContext(ctx)
|
|
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
close(sigch)
|
|
|
|
wg.Wait()
|
|
|
|
}
|
2021-09-05 21:41:49 +02:00
|
|
|
|
|
|
|
func TestUTLSConnHandshakePanic(t *testing.T) {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(1)
|
|
|
|
ctx := context.Background()
|
|
|
|
conn := &utlsConn{
|
|
|
|
testableHandshake: func() error {
|
|
|
|
defer wg.Done()
|
|
|
|
panic("mascetti")
|
|
|
|
},
|
|
|
|
}
|
|
|
|
err := conn.HandshakeContext(ctx)
|
|
|
|
if !errors.Is(err, ErrUTLSHandshakePanic) {
|
|
|
|
t.Fatal("not the error we expected", err)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|