Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
A
Actor Framework
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Metrics
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
cpp-libs
Actor Framework
Commits
493e1077
Unverified
Commit
493e1077
authored
Jun 30, 2023
by
Dominik Charousset
Committed by
GitHub
Jun 30, 2023
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1459
Refactor test cases and unify mock socket app
parents
c3b094f7
5985e72c
Changes
6
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
328 additions
and
327 deletions
+328
-327
libcaf_net/test/detail/rfc6455.cpp
libcaf_net/test/detail/rfc6455.cpp
+171
-171
libcaf_net/test/net-test.cpp
libcaf_net/test/net-test.cpp
+55
-0
libcaf_net/test/net-test.hpp
libcaf_net/test/net-test.hpp
+66
-0
libcaf_net/test/net/web_socket/client.cpp
libcaf_net/test/net/web_socket/client.cpp
+5
-38
libcaf_net/test/net/web_socket/framing.cpp
libcaf_net/test/net/web_socket/framing.cpp
+15
-51
libcaf_net/test/net/web_socket/server.cpp
libcaf_net/test/net/web_socket/server.cpp
+16
-67
No files found.
libcaf_net/test/detail/rfc6455.cpp
View file @
493e1077
This diff is collapsed.
Click to expand it.
libcaf_net/test/net-test.cpp
View file @
493e1077
...
...
@@ -118,6 +118,61 @@ ptrdiff_t mock_stream_transport::handle_input() {
return
result
;
}
// -- mock_web_socket_app ------------------------------------------------------
mock_web_socket_app
::
mock_web_socket_app
(
bool
request_messages_on_start
)
:
request_messages_on_start
(
request_messages_on_start
)
{
// nop
}
caf
::
error
mock_web_socket_app
::
start
(
caf
::
net
::
web_socket
::
lower_layer
*
ll
)
{
down
=
ll
;
if
(
request_messages_on_start
)
down
->
request_messages
();
return
caf
::
none
;
}
void
mock_web_socket_app
::
prepare_send
()
{
// nop
}
bool
mock_web_socket_app
::
done_sending
()
{
return
true
;
}
caf
::
error
mock_web_socket_app
::
accept
(
const
caf
::
net
::
http
::
request_header
&
hdr
)
{
// Store the request information in cfg to evaluate them later.
auto
&
ws
=
cfg
[
"web-socket"
].
as_dictionary
();
put
(
ws
,
"method"
,
to_rfc_string
(
hdr
.
method
()));
put
(
ws
,
"path"
,
std
::
string
{
hdr
.
path
()});
put
(
ws
,
"query"
,
hdr
.
query
());
put
(
ws
,
"fragment"
,
hdr
.
fragment
());
put
(
ws
,
"http-version"
,
hdr
.
version
());
if
(
hdr
.
num_fields
()
>
0
)
{
auto
&
fields
=
ws
[
"fields"
].
as_dictionary
();
hdr
.
for_each_field
([
&
fields
](
auto
key
,
auto
val
)
{
put
(
fields
,
std
::
string
{
key
},
std
::
string
{
val
});
});
}
return
caf
::
none
;
}
void
mock_web_socket_app
::
abort
(
const
caf
::
error
&
reason
)
{
abort_reason
=
reason
;
down
->
shutdown
(
reason
);
}
ptrdiff_t
mock_web_socket_app
::
consume_text
(
std
::
string_view
text
)
{
text_input
.
insert
(
text_input
.
end
(),
text
.
begin
(),
text
.
end
());
return
static_cast
<
ptrdiff_t
>
(
text
.
size
());
}
ptrdiff_t
mock_web_socket_app
::
consume_binary
(
caf
::
byte_span
bytes
)
{
binary_input
.
insert
(
binary_input
.
end
(),
bytes
.
begin
(),
bytes
.
end
());
return
static_cast
<
ptrdiff_t
>
(
bytes
.
size
());
}
// -- barrier ------------------------------------------------------------------
void
barrier
::
arrive_and_wait
()
{
...
...
libcaf_net/test/net-test.hpp
View file @
493e1077
...
...
@@ -5,11 +5,16 @@
#include "caf/net/octet_stream/upper_layer.hpp"
#include "caf/net/receive_policy.hpp"
#include "caf/net/socket.hpp"
#include "caf/net/web_socket/server.hpp"
#include "caf/settings.hpp"
#include "caf/span.hpp"
#include "caf/string_view.hpp"
#include "caf/test/bdd_dsl.hpp"
/// Implements a trivial transport layer that stores the contents of all
/// received frames in a respective output buffer, it can propagate the content
/// of the input buffer to the upper layer, and switch protocols if configured
/// so.
class
mock_stream_transport
:
public
caf
::
net
::
octet_stream
::
lower_layer
{
public:
// -- member types -----------------------------------------------------------
...
...
@@ -105,6 +110,67 @@ private:
caf
::
net
::
multiplexer
*
mpx_
;
};
/// Tag used to configure mock_web_socket_app to request messages on start
struct
request_messages_on_start_t
{};
constexpr
auto
request_messages_on_start
=
request_messages_on_start_t
{};
/// Implements a trivial WebSocket application that stores the contents of all
/// received messages in respective text/binary buffers. It can take both
/// roles, server and client, request messages and track whether the
/// lower layer was aborted.
class
mock_web_socket_app
:
public
caf
::
net
::
web_socket
::
upper_layer
::
server
{
public:
// -- constructor ------------------------------------------------------------
explicit
mock_web_socket_app
(
bool
request_messages_on_start
);
// -- factories --------------------------------------------------------------
static
auto
make
(
request_messages_on_start_t
)
{
return
std
::
make_unique
<
mock_web_socket_app
>
(
true
);
}
static
auto
make
()
{
return
std
::
make_unique
<
mock_web_socket_app
>
(
false
);
}
// -- initialization ---------------------------------------------------------
caf
::
error
start
(
caf
::
net
::
web_socket
::
lower_layer
*
ll
)
override
;
// -- implementation ---------------------------------------------------------
caf
::
error
accept
(
const
caf
::
net
::
http
::
request_header
&
hdr
)
override
;
void
prepare_send
()
override
;
bool
done_sending
()
override
;
void
abort
(
const
caf
::
error
&
reason
)
override
;
ptrdiff_t
consume_text
(
std
::
string_view
text
)
override
;
ptrdiff_t
consume_binary
(
caf
::
byte_span
bytes
)
override
;
bool
has_aborted
()
const
noexcept
{
return
!
abort_reason
.
empty
();
}
// -- member variables -------------------------------------------------------
std
::
string
text_input
;
caf
::
byte_buffer
binary_input
;
caf
::
net
::
web_socket
::
lower_layer
*
down
=
nullptr
;
caf
::
settings
cfg
;
bool
request_messages_on_start
=
false
;
caf
::
error
abort_reason
;
};
// Drop-in replacement for std::barrier (based on the TS API as of 2020).
class
barrier
{
public:
...
...
libcaf_net/test/net/web_socket/client.cpp
View file @
493e1077
...
...
@@ -14,43 +14,6 @@ namespace {
using
svec
=
std
::
vector
<
std
::
string
>
;
class
app_t
:
public
net
::
web_socket
::
upper_layer
{
public:
static
auto
make
()
{
return
std
::
make_unique
<
app_t
>
();
}
std
::
string
text_input
;
caf
::
byte_buffer
binary_input
;
error
start
(
net
::
web_socket
::
lower_layer
*
)
override
{
return
none
;
}
void
prepare_send
()
override
{
// nop
}
bool
done_sending
()
override
{
return
true
;
}
void
abort
(
const
error
&
reason
)
override
{
CAF_FAIL
(
"app::abort called: "
<<
reason
);
}
ptrdiff_t
consume_text
(
std
::
string_view
text
)
override
{
text_input
.
insert
(
text_input
.
end
(),
text
.
begin
(),
text
.
end
());
return
static_cast
<
ptrdiff_t
>
(
text
.
size
());
}
ptrdiff_t
consume_binary
(
byte_span
bytes
)
override
{
binary_input
.
insert
(
binary_input
.
end
(),
bytes
.
begin
(),
bytes
.
end
());
return
static_cast
<
ptrdiff_t
>
(
bytes
.
size
());
}
};
constexpr
std
::
string_view
key
=
"the sample nonce"
;
constexpr
std
::
string_view
http_request
...
...
@@ -93,7 +56,8 @@ auto make_handshake() {
SCENARIO
(
"the client performs the WebSocket handshake on startup"
)
{
GIVEN
(
"valid WebSocket handshake data"
)
{
WHEN
(
"starting a WebSocket client"
)
{
auto
app
=
app_t
::
make
();
auto
app
=
mock_web_socket_app
::
make
();
auto
app_ptr
=
app
.
get
();
auto
ws
=
net
::
web_socket
::
client
::
make
(
make_handshake
(),
std
::
move
(
app
));
auto
uut
=
mock_stream_transport
::
make
(
std
::
move
(
ws
));
THEN
(
"the client sends its HTTP request when initializing it"
)
{
...
...
@@ -105,6 +69,9 @@ SCENARIO("the client performs the WebSocket handshake on startup") {
CHECK_EQ
(
uut
->
handle_input
(),
static_cast
<
ptrdiff_t
>
(
http_response
.
size
()));
}
AND
(
"the client did not abort"
)
{
CHECK
(
!
app_ptr
->
has_aborted
());
}
}
}
}
libcaf_net/test/net/web_socket/framing.cpp
View file @
493e1077
...
...
@@ -13,59 +13,13 @@ using namespace std::literals;
namespace
{
class
app_t
:
public
net
::
web_socket
::
upper_layer
{
public:
static
auto
make
()
{
return
std
::
make_unique
<
app_t
>
();
}
std
::
string
text_input
;
caf
::
byte_buffer
binary_input
;
error
err
;
bool
aborted
=
false
;
net
::
web_socket
::
lower_layer
*
down
=
nullptr
;
error
start
(
net
::
web_socket
::
lower_layer
*
ll
)
override
{
down
=
ll
;
return
none
;
}
void
prepare_send
()
override
{
// nop
}
bool
done_sending
()
override
{
return
true
;
}
void
abort
(
const
error
&
reason
)
override
{
aborted
=
true
;
err
=
reason
;
down
->
shutdown
(
reason
);
}
ptrdiff_t
consume_text
(
std
::
string_view
text
)
override
{
text_input
.
insert
(
text_input
.
end
(),
text
.
begin
(),
text
.
end
());
return
static_cast
<
ptrdiff_t
>
(
text
.
size
());
}
ptrdiff_t
consume_binary
(
byte_span
bytes
)
override
{
binary_input
.
insert
(
binary_input
.
end
(),
bytes
.
begin
(),
bytes
.
end
());
return
static_cast
<
ptrdiff_t
>
(
bytes
.
size
());
}
};
struct
fixture
{
app_t
*
app
;
mock_web_socket_app
*
app
;
net
::
web_socket
::
framing
*
uut
;
std
::
unique_ptr
<
mock_stream_transport
>
transport
;
fixture
()
{
auto
app_layer
=
app_t
::
make
();
auto
app_layer
=
mock_web_socket_app
::
make
();
app
=
app_layer
.
get
();
auto
uut_layer
=
net
::
web_socket
::
framing
::
make_server
(
std
::
move
(
app_layer
));
...
...
@@ -100,6 +54,9 @@ SCENARIO("the client sends a ping and receives a pong response") {
pong_frame
);
CHECK_EQ
(
transport
->
output_buffer
(),
pong_frame
);
}
AND
(
"the client did not abort"
)
{
CHECK
(
!
app
->
has_aborted
());
}
}
transport
->
output_buffer
().
clear
();
WHEN
(
"the client sends a ping with some data"
)
{
...
...
@@ -114,6 +71,9 @@ SCENARIO("the client sends a ping and receives a pong response") {
pong_frame
);
CHECK_EQ
(
transport
->
output_buffer
(),
pong_frame
);
}
AND
(
"the client did not abort"
)
{
CHECK
(
!
app
->
has_aborted
());
}
}
transport
->
output_buffer
().
clear
();
WHEN
(
"the client sends a ping with the maximum allowed 125 bytes"
)
{
...
...
@@ -128,6 +88,9 @@ SCENARIO("the client sends a ping and receives a pong response") {
pong_frame
);
CHECK_EQ
(
transport
->
output_buffer
(),
pong_frame
);
}
AND
(
"the client did not abort"
)
{
CHECK
(
!
app
->
has_aborted
());
}
}
}
}
...
...
@@ -141,6 +104,7 @@ TEST_CASE("calling shutdown with protocol_error sets status in close header") {
auto
status
=
(
std
::
to_integer
<
int
>
(
transport
->
output_buffer
().
at
(
2
))
<<
8
)
+
std
::
to_integer
<
int
>
(
transport
->
output_buffer
().
at
(
3
));
CHECK_EQ
(
status
,
static_cast
<
int
>
(
net
::
web_socket
::
status
::
protocol_error
));
CHECK
(
!
app
->
has_aborted
());
}
SCENARIO
(
"the client sends an invalid ping that closes the connection"
)
{
...
...
@@ -153,9 +117,9 @@ SCENARIO("the client sends an invalid ping that closes the connection") {
transport
->
push
(
ping_frame
);
THEN
(
"the server aborts the application"
)
{
CHECK_EQ
(
transport
->
handle_input
(),
0
);
CHECK
(
app
->
aborted
);
CHECK_EQ
(
app
->
err
,
sec
::
protocol_error
);
MESSAGE
(
"Aborted with: "
<<
app
->
err
);
CHECK
(
app
->
has_aborted
()
);
CHECK_EQ
(
app
->
abort_reason
,
sec
::
protocol_error
);
MESSAGE
(
"Aborted with: "
<<
app
->
abort_reason
);
}
AND
(
"the server closes the connection with a protocol error"
)
{
detail
::
rfc6455
::
header
hdr
;
...
...
libcaf_net/test/net/web_socket/server.cpp
View file @
493e1077
...
...
@@ -20,67 +20,10 @@ namespace {
using
svec
=
std
::
vector
<
std
::
string
>
;
class
app_t
:
public
net
::
web_socket
::
upper_layer
::
server
{
public:
std
::
string
text_input
;
byte_buffer
binary_input
;
settings
cfg
;
static
auto
make
()
{
return
std
::
make_unique
<
app_t
>
();
}
error
start
(
net
::
web_socket
::
lower_layer
*
down
)
override
{
down
->
request_messages
();
return
none
;
}
error
accept
(
const
net
::
http
::
request_header
&
hdr
)
override
{
// Store the request information in cfg to evaluate them later.
auto
&
ws
=
cfg
[
"web-socket"
].
as_dictionary
();
put
(
ws
,
"method"
,
to_rfc_string
(
hdr
.
method
()));
put
(
ws
,
"path"
,
std
::
string
{
hdr
.
path
()});
put
(
ws
,
"query"
,
hdr
.
query
());
put
(
ws
,
"fragment"
,
hdr
.
fragment
());
put
(
ws
,
"http-version"
,
hdr
.
version
());
if
(
hdr
.
num_fields
()
>
0
)
{
auto
&
fields
=
ws
[
"fields"
].
as_dictionary
();
hdr
.
for_each_field
([
&
fields
](
auto
key
,
auto
val
)
{
put
(
fields
,
std
::
string
{
key
},
std
::
string
{
val
});
});
}
return
none
;
}
void
prepare_send
()
override
{
// nop
}
bool
done_sending
()
override
{
return
true
;
}
void
abort
(
const
error
&
reason
)
override
{
CAF_FAIL
(
"app::abort called: "
<<
reason
);
}
ptrdiff_t
consume_text
(
std
::
string_view
text
)
override
{
text_input
.
insert
(
text_input
.
end
(),
text
.
begin
(),
text
.
end
());
return
static_cast
<
ptrdiff_t
>
(
text
.
size
());
}
ptrdiff_t
consume_binary
(
byte_span
bytes
)
override
{
binary_input
.
insert
(
binary_input
.
end
(),
bytes
.
begin
(),
bytes
.
end
());
return
static_cast
<
ptrdiff_t
>
(
bytes
.
size
());
}
};
struct
fixture
{
fixture
()
{
using
namespace
caf
::
net
;
auto
app_ptr
=
app_t
::
make
(
);
auto
app_ptr
=
mock_web_socket_app
::
make
(
request_messages_on_start
);
app
=
app_ptr
.
get
();
auto
ws_ptr
=
net
::
web_socket
::
server
::
make
(
std
::
move
(
app_ptr
));
transport
=
mock_stream_transport
::
make
(
std
::
move
(
ws_ptr
));
...
...
@@ -126,7 +69,7 @@ struct fixture {
std
::
unique_ptr
<
mock_stream_transport
>
transport
;
app_t
*
app
;
mock_web_socket_app
*
app
;
std
::
minstd_rand
rng
;
};
...
...
@@ -148,9 +91,9 @@ constexpr std::string_view opening_handshake
if (CHECK(holds_alternative<std::string>(app->cfg, key))) \
CHECK_EQ(get<std::string>(app->cfg, key), expected_value);
CAF_TEST_FIXTURE_SCOPE
(
web_socket_tests
,
fixture
)
BEGIN_FIXTURE_SCOPE
(
fixture
)
CAF_TEST
(
applications
receive
handshake
data
via
config
)
{
TEST_CASE
(
"applications receive handshake data via config"
)
{
transport
->
push
(
opening_handshake
);
{
auto
consumed
=
transport
->
handle_input
();
...
...
@@ -174,9 +117,10 @@ CAF_TEST(applications receive handshake data via config) {
using
str_map
=
std
::
map
<
std
::
string
,
std
::
string
>
;
if
(
auto
query
=
get_as
<
str_map
>
(
app
->
cfg
,
"web-socket.query"
);
CHECK
(
query
))
CHECK_EQ
(
*
query
,
str_map
({{
"room"
s
,
"lounge"
s
}}));
CHECK
(
!
app
->
has_aborted
());
}
CAF_TEST
(
the
server
responds
with
an
HTTP
response
on
success
)
{
TEST_CASE
(
"the server responds with an HTTP response on success"
)
{
transport
->
push
(
opening_handshake
);
CHECK_EQ
(
transport
->
handle_input
(),
static_cast
<
ptrdiff_t
>
(
opening_handshake
.
size
()));
...
...
@@ -185,9 +129,10 @@ CAF_TEST(the server responds with an HTTP response on success) {
"Upgrade: websocket
\r\n
"
"Connection: Upgrade
\r\n
"
"Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
\r\n\r\n
"
);
CHECK
(
!
app
->
has_aborted
());
}
CAF_TEST
(
handshakes
may
arrive
in
chunks
)
{
TEST_CASE
(
"handshakes may arrive in chunks"
)
{
svec
bufs
;
size_t
chunk_size
=
opening_handshake
.
size
()
/
3
;
auto
i
=
opening_handshake
.
begin
();
...
...
@@ -203,9 +148,10 @@ CAF_TEST(handshakes may arrive in chunks) {
transport
->
push
(
bufs
[
2
]);
CHECK_EQ
(
static_cast
<
size_t
>
(
transport
->
handle_input
()),
opening_handshake
.
size
());
CHECK
(
!
app
->
has_aborted
());
}
CAF_TEST
(
data
may
follow
the
handshake
immediately
)
{
TEST_CASE
(
"data may follow the handshake immediately"
)
{
auto
hs_bytes
=
as_bytes
(
make_span
(
opening_handshake
));
byte_buffer
buf
{
hs_bytes
.
begin
(),
hs_bytes
.
end
()};
rfc6455_append
(
"Hello WebSocket!
\n
"
sv
,
buf
);
...
...
@@ -213,18 +159,20 @@ CAF_TEST(data may follow the handshake immediately) {
transport
->
push
(
buf
);
CHECK_EQ
(
transport
->
handle_input
(),
static_cast
<
ptrdiff_t
>
(
buf
.
size
()));
CHECK_EQ
(
app
->
text_input
,
"Hello WebSocket!
\n
Bye WebSocket!
\n
"
);
CHECK
(
!
app
->
has_aborted
());
}
CAF_TEST
(
data
may
arrive
later
)
{
TEST_CASE
(
"data may arrive later"
)
{
transport
->
push
(
opening_handshake
);
CHECK_EQ
(
transport
->
handle_input
(),
static_cast
<
ptrdiff_t
>
(
opening_handshake
.
size
()));
push
(
"Hello WebSocket!
\n
Bye WebSocket!
\n
"
sv
);
transport
->
handle_input
();
CHECK_EQ
(
app
->
text_input
,
"Hello WebSocket!
\n
Bye WebSocket!
\n
"
);
CHECK
(
!
app
->
has_aborted
());
}
CAF_TEST
(
data
may
arrive
fragmented
)
{
TEST_CASE
(
"data may arrive fragmented"
)
{
transport
->
push
(
opening_handshake
);
CHECK_EQ
(
transport
->
handle_input
(),
static_cast
<
ptrdiff_t
>
(
opening_handshake
.
size
()));
...
...
@@ -237,6 +185,7 @@ CAF_TEST(data may arrive fragmented) {
transport
->
push
(
buf
);
CHECK_EQ
(
transport
->
handle_input
(),
static_cast
<
ptrdiff_t
>
(
buf
.
size
()));
CHECK_EQ
(
app
->
text_input
,
"Hello WebSocket!
\n
Bye WebSocket!
\n
"
);
CHECK
(
!
app
->
has_aborted
());
}
CAF_TEST_FIXTURE_SCOPE_END
()
END_FIXTURE_SCOPE
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment