1
1
#include " inspector_socket.h"
2
-
3
2
#include " gtest/gtest.h"
4
3
5
4
#define PORT 9444
6
5
7
- using namespace node ::inspector;
6
+ namespace {
7
+
8
+ using node::inspector::InspectorSocket;
9
+ using node::inspector::inspector_from_stream;
10
+ using node::inspector::inspector_handshake_event;
11
+ using node::inspector::kInspectorHandshakeFailed ;
12
+ using node::inspector::kInspectorHandshakeHttpGet ;
13
+ using node::inspector::kInspectorHandshakeUpgraded ;
14
+ using node::inspector::kInspectorHandshakeUpgrading ;
8
15
9
16
static const int MAX_LOOP_ITERATIONS = 10000 ;
10
17
@@ -24,7 +31,7 @@ static enum inspector_handshake_event last_event = kInspectorHandshakeHttpGet;
24
31
static uv_loop_t loop;
25
32
static uv_tcp_t server, client_socket;
26
33
static InspectorSocket inspector;
27
- static std::string last_path;
34
+ static std::string last_path; // NOLINT(runtime/string)
28
35
static void (*handshake_delegate)(enum inspector_handshake_event state,
29
36
const std::string& path,
30
37
bool * should_continue);
@@ -45,7 +52,7 @@ static const char HANDSHAKE_REQ[] = "GET /ws/path HTTP/1.1\r\n"
45
52
" Sec-WebSocket-Version: 13\r\n\r\n " ;
46
53
47
54
class Timeout {
48
- public:
55
+ public:
49
56
explicit Timeout (uv_loop_t * loop) : timed_out(false ), done_(false ) {
50
57
uv_timer_init (loop, &timer_);
51
58
uv_timer_start (&timer_, Timeout::set_flag, 5000 , 0 );
@@ -58,8 +65,10 @@ class Timeout {
58
65
uv_run (&loop, UV_RUN_NOWAIT);
59
66
}
60
67
}
68
+
61
69
bool timed_out;
62
- private:
70
+
71
+ private:
63
72
static void set_flag (uv_timer_t * timer) {
64
73
Timeout* t = node::ContainerOf (&Timeout::timer_, timer);
65
74
t->timed_out = true ;
@@ -129,7 +138,7 @@ static void check_data_cb(read_expects* expectation, ssize_t nread,
129
138
EXPECT_TRUE (nread >= 0 && nread != UV_EOF);
130
139
ssize_t i;
131
140
char c, actual;
132
- ASSERT_TRUE (expectation->expected_len > 0 );
141
+ ASSERT_GT (expectation->expected_len , 0 );
133
142
for (i = 0 ; i < nread && expectation->pos <= expectation->expected_len ; i++) {
134
143
c = expectation->expected [expectation->pos ++];
135
144
actual = buf->base [i];
@@ -176,7 +185,7 @@ static void fail_callback(uv_stream_t* stream, ssize_t nread,
176
185
} else {
177
186
fprintf (stderr, " Read %zd bytes\n " , nread);
178
187
}
179
- ASSERT_TRUE (false ); // Shouldn't have been called
188
+ ASSERT_TRUE (false ); // Shouldn't have been called
180
189
}
181
190
182
191
static void expect_nothing_on_client () {
@@ -239,7 +248,7 @@ static void grow_expects_buffer(uv_handle_t* stream, size_t size, uv_buf_t* b) {
239
248
240
249
static void save_read_data (uv_stream_t * stream, ssize_t nread,
241
250
const uv_buf_t * buf) {
242
- expectations* expects =static_cast <expectations*>(
251
+ expectations* expects = static_cast <expectations*>(
243
252
inspector_from_stream (stream)->data );
244
253
expects->err_code = nread < 0 ? nread : 0 ;
245
254
if (nread > 0 ) {
@@ -344,7 +353,7 @@ static void on_connection(uv_connect_t* connect, int status) {
344
353
}
345
354
346
355
class InspectorSocketTest : public ::testing::Test {
347
- protected:
356
+ protected:
348
357
virtual void SetUp () {
349
358
inspector.reinit ();
350
359
handshake_delegate = stop_if_stop_path;
@@ -368,7 +377,7 @@ class InspectorSocketTest : public ::testing::Test {
368
377
connect.data = nullptr ;
369
378
uv_tcp_connect (&connect, &client_socket,
370
379
reinterpret_cast <const sockaddr*>(&addr), on_connection);
371
- uv_tcp_nodelay (&client_socket, 1 ); // The buffering messes up the test
380
+ uv_tcp_nodelay (&client_socket, 1 ); // The buffering messes up the test
372
381
SPIN_WHILE (!connect.data || !connected);
373
382
really_close (reinterpret_cast <uv_handle_t *>(&server));
374
383
}
@@ -420,7 +429,6 @@ TEST_F(InspectorSocketTest, ReadsAndWritesInspectorMessage) {
420
429
}
421
430
422
431
TEST_F (InspectorSocketTest, BufferEdgeCases) {
423
-
424
432
do_write (const_cast <char *>(HANDSHAKE_REQ), sizeof (HANDSHAKE_REQ) - 1 );
425
433
expect_handshake ();
426
434
@@ -498,7 +506,8 @@ TEST_F(InspectorSocketTest, AcceptsRequestInSeveralWrites) {
498
506
SPIN_WHILE (!inspector_ready);
499
507
expect_handshake ();
500
508
inspector_read_stop (&inspector);
501
- GTEST_ASSERT_EQ (uv_is_active (reinterpret_cast <uv_handle_t *>(&client_socket)), 0 );
509
+ GTEST_ASSERT_EQ (uv_is_active (reinterpret_cast <uv_handle_t *>(&client_socket)),
510
+ 0 );
502
511
manual_inspector_socket_cleanup ();
503
512
}
504
513
@@ -531,7 +540,6 @@ TEST_F(InspectorSocketTest, RequestWithoutKey) {
531
540
" Upgrade: websocket\r\n "
532
541
" Connection: Upgrade\r\n "
533
542
" Sec-WebSocket-Version: 13\r\n\r\n " ;
534
- ;
535
543
536
544
do_write (const_cast <char *>(BROKEN_REQUEST), sizeof (BROKEN_REQUEST) - 1 );
537
545
SPIN_WHILE (last_event != kInspectorHandshakeFailed );
@@ -549,7 +557,8 @@ TEST_F(InspectorSocketTest, KillsConnectionOnProtocolViolation) {
549
557
const char SERVER_FRAME[] = " I'm not a good WS frame. Nope!" ;
550
558
do_write (SERVER_FRAME, sizeof (SERVER_FRAME));
551
559
expect_server_read_error ();
552
- GTEST_ASSERT_EQ (uv_is_active (reinterpret_cast <uv_handle_t *>(&client_socket)), 0 );
560
+ GTEST_ASSERT_EQ (uv_is_active (reinterpret_cast <uv_handle_t *>(&client_socket)),
561
+ 0 );
553
562
}
554
563
555
564
TEST_F (InspectorSocketTest, CanStopReadingFromInspector) {
@@ -863,7 +872,7 @@ TEST_F(InspectorSocketTest, Send1Mb) {
863
872
outgoing.resize (outgoing.size () + message.size ());
864
873
mask_message (message, &outgoing[sizeof (FRAME_TO_SERVER_HEADER)], MASK);
865
874
866
- setup_inspector_expecting (); // Buffer on the client side.
875
+ setup_inspector_expecting (); // Buffer on the client side.
867
876
do_write (&outgoing[0 ], outgoing.size ());
868
877
expect_on_server (&message[0 ], message.size ());
869
878
@@ -896,3 +905,5 @@ TEST_F(InspectorSocketTest, ErrorCleansUpTheSocket) {
896
905
SPIN_WHILE (err > 0 );
897
906
EXPECT_EQ (UV_EPROTO, err);
898
907
}
908
+
909
+ } // anonymous namespace
0 commit comments