Skip to content

Commit

Permalink
Support include empty config file.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Aug 22, 2023
1 parent fe38804 commit a6f0a31
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 10 deletions.
2 changes: 1 addition & 1 deletion trunk/configure
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ fi
if [[ $SRS_UTEST == YES ]]; then
MODULE_FILES=("srs_utest" "srs_utest_amf0" "srs_utest_kernel" "srs_utest_core"
"srs_utest_config" "srs_utest_rtmp" "srs_utest_http" "srs_utest_avc" "srs_utest_reload"
"srs_utest_mp4" "srs_utest_service" "srs_utest_app" "srs_utest_rtc"
"srs_utest_mp4" "srs_utest_service" "srs_utest_app" "srs_utest_rtc" "srs_utest_config2"
"srs_utest_protocol" "srs_utest_protocol2" "srs_utest_kernel2")
if [[ $SRS_SRT == YES ]]; then
MODULE_FILES+=("srs_utest_srt")
Expand Down
11 changes: 8 additions & 3 deletions trunk/src/app/srs_app_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ namespace srs_internal

// read all.
int filesize = (int)reader.filesize();
// Ignore if empty file.
if (filesize <= 0) return err;

// create buffer
srs_freepa(start);
Expand Down Expand Up @@ -1068,6 +1070,9 @@ SrsJsonAny* SrsConfDirective::dumps_arg0_to_boolean()
srs_error_t SrsConfDirective::parse_conf(SrsConfigBuffer* buffer, SrsDirectiveContext ctx, SrsConfig* conf)
{
srs_error_t err = srs_success;

// Ignore empty config file.
if (ctx == SrsDirectiveContextFile && buffer->empty()) return err;

while (true) {
std::vector<string> args;
Expand Down Expand Up @@ -1127,7 +1132,7 @@ srs_error_t SrsConfDirective::parse_conf(SrsConfigBuffer* buffer, SrsDirectiveCo
}

if ((err = parse_conf(include_file_buffer, SrsDirectiveContextFile, conf)) != srs_success) {
return srs_error_wrap(err, "parse include buffer");
return srs_error_wrap(err, "parse include buffer %s", file.c_str());
}
}
}
Expand Down Expand Up @@ -2197,11 +2202,11 @@ srs_error_t SrsConfig::parse_file(const char* filename)
SrsConfigBuffer* buffer = NULL;
SrsAutoFree(SrsConfigBuffer, buffer);
if ((err = build_buffer(config_file, &buffer)) != srs_success) {
return srs_error_wrap(err, "buffer fullfill %s", config_file.c_str());
return srs_error_wrap(err, "buffer fullfill %s", filename);
}

if ((err = parse_buffer(buffer)) != srs_success) {
return srs_error_wrap(err, "parse buffer");
return srs_error_wrap(err, "parse buffer %s", filename);
}

return err;
Expand Down
35 changes: 29 additions & 6 deletions trunk/src/utest/srs_utest_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,14 @@ srs_error_t MockSrsConfig::build_buffer(std::string src, srs_internal::SrsConfig
{
srs_error_t err = srs_success;

string content = included_files[src];
if(content.empty()) {
// No file, error.
if(included_files.find(src) == included_files.end()) {
return srs_error_new(ERROR_SYSTEM_CONFIG_INVALID, "file %s: no found", src.c_str());
}

string content = included_files[src];

// Empty file, ok.
*pbuffer = new MockSrsConfigBuffer(content);

return err;
Expand Down Expand Up @@ -689,10 +692,30 @@ VOID TEST(ConfigDirectiveTest, ParseInvalidNoEndOfDirective)
VOID TEST(ConfigDirectiveTest, ParseInvalidNoEndOfSubDirective)
{
srs_error_t err;

MockSrsConfigBuffer buf("dir0 {");
SrsConfDirective conf;
HELPER_ASSERT_FAILED(conf.parse(&buf));

if (true) {
MockSrsConfigBuffer buf("");
SrsConfDirective conf;
HELPER_ASSERT_SUCCESS(conf.parse(&buf));
}

if (true) {
MockSrsConfigBuffer buf("# OK");
SrsConfDirective conf;
HELPER_ASSERT_SUCCESS(conf.parse(&buf));
}

if (true) {
MockSrsConfigBuffer buf("dir0 {");
SrsConfDirective conf;
HELPER_ASSERT_FAILED(conf.parse(&buf));
}

if (true) {
MockSrsConfigBuffer buf("dir0 {} dir1 {");
SrsConfDirective conf;
HELPER_ASSERT_FAILED(conf.parse(&buf));
}
}

VOID TEST(ConfigDirectiveTest, ParseInvalidNoStartOfSubDirective)
Expand Down
47 changes: 47 additions & 0 deletions trunk/src/utest/srs_utest_config2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//
// Copyright (c) 2013-2023 The SRS Authors
//
// SPDX-License-Identifier: MIT or MulanPSL-2.0
//
#include <srs_utest_config2.hpp>

#include <srs_kernel_error.hpp>
#include <srs_kernel_file.hpp>
#include <srs_utest_kernel.hpp>

VOID TEST(ConfigMainTest, CheckIncludeEmptyConfig)
{
srs_error_t err;

if (true) {
string filepath = _srs_tmp_file_prefix + "utest-main.conf";
MockFileRemover _mfr(filepath);

string included = _srs_tmp_file_prefix + "utest-included-empty.conf";
MockFileRemover _mfr2(included);

if (true) {
SrsFileWriter fw;
fw.open(included);
}

if (true) {
SrsFileWriter fw;
fw.open(filepath);
string content = _MIN_OK_CONF "include " + included + ";";
fw.write((void*)content.data(), (int)content.length(), NULL);
}

SrsConfig conf;
HELPER_ASSERT_SUCCESS(conf.parse_file(filepath.c_str()));
EXPECT_EQ(1, (int)conf.get_listens().size());
}

if (true) {
MockSrsConfig conf;
conf.mock_include("test.conf", "");
HELPER_ASSERT_SUCCESS(conf.parse(_MIN_OK_CONF "include test.conf;"));
EXPECT_EQ(1, (int)conf.get_listens().size());
}
}

16 changes: 16 additions & 0 deletions trunk/src/utest/srs_utest_config2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// Copyright (c) 2013-2023 The SRS Authors
//
// SPDX-License-Identifier: MIT or MulanPSL-2.0
//

#ifndef SRS_UTEST_CONFIG2_HPP
#define SRS_UTEST_CONFIG2_HPP

/*
#include <srs_utest_config2.hpp>
*/
#include <srs_utest_config.hpp>

#endif

0 comments on commit a6f0a31

Please sign in to comment.