From 6d68cd2997331ef29b5279578b84dcae204bcf76 Mon Sep 17 00:00:00 2001 From: Tomasen Date: Sun, 6 Dec 2015 15:16:00 +0800 Subject: [PATCH] pass test --- main.go | 5 ++++- main_test.go | 48 ++++++++++++++++++++++++------------------------ 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/main.go b/main.go index c897a77..e47f60d 100644 --- a/main.go +++ b/main.go @@ -58,9 +58,11 @@ func decryptBackendAddr(line []byte) ([]byte, error) { if ok { return addr, nil } + // Try to decrypt it (AES) addr, err := _Aes256CBC.Decrypt(_SecretPassphase, line) if err != nil { + panic(err) return nil, err } @@ -193,7 +195,8 @@ func handleConn(c net.Conn) { } if bytes.HasPrefix(bytes.ToLower(line), _cipherRequestHeader) { - cipherAddr = bytes.TrimSpace(line[(len(_cipherRequestHeader) + 1):]) + // copy instead of point + cipherAddr = []byte(string(bytes.TrimSpace(line[(len(_cipherRequestHeader) + 1):]))) continue } diff --git a/main_test.go b/main_test.go index a993b80..91152a6 100644 --- a/main_test.go +++ b/main_test.go @@ -165,6 +165,30 @@ func testProtocol(cipherAddr []byte) { } } +func TestHTTPServer(t *testing.T) { + cipherAddr, err := encryptText(_httpServerAddr, _secret) + if err != nil { + panic(err) + } + + client := &http.Client{} + req, _ := http.NewRequest("GET", "http://"+string(_defaultFrontdAddr), nil) + req.Header.Set(string(_cipherRequestHeader), string(cipherAddr)) + res, err := client.Do(req) + if err != nil { + panic(err) + } + + b, err := ioutil.ReadAll(res.Body) + if err != nil { + panic(err) + } + + if bytes.Compare(b, []byte("OK")) != 0 { + t.Fail() + } +} + func TestTextDecryptAES(t *testing.T) { o := aes256cbc.New() @@ -204,30 +228,6 @@ func TestProtocolDecrypt(*testing.T) { testProtocol(b) } -func TestHTTPServer(t *testing.T) { - cipherAddr, err := encryptText(_httpServerAddr, _secret) - if err != nil { - panic(err) - } - - client := &http.Client{} - req, _ := http.NewRequest("GET", "http://"+string(_defaultFrontdAddr), nil) - req.Header.Set(string(_cipherRequestHeader), string(cipherAddr)) - res, err := client.Do(req) - if err != nil { - panic(err) - } - - b, err := ioutil.ReadAll(res.Body) - if err != nil { - panic(err) - } - - if bytes.Compare(b, []byte("OK")) != 0 { - t.Fail() - } -} - // TODO: test decryption with extra bytes in packet and check data // TODO: test decryption with seperated packet simulate loss connection and check data