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 ;
@@ -128,7 +137,7 @@ static void check_data_cb(read_expects* expectation, ssize_t nread,
128
137
EXPECT_TRUE (nread >= 0 && nread != UV_EOF);
129
138
ssize_t i;
130
139
char c, actual;
131
- ASSERT_TRUE (expectation->expected_len > 0 );
140
+ ASSERT_GT (expectation->expected_len , 0 );
132
141
for (i = 0 ; i < nread && expectation->pos <= expectation->expected_len ; i++) {
133
142
c = expectation->expected [expectation->pos ++];
134
143
actual = buf->base [i];
@@ -175,7 +184,7 @@ static void fail_callback(uv_stream_t* stream, ssize_t nread,
175
184
} else {
176
185
fprintf (stderr, " Read %zd bytes\n " , nread);
177
186
}
178
- ASSERT_TRUE (false ); // Shouldn't have been called
187
+ ASSERT_TRUE (false ); // Shouldn't have been called
179
188
}
180
189
181
190
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) {
238
247
239
248
static void save_read_data (uv_stream_t * stream, ssize_t nread,
240
249
const uv_buf_t * buf) {
241
- expectations* expects =static_cast <expectations*>(
250
+ expectations* expects = static_cast <expectations*>(
242
251
inspector_from_stream (stream)->data );
243
252
expects->err_code = nread < 0 ? nread : 0 ;
244
253
if (nread > 0 ) {
@@ -343,7 +352,7 @@ static void on_connection(uv_connect_t* connect, int status) {
343
352
}
344
353
345
354
class InspectorSocketTest : public ::testing::Test {
346
- protected:
355
+ protected:
347
356
virtual void SetUp () {
348
357
inspector.reinit ();
349
358
handshake_delegate = stop_if_stop_path;
@@ -367,7 +376,7 @@ class InspectorSocketTest : public ::testing::Test {
367
376
connect.data = nullptr ;
368
377
uv_tcp_connect (&connect, &client_socket,
369
378
reinterpret_cast <const sockaddr*>(&addr), on_connection);
370
- uv_tcp_nodelay (&client_socket, 1 ); // The buffering messes up the test
379
+ uv_tcp_nodelay (&client_socket, 1 ); // The buffering messes up the test
371
380
SPIN_WHILE (!connect.data || !connected);
372
381
really_close (reinterpret_cast <uv_handle_t *>(&server));
373
382
}
@@ -419,7 +428,6 @@ TEST_F(InspectorSocketTest, ReadsAndWritesInspectorMessage) {
419
428
}
420
429
421
430
TEST_F (InspectorSocketTest, BufferEdgeCases) {
422
-
423
431
do_write (const_cast <char *>(HANDSHAKE_REQ), sizeof (HANDSHAKE_REQ) - 1 );
424
432
expect_handshake ();
425
433
@@ -497,7 +505,8 @@ TEST_F(InspectorSocketTest, AcceptsRequestInSeveralWrites) {
497
505
SPIN_WHILE (!inspector_ready);
498
506
expect_handshake ();
499
507
inspector_read_stop (&inspector);
500
- GTEST_ASSERT_EQ (uv_is_active (reinterpret_cast <uv_handle_t *>(&client_socket)), 0 );
508
+ GTEST_ASSERT_EQ (uv_is_active (reinterpret_cast <uv_handle_t *>(&client_socket)),
509
+ 0 );
501
510
manual_inspector_socket_cleanup ();
502
511
}
503
512
@@ -530,7 +539,6 @@ TEST_F(InspectorSocketTest, RequestWithoutKey) {
530
539
" Upgrade: websocket\r\n "
531
540
" Connection: Upgrade\r\n "
532
541
" Sec-WebSocket-Version: 13\r\n\r\n " ;
533
- ;
534
542
535
543
do_write (const_cast <char *>(BROKEN_REQUEST), sizeof (BROKEN_REQUEST) - 1 );
536
544
SPIN_WHILE (last_event != kInspectorHandshakeFailed );
@@ -548,7 +556,8 @@ TEST_F(InspectorSocketTest, KillsConnectionOnProtocolViolation) {
548
556
const char SERVER_FRAME[] = " I'm not a good WS frame. Nope!" ;
549
557
do_write (SERVER_FRAME, sizeof (SERVER_FRAME));
550
558
expect_server_read_error ();
551
- GTEST_ASSERT_EQ (uv_is_active (reinterpret_cast <uv_handle_t *>(&client_socket)), 0 );
559
+ GTEST_ASSERT_EQ (uv_is_active (reinterpret_cast <uv_handle_t *>(&client_socket)),
560
+ 0 );
552
561
}
553
562
554
563
TEST_F (InspectorSocketTest, CanStopReadingFromInspector) {
@@ -862,7 +871,7 @@ TEST_F(InspectorSocketTest, Send1Mb) {
862
871
outgoing.resize (outgoing.size () + message.size ());
863
872
mask_message (message, &outgoing[sizeof (FRAME_TO_SERVER_HEADER)], MASK);
864
873
865
- setup_inspector_expecting (); // Buffer on the client side.
874
+ setup_inspector_expecting (); // Buffer on the client side.
866
875
do_write (&outgoing[0 ], outgoing.size ());
867
876
expect_on_server (&message[0 ], message.size ());
868
877
@@ -895,3 +904,5 @@ TEST_F(InspectorSocketTest, ErrorCleansUpTheSocket) {
895
904
SPIN_WHILE (err > 0 );
896
905
EXPECT_EQ (UV_EPROTO, err);
897
906
}
907
+
908
+ } // anonymous namespace
0 commit comments