From a470330383d8ec17f44fd667865fb63a003a7f14 Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 19 Mar 2014 23:34:50 +0800 Subject: [PATCH] finish utest for handshake. --- trunk/src/rtmp/srs_protocol_handshake.cpp | 12 ++- trunk/src/utest/srs_utest.cpp | 71 +++++++++++++ trunk/src/utest/srs_utest.hpp | 32 ++++++ trunk/src/utest/srs_utest_handshake.cpp | 116 ++++++++-------------- trunk/src/utest/srs_utest_handshake.hpp | 31 ------ 5 files changed, 157 insertions(+), 105 deletions(-) diff --git a/trunk/src/rtmp/srs_protocol_handshake.cpp b/trunk/src/rtmp/srs_protocol_handshake.cpp index 9fbe07ece..2048a5d39 100644 --- a/trunk/src/rtmp/srs_protocol_handshake.cpp +++ b/trunk/src/rtmp/srs_protocol_handshake.cpp @@ -97,7 +97,7 @@ namespace srs "EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381" \ "FFFFFFFFFFFFFFFF" int __openssl_generate_key( - u_int8_t*& _private_key, u_int8_t*& _public_key, int32_t& size, + u_int8_t* _private_key, u_int8_t* _public_key, int32_t& size, DH*& pdh, int32_t& bits_count, u_int8_t*& shared_key, int32_t& shared_key_length, BIGNUM*& peer_public_key ){ int ret = ERROR_SUCCESS; @@ -200,7 +200,7 @@ namespace srs BIGNUM* peer_public_key = NULL; ret = __openssl_generate_key( - (u_int8_t*&)_private_key, (u_int8_t*&)_public_key, size, + (u_int8_t*)_private_key, (u_int8_t*)_public_key, size, pdh, bits_count, shared_key, shared_key_length, peer_public_key ); @@ -543,6 +543,14 @@ namespace srs u_int8_t* a = (u_int8_t*)pa; u_int8_t* b = (u_int8_t*)pb; + if (!a && !b) { + return true; + } + + if (!a || !b) { + return false; + } + for(int i = 0; i < size; i++){ if(a[i] != b[i]){ return false; diff --git a/trunk/src/utest/srs_utest.cpp b/trunk/src/utest/srs_utest.cpp index 1a223dedd..6171a1bd5 100644 --- a/trunk/src/utest/srs_utest.cpp +++ b/trunk/src/utest/srs_utest.cpp @@ -36,6 +36,77 @@ ISrsThreadContext* _srs_context = new ISrsThreadContext(); SrsConfig* _srs_config = NULL; SrsServer* _srs_server = NULL; +MockEmptyIO::MockEmptyIO() +{ +} + +MockEmptyIO::~MockEmptyIO() +{ +} + +bool MockEmptyIO::is_never_timeout(int64_t /*timeout_us*/) +{ + return true; +} + +int MockEmptyIO::read_fully(const void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/) +{ + return ERROR_SUCCESS; +} + +int MockEmptyIO::write(const void* /*buf*/, size_t /*size*/, ssize_t* /*nwrite*/) +{ + return ERROR_SUCCESS; +} + +void MockEmptyIO::set_recv_timeout(int64_t /*timeout_us*/) +{ +} + +int64_t MockEmptyIO::get_recv_timeout() +{ + return -1; +} + +int64_t MockEmptyIO::get_recv_bytes() +{ + return -1; +} + +int MockEmptyIO::get_recv_kbps() +{ + return 0; +} + +void MockEmptyIO::set_send_timeout(int64_t /*timeout_us*/) +{ +} + +int64_t MockEmptyIO::get_send_timeout() +{ + return 0; +} + +int64_t MockEmptyIO::get_send_bytes() +{ + return 0; +} + +int MockEmptyIO::get_send_kbps() +{ + return 0; +} + +int MockEmptyIO::writev(const iovec */*iov*/, int /*iov_size*/, ssize_t* /*nwrite*/) +{ + return ERROR_SUCCESS; +} + +int MockEmptyIO::read(const void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/) +{ + return ERROR_SUCCESS; +} + // basic test and samples. VOID TEST(SampleTest, FastSampleInt64Test) { diff --git a/trunk/src/utest/srs_utest.hpp b/trunk/src/utest/srs_utest.hpp index c36add338..6b1ccf87d 100644 --- a/trunk/src/utest/srs_utest.hpp +++ b/trunk/src/utest/srs_utest.hpp @@ -34,4 +34,36 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // we add an empty macro for upp to show the smart tips. #define VOID +#include + +class MockEmptyIO : public ISrsProtocolReaderWriter +{ +public: + MockEmptyIO(); + virtual ~MockEmptyIO(); +// for protocol +public: + virtual bool is_never_timeout(int64_t timeout_us); +// for handshake. +public: + virtual int read_fully(const void* buf, size_t size, ssize_t* nread); + virtual int write(const void* buf, size_t size, ssize_t* nwrite); +// for protocol +public: + virtual void set_recv_timeout(int64_t timeout_us); + virtual int64_t get_recv_timeout(); + virtual int64_t get_recv_bytes(); + virtual int get_recv_kbps(); +// for protocol +public: + virtual void set_send_timeout(int64_t timeout_us); + virtual int64_t get_send_timeout(); + virtual int64_t get_send_bytes(); + virtual int get_send_kbps(); + virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite); +// for protocol/amf0/msg-codec +public: + virtual int read(const void* buf, size_t size, ssize_t* nread); +}; + #endif diff --git a/trunk/src/utest/srs_utest_handshake.cpp b/trunk/src/utest/srs_utest_handshake.cpp index bf0f7e38b..e14016911 100644 --- a/trunk/src/utest/srs_utest_handshake.cpp +++ b/trunk/src/utest/srs_utest_handshake.cpp @@ -24,77 +24,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include - -MockEmptyIO::MockEmptyIO() -{ -} - -MockEmptyIO::~MockEmptyIO() -{ -} - -bool MockEmptyIO::is_never_timeout(int64_t /*timeout_us*/) -{ - return true; -} - -int MockEmptyIO::read_fully(const void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/) -{ - return ERROR_SUCCESS; -} - -int MockEmptyIO::write(const void* /*buf*/, size_t /*size*/, ssize_t* /*nwrite*/) -{ - return ERROR_SUCCESS; -} - -void MockEmptyIO::set_recv_timeout(int64_t /*timeout_us*/) -{ -} - -int64_t MockEmptyIO::get_recv_timeout() -{ - return -1; -} - -int64_t MockEmptyIO::get_recv_bytes() -{ - return -1; -} - -int MockEmptyIO::get_recv_kbps() -{ - return 0; -} - -void MockEmptyIO::set_send_timeout(int64_t /*timeout_us*/) -{ -} - -int64_t MockEmptyIO::get_send_timeout() -{ - return 0; -} - -int64_t MockEmptyIO::get_send_bytes() -{ - return 0; -} - -int MockEmptyIO::get_send_kbps() -{ - return 0; -} - -int MockEmptyIO::writev(const iovec */*iov*/, int /*iov_size*/, ssize_t* /*nwrite*/) -{ - return ERROR_SUCCESS; -} - -int MockEmptyIO::read(const void* /*buf*/, size_t /*size*/, ssize_t* /*nread*/) -{ - return ERROR_SUCCESS; -} +#include // verify the sha256 VOID TEST(HandshakeTest, OpensslSha256) @@ -124,6 +54,37 @@ VOID TEST(HandshakeTest, OpensslSha256) EXPECT_TRUE(srs_bytes_equals(digest, expect_digest, 32)); } +// verify the dh key +VOID TEST(HandshakeTest, DHKey) +{ + char pri_key[] = { + 0x6e, 0x65, 0x69, 0x2d, 0x69, 0x2d, 0x69, 0x73, + 0x6e, 0x69, 0x73, 0x6c, 0x65, 0x72, 0x69, 0x72, + 0x76, 0x65, 0x72, 0x69, 0x77, 0x74, 0x2e, 0x6e, + 0x72, 0x76, 0x72, 0x65, 0x72, 0x70, 0x72, 0x69, + 0x69, 0x70, 0x72, 0x73, 0x6e, 0x65, 0x72, 0x72, + 0x6e, 0x2d, 0x65, 0x74, 0x72, 0x6c, 0x69, 0x74, + 0x69, 0x65, 0x40, 0x69, 0x69, 0x76, 0x77, 0x2d, + 0x73, 0x65, 0x72, 0x72, 0x76, 0x73, 0x72, 0x2e, + 0x2d, 0x76, 0x65, 0x31, 0x65, 0x6d, 0x6d, 0x73, + 0x69, 0x73, 0x74, 0x2e, 0x74, 0x72, 0x65, 0x65, + 0x72, 0x65, 0x2d, 0x74, 0x69, 0x31, 0x65, 0x2d, + 0x6f, 0x77, 0x2e, 0x76, 0x77, 0x2d, 0x77, 0x72, + 0x65, 0x65, 0x31, 0x74, 0x73, 0x70, 0x74, 0x6e, + 0x72, 0x6e, 0x73, 0x6d, 0x2e, 0x69, 0x72, 0x2d, + 0x65, 0x69, 0x77, 0x69, 0x76, 0x72, 0x77, 0x72, + 0x32, 0x6e, 0x65, 0x6c, 0x2e, 0x2d, 0x6e, 0x69 + }; + + char pub_key1[128]; + openssl_generate_key(pri_key, pub_key1, 128); + + char pub_key2[128]; + openssl_generate_key(pri_key, pub_key2, 128); + + EXPECT_FALSE(srs_bytes_equals(pub_key1, pub_key2, 128)); +} + // flash will sendout a c0c1 encrypt by ssl. VOID TEST(HandshakeTest, VerifyFPC0C1) { @@ -263,5 +224,16 @@ VOID TEST(HandshakeTest, ComplexHandshake) VOID TEST(HandshakeTest, BytesEqual) { - //srs_bytes_equals + char a1[] = { 0x01 }; + char b1[] = { 0x02 }; + char a2[] = { 0x01, 0x02 }; + char b2[] = { 0x02, 0x03 }; + + EXPECT_TRUE(srs_bytes_equals(NULL, NULL, 0)); + EXPECT_FALSE(srs_bytes_equals(a1, NULL, 1)); + EXPECT_FALSE(srs_bytes_equals(NULL, a1, 1)); + EXPECT_FALSE(srs_bytes_equals(a1, b1, 1)); + EXPECT_TRUE(srs_bytes_equals(a1, a1, 1)); + EXPECT_TRUE(srs_bytes_equals(a1, a2, 1)); + EXPECT_FALSE(srs_bytes_equals(a1, b2, 1)); } diff --git a/trunk/src/utest/srs_utest_handshake.hpp b/trunk/src/utest/srs_utest_handshake.hpp index d45e9337d..81e46dd97 100644 --- a/trunk/src/utest/srs_utest_handshake.hpp +++ b/trunk/src/utest/srs_utest_handshake.hpp @@ -29,39 +29,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include -#include #include #include using namespace srs; -class MockEmptyIO : public ISrsProtocolReaderWriter -{ -public: - MockEmptyIO(); - virtual ~MockEmptyIO(); -// for protocol -public: - virtual bool is_never_timeout(int64_t timeout_us); -// for handshake. -public: - virtual int read_fully(const void* buf, size_t size, ssize_t* nread); - virtual int write(const void* buf, size_t size, ssize_t* nwrite); -// for protocol -public: - virtual void set_recv_timeout(int64_t timeout_us); - virtual int64_t get_recv_timeout(); - virtual int64_t get_recv_bytes(); - virtual int get_recv_kbps(); -// for protocol -public: - virtual void set_send_timeout(int64_t timeout_us); - virtual int64_t get_send_timeout(); - virtual int64_t get_send_bytes(); - virtual int get_send_kbps(); - virtual int writev(const iovec *iov, int iov_size, ssize_t* nwrite); -// for protocol/amf0/msg-codec -public: - virtual int read(const void* buf, size_t size, ssize_t* nread); -}; - #endif