1
0
Fork 0
mirror of https://github.com/ossrs/srs.git synced 2025-03-09 15:49:59 +00:00

Copy 4.0release

This commit is contained in:
winlin 2021-01-18 11:30:47 +08:00
parent 00395588bc
commit 5e3e013c60
183 changed files with 27373 additions and 13949 deletions

View file

@ -28,6 +28,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_server.hpp>
#include <srs_app_config.hpp>
#include <srs_app_log.hpp>
#include <srs_app_rtc_dtls.hpp>
#include <string>
using namespace std;
@ -41,9 +42,9 @@ srs_utime_t _srs_tmp_timeout = (100 * SRS_UTIME_MILLISECONDS);
// kernel module.
ISrsLog* _srs_log = new MockEmptyLog(SrsLogLevelDisabled);
ISrsThreadContext* _srs_context = new ISrsThreadContext();
ISrsContext* _srs_context = new SrsThreadContext();
// app module.
SrsConfig* _srs_config = NULL;
SrsConfig* _srs_config = new SrsConfig();
SrsServer* _srs_server = NULL;
bool _srs_in_docker = false;
@ -57,6 +58,10 @@ srs_error_t prepare_main() {
return srs_error_wrap(err, "init st");
}
if ((err = _srs_rtc_dtls_certificate->initialize()) != srs_success) {
return srs_error_wrap(err, "rtc dtls certificate initialize");
}
srs_freep(_srs_context);
_srs_context = new SrsThreadContext();
@ -129,3 +134,55 @@ VOID TEST(SampleTest, FastSampleMacrosTest)
EXPECT_NEAR(10, 15, 5);
}
VOID TEST(SampleTest, StringEQTest)
{
string str = "100";
EXPECT_TRUE("100" == str);
EXPECT_EQ("100", str);
EXPECT_STREQ("100", str.c_str());
}
class MockSrsContextId
{
public:
MockSrsContextId() {
bind_ = NULL;
}
MockSrsContextId(const MockSrsContextId& cp){
bind_ = NULL;
if (cp.bind_) {
bind_ = cp.bind_->copy();
}
}
MockSrsContextId& operator= (const MockSrsContextId& cp) {
srs_freep(bind_);
if (cp.bind_) {
bind_ = cp.bind_->copy();
}
return *this;
}
virtual ~MockSrsContextId() {
srs_freep(bind_);
}
public:
MockSrsContextId* copy() const {
MockSrsContextId* cp = new MockSrsContextId();
if (bind_) {
cp->bind_ = bind_->copy();
}
return cp;
}
private:
MockSrsContextId* bind_;
};
VOID TEST(SampleTest, ContextTest)
{
MockSrsContextId cid;
cid.bind_ = new MockSrsContextId();
static std::map<int, MockSrsContextId> cache;
cache[0] = cid;
cache[0] = cid;
}

View file

@ -24,6 +24,12 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#ifndef SRS_UTEST_PUBLIC_SHARED_HPP
#define SRS_UTEST_PUBLIC_SHARED_HPP
// Before define the private/protected, we must include some system header files.
// Or it may fail with:
// redeclared with different access struct __xfer_bufptrs
// @see https://stackoverflow.com/questions/47839718/sstream-redeclared-with-public-access-compiler-error
#include "gtest/gtest.h"
// Public all private and protected members.
#define private public
#define protected public
@ -33,7 +39,6 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_core.hpp>
#include "gtest/gtest.h"
#include <string>
using namespace std;
@ -51,23 +56,28 @@ extern int _srs_tmp_port;
extern srs_utime_t _srs_tmp_timeout;
// For errors.
// @remark we directly delete the err, because we allow user to append message if fail.
#define HELPER_EXPECT_SUCCESS(x) \
if ((err = x) != srs_success) fprintf(stderr, "err %s", srs_error_desc(err).c_str()); \
EXPECT_TRUE(srs_success == err); \
srs_freep(err)
#define HELPER_EXPECT_FAILED(x) EXPECT_TRUE(srs_success != (err = x)); srs_freep(err)
if (err != srs_success) delete err; \
EXPECT_TRUE(srs_success == err)
#define HELPER_EXPECT_FAILED(x) \
if ((err = x) != srs_success) delete err; \
EXPECT_TRUE(srs_success != err)
// For errors, assert.
// @remark The err is leak when error, but it's ok in utest.
// @remark we directly delete the err, because we allow user to append message if fail.
#define HELPER_ASSERT_SUCCESS(x) \
if ((err = x) != srs_success) fprintf(stderr, "err %s", srs_error_desc(err).c_str()); \
ASSERT_TRUE(srs_success == err); \
srs_freep(err)
#define HELPER_ASSERT_FAILED(x) ASSERT_TRUE(srs_success != (err = x)); srs_freep(err)
if (err != srs_success) delete err; \
ASSERT_TRUE(srs_success == err)
#define HELPER_ASSERT_FAILED(x) \
if ((err = x) != srs_success) delete err; \
ASSERT_TRUE(srs_success != err)
// For init array data.
#define HELPER_ARRAY_INIT(buf, sz, val) \
for (int i = 0; i < (int)sz; i++) (buf)[i]=val
for (int _iii = 0; _iii < (int)sz; _iii++) (buf)[_iii] = val
// Dump simple stream to string.
#define HELPER_BUFFER2STR(io) \
@ -95,7 +105,7 @@ extern srs_utime_t _srs_tmp_timeout;
// print the bytes.
void srs_bytes_print(char* pa, int size);
class MockEmptyLog : public SrsFastLog
class MockEmptyLog : public SrsFileLog
{
public:
MockEmptyLog(SrsLogLevel l);

View file

@ -32,7 +32,7 @@ using namespace std;
#include <srs_core_autofree.hpp>
#include <srs_protocol_json.hpp>
#include <srs_kernel_buffer.hpp>
using namespace _srs_internal;
using namespace srs_internal;
/**
* main scenario to use amf0.
@ -1323,7 +1323,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesString)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* o = SrsAmf0Any::str();
HELPER_EXPECT_FAILED(o->write(&b));
srs_freep(o);
@ -1354,7 +1354,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesString)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* o = SrsAmf0Any::str();
HELPER_EXPECT_FAILED(o->read(&b));
srs_freep(o);
@ -1452,7 +1452,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesBoolean)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* o = SrsAmf0Any::boolean();
HELPER_EXPECT_FAILED(o->write(&b));
srs_freep(o);
@ -1467,7 +1467,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesBoolean)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* o = SrsAmf0Any::boolean();
HELPER_EXPECT_FAILED(o->read(&b));
srs_freep(o);
@ -1549,7 +1549,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesNumber)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* o = SrsAmf0Any::number();
HELPER_EXPECT_FAILED(o->write(&b));
srs_freep(o);
@ -1564,7 +1564,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesNumber)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* o = SrsAmf0Any::number();
HELPER_EXPECT_FAILED(o->read(&b));
srs_freep(o);
@ -1673,14 +1673,14 @@ VOID TEST(ProtocolAMF0Test, InterfacesNull)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* o = SrsAmf0Any::null();
HELPER_EXPECT_FAILED(o->write(&b));
srs_freep(o);
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* o = SrsAmf0Any::null();
HELPER_EXPECT_FAILED(o->read(&b));
srs_freep(o);
@ -1739,14 +1739,14 @@ VOID TEST(ProtocolAMF0Test, InterfacesUndefined)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* o = SrsAmf0Any::undefined();
HELPER_EXPECT_FAILED(o->write(&b));
srs_freep(o);
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* o = SrsAmf0Any::undefined();
HELPER_EXPECT_FAILED(o->read(&b));
srs_freep(o);
@ -1811,14 +1811,14 @@ VOID TEST(ProtocolAMF0Test, InterfacesObject)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Object* o = SrsAmf0Any::object();
HELPER_EXPECT_FAILED(o->read(&b));
srs_freep(o);
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Object* o = SrsAmf0Any::object();
HELPER_EXPECT_FAILED(o->write(&b));
srs_freep(o);
@ -2014,14 +2014,14 @@ VOID TEST(ProtocolAMF0Test, InterfacesObjectEOF)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0ObjectEOF* o = new SrsAmf0ObjectEOF();
HELPER_EXPECT_FAILED(o->read(&b));
srs_freep(o);
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0ObjectEOF* o = new SrsAmf0ObjectEOF();
HELPER_EXPECT_FAILED(o->write(&b));
srs_freep(o);
@ -2106,14 +2106,14 @@ VOID TEST(ProtocolAMF0Test, InterfacesEcmaArray)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array();
HELPER_EXPECT_FAILED(o->read(&b));
srs_freep(o);
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0EcmaArray* o = SrsAmf0Any::ecma_array();
HELPER_EXPECT_FAILED(o->write(&b));
srs_freep(o);
@ -2233,14 +2233,14 @@ VOID TEST(ProtocolAMF0Test, InterfacesStrictArray)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0StrictArray* o = SrsAmf0Any::strict_array();
HELPER_EXPECT_FAILED(o->read(&b));
srs_freep(o);
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0StrictArray* o = SrsAmf0Any::strict_array();
HELPER_EXPECT_FAILED(o->write(&b));
srs_freep(o);
@ -2346,7 +2346,7 @@ VOID TEST(ProtocolAMF0Test, InterfacesError)
srs_error_t err;
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
HELPER_EXPECT_FAILED(SrsAmf0Any::discovery(&b, NULL));
}
@ -2490,7 +2490,7 @@ VOID TEST(ProtocolAMF0Test, Amf0Object2)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsAmf0Any* eof = SrsAmf0Any::object_eof();
HELPER_EXPECT_FAILED(srs_amf0_write_object_eof(&b, (SrsAmf0ObjectEOF*)eof));
srs_freep(eof);

View file

@ -36,7 +36,7 @@ VOID TEST(AppCoroutineTest, Dummy)
SrsDummyCoroutine dc;
if (true) {
EXPECT_EQ(0, dc.cid());
EXPECT_TRUE(dc.cid().empty());
srs_error_t err = dc.pull();
EXPECT_TRUE(err != srs_success);
@ -52,7 +52,7 @@ VOID TEST(AppCoroutineTest, Dummy)
if (true) {
dc.stop();
EXPECT_EQ(0, dc.cid());
EXPECT_TRUE(dc.cid().empty());
srs_error_t err = dc.pull();
EXPECT_TRUE(err != srs_success);
@ -68,7 +68,7 @@ VOID TEST(AppCoroutineTest, Dummy)
if (true) {
dc.interrupt();
EXPECT_EQ(0, dc.cid());
EXPECT_TRUE(dc.cid().empty());
srs_error_t err = dc.pull();
EXPECT_TRUE(err != srs_success);
@ -88,11 +88,12 @@ public:
srs_error_t err;
srs_cond_t running;
srs_cond_t exited;
int cid;
SrsContextId cid;
// Quit without error.
bool quit;
public:
MockCoroutineHandler() : trd(NULL), err(srs_success), cid(0), quit(false) {
MockCoroutineHandler() : trd(NULL), err(srs_success), quit(false) {
cid.set_value("0");
running = srs_cond_new();
exited = srs_cond_new();
}
@ -128,12 +129,12 @@ VOID TEST(AppCoroutineTest, StartStop)
MockCoroutineHandler ch;
SrsSTCoroutine sc("test", &ch);
ch.trd = &sc;
EXPECT_EQ(0, sc.cid());
EXPECT_TRUE(sc.cid().empty());
// Thread stop after created.
sc.stop();
EXPECT_EQ(0, sc.cid());
EXPECT_TRUE(sc.cid().empty());
srs_error_t err = sc.pull();
EXPECT_TRUE(srs_success != err);
@ -151,13 +152,13 @@ VOID TEST(AppCoroutineTest, StartStop)
MockCoroutineHandler ch;
SrsSTCoroutine sc("test", &ch);
ch.trd = &sc;
EXPECT_EQ(0, sc.cid());
EXPECT_TRUE(sc.cid().empty());
EXPECT_TRUE(srs_success == sc.start());
EXPECT_TRUE(srs_success == sc.pull());
srs_cond_timedwait(ch.running, 100 * SRS_UTIME_MILLISECONDS);
EXPECT_TRUE(sc.cid() > 0);
EXPECT_TRUE(!sc.cid().empty());
// Thread stop after started.
sc.stop();
@ -178,7 +179,7 @@ VOID TEST(AppCoroutineTest, StartStop)
MockCoroutineHandler ch;
SrsSTCoroutine sc("test", &ch);
ch.trd = &sc;
EXPECT_EQ(0, sc.cid());
EXPECT_TRUE(sc.cid().empty());
EXPECT_TRUE(srs_success == sc.start());
EXPECT_TRUE(srs_success == sc.pull());
@ -220,16 +221,17 @@ VOID TEST(AppCoroutineTest, Cycle)
if (true) {
MockCoroutineHandler ch;
SrsSTCoroutine sc("test", &ch, 250);
SrsContextId cid;
SrsSTCoroutine sc("test", &ch, cid.set_value("250"));
ch.trd = &sc;
EXPECT_EQ(250, sc.cid());
EXPECT_TRUE(!sc.cid().compare(cid));
EXPECT_TRUE(srs_success == sc.start());
EXPECT_TRUE(srs_success == sc.pull());
// After running, the cid in cycle should equal to the thread.
srs_cond_timedwait(ch.running, 100 * SRS_UTIME_MILLISECONDS);
EXPECT_EQ(250, ch.cid);
EXPECT_TRUE(!ch.cid.compare(cid));
}
if (true) {

View file

@ -35,7 +35,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define _MIN_OK_CONF "listen 1935; "
class MockSrsConfigBuffer : public _srs_internal::SrsConfigBuffer
class MockSrsConfigBuffer : public srs_internal::SrsConfigBuffer
{
public:
MockSrsConfigBuffer(std::string buf);

View file

@ -41,25 +41,25 @@ VOID TEST(CoreAutoFreeTest, Free)
VOID TEST(CoreMacroseTest, Check)
{
#ifndef SRS_AUTO_BUILD_TS
#ifndef SRS_BUILD_TS
EXPECT_TRUE(false);
#endif
#ifndef SRS_AUTO_BUILD_DATE
#ifndef SRS_BUILD_DATE
EXPECT_TRUE(false);
#endif
#ifndef SRS_AUTO_UNAME
#ifndef SRS_UNAME
EXPECT_TRUE(false);
#endif
#ifndef SRS_AUTO_USER_CONFIGURE
#ifndef SRS_USER_CONFIGURE
EXPECT_TRUE(false);
#endif
#ifndef SRS_AUTO_CONFIGURE
#ifndef SRS_CONFIGURE
EXPECT_TRUE(false);
#endif
#ifndef SRS_AUTO_PREFIX
#ifndef SRS_PREFIX
EXPECT_TRUE(false);
#endif
#ifndef SRS_AUTO_CONSTRIBUTORS
#ifndef SRS_CONSTRIBUTORS
EXPECT_TRUE(false);
#endif
}

File diff suppressed because one or more lines are too long

View file

@ -361,7 +361,7 @@ MockSrsCodec::~MockSrsCodec()
{
}
int MockSrsCodec::nb_bytes()
uint64_t MockSrsCodec::nb_bytes()
{
return 0;
}
@ -2393,6 +2393,89 @@ VOID TEST(KernelUtility, Base64Decode)
EXPECT_TRUE(expect == plaintext);
}
VOID TEST(KernelUtility, Base64Encode)
{
srs_error_t err;
string expect = "dXNlcjpwYXNzd29yZA==";
string plaintext = "user:password";
string cipher;
HELPER_EXPECT_SUCCESS(srs_av_base64_encode(plaintext, cipher));
EXPECT_TRUE(expect == cipher);
}
VOID TEST(KernelUtility, Base64)
{
srs_error_t err = srs_success;
struct testpair {
string decoded;
string encoded;
};
struct testpair data[] = {
// RFC 3548 examples
{"\x14\xfb\x9c\x03\xd9\x7e", "FPucA9l+"},
{"\x14\xfb\x9c\x03\xd9", "FPucA9k="},
{"\x14\xfb\x9c\x03", "FPucAw=="},
// RFC 4648 examples
{"", ""},
{"f", "Zg=="},
{"fo", "Zm8="},
{"foo", "Zm9v"},
{"foob", "Zm9vYg=="},
{"fooba", "Zm9vYmE="},
{"foobar", "Zm9vYmFy"},
// Wikipedia examples
{"sure.", "c3VyZS4="},
{"sure", "c3VyZQ=="},
{"sur", "c3Vy"},
{"su", "c3U="},
{"leasure.", "bGVhc3VyZS4="},
{"easure.", "ZWFzdXJlLg=="},
{"asure.", "YXN1cmUu"},
{"sure.", "c3VyZS4="},
{"Twas brillig, and the slithy toves", "VHdhcyBicmlsbGlnLCBhbmQgdGhlIHNsaXRoeSB0b3Zlcw=="}
};
for(int i = 0; i < (int)(sizeof(data) / sizeof(struct testpair)); ++i) {
struct testpair& d = data[i];
string cipher;
HELPER_EXPECT_SUCCESS(srs_av_base64_encode(d.decoded, cipher));
EXPECT_STREQ(d.encoded.c_str(), cipher.c_str());
string plaintext;
HELPER_EXPECT_SUCCESS(srs_av_base64_decode(d.encoded, plaintext));
EXPECT_STREQ(d.decoded.c_str(), plaintext.c_str());
}
string expected = "sure";
string examples[11] = {
"c3VyZQ==",
"c3VyZQ==\r",
"c3VyZQ==\n",
"c3VyZQ==\r\n",
"c3VyZ\r\nQ==",
"c3V\ryZ\nQ==",
"c3V\nyZ\rQ==",
"c3VyZ\nQ==",
"c3VyZQ\n==",
"c3VyZQ=\n=",
"c3VyZQ=\r\n\r\n=",
};
for(int i = 0; i < 11; ++i) {
string& encoded_str = examples[i];
string plaintext;
HELPER_EXPECT_SUCCESS(srs_av_base64_decode(encoded_str, plaintext));
EXPECT_STREQ(expected.c_str(), plaintext.c_str());
}
}
VOID TEST(KernelUtility, StringToHex)
{
if (true) {
@ -2543,12 +2626,127 @@ VOID TEST(KernelUtility, StringUtils)
flags.push_back("x");
EXPECT_TRUE("" == srs_string_min_match("srs", flags));
}
}
VOID TEST(KernelUtility, StringSplitUtils)
{
if (true) {
vector<string> ss = srs_string_split("ossrs", "r");
EXPECT_EQ(2, (int)ss.size());
EXPECT_STREQ("oss", ss.at(0).c_str());
EXPECT_STREQ("s", ss.at(1).c_str());
}
if (true) {
vector<string> ss = srs_string_split("ossrs", "");
EXPECT_EQ(1, (int)ss.size());
EXPECT_STREQ("ossrs", ss.at(0).c_str());
}
if (true) {
vector<string> ss = srs_string_split("ossrs", "live");
EXPECT_EQ(1, (int)ss.size());
EXPECT_STREQ("ossrs", ss.at(0).c_str());
}
if (true) {
vector<string> ss = srs_string_split("srs,live,rtc", ",");
EXPECT_EQ(3, (int)ss.size());
EXPECT_STREQ("srs", ss.at(0).c_str());
EXPECT_STREQ("live", ss.at(1).c_str());
EXPECT_STREQ("rtc", ss.at(2).c_str());
}
if (true) {
vector<string> ss = srs_string_split("srs,,rtc", ",");
EXPECT_EQ(3, (int)ss.size());
EXPECT_STREQ("srs", ss.at(0).c_str());
EXPECT_STREQ("", ss.at(1).c_str());
EXPECT_STREQ("rtc", ss.at(2).c_str());
}
if (true) {
vector<string> ss = srs_string_split("srs,,,rtc", ",");
EXPECT_EQ(4, (int)ss.size());
EXPECT_STREQ("srs", ss.at(0).c_str());
EXPECT_STREQ("", ss.at(1).c_str());
EXPECT_STREQ("", ss.at(2).c_str());
EXPECT_STREQ("rtc", ss.at(3).c_str());
}
if (true) {
vector<string> ss = srs_string_split("srs,live,", ",");
EXPECT_EQ(3, (int)ss.size());
EXPECT_STREQ("srs", ss.at(0).c_str());
EXPECT_STREQ("live", ss.at(1).c_str());
EXPECT_STREQ("", ss.at(2).c_str());
}
if (true) {
vector<string> ss = srs_string_split(",live,rtc", ",");
EXPECT_EQ(3, (int)ss.size());
EXPECT_STREQ("", ss.at(0).c_str());
EXPECT_STREQ("live", ss.at(1).c_str());
EXPECT_STREQ("rtc", ss.at(2).c_str());
}
if (true) {
EXPECT_TRUE("srs" == srs_string_split("srs", "").at(0));
EXPECT_TRUE("s" == srs_string_split("srs", "r").at(0));
EXPECT_TRUE("s" == srs_string_split("srs", "rs").at(0));
}
if (true) {
vector<string> ss = srs_string_split("/xxx/yyy", "/");
EXPECT_EQ(3, (int)ss.size());
EXPECT_STREQ("", ss.at(0).c_str());
EXPECT_STREQ("xxx", ss.at(1).c_str());
EXPECT_STREQ("yyy", ss.at(2).c_str());
}
}
VOID TEST(KernelUtility, StringSplitUtils2)
{
if (true) {
vector<string> flags;
flags.push_back("e");
flags.push_back("wo");
vector<string> ss = srs_string_split("hello, world", flags);
EXPECT_EQ(3, (int)ss.size());
EXPECT_STREQ("h", ss.at(0).c_str());
EXPECT_STREQ("llo, ", ss.at(1).c_str());
EXPECT_STREQ("rld", ss.at(2).c_str());
}
if (true) {
vector<string> flags;
flags.push_back("");
flags.push_back("");
vector<string> ss = srs_string_split("hello, world", flags);
EXPECT_EQ(1, (int)ss.size());
EXPECT_STREQ("hello, world", ss.at(0).c_str());
}
if (true) {
vector<string> flags;
flags.push_back(",");
flags.push_back(" ");
vector<string> ss = srs_string_split("hello, world", flags);
EXPECT_EQ(3, (int)ss.size());
EXPECT_STREQ("hello", ss.at(0).c_str());
EXPECT_STREQ("", ss.at(1).c_str());
EXPECT_STREQ("world", ss.at(2).c_str());
}
if (true) {
vector<string> flags;
flags.push_back(",");
vector<string> ss = srs_string_split("hello,,world", flags);
EXPECT_EQ(3, (int)ss.size());
EXPECT_STREQ("hello", ss.at(0).c_str());
EXPECT_STREQ("", ss.at(1).c_str());
EXPECT_STREQ("world", ss.at(2).c_str());
}
}
VOID TEST(KernelUtility, BytesUtils)
@ -2596,7 +2794,7 @@ VOID TEST(KernelUtility, AnnexbUtils)
if (true) {
EXPECT_TRUE(!srs_avc_startswith_annexb(NULL, NULL));
SrsBuffer buf;
SrsBuffer buf(NULL, 0);
EXPECT_TRUE(!srs_avc_startswith_annexb(&buf, NULL));
}
@ -2654,7 +2852,7 @@ VOID TEST(KernelUtility, AdtsUtils)
if (true) {
EXPECT_TRUE(!srs_aac_startswith_adts(NULL));
SrsBuffer buf;
SrsBuffer buf(NULL, 0);
EXPECT_TRUE(!srs_aac_startswith_adts(&buf));
}
@ -3713,11 +3911,11 @@ VOID TEST(KernelFileTest, FileWriteReader)
}
// Mock the system call hooks.
extern _srs_open_t _srs_open_fn;
extern _srs_write_t _srs_write_fn;
extern _srs_read_t _srs_read_fn;
extern _srs_lseek_t _srs_lseek_fn;
extern _srs_close_t _srs_close_fn;
extern srs_open_t _srs_open_fn;
extern srs_write_t _srs_write_fn;
extern srs_read_t _srs_read_fn;
extern srs_lseek_t _srs_lseek_fn;
extern srs_close_t _srs_close_fn;
int mock_open(const char* /*path*/, int /*oflag*/, ...) {
return -1;
@ -3742,13 +3940,13 @@ int mock_close(int /*fildes*/) {
class MockSystemIO
{
private:
_srs_open_t oo;
_srs_write_t ow;
_srs_read_t _or;
_srs_lseek_t os;
_srs_close_t oc;
srs_open_t oo;
srs_write_t ow;
srs_read_t _or;
srs_lseek_t os;
srs_close_t oc;
public:
MockSystemIO(_srs_open_t o = NULL, _srs_write_t w = NULL, _srs_read_t r = NULL, _srs_lseek_t s = NULL, _srs_close_t c = NULL) {
MockSystemIO(srs_open_t o = NULL, srs_write_t w = NULL, srs_read_t r = NULL, srs_lseek_t s = NULL, srs_close_t c = NULL) {
oo = _srs_open_fn;
ow = _srs_write_fn;
os = _srs_lseek_fn;
@ -3843,7 +4041,7 @@ VOID TEST(KernelFileWriterTest, WriteSpecialCase)
off_t seeked = 0;
HELPER_EXPECT_SUCCESS(f.lseek(0, SEEK_CUR, &seeked));
#ifdef SRS_AUTO_OSX
#ifdef SRS_OSX
EXPECT_EQ(10, seeked);
#else
EXPECT_EQ(0, seeked);
@ -4090,28 +4288,6 @@ VOID TEST(KernelFLVTest, CoverSharedPtrMessage)
}
}
VOID TEST(KernelLogTest, CoverAll)
{
srs_error_t err;
if (true) {
ISrsLog l;
HELPER_EXPECT_SUCCESS(l.initialize());
l.reopen();
l.verbose("TAG", 0, "log");
l.info("TAG", 0, "log");
l.trace("TAG", 0, "log");
l.warn("TAG", 0, "log");
l.error("TAG", 0, "log");
ISrsThreadContext ctx;
ctx.set_id(10);
EXPECT_EQ(0, ctx.get_id());
EXPECT_EQ(0, ctx.generate_id());
}
}
VOID TEST(KernelMp3Test, CoverAll)
{
srs_error_t err;
@ -4212,8 +4388,8 @@ VOID TEST(KernelUtilityTest, CoverBitsBufferAll)
}
}
#ifndef SRS_AUTO_OSX
extern _srs_gettimeofday_t _srs_gettimeofday;
#ifndef SRS_OSX
extern srs_gettimeofday_t _srs_gettimeofday;
int mock_gettimeofday(struct timeval* /*tp*/, struct timezone* /*tzp*/) {
return -1;
}
@ -4221,9 +4397,9 @@ int mock_gettimeofday(struct timeval* /*tp*/, struct timezone* /*tzp*/) {
class MockTime
{
private:
_srs_gettimeofday_t ot;
srs_gettimeofday_t ot;
public:
MockTime(_srs_gettimeofday_t t = NULL) {
MockTime(srs_gettimeofday_t t = NULL) {
ot = _srs_gettimeofday;
if (t) {
_srs_gettimeofday = t;
@ -4422,17 +4598,6 @@ VOID TEST(KernelUtilityTest, CoverTimeUtilityAll)
EXPECT_STREQ("off", srs_bool2switch(false).c_str());
}
if (true) {
vector<string> flags;
flags.push_back("e");
flags.push_back("wo");
vector<string> ss = srs_string_split("hello, world", flags);
EXPECT_EQ(3, (int)ss.size());
EXPECT_STREQ("h", ss.at(0).c_str());
EXPECT_STREQ("llo, ", ss.at(1).c_str());
EXPECT_STREQ("rld", ss.at(2).c_str());
}
if (true) {
EXPECT_EQ('H', av_toupper('h'));
}
@ -4506,7 +4671,7 @@ VOID TEST(KernelTSTest, CoverContextUtility)
SrsTsMessage m(&c, &p);
m.PES_packet_length = 8;
SrsBuffer b;
SrsBuffer b(NULL, 0);
int nb_bytes = 0;
HELPER_EXPECT_SUCCESS(m.dump(&b, &nb_bytes));
@ -4625,7 +4790,7 @@ VOID TEST(KernelTSTest, CoverContextEncode)
MockTsHandler h;
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
HELPER_EXPECT_SUCCESS(ctx.decode(&b, &h));
EXPECT_TRUE(NULL == h.msg);
}

View file

@ -130,7 +130,7 @@ public:
MockSrsCodec();
virtual ~MockSrsCodec();
public:
virtual int nb_bytes();
virtual uint64_t nb_bytes();
virtual srs_error_t encode(SrsBuffer* buf);
virtual srs_error_t decode(SrsBuffer* buf);
};

View file

@ -220,7 +220,7 @@ VOID TEST(KernelMp4Test, DiscoveryBox)
SrsMp4Box* pbox;
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
HELPER_ASSERT_FAILED(SrsMp4Box::discovery(&b, &pbox));
}
@ -419,7 +419,7 @@ VOID TEST(KernelMp4Test, UUIDBoxDecode)
}
if (true) {
SrsBuffer b;
SrsBuffer b(NULL, 0);
SrsMp4Box box;
HELPER_ASSERT_FAILED(box.decode(&b));
}
@ -464,7 +464,7 @@ VOID TEST(KernelMp4Test, UUIDBoxEncode)
SrsMp4Box box;
box.type = SrsMp4BoxTypeFREE;
box.usertype.resize(8);
ASSERT_EQ(8, box.nb_bytes());
ASSERT_EQ(8, (int)box.nb_bytes());
HELPER_ASSERT_SUCCESS(box.encode(&b));
}
@ -475,7 +475,7 @@ VOID TEST(KernelMp4Test, UUIDBoxEncode)
SrsMp4Box box;
box.type = SrsMp4BoxTypeUUID;
box.usertype.resize(16);
ASSERT_EQ(24, box.nb_bytes());
ASSERT_EQ(24, (int)box.nb_bytes());
HELPER_ASSERT_SUCCESS(box.encode(&b));
}
}
@ -499,7 +499,7 @@ VOID TEST(KernelMp4Test, FullBoxDump)
SrsMp4FileTypeBox box;
box.major_brand = SrsMp4BoxBrandISO2;
box.compatible_brands.push_back(SrsMp4BoxBrandISOM);
EXPECT_EQ(20, box.update_size());
EXPECT_EQ(20, (int)box.update_size());
stringstream ss;
SrsMp4DumpContext dc;
@ -514,7 +514,7 @@ VOID TEST(KernelMp4Test, FullBoxDump)
box.type = SrsMp4BoxTypeFTYP;
box.version = 1;
box.flags = 0x02;
EXPECT_EQ(12, box.update_size());
EXPECT_EQ(12, (int)box.update_size());
stringstream ss;
SrsMp4DumpContext dc;
@ -528,7 +528,7 @@ VOID TEST(KernelMp4Test, FullBoxDump)
SrsMp4FullBox box;
box.type = SrsMp4BoxTypeFTYP;
box.version = 1;
EXPECT_EQ(12, box.update_size());
EXPECT_EQ(12, (int)box.update_size());
stringstream ss;
SrsMp4DumpContext dc;
@ -541,7 +541,7 @@ VOID TEST(KernelMp4Test, FullBoxDump)
if (true) {
SrsMp4FullBox box;
box.type = SrsMp4BoxTypeFTYP;
EXPECT_EQ(12, box.update_size());
EXPECT_EQ(12, (int)box.update_size());
stringstream ss;
SrsMp4DumpContext dc;
@ -570,7 +570,7 @@ VOID TEST(KernelMp4Test, MFHDBox)
if (true) {
SrsMp4MovieFragmentHeaderBox box;
box.sequence_number = 3;
EXPECT_EQ(16, box.update_size());
EXPECT_EQ(16, (int)box.update_size());
stringstream ss;
SrsMp4DumpContext dc;
@ -596,7 +596,7 @@ VOID TEST(KernelMp4Test, TFHDBox)
if (true) {
SrsMp4TrackFragmentHeaderBox box;
box.track_id = 100;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -631,7 +631,7 @@ VOID TEST(KernelMp4Test, TFHDBox)
box.default_sample_duration = 12;
box.default_sample_size = 13;
box.default_sample_flags = 14;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -667,7 +667,7 @@ VOID TEST(KernelMp4Test, TFDTBox)
if (true) {
SrsMp4TrackFragmentDecodeTimeBox box;
box.base_media_decode_time = 100;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -694,7 +694,7 @@ VOID TEST(KernelMp4Test, TFDTBox)
SrsMp4TrackFragmentDecodeTimeBox box;
box.version = 1;
box.base_media_decode_time = 100;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -724,7 +724,7 @@ VOID TEST(KernelMp4Test, TRUNBox)
if (true) {
SrsMp4TrackFragmentRunBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -754,7 +754,7 @@ VOID TEST(KernelMp4Test, TRUNBox)
entry->sample_duration = 1000;
box.entries.push_back(entry);
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -788,7 +788,7 @@ VOID TEST(KernelMp4Test, FreeBox)
if (true) {
SrsMp4FreeSpaceBox box(SrsMp4BoxTypeFREE);
box.data.resize(4);
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -818,7 +818,7 @@ VOID TEST(KernelMp4Test, MOOVBox)
if (true) {
SrsMp4MovieBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -891,7 +891,7 @@ VOID TEST(KernelMp4Test, TREXBox)
SrsMp4TrackExtendsBox box;
box.track_ID = 1; box.default_sample_description_index = 2; box.default_sample_size = 3;
box.default_sample_duration = 4; box.default_sample_flags = 5;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -933,7 +933,7 @@ VOID TEST(KernelMp4Test, TKHDBox)
if (true) {
SrsMp4TrackHeaderBox box;
box.track_ID = 1;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -960,7 +960,7 @@ VOID TEST(KernelMp4Test, TKHDBox)
SrsMp4TrackHeaderBox box;
box.version = 1;
box.track_ID = 1;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -990,7 +990,7 @@ VOID TEST(KernelMp4Test, ELSTBox)
if (true) {
SrsMp4EditListBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1018,7 +1018,7 @@ VOID TEST(KernelMp4Test, ELSTBox)
SrsMp4ElstEntry entry;
box.entries.push_back(entry);
}
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1054,7 +1054,7 @@ VOID TEST(KernelMp4Test, MDHDBox)
if (true) {
SrsMp4MediaHeaderBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1081,7 +1081,7 @@ VOID TEST(KernelMp4Test, MDHDBox)
box.set_language0('C');
box.set_language1('N');
box.set_language2('E');
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1136,7 +1136,7 @@ VOID TEST(KernelMp4Test, HDLRBox)
if (true) {
SrsMp4HandlerReferenceBox box;
box.handler_type = SrsMp4HandlerTypeSOUN;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1162,7 +1162,7 @@ VOID TEST(KernelMp4Test, HDLRBox)
if (true) {
SrsMp4HandlerReferenceBox box;
box.handler_type = SrsMp4HandlerTypeVIDE;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1189,7 +1189,7 @@ VOID TEST(KernelMp4Test, HDLRBox)
SrsMp4HandlerReferenceBox box;
box.handler_type = SrsMp4HandlerTypeVIDE;
box.name = "srs";
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1247,7 +1247,7 @@ VOID TEST(KernelMp4Test, URLBox)
if (true) {
SrsMp4DataEntryUrlBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1271,7 +1271,7 @@ VOID TEST(KernelMp4Test, URLBox)
if (true) {
SrsMp4DataEntryUrnBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1304,7 +1304,7 @@ VOID TEST(KernelMp4Test, URLBox)
SrsMp4DataReferenceBox box;
SrsMp4DataEntryUrnBox* urn = new SrsMp4DataEntryUrnBox();
box.append(urn);
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1341,7 +1341,7 @@ VOID TEST(KernelMp4Test, SampleDescBox)
if (true) {
SrsMp4VisualSampleEntry box;
box.data_reference_index = 1;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1365,7 +1365,7 @@ VOID TEST(KernelMp4Test, SampleDescBox)
if (true) {
SrsMp4AvccBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1390,7 +1390,7 @@ VOID TEST(KernelMp4Test, SampleDescBox)
if (true) {
SrsMp4AudioSampleEntry box;
box.data_reference_index = 1;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1420,7 +1420,7 @@ VOID TEST(KernelMp4Test, SpecificInfoBox)
if (true) {
SrsMp4DecoderSpecificInfo box;
box.asc.resize(2);
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1444,7 +1444,7 @@ VOID TEST(KernelMp4Test, SpecificInfoBox)
if (true) {
SrsMp4DecoderConfigDescriptor box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1468,7 +1468,7 @@ VOID TEST(KernelMp4Test, SpecificInfoBox)
if (true) {
SrsMp4ES_Descriptor box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1498,7 +1498,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4SampleDescriptionBox box;
box.entries.push_back(new SrsMp4SampleEntry());
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1523,7 +1523,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4DecodingTime2SampleBox box;
box.entries.push_back(SrsMp4SttsEntry());
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1548,7 +1548,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4CompositionTime2SampleBox box;
box.entries.push_back(SrsMp4CttsEntry());
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1572,7 +1572,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4SyncSampleBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1596,7 +1596,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4Sample2ChunkBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1620,7 +1620,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4ChunkOffsetBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1644,7 +1644,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4ChunkLargeOffsetBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1668,7 +1668,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4SampleSizeBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1693,7 +1693,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4UserDataBox box;
box.data.resize(2);
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1717,7 +1717,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4SegmentIndexBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1741,7 +1741,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4MovieHeaderBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;
@ -1772,7 +1772,7 @@ VOID TEST(KernelMp4Test, STSDBox)
if (true) {
SrsMp4CompositionTime2SampleBox box;
EXPECT_EQ((int)sizeof(buf), box.nb_bytes());
EXPECT_EQ((int)sizeof(buf), (int)box.nb_bytes());
HELPER_EXPECT_SUCCESS(box.encode(&b));
stringstream ss;

View file

@ -352,7 +352,7 @@ VOID TEST(ProtocolHandshakeTest, OpensslSha256)
// verify the dh key
VOID TEST(ProtocolHandshakeTest, DHKey)
{
_srs_internal::SrsDH dh;
srs_internal::SrsDH dh;
ASSERT_TRUE(ERROR_SUCCESS == dh.initialize(true));
@ -368,7 +368,7 @@ VOID TEST(ProtocolHandshakeTest, DHKey)
EXPECT_TRUE(srs_bytes_equals(pub_key1, pub_key2, 128));
// another dh
_srs_internal::SrsDH dh0;
srs_internal::SrsDH dh0;
ASSERT_TRUE(ERROR_SUCCESS == dh0.initialize(true));
@ -5999,15 +5999,17 @@ VOID TEST(ProtocolHTTPTest, HTTPParser)
VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
{
srs_error_t err = srs_success;
if (true) {
MockBufferIO bio;
SrsHttpParser hp;
bio.append("GET /gslb/v1/versions HTTP/1.1\r\nContent-Length: 5\r\n\r\nHello");
EXPECT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST));
ISrsHttpMessage* req = NULL;
ASSERT_TRUE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req));
// We should read body, or next parsing message will fail.
// @see https://github.com/ossrs/srs/issues/1181
@ -6019,7 +6021,7 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
// Should fail because there is body which not read.
// @see https://github.com/ossrs/srs/issues/1181
ASSERT_FALSE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_FAILED(hp.parse_message(&bio, &req));
srs_freep(req);
}
@ -6028,11 +6030,11 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
SrsHttpParser hp;
bio.append("GET");
EXPECT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST));
// Should fail if not completed message.
ISrsHttpMessage* req = NULL;
ASSERT_FALSE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_FAILED(hp.parse_message(&bio, &req));
srs_freep(req);
}
@ -6041,14 +6043,14 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
SrsHttpParser hp;
bio.append("GET /gslb/v1/versions HTTP/1.1\r\nContent-Length: 5\r\n\r\nHello");
ASSERT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST));
ISrsHttpMessage* req = NULL;
SrsAutoFree(ISrsHttpMessage, req);
ASSERT_TRUE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req));
char v[64] = {0};
EXPECT_TRUE(0 == req->body_reader()->read(v, sizeof(v), NULL));
HELPER_ASSERT_SUCCESS(req->body_reader()->read(v, sizeof(v), NULL));
EXPECT_TRUE(string("Hello") == string(v));
EXPECT_TRUE(req->body_reader()->eof());
@ -6059,11 +6061,11 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
SrsHttpParser hp;
bio.append("GET /gslb/v1/versions HTTP/1.1\r\nContent-Length: 0\r\n\r\n");
ASSERT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST));
ISrsHttpMessage* req = NULL;
SrsAutoFree(ISrsHttpMessage, req);
EXPECT_TRUE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req));
}
if (true) {
@ -6071,11 +6073,11 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
SrsHttpParser hp;
bio.append("GET /gslb/v1/versions HTTP/1.1\r\n\r\n");
ASSERT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST));
ISrsHttpMessage* req = NULL;
SrsAutoFree(ISrsHttpMessage, req);
EXPECT_TRUE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req));
}
if (true) {
@ -6083,11 +6085,11 @@ VOID TEST(ProtocolHTTPTest, ParseHTTPMessage)
SrsHttpParser hp;
bio.append("GET /gslb/v1/versions HTTP/1.1\r\n\r\n");
ASSERT_TRUE(0 == hp.initialize(HTTP_REQUEST, false));
HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST));
ISrsHttpMessage* req = NULL;
SrsAutoFree(ISrsHttpMessage, req);
EXPECT_TRUE(0 == hp.parse_message(&bio, &req));
HELPER_ASSERT_SUCCESS(hp.parse_message(&bio, &req));
}
}

View file

@ -37,7 +37,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_protocol_stream.hpp>
#include <srs_protocol_kbps.hpp>
using namespace _srs_internal;
using namespace srs_internal;
#include <srs_protocol_io.hpp>

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,33 @@
/*
The MIT License (MIT)
Copyright (c) 2013-2020 Winlin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef SRS_UTEST_RTC_HPP
#define SRS_UTEST_RTC_HPP
/*
#include <srs_utest_rtc.hpp>
*/
#include <srs_utest.hpp>
#endif

View file

@ -92,7 +92,7 @@ VOID TEST(ProtocolRTMPTest, PacketEncode)
MockPacket pkt;
pkt.size = 1024;
SrsBuffer b;
SrsBuffer b(NULL, 0);
HELPER_EXPECT_FAILED(pkt.decode(&b));
}
@ -3139,7 +3139,7 @@ VOID TEST(ProtocolRTMPTest, MergeReadHandler)
char* _strcpy(const char* src)
{
return strcpy(new char[strlen(src)], src);
return strcpy(new char[strlen(src) + 1], src);
}
VOID TEST(ProtocolRTMPTest, CreateRTMPMessage)

View file

@ -38,20 +38,36 @@ using namespace std;
#include <srs_service_utility.hpp>
#include <srs_service_http_client.hpp>
#include <srs_service_rtmp_conn.hpp>
#include <srs_service_conn.hpp>
#include <sys/socket.h>
#include <netdb.h>
class MockSrsConnection : public ISrsConnection
MockSrsConnection::MockSrsConnection()
{
public:
MockSrsConnection() {
do_switch = false;
}
MockSrsConnection::~MockSrsConnection()
{
if (do_switch) {
srs_usleep(0);
}
virtual ~MockSrsConnection() {
}
virtual std::string remote_ip() {
return "127.0.0.1";
}
};
}
const SrsContextId& MockSrsConnection::get_id()
{
return _srs_context->get_id();
}
std::string MockSrsConnection::desc()
{
return "Mock";
}
std::string MockSrsConnection::remote_ip()
{
return "127.0.0.1";
}
VOID TEST(ServiceTimeTest, TimeUnit)
{
@ -110,7 +126,7 @@ VOID TEST(TCPServerTest, PingPong)
SrsTcpClient c(_srs_tmp_host, _srs_tmp_port, _srs_tmp_timeout);
HELPER_EXPECT_SUCCESS(c.connect());
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
EXPECT_TRUE(h.fd != NULL);
}
@ -123,8 +139,8 @@ VOID TEST(TCPServerTest, PingPong)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_AUTO_OSX
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
HELPER_EXPECT_SUCCESS(skt.initialize(h.fd));
@ -145,8 +161,8 @@ VOID TEST(TCPServerTest, PingPong)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_AUTO_OSX
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
HELPER_EXPECT_SUCCESS(skt.initialize(h.fd));
@ -169,8 +185,8 @@ VOID TEST(TCPServerTest, PingPong)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_AUTO_OSX
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
HELPER_EXPECT_SUCCESS(skt.initialize(h.fd));
@ -204,8 +220,8 @@ VOID TEST(TCPServerTest, PingPongWithTimeout)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_AUTO_OSX
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
HELPER_EXPECT_SUCCESS(skt.initialize(h.fd));
@ -226,8 +242,8 @@ VOID TEST(TCPServerTest, PingPongWithTimeout)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_AUTO_OSX
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
HELPER_EXPECT_SUCCESS(skt.initialize(h.fd));
@ -248,8 +264,8 @@ VOID TEST(TCPServerTest, PingPongWithTimeout)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_AUTO_OSX
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
HELPER_EXPECT_SUCCESS(skt.initialize(h.fd));
@ -382,7 +398,7 @@ VOID TEST(TCPServerTest, StringIsHex)
char* str = (char*)"!1234567890";
char* parsed = str; errno = 0;
EXPECT_EQ(0x0, ::strtol(str, &parsed, 16));
#ifndef SRS_AUTO_OSX
#ifndef SRS_OSX
EXPECT_EQ(0, errno);
#endif
EXPECT_EQ(str, parsed);
@ -400,7 +416,7 @@ VOID TEST(TCPServerTest, StringIsHex)
char* str = (char*)"";
char* parsed = str; errno = 0;
EXPECT_EQ(0x0, ::strtol(str, &parsed, 16));
#ifndef SRS_AUTO_OSX
#ifndef SRS_OSX
EXPECT_EQ(0, errno);
#endif
EXPECT_EQ(str, parsed);
@ -428,8 +444,8 @@ VOID TEST(TCPServerTest, WritevIOVC)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_AUTO_OSX
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
HELPER_EXPECT_SUCCESS(skt.initialize(h.fd));
@ -458,8 +474,8 @@ VOID TEST(TCPServerTest, WritevIOVC)
HELPER_EXPECT_SUCCESS(c.connect());
SrsStSocket skt;
srs_usleep(100 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_AUTO_OSX
srs_usleep(30 * SRS_UTIME_MILLISECONDS);
#ifdef SRS_OSX
ASSERT_TRUE(h.fd != NULL);
#endif
HELPER_EXPECT_SUCCESS(skt.initialize(h.fd));
@ -480,7 +496,7 @@ VOID TEST(TCPServerTest, WritevIOVC)
}
}
VOID TEST(TCPServerTest, MessageConnection)
VOID TEST(HTTPServerTest, MessageConnection)
{
srs_error_t err;
@ -523,18 +539,17 @@ VOID TEST(TCPServerTest, MessageConnection)
if (true) {
SrsHttpMessage m;
m.set_basic(100, 0, 0); EXPECT_STREQ("OTHER", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_GET, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_GET, m.method()); EXPECT_STREQ("GET", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_PUT, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_PUT, m.method()); EXPECT_STREQ("PUT", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_POST, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_POST, m.method()); EXPECT_STREQ("POST", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_DELETE, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_DELETE, m.method()); EXPECT_STREQ("DELETE", m.method_str().c_str());
m.set_basic(SRS_CONSTS_HTTP_OPTIONS, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_OPTIONS, m.method()); EXPECT_STREQ("OPTIONS", m.method_str().c_str());
m.set_basic(HTTP_REQUEST, 100, 0, 0); EXPECT_STREQ("OTHER", m.method_str().c_str());
m.set_basic(HTTP_REQUEST, SRS_CONSTS_HTTP_GET, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_GET, m.method()); EXPECT_STREQ("GET", m.method_str().c_str());
m.set_basic(HTTP_REQUEST, SRS_CONSTS_HTTP_PUT, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_PUT, m.method()); EXPECT_STREQ("PUT", m.method_str().c_str());
m.set_basic(HTTP_REQUEST, SRS_CONSTS_HTTP_POST, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_POST, m.method()); EXPECT_STREQ("POST", m.method_str().c_str());
m.set_basic(HTTP_REQUEST, SRS_CONSTS_HTTP_DELETE, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_DELETE, m.method()); EXPECT_STREQ("DELETE", m.method_str().c_str());
m.set_basic(HTTP_REQUEST, SRS_CONSTS_HTTP_OPTIONS, 0, 0); EXPECT_EQ(SRS_CONSTS_HTTP_OPTIONS, m.method()); EXPECT_STREQ("OPTIONS", m.method_str().c_str());
}
if (true) {
SrsHttpMessage m;
EXPECT_TRUE(m.is_keep_alive());
EXPECT_FALSE(m.is_infinite_chunked());
}
if (true) {
@ -558,46 +573,17 @@ VOID TEST(TCPServerTest, MessageConnection)
if (true) {
SrsHttpMessage m;
HELPER_EXPECT_SUCCESS(m.set_url("http://127.0.0.1/v1/streams/100", false));
EXPECT_EQ(100, m.parse_rest_id("/v1/streams/")); EXPECT_FALSE(m.is_jsonp());
EXPECT_STREQ("100", m.parse_rest_id("/v1/streams/").c_str()); EXPECT_FALSE(m.is_jsonp());
}
if (true) {
SrsHttpMessage m;
HELPER_EXPECT_SUCCESS(m.set_url("http://127.0.0.1/v1/streams/abc", false));
EXPECT_STREQ("abc", m.parse_rest_id("/v1/streams/").c_str()); EXPECT_FALSE(m.is_jsonp());
}
}
VOID TEST(TCPServerTest, MessageInfinityChunked)
{
srs_error_t err;
if (true) {
SrsHttpMessage m;
EXPECT_FALSE(m.is_infinite_chunked());
HELPER_EXPECT_SUCCESS(m.enter_infinite_chunked());
EXPECT_TRUE(m.is_infinite_chunked());
}
if (true) {
SrsHttpMessage m;
HELPER_EXPECT_SUCCESS(m.enter_infinite_chunked());
HELPER_EXPECT_SUCCESS(m.enter_infinite_chunked());
EXPECT_TRUE(m.is_infinite_chunked());
}
if (true) {
SrsHttpMessage m;
SrsHttpHeader hdr;
hdr.set("Transfer-Encoding", "chunked");
m.set_header(&hdr, false);
HELPER_EXPECT_FAILED(m.enter_infinite_chunked());
}
if (true) {
SrsHttpMessage m;
SrsHttpHeader hdr;
hdr.set("Content-Length", "100");
m.set_header(&hdr, false);
HELPER_EXPECT_FAILED(m.enter_infinite_chunked());
}
}
VOID TEST(TCPServerTest, MessageTurnRequest)
VOID TEST(HTTPServerTest, MessageTurnRequest)
{
srs_error_t err;
@ -645,7 +631,63 @@ VOID TEST(TCPServerTest, MessageTurnRequest)
}
}
VOID TEST(TCPServerTest, MessageWritev)
VOID TEST(HTTPServerTest, ContentLength)
{
srs_error_t err;
if (true) {
MockBufferIO io;
io.append("HTTP/1.1 200 OK\r\nContent-Length: 11\r\n\r\n");
SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE));
ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg));
char buf[32]; ssize_t nread = 0;
ISrsHttpResponseReader* r = msg->body_reader();
io.append("Hello");
HELPER_ARRAY_INIT(buf, sizeof(buf), 0);
HELPER_ASSERT_SUCCESS(r->read(buf, 5, &nread));
EXPECT_EQ(5, nread);
EXPECT_STREQ("Hello", buf);
io.append("World!");
HELPER_ARRAY_INIT(buf, sizeof(buf), 0);
HELPER_ASSERT_SUCCESS(r->read(buf, 6, &nread));
EXPECT_EQ(6, nread);
EXPECT_STREQ("World!", buf);
}
}
VOID TEST(HTTPServerTest, HTTPChunked)
{
srs_error_t err;
if (true) {
MockBufferIO io;
io.append("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n");
SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE));
ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg));
char buf[32]; ssize_t nread = 0;
ISrsHttpResponseReader* r = msg->body_reader();
io.append("5\r\nHello\r\n");
HELPER_ARRAY_INIT(buf, sizeof(buf), 0);
HELPER_ASSERT_SUCCESS(r->read(buf, 5, &nread));
EXPECT_EQ(5, nread);
EXPECT_STREQ("Hello", buf);
io.append("6\r\nWorld!\r\n");
HELPER_ARRAY_INIT(buf, sizeof(buf), 0);
HELPER_ASSERT_SUCCESS(r->read(buf, 6, &nread));
EXPECT_EQ(6, nread);
EXPECT_STREQ("World!", buf);
}
}
VOID TEST(HTTPServerTest, InfiniteChunked)
{
srs_error_t err;
@ -654,14 +696,9 @@ VOID TEST(TCPServerTest, MessageWritev)
MockBufferIO io;
io.append("HTTP/1.1 200 OK\r\n\r\n");
SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE, false));
SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE));
ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg));
if (true) {
SrsHttpMessage* hm = dynamic_cast<SrsHttpMessage*>(msg);
ASSERT_TRUE(hm != NULL);
hm->enter_infinite_chunked();
}
SrsAutoFree(ISrsHttpMessage, msg);
char buf[32]; ssize_t nread = 0;
ISrsHttpResponseReader* r = msg->body_reader();
@ -677,8 +714,109 @@ VOID TEST(TCPServerTest, MessageWritev)
HELPER_ASSERT_SUCCESS(r->read(buf, 8, &nread));
EXPECT_EQ(8, nread);
EXPECT_STREQ("\r\nWorld!", buf);
EXPECT_FALSE(r->eof());
}
// If read error, it's EOF.
if (true) {
MockBufferIO io;
io.append("HTTP/1.1 200 OK\r\n\r\n");
SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE));
ISrsHttpMessage* msg = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &msg));
char buf[32]; ssize_t nread = 0;
ISrsHttpResponseReader* r = msg->body_reader();
io.append("Hello");
HELPER_ARRAY_INIT(buf, sizeof(buf), 0);
HELPER_ASSERT_SUCCESS(r->read(buf, 10, &nread));
EXPECT_EQ(5, nread);
EXPECT_STREQ("Hello", buf);
io.in_err = srs_error_new(ERROR_SOCKET_READ, "EOF");
HELPER_ASSERT_SUCCESS(r->read(buf, 10, &nread));
EXPECT_TRUE(r->eof());
}
}
VOID TEST(HTTPServerTest, OPTIONSRead)
{
srs_error_t err;
// If request, it has no content-length, not chunked, it's not infinite chunked,
// actually, it has no body.
if (true) {
MockBufferIO io;
io.append("OPTIONS /rtc/v1/play HTTP/1.1\r\n\r\n");
SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST));
ISrsHttpMessage* req = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req));
SrsAutoFree(ISrsHttpMessage, req);
ISrsHttpResponseReader* br = req->body_reader();
EXPECT_TRUE(br->eof());
}
// If response, it has no content-length, not chunked, it's infinite chunked,
if (true) {
MockBufferIO io;
io.append("HTTP/1.1 200 OK\r\n\r\n");
SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_RESPONSE));
ISrsHttpMessage* req = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req));
SrsAutoFree(ISrsHttpMessage, req);
ISrsHttpResponseReader* br = req->body_reader();
EXPECT_FALSE(br->eof());
}
// So if OPTIONS has body, with chunked or content-length, it's ok to parsing it.
if (true) {
MockBufferIO io;
io.append("OPTIONS /rtc/v1/play HTTP/1.1\r\nContent-Length: 5\r\n\r\nHello");
SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST));
ISrsHttpMessage* req = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req));
SrsAutoFree(ISrsHttpMessage, req);
ISrsHttpResponseReader* br = req->body_reader();
EXPECT_FALSE(br->eof());
string b; HELPER_ASSERT_SUCCESS(req->body_read_all(b));
EXPECT_STREQ("Hello", b.c_str());
// The body will use as next HTTP request message.
io.append("GET /rtc/v1/play HTTP/1.1\r\n\r\n");
ISrsHttpMessage* req2 = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req2));
SrsAutoFree(ISrsHttpMessage, req2);
}
// So if OPTIONS has body, but not specified the size, we think it has no body,
// and the body is parsed fail as the next parsing.
if (true) {
MockBufferIO io;
io.append("OPTIONS /rtc/v1/play HTTP/1.1\r\n\r\n");
SrsHttpParser hp; HELPER_ASSERT_SUCCESS(hp.initialize(HTTP_REQUEST));
ISrsHttpMessage* req = NULL; HELPER_ASSERT_SUCCESS(hp.parse_message(&io, &req));
SrsAutoFree(ISrsHttpMessage, req);
ISrsHttpResponseReader* br = req->body_reader();
EXPECT_TRUE(br->eof());
// The body will use as next HTTP request message.
io.append("Hello");
ISrsHttpMessage* req2 = NULL; HELPER_ASSERT_FAILED(hp.parse_message(&io, &req2));
SrsAutoFree(ISrsHttpMessage, req2);
}
}
VOID TEST(HTTPServerTest, MessageWritev)
{
srs_error_t err;
// Directly writev, merge to one chunk.
if (true) {
MockResponseWriter w;
@ -782,7 +920,7 @@ class MockOnCycleThread : public ISrsCoroutineHandler
public:
SrsSTCoroutine trd;
srs_cond_t cond;
MockOnCycleThread() : trd("mock", this, 0) {
MockOnCycleThread() : trd("mock", this) {
cond = srs_cond_new();
};
virtual ~MockOnCycleThread() {
@ -822,7 +960,7 @@ class MockOnCycleThread2 : public ISrsCoroutineHandler
public:
SrsSTCoroutine trd;
srs_mutex_t lock;
MockOnCycleThread2() : trd("mock", this, 0) {
MockOnCycleThread2() : trd("mock", this) {
lock = srs_mutex_new();
};
virtual ~MockOnCycleThread2() {
@ -863,7 +1001,7 @@ class MockOnCycleThread3 : public ISrsCoroutineHandler
public:
SrsSTCoroutine trd;
srs_netfd_t fd;
MockOnCycleThread3() : trd("mock", this, 0) {
MockOnCycleThread3() : trd("mock", this) {
fd = NULL;
};
virtual ~MockOnCycleThread3() {
@ -1000,8 +1138,6 @@ VOID TEST(TCPServerTest, CoverUtility)
EXPECT_FALSE(srs_net_device_is_internet((sockaddr*)r->ai_addr));
}
EXPECT_FALSE(srs_net_device_is_internet("eth0"));
if (true) {
sockaddr_in addr;
addr.sin_family = AF_INET;
@ -1116,7 +1252,7 @@ class MockOnCycleThread4 : public ISrsCoroutineHandler
public:
SrsSTCoroutine trd;
srs_netfd_t fd;
MockOnCycleThread4() : trd("mock", this, 0) {
MockOnCycleThread4() : trd("mock", this) {
fd = NULL;
};
virtual ~MockOnCycleThread4() {
@ -1176,7 +1312,7 @@ public:
}
};
VOID TEST(TCPServerTest, HTTPClientUtility)
VOID TEST(HTTPClientTest, HTTPClientUtility)
{
srs_error_t err;
@ -1186,7 +1322,7 @@ VOID TEST(TCPServerTest, HTTPClientUtility)
HELPER_ASSERT_SUCCESS(trd.start("127.0.0.1", 8080));
SrsHttpClient client;
HELPER_ASSERT_SUCCESS(client.initialize("127.0.0.1", 8080, 1*SRS_UTIME_SECONDS));
HELPER_ASSERT_SUCCESS(client.initialize("http", "127.0.0.1", 8080, 1*SRS_UTIME_SECONDS));
ISrsHttpMessage* res = NULL;
SrsAutoFree(ISrsHttpMessage, res);
@ -1208,7 +1344,7 @@ VOID TEST(TCPServerTest, HTTPClientUtility)
HELPER_ASSERT_SUCCESS(trd.start("127.0.0.1", 8080));
SrsHttpClient client;
HELPER_ASSERT_SUCCESS(client.initialize("127.0.0.1", 8080, 1*SRS_UTIME_SECONDS));
HELPER_ASSERT_SUCCESS(client.initialize("http", "127.0.0.1", 8080, 1*SRS_UTIME_SECONDS));
ISrsHttpMessage* res = NULL;
SrsAutoFree(ISrsHttpMessage, res);
@ -1230,7 +1366,7 @@ VOID TEST(TCPServerTest, HTTPClientUtility)
HELPER_ASSERT_SUCCESS(trd.start("127.0.0.1", 8080));
SrsHttpClient client;
HELPER_ASSERT_SUCCESS(client.initialize("127.0.0.1", 8080, 1*SRS_UTIME_SECONDS));
HELPER_ASSERT_SUCCESS(client.initialize("http", "127.0.0.1", 8080, 1*SRS_UTIME_SECONDS));
client.set_recv_timeout(1 * SRS_UTIME_SECONDS);
client.set_header("agent", "srs");
@ -1251,7 +1387,7 @@ VOID TEST(TCPServerTest, HTTPClientUtility)
}
}
class MockConnectionManager : public IConnectionManager
class MockConnectionManager : public ISrsResourceManager
{
public:
MockConnectionManager() {
@ -1259,7 +1395,7 @@ public:
virtual ~MockConnectionManager() {
}
public:
virtual void remove(ISrsConnection* /*c*/) {
virtual void remove(ISrsResource* /*c*/) {
}
};
@ -1268,38 +1404,56 @@ VOID TEST(TCPServerTest, ContextUtility)
if (true) {
SrsThreadContext ctx;
EXPECT_EQ(0, ctx.set_id(100));
EXPECT_EQ(100, ctx.set_id(1000));
EXPECT_EQ(1000, ctx.get_id());
if (true) {
SrsContextId cid;
EXPECT_TRUE(!ctx.set_id(cid.set_value("100")).compare(cid));
}
if (true) {
SrsContextId cid;
EXPECT_TRUE(!ctx.set_id(cid.set_value("1000")).compare(cid));
}
if (true) {
SrsContextId cid;
EXPECT_TRUE(!ctx.get_id().compare(cid.set_value("1000")));
}
ctx.clear_cid();
EXPECT_EQ(0, ctx.set_id(100));
if (true) {
SrsContextId cid;
EXPECT_TRUE(!ctx.set_id(cid.set_value("100")).compare(cid));
}
}
SrsContextId cid;
cid.set_value("100");
int base_size = 0;
if (true) {
errno = 0;
int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0);
ASSERT_TRUE(srs_log_header(buf, 1024, true, true, "SRS", 100, "Trace", &size));
ASSERT_TRUE(srs_log_header(buf, 1024, true, true, "SRS", cid, "Trace", &size));
base_size = size;
EXPECT_TRUE(base_size > 0);
}
if (true) {
errno = 0;
int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0);
ASSERT_TRUE(srs_log_header(buf, 1024, false, true, "SRS", 100, "Trace", &size));
ASSERT_TRUE(srs_log_header(buf, 1024, false, true, "SRS", cid, "Trace", &size));
EXPECT_EQ(base_size, size);
}
if (true) {
errno = 0;
int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0);
ASSERT_TRUE(srs_log_header(buf, 1024, false, true, NULL, 100, "Trace", &size));
ASSERT_TRUE(srs_log_header(buf, 1024, false, true, NULL, cid, "Trace", &size));
EXPECT_EQ(base_size - 5, size);
}
if (true) {
errno = 0;
int size = 0; char buf[1024]; HELPER_ARRAY_INIT(buf, 1024, 0);
ASSERT_TRUE(srs_log_header(buf, 1024, false, false, NULL, 100, "Trace", &size));
ASSERT_TRUE(srs_log_header(buf, 1024, false, false, NULL, cid, "Trace", &size));
EXPECT_EQ(base_size - 8, size);
}

View file

@ -30,6 +30,22 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_utest.hpp>
#include <srs_app_st.hpp>
#include <srs_service_conn.hpp>
class MockSrsConnection : public ISrsConnection
{
public:
// Whether switch the coroutine context when free the object, for special case test.
bool do_switch;
public:
MockSrsConnection();
virtual ~MockSrsConnection();
// Interface ISrsConnection.
public:
virtual const SrsContextId& get_id();
virtual std::string desc();
virtual std::string remote_ip();
};
#endif