Skip to content

Commit

Permalink
Code deduplication and coverage improvements for dyn_common.
Browse files Browse the repository at this point in the history
Also remove dync_common from public interface of dfi.
  • Loading branch information
PengZheng committed Dec 15, 2023
1 parent ad1fa88 commit cced65c
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "dfi_utils.h"
#include "remote_interceptors_handler.h"

#include <string.h>

struct export_reference {
endpoint_description_t *endpoint; //owner
service_reference_pt reference;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <sys/uio.h>
#include <jansson.h>
#include <assert.h>
#include <string.h>

struct rsa_json_rpc_endpoint {
celix_bundle_context_t* ctx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>

struct rsa_json_rpc {
celix_bundle_context_t *ctx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
#include "celix_build_assert.h"
#include "celix_long_hash_map.h"
#include <sys/queue.h>
#include <stdbool.h>
#include <assert.h>
#include <stdbool.h>
#include <string.h>

struct rsa_json_rpc_proxy_factory {
celix_bundle_context_t* ctx;
Expand Down
4 changes: 3 additions & 1 deletion libs/dfi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ if (CELIX_DFI)
add_library(dfi SHARED ${SOURCES})
target_include_directories(dfi PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>
$<INSTALL_INTERFACE:include/celix/dfi>)
$<INSTALL_INTERFACE:include/celix/dfi>
PRIVATE ${CMAKE_CURRENT_LIST_DIR}/src)
target_link_libraries(dfi PRIVATE libffi::libffi)
target_link_libraries(dfi PUBLIC jansson::jansson Celix::utils)##The public header file(dyn_interface.h) Celix::utils(celix_version.h)
set_target_properties(dfi PROPERTIES
Expand Down Expand Up @@ -63,6 +64,7 @@ if (CELIX_DFI)
add_library(dfi_cut STATIC ${SOURCES})
target_compile_definitions(dfi_cut PUBLIC CELIX_DFI_STATIC_DEFINE)
target_include_directories(dfi_cut PUBLIC
${CMAKE_CURRENT_LIST_DIR}/src
${CMAKE_CURRENT_LIST_DIR}/include
${CMAKE_BINARY_DIR}/celix/gen/includes/dfi
)
Expand Down
3 changes: 2 additions & 1 deletion libs/dfi/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ add_executable(test_dfi


target_include_directories(test_dfi PRIVATE ${CMAKE_CURRENT_LIST_DIR}/../src)
target_link_libraries(test_dfi PRIVATE Celix::dfi Celix::utils libffi::libffi jansson::jansson GTest::gtest GTest::gtest_main)
target_link_libraries(test_dfi PRIVATE dfi_cut Celix::utils libffi::libffi jansson::jansson GTest::gtest GTest::gtest_main)
celix_deprecated_utils_headers(test_dfi)

file(COPY ${CMAKE_CURRENT_LIST_DIR}/descriptors DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
Expand All @@ -44,6 +44,7 @@ if (EI_TESTS)
target_link_libraries(test_dfi_with_ei PRIVATE
dfi_cut
Celix::malloc_ei
Celix::stdio_ei
GTest::gtest GTest::gtest_main
)
add_test(NAME run_test_dfi_with_ei COMMAND test_dfi_with_ei)
Expand Down
17 changes: 16 additions & 1 deletion libs/dfi/gtest/src/dyn_interface_ei_tests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@
*/

#include "dyn_interface.h"
#include "malloc_ei.h"
#include "dyn_common.h"
#include "celix_err.h"
#include "celix_stdio_cleanup.h"
#include "malloc_ei.h"
#include "stdio_ei.h"

#include <errno.h>
#include <string.h>
#include <string>
#include <gtest/gtest.h>

class DynInterfaceErrorInjectionTestSuite : public ::testing::Test {
Expand All @@ -30,6 +35,7 @@ class DynInterfaceErrorInjectionTestSuite : public ::testing::Test {

~DynInterfaceErrorInjectionTestSuite() override {
celix_ei_expect_calloc(nullptr, 0, nullptr);
celix_ei_expect_open_memstream(nullptr, 0, nullptr);
}
};

Expand Down Expand Up @@ -65,4 +71,13 @@ TEST_F(DynInterfaceErrorInjectionTestSuite, ParseError) {
status = dynInterface_parse(desc, &dynIntf);
ASSERT_NE(0, status);
ASSERT_STREQ("Error allocating memory for method entry", celix_err_popLastError());

rewind(desc);
// not enough memory for open_memstream
celix_ei_expect_open_memstream((void*) dynCommon_parseNameAlsoAccept, 0, nullptr);
status = dynInterface_parse(desc, &dynIntf);
ASSERT_NE(0, status);
std::string msg = "Error creating mem stream for name. ";
msg += strerror(ENOMEM);
ASSERT_STREQ(msg.c_str(), celix_err_popLastError());
}
1 change: 0 additions & 1 deletion libs/dfi/include/dyn_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#ifndef __DYN_INTERFACE_H_
#define __DYN_INTERFACE_H_

#include "dyn_common.h"
#include "dyn_type.h"
#include "dyn_function.h"
#include "celix_cleanup.h"
Expand Down
1 change: 0 additions & 1 deletion libs/dfi/include/dyn_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#ifndef __DYN_MESSAGE_H_
#define __DYN_MESSAGE_H_

#include "dyn_common.h"
#include "dyn_type.h"
#include "celix_version.h"
#include "celix_dfi_export.h"
Expand Down
98 changes: 56 additions & 42 deletions libs/dfi/src/dyn_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "dyn_common.h"
#include "celix_err.h"
#include "celix_stdlib_cleanup.h"

#include <stdlib.h>
#include <stdio.h>
Expand All @@ -28,7 +29,6 @@
static const int OK = 0;
static const int ERROR = 1;

static bool dynCommon_charIn(int c, const char *acceptedChars);

int dynCommon_parseName(FILE *stream, char **result) {
return dynCommon_parseNameAlsoAccept(stream, NULL, result);
Expand All @@ -41,21 +41,20 @@ int dynCommon_parseNameAlsoAccept(FILE *stream, const char *acceptedChars, char
size_t size = 0;
int strLen = 0;
FILE *name = open_memstream(&buf, &size);

if (name != NULL) {
int c = getc(stream);
while (isalnum(c) || c == '_' || dynCommon_charIn(c, acceptedChars)) {
fputc(c, name);
c = getc(stream);
strLen += 1;
}
fflush(name);
fclose(name);
ungetc(c, stream);
} else {
status = ERROR;
if (name == NULL) {
celix_err_pushf("Error creating mem stream for name. %s", strerror(errno));
return ERROR;
}

int c = getc(stream);
while (isalnum(c) || c == '_' || (acceptedChars != NULL && strchr(acceptedChars, c) != NULL)) {
fputc(c, name);
c = getc(stream);
strLen += 1;
}
fflush(name);
fclose(name);
ungetc(c, stream);

if (status == OK) {
if (strLen == 0) {
Expand All @@ -75,8 +74,8 @@ int dynCommon_parseNameAlsoAccept(FILE *stream, const char *acceptedChars, char

int dynCommon_parseNameValue(FILE *stream, char **outName, char **outValue) {
int status;
char *name = NULL;
char *value = NULL;
celix_autofree char *name = NULL;
celix_autofree char *value = NULL;

status = dynCommon_parseName(stream, &name);
if (status == OK) {
Expand All @@ -89,45 +88,22 @@ int dynCommon_parseNameValue(FILE *stream, char **outName, char **outValue) {
}

if (status == OK) {
*outName = name;
*outValue = value;
} else {
if (name != NULL) {
free(name);
}
if (value != NULL) {
free(value);
}
*outName = celix_steal_ptr(name);
*outValue = celix_steal_ptr(value);
}
return status;
}

int dynCommon_eatChar(FILE *stream, int expected) {
int status = OK;
long loc = ftell(stream);
int c = fgetc(stream);
if (c != expected) {
status = ERROR;
celix_err_pushf("Error parsing, expected token '%c' got '%c' at position %li", expected, c, loc);
celix_err_pushf("Error parsing, expected token '%c' got '%c' at position %li", expected, c, ftell(stream));
}
return status;
}

static bool dynCommon_charIn(int c, const char *acceptedChars) {
bool status = false;
if (acceptedChars != NULL) {
int i;
for (i = 0; acceptedChars[i] != '\0'; i += 1) {
if (c == acceptedChars[i]) {
status = true;
break;
}
}
}

return status;
}

void dynCommon_clearNamValHead(struct namvals_head *head) {
struct namval_entry *entry = TAILQ_FIRST(head);
while (entry != NULL) {
Expand All @@ -143,3 +119,41 @@ void dynCommon_clearNamValHead(struct namvals_head *head) {
free(tmp);
}
}

int dynCommon_parseNameValueSection(FILE *stream, struct namvals_head *head) {
int status = OK;

int peek = fgetc(stream);
while (peek != ':' && peek != EOF) {
ungetc(peek, stream);

celix_autofree char *name = NULL;
celix_autofree char *value = NULL;
status = dynCommon_parseNameValue(stream, &name, &value);

if (status == OK) {
status = dynCommon_eatChar(stream, '\n');
}

struct namval_entry *entry = NULL;
if (status == OK) {
entry = calloc(1, sizeof(*entry));
if (entry != NULL) {
entry->name = celix_steal_ptr(name);
entry->value = celix_steal_ptr(value);
TAILQ_INSERT_TAIL(head, entry, entries);
} else {
status = ERROR;
celix_err_pushf("Error allocating memory for namval entry");
}
}

if (status != OK) {
break;
}
peek = fgetc(stream);
}
ungetc(peek, stream);

return status;
}
29 changes: 22 additions & 7 deletions libs/dfi/include/dyn_common.h → libs/dfi/src/dyn_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
#include <string.h>
#include <sys/queue.h>

#include "celix_dfi_export.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -51,7 +49,7 @@ struct namval_entry {
* @return 0 if successful, otherwise 1.
* @alsoseee dynCommon_parseNameAlsoAccept
*/
CELIX_DFI_EXPORT int dynCommon_parseName(FILE *stream, char **result);
int dynCommon_parseName(FILE *stream, char **result);

/**
* @brief Parse the name of dynamic type from the given stream.
Expand All @@ -66,7 +64,7 @@ CELIX_DFI_EXPORT int dynCommon_parseName(FILE *stream, char **result);
* @return 0 if successful, otherwise 1.
* @alsoseee dynCommon_parseName
*/
CELIX_DFI_EXPORT int dynCommon_parseNameAlsoAccept(FILE *stream, const char *acceptedChars, char **result);
int dynCommon_parseNameAlsoAccept(FILE *stream, const char *acceptedChars, char **result);

/**
* @brief Parse the name and value of a name-value pair from the given stream. The name is only allowed to contain [a-zA-Z0-9_].
Expand All @@ -80,7 +78,24 @@ CELIX_DFI_EXPORT int dynCommon_parseNameAlsoAccept(FILE *stream, const char *acc
* @param[out] value The value of the name-value pair.
* @return 0 if successful, otherwise 1.
*/
CELIX_DFI_EXPORT int dynCommon_parseNameValue(FILE *stream, char **name, char **value);
int dynCommon_parseNameValue(FILE *stream, char **name, char **value);

/**
* @brief Parses a section of name-value pairs from the given stream. The name is only allowed to contain [a-zA-Z0-9_].
*
* This function reads from the provided stream until it encounters a new section or EOF.
* Each name-value pair is added to the provided namvals_head structure.
*
* The caller is responsible for managing the memory of the namvals_head structure.
* Use `dynCommon_clearNamValHead` to clear the name-value pairs when they are no longer needed.
*
* In case of an error, an error message is added to celix_err.
*
* @param[in] stream The input stream.
* @param[out] head The namvals_head structure where the parsed name-value pairs will be stored.
* @return 0 if successful, otherwise 1.
*/
int dynCommon_parseNameValueSection(FILE *stream, struct namvals_head *head);

/**
* @brief Eat the given character from the given stream.
Expand All @@ -91,14 +106,14 @@ CELIX_DFI_EXPORT int dynCommon_parseNameValue(FILE *stream, char **name, char **
* @param[in] c The character to be eaten.
* @return 0 if successful, otherwise 1.
*/
CELIX_DFI_EXPORT int dynCommon_eatChar(FILE *stream, int c);
int dynCommon_eatChar(FILE *stream, int c);

/**
* @brief Clear the given name-value pairs.
*
* @param[in] head The name-value pairs to be cleared.
*/
CELIX_DFI_EXPORT void dynCommon_clearNamValHead(struct namvals_head *head);
void dynCommon_clearNamValHead(struct namvals_head *head);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit cced65c

Please sign in to comment.