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 15, 2023
1 parent fe38804 commit e94861a
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 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
5 changes: 5 additions & 0 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 (buffer->empty()) return err;

while (true) {
std::vector<string> args;
Expand Down
7 changes: 5 additions & 2 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
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 e94861a

Please sign in to comment.