Improve test coverage for config.

pull/1568/head
winlin 5 years ago
parent a71d93188b
commit de87b64f59

@ -222,6 +222,14 @@ bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b, string excep
return true;
}
void set_config_directive(SrsConfDirective* parent, string dir, string value)
{
SrsConfDirective* d = parent->get_or_create(dir);
d->name = dir;
d->args.clear();
d->args.push_back(value);
}
bool srs_config_hls_is_on_error_ignore(string strategy)
{
return strategy == "ignore";
@ -864,6 +872,7 @@ srs_error_t SrsConfDirective::persistence(SrsFileWriter* writer, int level)
return err;
}
// LCOV_EXCL_START
SrsJsonArray* SrsConfDirective::dumps_args()
{
SrsJsonArray* arr = SrsJsonAny::array();
@ -893,6 +902,7 @@ SrsJsonAny* SrsConfDirective::dumps_arg0_to_boolean()
{
return SrsJsonAny::boolean(arg0() == "on");
}
// LCOV_EXCL_STOP
// see: ngx_conf_parse
srs_error_t SrsConfDirective::parse_conf(SrsConfigBuffer* buffer, SrsDirectiveType type)
@ -1122,24 +1132,6 @@ bool SrsConfig::is_dolphin()
return dolphin;
}
void SrsConfig::set_config_directive(SrsConfDirective* parent, string dir, string value)
{
SrsConfDirective* d = parent->get(dir);
if (!d) {
d = new SrsConfDirective();
if (!dir.empty()) {
d->name = dir;
}
parent->directives.push_back(d);
}
d->args.clear();
if (!value.empty()) {
d->args.push_back(value);
}
}
void SrsConfig::subscribe(ISrsReloadHandler* handler)
{
std::vector<ISrsReloadHandler*>::iterator it;

@ -308,9 +308,7 @@ public:
public:
// Whether srs is in dolphin mode.
virtual bool is_dolphin();
private:
virtual void set_config_directive(SrsConfDirective* parent, std::string dir, std::string value);
// Reload
// Reload
public:
// For reload handler to register itself,
// when config service do the reload, callback the handler.

@ -32,6 +32,7 @@ using namespace std;
#include <srs_kernel_utility.hpp>
#include <srs_service_st.hpp>
#include <srs_rtmp_stack.hpp>
#include <srs_utest_kernel.hpp>
MockSrsConfigBuffer::MockSrsConfigBuffer(string buf)
{
@ -2281,3 +2282,212 @@ VOID TEST(ConfigUnitTest, TransformForVhost)
}
}
VOID TEST(ConfigUnitTest, DirectiveCopy)
{
if (true) {
SrsConfDirective d;
d.name = "vhost";
d.get_or_create("enabled", "on");
SrsConfDirective* cp = d.copy();
ASSERT_TRUE(cp != NULL);
EXPECT_STREQ("vhost", cp->name.c_str());
ASSERT_TRUE(cp->get("enabled") != NULL);
EXPECT_STREQ("on", cp->get("enabled")->arg0().c_str());
srs_freep(cp);
}
if (true) {
SrsConfDirective d;
d.name = "vhost";
d.get_or_create("enabled", "on");
SrsConfDirective* cp = d.copy("enabled");
ASSERT_TRUE(cp != NULL);
EXPECT_STREQ("vhost", cp->name.c_str());
ASSERT_TRUE(cp->get("enabled") == NULL);
srs_freep(cp);
}
if (true) {
SrsConfDirective d;
d.name = "vhost";
d.get_or_create("enabled", "on");
d.get_or_create("hls");
SrsConfDirective* cp = d.copy("hls");
ASSERT_TRUE(cp != NULL);
EXPECT_STREQ("vhost", cp->name.c_str());
ASSERT_TRUE(cp->get("enabled") != NULL);
EXPECT_STREQ("on", cp->get("enabled")->arg0().c_str());
srs_freep(cp);
}
if (true) {
SrsConfDirective d;
EXPECT_TRUE(d.arg0().empty());
EXPECT_TRUE(d.arg1().empty());
EXPECT_TRUE(d.arg2().empty());
EXPECT_TRUE(d.arg3().empty());
}
if (true) {
SrsConfDirective d;
d.args.push_back("a0");
EXPECT_STREQ("a0", d.arg0().c_str());
EXPECT_TRUE(d.arg1().empty());
EXPECT_TRUE(d.arg2().empty());
EXPECT_TRUE(d.arg3().empty());
}
if (true) {
SrsConfDirective d;
d.args.push_back("a0");
d.args.push_back("a1");
EXPECT_STREQ("a0", d.arg0().c_str());
EXPECT_STREQ("a1", d.arg1().c_str());
EXPECT_TRUE(d.arg2().empty());
EXPECT_TRUE(d.arg3().empty());
}
if (true) {
SrsConfDirective d;
d.args.push_back("a0");
d.args.push_back("a1");
d.args.push_back("a2");
EXPECT_STREQ("a0", d.arg0().c_str());
EXPECT_STREQ("a1", d.arg1().c_str());
EXPECT_STREQ("a2", d.arg2().c_str());
EXPECT_TRUE(d.arg3().empty());
}
if (true) {
SrsConfDirective d;
d.args.push_back("a0");
d.args.push_back("a1");
d.args.push_back("a2");
d.args.push_back("a3");
EXPECT_STREQ("a0", d.arg0().c_str());
EXPECT_STREQ("a1", d.arg1().c_str());
EXPECT_STREQ("a2", d.arg2().c_str());
EXPECT_STREQ("a3", d.arg3().c_str());
}
if (true) {
SrsConfDirective d;
d.set_arg0("a0");
EXPECT_STREQ("a0", d.arg0().c_str());
EXPECT_TRUE(d.arg1().empty());
EXPECT_TRUE(d.arg2().empty());
EXPECT_TRUE(d.arg3().empty());
}
if (true) {
SrsConfDirective d;
d.args.push_back("a0");
d.set_arg0("a0");
EXPECT_STREQ("a0", d.arg0().c_str());
EXPECT_TRUE(d.arg1().empty());
EXPECT_TRUE(d.arg2().empty());
EXPECT_TRUE(d.arg3().empty());
}
if (true) {
SrsConfDirective d;
d.args.push_back("a1");
d.set_arg0("a0");
EXPECT_STREQ("a0", d.arg0().c_str());
EXPECT_TRUE(d.arg1().empty());
EXPECT_TRUE(d.arg2().empty());
EXPECT_TRUE(d.arg3().empty());
}
if (true) {
SrsConfDirective d;
SrsConfDirective* vhost = d.get_or_create("vhost");
d.remove(vhost);
srs_freep(vhost);
EXPECT_TRUE(d.get("vhost") == NULL);
}
}
extern void set_config_directive(SrsConfDirective* parent, string dir, string value);
VOID TEST(ConfigUnitTest, PersistenceConfig)
{
srs_error_t err;
if (true) {
SrsConfDirective d;
MockSrsFileWriter fw;
HELPER_ASSERT_SUCCESS(d.persistence(&fw, 0));
EXPECT_STREQ("", fw.str().c_str());
}
if (true) {
SrsConfDirective d;
d.name = "root";
d.args.push_back("on");
MockSrsFileWriter fw;
HELPER_ASSERT_SUCCESS(d.persistence(&fw, 0));
EXPECT_STREQ("", fw.str().c_str());
}
if (true) {
SrsConfDirective d;
d.get_or_create("global");
MockSrsFileWriter fw;
HELPER_ASSERT_SUCCESS(d.persistence(&fw, 0));
EXPECT_STREQ("global;\n", fw.str().c_str());
}
if (true) {
SrsConfDirective d;
d.get_or_create("global", "on");
MockSrsFileWriter fw;
HELPER_ASSERT_SUCCESS(d.persistence(&fw, 0));
EXPECT_STREQ("global on;\n", fw.str().c_str());
}
if (true) {
SrsConfDirective d;
SrsConfDirective* p = d.get_or_create("global", "on");
p->get_or_create("child", "100");
p->get_or_create("sibling", "101");
MockSrsFileWriter fw;
HELPER_ASSERT_SUCCESS(d.persistence(&fw, 0));
EXPECT_STREQ("global on {\n child 100;\n sibling 101;\n}\n", fw.str().c_str());
}
if (true) {
SrsConfDirective d;
SrsConfDirective* p = d.get_or_create("global", "on");
SrsConfDirective* pp = p->get_or_create("child", "100");
p->get_or_create("sibling", "101");
pp->get_or_create("grandson", "200");
MockSrsFileWriter fw;
HELPER_ASSERT_SUCCESS(d.persistence(&fw, 0));
EXPECT_STREQ("global on {\n child 100 {\n grandson 200;\n }\n sibling 101;\n}\n", fw.str().c_str());
}
if (true) {
SrsConfDirective d;
set_config_directive(&d, "vhost", "on");
ASSERT_TRUE(d.get("vhost") != NULL);
EXPECT_STREQ("on", d.get("vhost")->arg0().c_str());
set_config_directive(&d, "vhost", "off");
ASSERT_TRUE(d.get("vhost") != NULL);
EXPECT_STREQ("off", d.get("vhost")->arg0().c_str());
}
}

@ -195,6 +195,16 @@ char* MockSrsFileWriter::data()
return uf->_data.bytes();
}
string MockSrsFileWriter::str()
{
int size = filesize();
if (size == 0) {
return "";
}
return string(data(), size);
}
srs_error_t MockSrsFileWriter::write(void* buf, size_t count, ssize_t* pnwrite)
{
if (err != srs_success) {

@ -74,6 +74,7 @@ public:
virtual int64_t tellg();
virtual int64_t filesize();
virtual char* data();
virtual string str();
public:
virtual srs_error_t write(void* buf, size_t count, ssize_t* pnwrite);
virtual srs_error_t lseek(off_t offset, int whence, off_t* seeked);

Loading…
Cancel
Save