From a71d93188bbe661d0b1e1b05c6aca576f7067917 Mon Sep 17 00:00:00 2001 From: winlin Date: Wed, 1 Jan 2020 13:41:19 +0800 Subject: [PATCH] Improve test coverage for config. --- trunk/src/app/srs_app_config.cpp | 38 +-- trunk/src/app/srs_app_config.hpp | 30 +++ trunk/src/utest/srs_utest_config.cpp | 354 +++++++++++++++++++++++++++ trunk/src/utest/srs_utest_kernel.cpp | 38 +++ 4 files changed, 428 insertions(+), 32 deletions(-) diff --git a/trunk/src/app/srs_app_config.cpp b/trunk/src/app/srs_app_config.cpp index 1b4db7946..b5e1ca9d8 100644 --- a/trunk/src/app/srs_app_config.cpp +++ b/trunk/src/app/srs_app_config.cpp @@ -80,36 +80,6 @@ const char* _srs_version = "XCORE-" RTMP_SIG_SRS_SERVER; */ srs_error_t srs_config_dumps_engine(SrsConfDirective* dir, SrsJsonObject* engine); -/** - * whether the two vector actual equals, for instance, - * srs_vector_actual_equals([0, 1, 2], [0, 1, 2]) ==== true - * srs_vector_actual_equals([0, 1, 2], [2, 1, 0]) ==== true - * srs_vector_actual_equals([0, 1, 2], [0, 2, 1]) ==== true - * srs_vector_actual_equals([0, 1, 2], [0, 1, 2, 3]) ==== false - * srs_vector_actual_equals([1, 2, 3], [0, 1, 2]) ==== false - */ -template -bool srs_vector_actual_equals(const vector& a, const vector& b) -{ - // all elements of a in b. - for (int i = 0; i < (int)a.size(); i++) { - const T& e = a.at(i); - if (::find(b.begin(), b.end(), e) == b.end()) { - return false; - } - } - - // all elements of b in a. - for (int i = 0; i < (int)b.size(); i++) { - const T& e = b.at(i); - if (::find(a.begin(), a.end(), e) == a.end()) { - return false; - } - } - - return true; -} - /** * whether the ch is common space. */ @@ -132,7 +102,8 @@ namespace _srs_internal { srs_freepa(start); } - + + // LCOV_EXCL_START srs_error_t SrsConfigBuffer::fullfill(const char* filename) { srs_error_t err = srs_success; @@ -160,6 +131,7 @@ namespace _srs_internal return err; } + // LCOV_EXCL_STOP bool SrsConfigBuffer::empty() { @@ -413,7 +385,7 @@ srs_error_t srs_config_transform_vhost(SrsConfDirective* root) if (enabled) { SrsConfDirective* mr = publish->get_or_create("mr"); mr->args = enabled->args; - srs_warn("transform: vhost.mr.enabled to vhost.publish.mr.enabled for %s", dir->name.c_str()); + srs_warn("transform: vhost.mr.enabled to vhost.publish.mr for %s", dir->name.c_str()); } SrsConfDirective* latency = conf->get("latency"); @@ -525,6 +497,7 @@ srs_error_t srs_config_transform_vhost(SrsConfDirective* root) return err; } +// LCOV_EXCL_START srs_error_t srs_config_dumps_engine(SrsConfDirective* dir, SrsJsonObject* engine) { srs_error_t err = srs_success; @@ -626,6 +599,7 @@ srs_error_t srs_config_dumps_engine(SrsConfDirective* dir, SrsJsonObject* engine return err; } +// LCOV_EXCL_STOP SrsConfDirective::SrsConfDirective() { diff --git a/trunk/src/app/srs_app_config.hpp b/trunk/src/app/srs_app_config.hpp index c5a91740c..602e5e22c 100644 --- a/trunk/src/app/srs_app_config.hpp +++ b/trunk/src/app/srs_app_config.hpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -46,6 +47,35 @@ class SrsRequest; class SrsJsonArray; class SrsConfDirective; +/** + * whether the two vector actual equals, for instance, + * srs_vector_actual_equals([0, 1, 2], [0, 1, 2]) ==== true + * srs_vector_actual_equals([0, 1, 2], [2, 1, 0]) ==== true + * srs_vector_actual_equals([0, 1, 2], [0, 2, 1]) ==== true + * srs_vector_actual_equals([0, 1, 2], [0, 1, 2, 3]) ==== false + * srs_vector_actual_equals([1, 2, 3], [0, 1, 2]) ==== false + */ +template +bool srs_vector_actual_equals(const std::vector& a, const std::vector& b) +{ + // all elements of a in b. + for (int i = 0; i < (int)a.size(); i++) { + const T& e = a.at(i); + if (std::find(b.begin(), b.end(), e) == b.end()) { + return false; + } + } + + // all elements of b in a. + for (int i = 0; i < (int)b.size(); i++) { + const T& e = b.at(i); + if (std::find(a.begin(), a.end(), e) == a.end()) { + return false; + } + } + + return true; +} namespace _srs_internal { diff --git a/trunk/src/utest/srs_utest_config.cpp b/trunk/src/utest/srs_utest_config.cpp index 43622c3a7..864637380 100644 --- a/trunk/src/utest/srs_utest_config.cpp +++ b/trunk/src/utest/srs_utest_config.cpp @@ -31,6 +31,7 @@ using namespace std; #include #include #include +#include MockSrsConfigBuffer::MockSrsConfigBuffer(string buf) { @@ -1927,3 +1928,356 @@ VOID TEST(ConfigUnitTest, CheckDefaultValuesGlobal) } } +VOID TEST(ConfigUnitTest, VectorEquals) +{ + if (true) { + vector a, b; + a.push_back(0); a.push_back(1); a.push_back(2); + b.push_back(0); b.push_back(1); b.push_back(2); + EXPECT_TRUE(srs_vector_actual_equals(a, b)); + } + + if (true) { + vector a, b; + a.push_back(0); a.push_back(1); a.push_back(2); + b.push_back(2); b.push_back(1); b.push_back(0); + EXPECT_TRUE(srs_vector_actual_equals(a, b)); + } + + if (true) { + vector a, b; + a.push_back(0); a.push_back(1); a.push_back(2); + b.push_back(0); b.push_back(2); b.push_back(1); + EXPECT_TRUE(srs_vector_actual_equals(a, b)); + } + + if (true) { + vector a, b; + a.push_back(0); a.push_back(1); a.push_back(2); + b.push_back(0); b.push_back(1); b.push_back(2); b.push_back(3); + EXPECT_FALSE(srs_vector_actual_equals(a, b)); + } + + if (true) { + vector a, b; + a.push_back(1); a.push_back(2); a.push_back(3); + b.push_back(0); b.push_back(1); b.push_back(2); + EXPECT_FALSE(srs_vector_actual_equals(a, b)); + } +} + +extern bool srs_directive_equals_self(SrsConfDirective* a, SrsConfDirective* b); +extern bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b); +extern bool srs_directive_equals(SrsConfDirective* a, SrsConfDirective* b, string except); + +VOID TEST(ConfigUnitTest, DirectiveEquals) +{ + EXPECT_TRUE(srs_directive_equals_self(NULL, NULL)); + + if (true) { + SrsConfDirective* a = new SrsConfDirective(); + EXPECT_FALSE(srs_directive_equals_self(a, NULL)); + EXPECT_FALSE(srs_directive_equals_self(NULL, a)); + srs_freep(a); + } + + if (true) { + SrsConfDirective* a = new SrsConfDirective(); + SrsConfDirective* b = a; + EXPECT_TRUE(srs_directive_equals_self(a, b)); + srs_freep(a); + } + + if (true) { + SrsConfDirective* a = new SrsConfDirective(); + a->name = "hls"; + SrsConfDirective* b = new SrsConfDirective(); + b->name = "dvr"; + EXPECT_FALSE(srs_directive_equals_self(a, b)); + srs_freep(a); srs_freep(b); + } + + if (true) { + SrsConfDirective* a = new SrsConfDirective(); + a->directives.push_back(new SrsConfDirective()); + SrsConfDirective* b = new SrsConfDirective(); + EXPECT_FALSE(srs_directive_equals_self(a, b)); + srs_freep(a); srs_freep(b); + } + + if (true) { + SrsConfDirective* a = new SrsConfDirective(); + a->directives.push_back(new SrsConfDirective()); + a->at(0)->name = "hls"; + SrsConfDirective* b = new SrsConfDirective(); + b->directives.push_back(new SrsConfDirective()); + EXPECT_TRUE(srs_directive_equals(a, b, "hls")); + srs_freep(a); srs_freep(b); + } +} + +VOID TEST(ConfigUnitTest, OperatorEquals) +{ + EXPECT_TRUE(srs_config_hls_is_on_error_ignore("ignore")); + EXPECT_FALSE(srs_config_hls_is_on_error_ignore("xxx")); + + EXPECT_TRUE(srs_config_hls_is_on_error_continue("continue")); + EXPECT_FALSE(srs_config_hls_is_on_error_continue("xxx")); + + EXPECT_TRUE(srs_config_ingest_is_file("file")); + EXPECT_FALSE(srs_config_ingest_is_file("xxx")); + + EXPECT_TRUE(srs_config_ingest_is_stream("stream")); + EXPECT_FALSE(srs_config_ingest_is_stream("xxx")); + + EXPECT_TRUE(srs_config_dvr_is_plan_segment("segment")); + EXPECT_FALSE(srs_config_dvr_is_plan_segment("xxx")); + + EXPECT_TRUE(srs_config_dvr_is_plan_session("session")); + EXPECT_FALSE(srs_config_dvr_is_plan_session("xxx")); + + EXPECT_TRUE(srs_stream_caster_is_udp("mpegts_over_udp")); + EXPECT_FALSE(srs_stream_caster_is_udp("xxx")); + + EXPECT_TRUE(srs_stream_caster_is_rtsp("rtsp")); + EXPECT_FALSE(srs_stream_caster_is_rtsp("xxx")); + + EXPECT_TRUE(srs_stream_caster_is_flv("flv")); + EXPECT_FALSE(srs_stream_caster_is_flv("xxx")); + + EXPECT_STREQ("on", srs_config_bool2switch("true").c_str()); + EXPECT_STREQ("off", srs_config_bool2switch("false").c_str()); + EXPECT_STREQ("off", srs_config_bool2switch("xxx").c_str()); +} + +VOID TEST(ConfigUnitTest, ApplyFilter) +{ + EXPECT_TRUE(srs_config_apply_filter(NULL, NULL)); + + if (true) { + SrsConfDirective d; + EXPECT_TRUE(srs_config_apply_filter(&d, NULL)); + } + + if (true) { + SrsConfDirective d; + d.args.push_back("all"); + EXPECT_TRUE(srs_config_apply_filter(&d, NULL)); + } + + if (true) { + SrsConfDirective d; + SrsRequest r; + r.app = "live"; r.stream = "stream"; + d.args.push_back("live/stream"); + EXPECT_TRUE(srs_config_apply_filter(&d, &r)); + } + + if (true) { + SrsConfDirective d; + d.args.push_back("live/stream"); + SrsRequest r; + EXPECT_FALSE(srs_config_apply_filter(&d, &r)); + } +} + +VOID TEST(ConfigUnitTest, TransformForVhost) +{ + srs_error_t err; + + if (true) { + SrsConfDirective root; + root.get_or_create("http_stream"); + + HELPER_ASSERT_SUCCESS(srs_config_transform_vhost(&root)); + + SrsConfDirective* p = root.get("http_server"); + ASSERT_TRUE(p != NULL); + } + + if (true) { + SrsConfDirective root; + SrsConfDirective* vhost = root.get_or_create("vhost"); + if (true) { + vhost->get_or_create("http"); + } + + HELPER_ASSERT_SUCCESS(srs_config_transform_vhost(&root)); + + SrsConfDirective* p = vhost->get("http_static"); + ASSERT_TRUE(p != NULL); + } + + if (true) { + SrsConfDirective root; + SrsConfDirective* vhost = root.get_or_create("vhost"); + if (true) { + SrsConfDirective* p = vhost->get_or_create("http_remux"); + p->get_or_create("hstrs", "on"); + } + + HELPER_ASSERT_SUCCESS(srs_config_transform_vhost(&root)); + + SrsConfDirective* p = vhost->get("http_remux"); + ASSERT_TRUE(p != NULL); + ASSERT_TRUE(p->get("hstrs") == NULL); + } + + if (true) { + SrsConfDirective root; + SrsConfDirective* vhost = root.get_or_create("vhost"); + if (true) { + vhost->get_or_create("refer", "refer-v"); + vhost->get_or_create("refer_play", "refer-play-v"); + vhost->get_or_create("refer_publish", "refer-publish-v"); + } + + HELPER_ASSERT_SUCCESS(srs_config_transform_vhost(&root)); + + SrsConfDirective* p = vhost->get("refer"); + ASSERT_TRUE(p != NULL); + + ASSERT_TRUE(p->get("enabled") != NULL); + EXPECT_STREQ("on", p->get("enabled")->arg0().c_str()); + + ASSERT_TRUE(p->get("all") != NULL); + EXPECT_STREQ("refer-v", p->get("all")->arg0().c_str()); + + ASSERT_TRUE(p->get("play") != NULL); + EXPECT_STREQ("refer-play-v", p->get("play")->arg0().c_str()); + + ASSERT_TRUE(p->get("publish") != NULL); + EXPECT_STREQ("refer-publish-v", p->get("publish")->arg0().c_str()); + } + + if (true) { + SrsConfDirective root; + SrsConfDirective* vhost = root.get_or_create("vhost"); + if (true) { + SrsConfDirective* mr = vhost->get_or_create("mr"); + mr->get_or_create("enabled", "on"); + mr->get_or_create("latency", "100"); + } + + HELPER_ASSERT_SUCCESS(srs_config_transform_vhost(&root)); + + SrsConfDirective* publish = vhost->get("publish"); + ASSERT_TRUE(publish != NULL); + + SrsConfDirective* p = publish->get("mr"); + ASSERT_TRUE(p != NULL); + EXPECT_STREQ("on", p->arg0().c_str()); + + p = publish->get("mr_latency"); + ASSERT_TRUE(p != NULL); + EXPECT_STREQ("100", p->arg0().c_str()); + } + + if (true) { + SrsConfDirective root; + SrsConfDirective* vhost = root.get_or_create("vhost"); + if (true) { + vhost->get_or_create("publish_1stpkt_timeout", "100"); + } + + HELPER_ASSERT_SUCCESS(srs_config_transform_vhost(&root)); + + SrsConfDirective* publish = vhost->get("publish"); + ASSERT_TRUE(publish != NULL); + + SrsConfDirective* p = publish->get("firstpkt_timeout"); + ASSERT_TRUE(p != NULL); + EXPECT_STREQ("100", p->arg0().c_str()); + } + + if (true) { + SrsConfDirective root; + SrsConfDirective* vhost = root.get_or_create("vhost"); + if (true) { + vhost->get_or_create("publish_normal_timeout", "100"); + } + + HELPER_ASSERT_SUCCESS(srs_config_transform_vhost(&root)); + + SrsConfDirective* publish = vhost->get("publish"); + ASSERT_TRUE(publish != NULL); + + SrsConfDirective* p = publish->get("normal_timeout"); + ASSERT_TRUE(p != NULL); + EXPECT_STREQ("100", p->arg0().c_str()); + } + + if (true) { + SrsConfDirective root; + SrsConfDirective* vhost = root.get_or_create("vhost"); + if (true) { + vhost->get_or_create("time_jitter", "on"); + vhost->get_or_create("mix_correct", "on"); + vhost->get_or_create("atc", "on"); + vhost->get_or_create("atc_auto", "on"); + vhost->get_or_create("mw_latency", "on"); + vhost->get_or_create("gop_cache", "on"); + vhost->get_or_create("queue_length", "on"); + vhost->get_or_create("send_min_interval", "on"); + vhost->get_or_create("reduce_sequence_header", "on"); + } + + HELPER_ASSERT_SUCCESS(srs_config_transform_vhost(&root)); + + SrsConfDirective* p = vhost->get("play"); + ASSERT_TRUE(p != NULL); + + ASSERT_TRUE(p->get("time_jitter") != NULL); + ASSERT_TRUE(p->get("mix_correct") != NULL); + ASSERT_TRUE(p->get("atc") != NULL); + ASSERT_TRUE(p->get("atc_auto") != NULL); + ASSERT_TRUE(p->get("mw_latency") != NULL); + ASSERT_TRUE(p->get("gop_cache") != NULL); + ASSERT_TRUE(p->get("queue_length") != NULL); + ASSERT_TRUE(p->get("send_min_interval") != NULL); + ASSERT_TRUE(p->get("reduce_sequence_header") != NULL); + } + + if (true) { + SrsConfDirective root; + SrsConfDirective* vhost = root.get_or_create("vhost"); + if (true) { + vhost->get_or_create("forward", "forward-v"); + } + + HELPER_ASSERT_SUCCESS(srs_config_transform_vhost(&root)); + + SrsConfDirective* p = vhost->get("forward"); + ASSERT_TRUE(p != NULL); + + ASSERT_TRUE(p->get("enabled") != NULL); + EXPECT_STREQ("on", p->get("enabled")->arg0().c_str()); + + ASSERT_TRUE(p->get("destination") != NULL); + EXPECT_STREQ("forward-v", p->get("destination")->arg0().c_str()); + } + + if (true) { + SrsConfDirective root; + SrsConfDirective* vhost = root.get_or_create("vhost"); + if (true) { + vhost->get_or_create("mode", "on"); + vhost->get_or_create("origin", "on"); + vhost->get_or_create("token_traverse", "on"); + vhost->get_or_create("vhost", "on"); + vhost->get_or_create("debug_srs_upnode", "on"); + } + + HELPER_ASSERT_SUCCESS(srs_config_transform_vhost(&root)); + + SrsConfDirective* p = vhost->get("cluster"); + ASSERT_TRUE(p != NULL); + + ASSERT_TRUE(p->get("mode") != NULL); + ASSERT_TRUE(p->get("origin") != NULL); + ASSERT_TRUE(p->get("token_traverse") != NULL); + ASSERT_TRUE(p->get("vhost") != NULL); + ASSERT_TRUE(p->get("debug_srs_upnode") != NULL); + } +} + diff --git a/trunk/src/utest/srs_utest_kernel.cpp b/trunk/src/utest/srs_utest_kernel.cpp index aa8f91cc7..e73914c13 100644 --- a/trunk/src/utest/srs_utest_kernel.cpp +++ b/trunk/src/utest/srs_utest_kernel.cpp @@ -5093,6 +5093,44 @@ VOID TEST(KernelMP4Test, CoverMP4MultipleAVs) } } +VOID TEST(KernelMP4Test, CoverMP4CodecErrorNoFrames) +{ + srs_error_t err; + + MockSrsFileWriter f; + + // MP4 encoder. + if (true) { + SrsMp4Encoder enc; SrsFormat fmt; + HELPER_EXPECT_SUCCESS(enc.initialize(&f)); + HELPER_EXPECT_SUCCESS(fmt.initialize()); + + if (true) { + uint8_t raw[] = { + 0x17, 0x00, 0x00, 0x00, 0x00, 0x01, 0x64, 0x00, 0x20, 0xff, 0xe1, 0x00, 0x19, 0x67, 0x64, 0x00, 0x20, 0xac, 0xd9, 0x40, 0xc0, 0x29, 0xb0, 0x11, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03, 0x00, 0x32, 0x0f, 0x18, 0x31, 0x96, 0x01, 0x00, 0x05, 0x68, 0xeb, 0xec, 0xb2, 0x2c + }; + HELPER_EXPECT_SUCCESS(fmt.on_video(0, (char*)raw, sizeof(raw))); + HELPER_EXPECT_SUCCESS(enc.write_sample( + &fmt, SrsMp4HandlerTypeVIDE, fmt.video->frame_type, fmt.video->avc_packet_type, 0, 0, (uint8_t*)fmt.raw, fmt.nb_raw + )); + EXPECT_EQ(768, enc.width); EXPECT_EQ(320, enc.height); + } + + if (true) { + uint8_t raw[] = { + 0xaf, 0x00, 0x12, 0x10 + }; + HELPER_EXPECT_SUCCESS(fmt.on_audio(0, (char*)raw, sizeof(raw))); + HELPER_EXPECT_SUCCESS(enc.write_sample( + &fmt, SrsMp4HandlerTypeSOUN, 0x00, fmt.audio->aac_packet_type, 0, 0, (uint8_t*)fmt.raw, fmt.nb_raw + )); + } + + HELPER_ASSERT_FAILED(enc.flush()); + //mock_print_mp4(string(f.data(), f.filesize())); + } +} + uint8_t* mock_copy_bytes(char* data, int size) { uint8_t* cp = new uint8_t[size];