Skip to content

Commit

Permalink
For #1042, cover RTMP client protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Dec 11, 2019
1 parent 69817a9 commit 41a9f15
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 0 deletions.
5 changes: 5 additions & 0 deletions trunk/src/utest/srs_utest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ extern srs_utime_t _srs_tmp_timeout;
#define HELPER_EXPECT_SUCCESS(x) EXPECT_TRUE(srs_success == (err = x)); srs_freep(err)
#define HELPER_EXPECT_FAILED(x) EXPECT_TRUE(srs_success != (err = x)); srs_freep(err)

// For errors, assert.
// @remark The err is leak when error, but it's ok in utest.
#define HELPER_ASSERT_SUCCESS(x) ASSERT_TRUE(srs_success == (err = x)); srs_freep(err)
#define HELPER_ASSERT_FAILED(x) ASSERT_TRUE(srs_success != (err = x)); srs_freep(err)

// For init array data.
#define HELPER_ARRAY_INIT(buf, sz, val) \
for (int i = 0; i < (int)sz; i++) (buf)[i]=val
Expand Down
30 changes: 30 additions & 0 deletions trunk/src/utest/srs_utest_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ MockBufferIO* MockBufferIO::append(string data)
return this;
}

MockBufferIO* MockBufferIO::append(MockBufferIO* data)
{
in_buffer.append(&data->in_buffer);
return this;
}

MockBufferIO* MockBufferIO::append(uint8_t* data, int size)
{
in_buffer.append((char*)data, size);
Expand All @@ -130,6 +136,12 @@ MockBufferIO* MockBufferIO::out_append(string data)
return this;
}

MockBufferIO* MockBufferIO::out_append(MockBufferIO* data)
{
out_buffer.append(&data->out_buffer);
return this;
}

MockBufferIO* MockBufferIO::out_append(uint8_t* data, int size)
{
out_buffer.append((char*)data, size);
Expand Down Expand Up @@ -488,6 +500,15 @@ VOID TEST(ProtocolHandshakeTest, ComplexHandshake)
SrsRtmpClient r(&io);
HELPER_EXPECT_SUCCESS(r.complex_handshake());
}

if (true) {
MockBufferIO io;
io.append(c0c1, 1537);
io.append(c2, 1536);

SrsRtmpServer r(&io);
HELPER_EXPECT_SUCCESS(r.handshake());
}
}

VOID TEST(ProtocolHandshakeTest, SimpleHandshake)
Expand Down Expand Up @@ -539,6 +560,15 @@ VOID TEST(ProtocolHandshakeTest, SimpleHandshake)
SrsRtmpClient r(&io);
HELPER_EXPECT_SUCCESS(r.simple_handshake());
}

if (true) {
MockBufferIO io;
io.append(c0c1, 1537);
io.append(c2, 1536);

SrsRtmpServer r(&io);
HELPER_EXPECT_SUCCESS(r.handshake());
}
}

/**
Expand Down
2 changes: 2 additions & 0 deletions trunk/src/utest/srs_utest_protocol.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ class MockBufferIO : public ISrsProtocolReadWriter
public:
virtual int length();
virtual MockBufferIO* append(std::string data);
virtual MockBufferIO* append(MockBufferIO* data);
virtual MockBufferIO* append(uint8_t* data, int size);
public:
virtual int out_length();
virtual MockBufferIO* out_append(std::string data);
virtual MockBufferIO* out_append(MockBufferIO* data);
virtual MockBufferIO* out_append(uint8_t* data, int size);
// for handshake.
public:
Expand Down
153 changes: 153 additions & 0 deletions trunk/src/utest/srs_utest_protostack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,3 +1450,156 @@ VOID TEST(ProtoStackTest, ServerInfo)
EXPECT_EQ(0, si.build);
}

VOID TEST(ProtoStackTest, ClientCommandMessage)
{
srs_error_t err;

// ConnectApp.
if (true) {
MockBufferIO io;

if (true) {
SrsConnectAppResPacket* res = new SrsConnectAppResPacket();

SrsAmf0EcmaArray* data = SrsAmf0Any::ecma_array();
res->info->set("data", data);

data->set("srs_server_ip", SrsAmf0Any::str("1.2.3.4"));
data->set("srs_server", SrsAmf0Any::str("srs"));
data->set("srs_id", SrsAmf0Any::number(100));
data->set("srs_pid", SrsAmf0Any::number(200));
data->set("srs_version", SrsAmf0Any::str("3.4.5.678"));

MockBufferIO tio;
SrsProtocol p(&tio);
HELPER_EXPECT_SUCCESS(p.send_and_free_packet(res, 0));

io.in_buffer.append(&tio.out_buffer);
}

SrsRequest req;
SrsRtmpClient r(&io);

SrsServerInfo si;
HELPER_EXPECT_SUCCESS(r.connect_app("live", "rtmp://127.0.0.1/live", &req, true, &si));
EXPECT_STREQ("1.2.3.4", si.ip.c_str());
EXPECT_STREQ("srs", si.sig.c_str());
EXPECT_EQ(100, si.cid);
EXPECT_EQ(200, si.pid);
EXPECT_EQ(3, si.major);
EXPECT_EQ(4, si.minor);
EXPECT_EQ(5, si.revision);
EXPECT_EQ(678, si.build);
}

// CreateStream.
if (true) {
MockBufferIO io;

if (true) {
SrsCreateStreamResPacket* res = new SrsCreateStreamResPacket(2.0, 3.0);

MockBufferIO tio;
SrsProtocol p(&tio);
HELPER_EXPECT_SUCCESS(p.send_and_free_packet(res, 0));

io.in_buffer.append(&tio.out_buffer);
}

SrsRtmpClient r(&io);

int stream_id = 0;
HELPER_EXPECT_SUCCESS(r.create_stream(stream_id));
EXPECT_EQ(3, stream_id);
}

// Play.
if (true) {
MockBufferIO io;
SrsRtmpClient r(&io);
HELPER_EXPECT_SUCCESS(r.play("livestream", 1, 128));
EXPECT_TRUE(io.out_length() > 0);
}

// Publish.
if (true) {
MockBufferIO io;
SrsRtmpClient r(&io);
HELPER_EXPECT_SUCCESS(r.publish("livestream", 1, 128));
EXPECT_TRUE(io.out_length() > 0);
}

// FMLE publish.
if (true) {
MockBufferIO io;

if (true) {
SrsCreateStreamResPacket* res = new SrsCreateStreamResPacket(4.0, 3.0);

MockBufferIO tio;
SrsProtocol p(&tio);
HELPER_EXPECT_SUCCESS(p.send_and_free_packet(res, 0));

io.in_buffer.append(&tio.out_buffer);
}

SrsRtmpClient r(&io);

int stream_id = 0;
HELPER_EXPECT_SUCCESS(r.fmle_publish("livestream", stream_id));
EXPECT_EQ(3, stream_id);
}
}

VOID TEST(ProtoStackTest, ServerCommandMessage)
{
srs_error_t err;

// ConnectApp.
if (true) {
MockBufferIO io;

if (true) {
SrsConnectAppPacket* res = new SrsConnectAppPacket();
res->command_object->set("tcUrl", SrsAmf0Any::str("rtmp://127.0.0.1/live"));

MockBufferIO tio;
SrsProtocol p(&tio);
HELPER_EXPECT_SUCCESS(p.send_and_free_packet(res, 0));

io.in_buffer.append(&tio.out_buffer);
}

SrsRtmpServer r(&io);

SrsRequest req;
HELPER_EXPECT_SUCCESS(r.connect_app(&req));
EXPECT_STREQ("rtmp", req.schema.c_str());
EXPECT_STREQ("127.0.0.1", req.host.c_str());
EXPECT_STREQ("127.0.0.1", req.vhost.c_str());
EXPECT_STREQ("live", req.app.c_str());
}

// Window ACK size.
if (true) {
MockBufferIO io;
SrsRtmpServer r(&io);
HELPER_EXPECT_SUCCESS(r.set_window_ack_size(1024));

if (true) {
MockBufferIO tio;
tio.in_buffer.append(&io.out_buffer);

SrsProtocol p(&tio);

SrsCommonMessage* msg = NULL;
SrsSetWindowAckSizePacket* pkt = NULL;
HELPER_EXPECT_SUCCESS(p.expect_message(&msg, &pkt));
EXPECT_EQ(1024, pkt->ackowledgement_window_size);

srs_freep(msg);
srs_freep(pkt);
}
}
}

0 comments on commit 41a9f15

Please sign in to comment.