func Test_DialTimeout(t *testing.T) { oldTimeout := cfgDialTimeout cfgDialTimeout = 10 defer func() { cfgDialTimeout = oldTimeout }() listener, err := net.Listen("tcp", "0.0.0.0:0") utest.IsNilNow(t, err) defer listener.Close() conn, err := net.Dial("tcp", cfgGatewayAddr) utest.IsNilNow(t, err) defer conn.Close() encryptedAddr, err := aes256cbc.EncryptString("test", listener.Addr().String()) utest.IsNilNow(t, err) _, err = conn.Write([]byte(encryptedAddr)) utest.IsNilNow(t, err) _, err = conn.Write([]byte("\n")) utest.IsNilNow(t, err) code := make([]byte, 3) _, err = io.ReadFull(conn, code) utest.IsNilNow(t, err) utest.EqualNow(t, string(code), string(codeDialTimeout)) }
func Test_Transfer(t *testing.T) { listener, err := net.Listen("tcp", "0.0.0.0:0") utest.IsNilNow(t, err) defer listener.Close() go func() { for { conn, err := listener.Accept() if err != nil { continue } go func() { defer conn.Close() io.Copy(conn, conn) }() } }() for i := 0; i < 20; i++ { conn, err := net.Dial("tcp", cfgGatewayAddr) utest.IsNilNow(t, err) defer conn.Close() encryptedAddr, err := aes256cbc.EncryptString(string(cfgSecret), listener.Addr().String()) utest.IsNilNow(t, err) _, err = conn.Write([]byte(encryptedAddr)) utest.IsNilNow(t, err) _, err = conn.Write([]byte("\nabc")) utest.IsNilNow(t, err) code := make([]byte, 6) _, err = io.ReadFull(conn, code) utest.IsNilNow(t, err) utest.EqualNow(t, string(code[:3]), string(codeOK)) utest.EqualNow(t, string(code[3:]), "abc") for j := 0; j < 10000; j++ { b1 := RandBytes(256) _, err = conn.Write(b1) utest.IsNilNow(t, err) b2 := make([]byte, len(b1)) _, err = io.ReadFull(conn, b2) utest.IsNilNow(t, err) utest.EqualNow(t, b1, b2) } } }
func Test_CodeDialErr(t *testing.T) { conn, err := net.Dial("tcp", cfgGatewayAddr) utest.IsNilNow(t, err) defer conn.Close() encryptedAddr, err := aes256cbc.EncryptString("test", "0.0.0.0:0") utest.IsNilNow(t, err) _, err = conn.Write([]byte(encryptedAddr)) utest.IsNilNow(t, err) _, err = conn.Write([]byte("\n")) utest.IsNilNow(t, err) code := make([]byte, 3) _, err = io.ReadFull(conn, code) utest.IsNilNow(t, err) utest.EqualNow(t, string(code), string(codeDialErr)) }
func Test_OK(t *testing.T) { listener, err := net.Listen("tcp", "0.0.0.0:0") utest.IsNilNow(t, err) defer listener.Close() conn, err := net.Dial("tcp", cfgGatewayAddr) utest.IsNilNow(t, err) defer conn.Close() encryptedAddr, err := aes256cbc.EncryptString(string(cfgSecret), listener.Addr().String()) utest.IsNilNow(t, err) _, err = conn.Write([]byte(encryptedAddr)) utest.IsNilNow(t, err) _, err = conn.Write([]byte("\n")) utest.IsNilNow(t, err) code := make([]byte, 3) _, err = io.ReadFull(conn, code) utest.IsNilNow(t, err) utest.EqualNow(t, string(code), string(codeOK)) }