@ -1,10 +1,17 @@
# include "inspector_socket.h"
# include "gtest/gtest.h"
# define PORT 9444
using namespace node : : inspector ;
namespace {
using node : : inspector : : InspectorSocket ;
using node : : inspector : : inspector_from_stream ;
using node : : inspector : : inspector_handshake_event ;
using node : : inspector : : kInspectorHandshakeFailed ;
using node : : inspector : : kInspectorHandshakeHttpGet ;
using node : : inspector : : kInspectorHandshakeUpgraded ;
using node : : inspector : : kInspectorHandshakeUpgrading ;
static const int MAX_LOOP_ITERATIONS = 10000 ;
@ -24,7 +31,7 @@ static enum inspector_handshake_event last_event = kInspectorHandshakeHttpGet;
static uv_loop_t loop ;
static uv_tcp_t server , client_socket ;
static InspectorSocket inspector ;
static std : : string last_path ;
static std : : string last_path ; // NOLINT(runtime/string)
static void ( * handshake_delegate ) ( enum inspector_handshake_event state ,
const std : : string & path ,
bool * should_continue ) ;
@ -45,7 +52,7 @@ static const char HANDSHAKE_REQ[] = "GET /ws/path HTTP/1.1\r\n"
" Sec-WebSocket-Version: 13 \r \n \r \n " ;
class Timeout {
public :
public :
explicit Timeout ( uv_loop_t * loop ) : timed_out ( false ) , done_ ( false ) {
uv_timer_init ( loop , & timer_ ) ;
uv_timer_start ( & timer_ , Timeout : : set_flag , 5000 , 0 ) ;
@ -58,8 +65,10 @@ public:
uv_run ( & loop , UV_RUN_NOWAIT ) ;
}
}
bool timed_out ;
private :
private :
static void set_flag ( uv_timer_t * timer ) {
Timeout * t = node : : ContainerOf ( & Timeout : : timer_ , timer ) ;
t - > timed_out = true ;
@ -128,7 +137,7 @@ static void check_data_cb(read_expects* expectation, ssize_t nread,
EXPECT_TRUE ( nread > = 0 & & nread ! = UV_EOF ) ;
ssize_t i ;
char c , actual ;
ASSERT_TRUE ( expectation - > expected_len > 0 ) ;
ASSERT_G T ( expectation - > expected_len , 0 ) ;
for ( i = 0 ; i < nread & & expectation - > pos < = expectation - > expected_len ; i + + ) {
c = expectation - > expected [ expectation - > pos + + ] ;
actual = buf - > base [ i ] ;
@ -175,7 +184,7 @@ static void fail_callback(uv_stream_t* stream, ssize_t nread,
} else {
fprintf ( stderr , " Read %zd bytes \n " , nread ) ;
}
ASSERT_TRUE ( false ) ; // Shouldn't have been called
ASSERT_TRUE ( false ) ; // Shouldn't have been called
}
static void expect_nothing_on_client ( ) {
@ -238,7 +247,7 @@ static void grow_expects_buffer(uv_handle_t* stream, size_t size, uv_buf_t* b) {
static void save_read_data ( uv_stream_t * stream , ssize_t nread ,
const uv_buf_t * buf ) {
expectations * expects = static_cast < expectations * > (
expectations * expects = static_cast < expectations * > (
inspector_from_stream ( stream ) - > data ) ;
expects - > err_code = nread < 0 ? nread : 0 ;
if ( nread > 0 ) {
@ -343,7 +352,7 @@ static void on_connection(uv_connect_t* connect, int status) {
}
class InspectorSocketTest : public : : testing : : Test {
protected :
protected :
virtual void SetUp ( ) {
inspector . reinit ( ) ;
handshake_delegate = stop_if_stop_path ;
@ -367,7 +376,7 @@ protected:
connect . data = nullptr ;
uv_tcp_connect ( & connect , & client_socket ,
reinterpret_cast < const sockaddr * > ( & addr ) , on_connection ) ;
uv_tcp_nodelay ( & client_socket , 1 ) ; // The buffering messes up the test
uv_tcp_nodelay ( & client_socket , 1 ) ; // The buffering messes up the test
SPIN_WHILE ( ! connect . data | | ! connected ) ;
really_close ( reinterpret_cast < uv_handle_t * > ( & server ) ) ;
}
@ -419,7 +428,6 @@ TEST_F(InspectorSocketTest, ReadsAndWritesInspectorMessage) {
}
TEST_F ( InspectorSocketTest , BufferEdgeCases ) {
do_write ( const_cast < char * > ( HANDSHAKE_REQ ) , sizeof ( HANDSHAKE_REQ ) - 1 ) ;
expect_handshake ( ) ;
@ -497,7 +505,8 @@ TEST_F(InspectorSocketTest, AcceptsRequestInSeveralWrites) {
SPIN_WHILE ( ! inspector_ready ) ;
expect_handshake ( ) ;
inspector_read_stop ( & inspector ) ;
GTEST_ASSERT_EQ ( uv_is_active ( reinterpret_cast < uv_handle_t * > ( & client_socket ) ) , 0 ) ;
GTEST_ASSERT_EQ ( uv_is_active ( reinterpret_cast < uv_handle_t * > ( & client_socket ) ) ,
0 ) ;
manual_inspector_socket_cleanup ( ) ;
}
@ -530,7 +539,6 @@ TEST_F(InspectorSocketTest, RequestWithoutKey) {
" Upgrade: websocket \r \n "
" Connection: Upgrade \r \n "
" Sec-WebSocket-Version: 13 \r \n \r \n " ;
;
do_write ( const_cast < char * > ( BROKEN_REQUEST ) , sizeof ( BROKEN_REQUEST ) - 1 ) ;
SPIN_WHILE ( last_event ! = kInspectorHandshakeFailed ) ;
@ -548,7 +556,8 @@ TEST_F(InspectorSocketTest, KillsConnectionOnProtocolViolation) {
const char SERVER_FRAME [ ] = " I'm not a good WS frame. Nope! " ;
do_write ( SERVER_FRAME , sizeof ( SERVER_FRAME ) ) ;
expect_server_read_error ( ) ;
GTEST_ASSERT_EQ ( uv_is_active ( reinterpret_cast < uv_handle_t * > ( & client_socket ) ) , 0 ) ;
GTEST_ASSERT_EQ ( uv_is_active ( reinterpret_cast < uv_handle_t * > ( & client_socket ) ) ,
0 ) ;
}
TEST_F ( InspectorSocketTest , CanStopReadingFromInspector ) {
@ -862,7 +871,7 @@ TEST_F(InspectorSocketTest, Send1Mb) {
outgoing . resize ( outgoing . size ( ) + message . size ( ) ) ;
mask_message ( message , & outgoing [ sizeof ( FRAME_TO_SERVER_HEADER ) ] , MASK ) ;
setup_inspector_expecting ( ) ; // Buffer on the client side.
setup_inspector_expecting ( ) ; // Buffer on the client side.
do_write ( & outgoing [ 0 ] , outgoing . size ( ) ) ;
expect_on_server ( & message [ 0 ] , message . size ( ) ) ;
@ -895,3 +904,5 @@ TEST_F(InspectorSocketTest, ErrorCleansUpTheSocket) {
SPIN_WHILE ( err > 0 ) ;
EXPECT_EQ ( UV_EPROTO , err ) ;
}
} // anonymous namespace