Skip to content

Commit

Permalink
Remove unnecessary calloc and fix crash caused by pointer reference i…
Browse files Browse the repository at this point in the history
…n jsonRpc_call.
  • Loading branch information
PengZheng committed Jan 22, 2024
1 parent 6223856 commit ba398d3
Show file tree
Hide file tree
Showing 4 changed files with 324 additions and 277 deletions.
13 changes: 13 additions & 0 deletions libs/dfi/gtest/descriptors/example7.descriptor
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
:header
type=interface
name=calculator
version=1.0.0
:annotations
classname=org.example.Calculator
:types
StatsResult={DDD[D average min max input}
:methods
add(DD)D=add(#am=handle;PDD#am=pre;TOut=*D;lOut;)N
sub(DD)D=sub(#am=handle;PDD*#am=pre;D)N
sqrt(D)D=sqrt(#am=handle;PD*#am=pre;D)N
stats([D)LStatsResult;=stats(#am=handle;P[D#am=out;TOut=*LStatsResult;;lOut;)N
56 changes: 56 additions & 0 deletions libs/dfi/gtest/src/json_rpc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,27 @@ extern "C" {
tst_serv serv {nullptr, addFailed, nullptr, nullptr, nullptr};

rc = jsonRpc_call(intf, &serv, R"({)", &result);
EXPECT_STREQ("Got json error: string or '}' expected near end of file", celix_err_popLastError());
ASSERT_EQ(1, rc);

rc = jsonRpc_call(intf, &serv, R"({"a": [1.0,2.0]})", &result);
EXPECT_STREQ("Error getting method signature", celix_err_popLastError());
ASSERT_EQ(1, rc);

//request missing argument
rc = jsonRpc_call(intf, &serv, R"({"m":"stats([D)LStatsResult;"})", &result);
EXPECT_STREQ("Error getting arguments array", celix_err_popLastError());
ASSERT_EQ(1, rc);

//request non-array argument
rc = jsonRpc_call(intf, &serv, R"({"m":"stats([D)LStatsResult;", "a": "hello"})", &result);
EXPECT_STREQ("Error getting arguments array", celix_err_popLastError());
ASSERT_EQ(1, rc);

// argument number mismatch
//rc = jsonRpc_call(intf, &serv, R"({"m":"stats([D)LStatsResult;", "a": []})", &result);
//ASSERT_EQ(1, rc);

//request argument type mismatch
rc = jsonRpc_call(intf, &serv, R"({"m":"stats([D)LStatsResult;", "a": [1.0]})", &result);
ASSERT_EQ(1, rc);
Expand Down Expand Up @@ -480,6 +496,7 @@ extern "C" {

rc = jsonRpc_call(intf, &serv, R"({"m":"unknown", "a": [1.0,2.0]})", &result);
ASSERT_EQ(1, rc);
EXPECT_STREQ("Cannot find method with sig 'unknown'", celix_err_popLastError());

dynInterface_destroy(intf);
}
Expand Down Expand Up @@ -785,6 +802,7 @@ class JsonRpcTests : public ::testing::Test {

}
~JsonRpcTests() override {
celix_err_resetErrors();
}

};
Expand Down Expand Up @@ -817,6 +835,25 @@ TEST_F(JsonRpcTests, handleTestOut) {
handleTestOut();
}

TEST_F(JsonRpcTests, callPreReference) {
dyn_interface_type *intf = nullptr;
FILE *desc = fopen("descriptors/example7.descriptor", "r");
ASSERT_TRUE(desc != nullptr);
int rc = dynInterface_parse(desc, &intf);
ASSERT_EQ(0, rc);
fclose(desc);

char *result = nullptr;
tst_serv serv {nullptr, add, nullptr, nullptr, nullptr};

rc = jsonRpc_call(intf, &serv, R"({"m":"add(DD)D", "a": [1.0,2.0]})", &result);
ASSERT_EQ(0, rc);
ASSERT_TRUE(strstr(result, "3.0") != nullptr);

free(result);
dynInterface_destroy(intf);
}

TEST_F(JsonRpcTests, callPre) {
callTestPreAllocated();
}
Expand All @@ -829,6 +866,25 @@ TEST_F(JsonRpcTests, callOut) {
callTestOutput();
}

TEST_F(JsonRpcTests, callOutReference) {
dyn_interface_type *intf = nullptr;
FILE *desc = fopen("descriptors/example7.descriptor", "r");
ASSERT_TRUE(desc != nullptr);
int rc = dynInterface_parse(desc, &intf);
ASSERT_EQ(0, rc);
fclose(desc);

char *result = nullptr;
tst_serv serv {nullptr, nullptr, nullptr, nullptr, stats};

rc = jsonRpc_call(intf, &serv, R"({"m":"stats([D)LStatsResult;", "a": [[1.0,2.0]]})", &result);
ASSERT_EQ(0, rc);
ASSERT_TRUE(strstr(result, "1.5") != nullptr);

free(result);
dynInterface_destroy(intf);
}

TEST_F(JsonRpcTests, callTestInvalidRequest) {
callTestInvalidRequest();
}
Expand Down
6 changes: 3 additions & 3 deletions libs/dfi/include/json_rpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extern "C" {
* @return 0 if successful, otherwise 1.
*
*/
CELIX_DFI_EXPORT int jsonRpc_call(dyn_interface_type *intf, void *service, const char *request, char **out);
CELIX_DFI_EXPORT int jsonRpc_call(const dyn_interface_type* intf, void* service, const char* request, char** out);

/**
* @brief Prepare a JSON-RPC request for a given function.
Expand All @@ -60,7 +60,7 @@ CELIX_DFI_EXPORT int jsonRpc_call(dyn_interface_type *intf, void *service, const
* @return 0 if successful, otherwise 1.
*
*/
CELIX_DFI_EXPORT int jsonRpc_prepareInvokeRequest(dyn_function_type *func, const char *id, void *args[], char **out);
CELIX_DFI_EXPORT int jsonRpc_prepareInvokeRequest(const dyn_function_type* func, const char* id, void* args[], char** out);

/**
* @brief Handle a JSON-RPC reply for a given function.
Expand All @@ -74,7 +74,7 @@ CELIX_DFI_EXPORT int jsonRpc_prepareInvokeRequest(dyn_function_type *func, const
* @return 0 if successful, otherwise 1.
*
*/
CELIX_DFI_EXPORT int jsonRpc_handleReply(dyn_function_type *func, const char *reply, void *args[], int *rsErrno);
CELIX_DFI_EXPORT int jsonRpc_handleReply(const dyn_function_type* func, const char* reply, void* args[], int* rsErrno);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit ba398d3

Please sign in to comment.