-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_go.go
51 lines (42 loc) · 1.06 KB
/
test_go.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// +build test,!js
package ninchat
import (
"crypto/tls"
"log"
"net"
"os"
"reflect"
"unsafe"
)
func init() {
if s := os.Getenv("NINCHAT_TEST_ADDRESS"); s != "" {
defaultAddress = s
}
if certFile := os.Getenv("NINCHAT_TEST_CLIENT_CERT"); certFile != "" {
cert, err := tls.LoadX509KeyPair(certFile, os.Getenv("NINCHAT_TEST_CLIENT_KEY"))
if err != nil {
panic(err)
}
tlsConfig.Certificates = append(tlsConfig.Certificates, cert)
}
if os.Getenv("NINCHAT_TEST_INSECURE") == "1" {
tlsConfig.InsecureSkipVerify = true
}
}
func (s *Session) testDisconnect() (ok bool) {
tlsConn := s.test.ws.conn.UnderlyingConn().(*tls.Conn)
fieldAddr := reflect.ValueOf(tlsConn).Elem().FieldByName("conn").UnsafeAddr()
netConn := *(*net.Conn)(unsafe.Pointer(fieldAddr))
tcpConn := netConn.(*net.TCPConn)
// Corrupt the outgoing TLS stream.
if _, err := tcpConn.Write([]byte{42}); err != nil {
log.Printf("TestDisconnect: %v", err)
return
}
if err := tcpConn.CloseWrite(); err != nil {
log.Printf("TestDisconnect: %v", err)
return
}
ok = true
return
}