Skip to content

Commit

Permalink
[irods#6810] Added server-side unit test for msp_to_string
Browse files Browse the repository at this point in the history
  • Loading branch information
FifthPotato authored and alanking committed Oct 30, 2023
1 parent 6f6c235 commit 8a3ff6c
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 12 deletions.
1 change: 1 addition & 0 deletions lib/core/include/irods/msParam.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ int
parseMspForPhyPathReg( msParam_t *inpParam, keyValPair_t *condInput );
int
parseMspForPosInt( msParam_t *inpParam );
int msp_to_string(msParam_t* _inp_param, char** _out_param);
char *
parseMspForStr( msParam_t *inpParam );
int
Expand Down
27 changes: 15 additions & 12 deletions lib/core/src/msParam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1022,29 +1022,32 @@ parseMspForPosInt( msParam_t* inpParam ) {
return myInt;
}

int msp_to_string(msParam_t* _inpParam, char** _outParam)
int msp_to_string(msParam_t* _inp_param, char** _out_param)
{
if (_outParam == nullptr) {
log_msi::error("{}: _outParam is null", __func__);
if (nullptr == _out_param) {
log_msi::error("{}: _out_param is null", __func__);
return SYS_NULL_INPUT;
}

if (_inpParam == nullptr || _inpParam->inOutStruct == nullptr) {
log_msi::error("{}: _inpParam is null or _inpParam's inOutStruct is null", __func__);
*_outParam = nullptr;
if (nullptr == _inp_param || nullptr == _inp_param->inOutStruct) {
log_msi::error("{}: _inp_param is null or _inpParam's inOutStruct is null", __func__);
return SYS_NULL_INPUT;
}

if (strcmp(_inpParam->type, STR_MS_T) != 0) {
log_msi::error("{}: _inpParam type {} is not STR_MS_T", __func__, _inpParam->type);
*_outParam = nullptr;
if (nullptr == _inp_param->type) {
log_msi::error("{}: _inp_param->type is null", __func__);
return SYS_NULL_INPUT;
}

if (strcmp(_inp_param->type, STR_MS_T) != 0) {
log_msi::error("{}: _inp_param type [{}] is not STR_MS_T", __func__, _inp_param->type);
return USER_PARAM_TYPE_ERR;
}

*_outParam = static_cast<char*>(_inpParam->inOutStruct);
*_out_param = static_cast<char*>(_inp_param->inOutStruct);

if (strcmp(*_outParam, "null") == 0) {
*_outParam = nullptr;
if (strcmp(*_out_param, "null") == 0) {
*_out_param = nullptr;
}

return 0;
Expand Down
1 change: 1 addition & 0 deletions plugins/microservices/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ set(
if (IRODS_MICROSERVICE_TEST_PLUGINS_BUILD OR IRODS_ENABLE_ALL_TESTS)
list(APPEND IRODS_MICROSERVICE_PLUGINS test_issue_6782)
list(APPEND IRODS_MICROSERVICE_PLUGINS test_issue_6829)
list(APPEND IRODS_MICROSERVICE_PLUGINS test_msp_functions)
list(APPEND IRODS_MICROSERVICE_PLUGINS test_scoped_client_identity)
list(APPEND IRODS_MICROSERVICE_PLUGINS test_scoped_permission)
list(APPEND IRODS_MICROSERVICE_PLUGINS test_user_administration)
Expand Down
115 changes: 115 additions & 0 deletions plugins/microservices/src/test_msp_functions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#include "irods/irods_ms_plugin.hpp"
#include "irods/msParam.h"

#include "msi_assertion_macros.hpp"

namespace
{
auto test_null_cases([[maybe_unused]] RuleExecInfo* _rei) -> int
{
IRODS_MSI_TEST_BEGIN("testing msp_to_string: msp_to_string null cases")

char* good = nullptr;
msParam_t in{};

auto ret = msp_to_string(nullptr, nullptr);
IRODS_MSI_ASSERT(SYS_NULL_INPUT == ret);

ret = msp_to_string(nullptr, &good);
IRODS_MSI_ASSERT((SYS_NULL_INPUT == ret));

in.inOutStruct = nullptr;
ret = msp_to_string(&in, &good);
IRODS_MSI_ASSERT((SYS_NULL_INPUT == ret));

//NOLINTNEXTLINE(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
in.inOutStruct = malloc(sizeof(int));
//NOLINTNEXTLINE(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
const auto cleanup = irods::at_scope_exit{[&] { free(in.inOutStruct); }};

ret = msp_to_string(&in, &good);
IRODS_MSI_ASSERT((SYS_NULL_INPUT == ret));

IRODS_MSI_TEST_END
} // test_null_cases

auto test_bad_param_case([[maybe_unused]] RuleExecInfo* _rei) -> int
{
IRODS_MSI_TEST_BEGIN("testing msp_to_string: msp_to_string bad param type")
char* good = nullptr;
msParam_t in{};
//NOLINTNEXTLINE(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
in.inOutStruct = malloc(sizeof(int));
in.type = strdup(INT_MS_T);

const auto cleanup = irods::at_scope_exit{[&] {
//NOLINTNEXTLINE(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
free(in.inOutStruct);
//NOLINTNEXTLINE(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
free(in.type);
}};

const auto ret = msp_to_string(&in, &good);
IRODS_MSI_ASSERT((USER_PARAM_TYPE_ERR == ret));

IRODS_MSI_TEST_END
} // test_bad_param_case

auto test_good_cases([[maybe_unused]] RuleExecInfo* _rei) -> int
{
IRODS_MSI_TEST_BEGIN("testing msp_to_string: msp_to_string valid cases")

msParam_t in{};
char* out = nullptr;
char* nullstr = strdup("null");
char* validstr = strdup("S. tuberosum");

in.inOutStruct = nullstr;
in.type = strdup(STR_MS_T);

const auto cleanup = irods::at_scope_exit{[&] {
//NOLINTNEXTLINE(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
free(nullstr);
//NOLINTNEXTLINE(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
free(in.type);
//NOLINTNEXTLINE(cppcoreguidelines-no-malloc,cppcoreguidelines-owning-memory)
free(validstr);
}};

auto ret = msp_to_string(&in, &out);

IRODS_MSI_ASSERT((0 == ret));
IRODS_MSI_ASSERT(nullptr == out);

in.inOutStruct = validstr;
ret = msp_to_string(&in, &out);

IRODS_MSI_ASSERT((0 == ret));
IRODS_MSI_ASSERT((strcmp(out, "S. tuberosum") == 0));

IRODS_MSI_TEST_END
} // test_good_cases

auto msi_impl([[maybe_unused]] RuleExecInfo* _rei) -> int
{
IRODS_MSI_TEST_CASE(test_null_cases, _rei);
IRODS_MSI_TEST_CASE(test_bad_param_case, _rei);
IRODS_MSI_TEST_CASE(test_good_cases, _rei);

return 0;
} // msi_impl

template <typename... Args, typename Function>
auto make_msi(const std::string& _name, Function _func) -> irods::ms_table_entry*
{
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
auto* msi = new irods::ms_table_entry{sizeof...(Args)};
msi->add_operation(_name, std::function<int(Args..., ruleExecInfo_t*)>(_func));
return msi;
} // make_msi
} // anonymous namespace

extern "C" auto plugin_factory() -> irods::ms_table_entry*
{
return make_msi<>("msi_test_msp_functions", msi_impl);
} // plugin_factory
4 changes: 4 additions & 0 deletions scripts/irods/test/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,10 @@ def test_scoped_client_identity_updates_zone_of_RsComm__issue_6268(self):
def test_scoped_permission__issue_7032(self):
self.admin.assert_icommand(['irule', '-r', 'irods_rule_engine_plugin-irods_rule_language-instance', 'msi_test_scoped_permission', 'null', 'ruleExecOut'])

@unittest.skipUnless(plugin_name == 'irods_rule_engine_plugin-irods_rule_language', "Not implemented for other REPs.")
def test_msp_functions(self):
self.admin.assert_icommand(['irule', '-r', 'irods_rule_engine_plugin-irods_rule_language-instance', 'msi_test_msp_functions', 'null', 'ruleExecOut'])

@unittest.skipUnless(plugin_name == 'irods_rule_engine_plugin-irods_rule_language', "Not implemented for other REPs.")
def test_user_administration__issue_7208(self):
self.admin.assert_icommand(['irule', '-r', 'irods_rule_engine_plugin-irods_rule_language-instance', 'msi_test_user_administration', 'null', 'ruleExecOut'])
Expand Down

0 comments on commit 8a3ff6c

Please sign in to comment.