Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Netstring feature #726

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class CelixConan(ConanFile):
"build_framework": False,
"build_rcm": False,
"build_utils": False,
"build_netstring": False,
"celix_cxx14": True,
"celix_cxx17": True,
"celix_install_deprecated_api": False,
Expand Down
6 changes: 6 additions & 0 deletions examples/conan_test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,10 @@ endif ()
option(TEST_COMPONENTS_READY_CHECK "Test the components.ready condition checking bundle" OFF)
if (TEST_COMPONENTS_READY_CHECK)
add_celix_container("use_components_ready_check" BUNDLES Celix::components_ready_check hello)
endif ()

option(TEST_CELIX_NETSTRING "Test Celix Netstring" OFF)
if (TEST_CELIX_NETSTRING)
add_executable(use_celix_netstring test_celix_netstring.c)
target_link_libraries(use_celix_netstring PRIVATE Celix::netstring)
endif ()
3 changes: 3 additions & 0 deletions examples/conan_test_package/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def build(self):
cmake.definitions["TEST_CELIX_DFI"] = self.options["celix"].build_celix_dfi
cmake.definitions["TEST_UTILS"] = self.options["celix"].build_utils
cmake.definitions["TEST_COMPONENTS_READY_CHECK"] = self.options["celix"].build_components_ready_check
cmake.definitions["TEST_CELIX_NETSTRING"] = self.options["celix"].build_netstring
cmake.definitions["CMAKE_PROJECT_test_package_INCLUDE"] = os.path.join(self.build_folder, "conan_paths.cmake")
# the following is workaround https://github.com/conan-io/conan/issues/7192
if self.settings.os == "Linux":
Expand Down Expand Up @@ -124,3 +125,5 @@ def test(self):
if self.options["celix"].build_components_ready_check:
self.run("./use_components_ready_check",
cwd=os.path.join("deploy", "use_components_ready_check"), run_environment=True)
if self.options["celix"].build_netstring:
self.run("./use_celix_netstring", run_environment=True)
33 changes: 33 additions & 0 deletions examples/conan_test_package/test_celix_netstring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#include <celix_netstring.h>
#include <stdio.h>
#include <string.h>
#include <stddef.h>

int main() {
const char *input = "Hello World";
size_t inputLen = strlen(input);

char buffer[128]={0};
size_t offset = 0;
int rc = celix_netstring_encodeb(input, inputLen, buffer, sizeof(buffer), &offset);
printf("hello netstring, rc = %i\n", rc);
return 0;
}
2 changes: 2 additions & 0 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ add_subdirectory(pushstreams)

add_subdirectory(framework)

add_subdirectory(netstring)

#launcher
add_subdirectory(launcher)

Expand Down
1 change: 1 addition & 0 deletions libs/error_injector/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ target_link_options(stdio_ei INTERFACE
LINKER:--wrap,fread
LINKER:--wrap,fputc
LINKER:--wrap,fputs
LINKER:--wrap,fprintf
LINKER:--wrap,fclose
LINKER:--wrap,fgetc
LINKER:--wrap,fmemopen
Expand Down
2 changes: 2 additions & 0 deletions libs/error_injector/stdio/include/stdio_ei.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ CELIX_EI_DECLARE(fputc, int);

CELIX_EI_DECLARE(fputs, int);

CELIX_EI_DECLARE(fprintf, int);

CELIX_EI_DECLARE(fclose, int);

CELIX_EI_DECLARE(fgetc, int);
Expand Down
16 changes: 16 additions & 0 deletions libs/error_injector/stdio/src/stdio_ei.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

#include <errno.h>
#include <stdarg.h>
#include "stdio_ei.h"

extern "C" {
Expand Down Expand Up @@ -87,7 +88,9 @@ size_t __wrap_fread(void* __restrict __ptr, size_t __size, size_t __n, FILE* __r
int __real_fputc(int __c, FILE* __stream);
CELIX_EI_DEFINE(fputc, int)
int __wrap_fputc(int __c, FILE* __stream) {
errno = ENOSPC;
CELIX_EI_IMPL(fputc);
errno = 0;
return __real_fputc(__c, __stream);
}

Expand All @@ -98,6 +101,19 @@ int __wrap_fputs(const char* __s, FILE* __stream) {
return __real_fputs(__s, __stream);
}

int __real_fprintf(FILE *stream, const char *format, ...);
CELIX_EI_DEFINE(fprintf, int)
int __wrap_fprintf(FILE *stream, const char *format, ...) {
errno = ENOSPC;
CELIX_EI_IMPL(fprintf);
errno = 0;
va_list args;
va_start(args, format);
int ret = vfprintf(stream, format, args);
va_end(args);
return ret;
}

int __real_fclose(FILE* __stream);
CELIX_EI_DEFINE(fclose, int)
int __wrap_fclose(FILE* __stream) {
Expand Down
64 changes: 64 additions & 0 deletions libs/netstring/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

celix_subproject(NETSTRING "Option to enable building the netstring" OFF)
if (NETSTRING)

set(NETSTRING_SRC
src/celix_netstring.c
)

add_library(netstring SHARED ${NETSTRING_SRC})

set_target_properties(netstring
PROPERTIES
VERSION 1.0.0
SOVERSION 1
OUTPUT_NAME "celix_netstring")
celix_target_hide_symbols(netstring)

target_include_directories(netstring PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/api>
)

generate_export_header(netstring
BASE_NAME "CELIX_NETSTRING"
EXPORT_FILE_NAME "${CMAKE_BINARY_DIR}/celix/gen/includes/netstring/celix_netstring_export.h")
target_include_directories(netstring PUBLIC $<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/celix/gen/includes/netstring>)

install(TARGETS netstring EXPORT celix LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT netstring
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/celix/netstring)
install(DIRECTORY api/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/celix/netstring/ COMPONENT netstring)
install(DIRECTORY ${CMAKE_BINARY_DIR}/celix/gen/includes/netstring/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/celix/netstring/
COMPONENT netstring)

#Alias setup to match external usage
add_library(Celix::netstring ALIAS netstring)

if (ENABLE_TESTING)
add_library(netstring_cut STATIC ${NETSTRING_SRC})
target_include_directories(netstring_cut PUBLIC
${CMAKE_CURRENT_LIST_DIR}/api
${CMAKE_BINARY_DIR}/celix/gen/includes/netstring
${CMAKE_BINARY_DIR}/celix/gen/src/netstring
)

add_subdirectory(gtest)

endif ()
endif ()
88 changes: 88 additions & 0 deletions libs/netstring/api/celix_netstring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#ifndef CELIX_CELIX_NETSTRING_H
#define CELIX_CELIX_NETSTRING_H
#ifdef __cplusplus
extern "C" {
#endif
#include "celix_netstring_export.h"
#include <stdio.h>
#include <stddef.h>

/**
* @brief Encode the given binary data to a netstring and write it to the given file.
* @param[in] bytes The binary data to encode
* @param[in] bytesSize The size of the binary data
* @param[out] netstringOut The file descriptor to write the netstring to
* @return returns 0 on success, or errno on failure.
* @section ERRORS
* - EINVAL Invalid argument
* - Other errors, see errno of fprintf, fwrite and fputc
*/
CELIX_NETSTRING_EXPORT
int celix_netstring_encodef(const char* bytes, size_t bytesSize, FILE* netstringOut);

/**
* @brief Encode the given binary data to a netstring and write it to the given buffer.
* @param[in] bytes The binary data to encode
* @param[in] bytesSize The size of the binary data
* @param[out] netstringBufferOut The buffer to write the netstring to
* @param[in] bufferSize The total size of the buffer
* @param[in,out] bufferOffsetInOut The offset in the buffer to write the netstring to. And Updated with the new offset after writing the netstring.
* @return returns 0 on success, or errno on failure.
* @section ERRORS
* - EINVAL Invalid argument
* - ENOMEM Not enough space in the buffer
*/
CELIX_NETSTRING_EXPORT
int celix_netstring_encodeb(const char* bytes, size_t bytesSize, char* netstringBufferOut, size_t bufferSize, size_t* bufferOffsetInOut);

/**
* @brief Decode the first section of the given netstring from the given file. And allocate a buffer with the binary data.
* @param[in] netstringIn The file descriptor to read the netstring from
* @param[out] bytes The binary data of netstring first section. The memory is allocated with malloc and the caller should release it with free.
* @param[out] bytesSize The size of the binary data
* @return returns 0 on success, or errno on failure.
* @section ERRORS
* - EINVAL Invalid argument or invalid netstring format
* - Other errors, see errno of fread and fgetc
*/
CELIX_NETSTRING_EXPORT
int celix_netstring_decodef(FILE* netstringIn, char** bytes, size_t* bytesSize);

/**
* @brief Decode the first section of the given netstring from the given buffer.
* @param[in] bufferIn The buffer to read the netstring from
* @param[in] bufferInSize The size of the buffer
* @param[out] bytes The binary data of netstring first section. It points to the memory of bufferIn. No memory is allocated.
* @param[out] bytesSize The size of the binary data
* @return returns 0 on success, or errno on failure.
* @section ERRORS
* - EINVAL Invalid argument or invalid netstring format
*/
CELIX_NETSTRING_EXPORT
int celix_netstring_decodeb(const char* bufferIn, size_t bufferInSize, const char** bytes, size_t* bytesSize);


#ifdef __cplusplus
}
#endif

#endif //CELIX_CELIX_NETSTRING_H
44 changes: 44 additions & 0 deletions libs/netstring/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

add_executable(test_celix_netstring src/CelixNetstringTestSuite.cc)

target_include_directories(test_celix_netstring PRIVATE src)

target_link_libraries(test_celix_netstring PRIVATE netstring_cut GTest::gtest GTest::gtest_main)

add_test(NAME run_test_celix_netstring COMMAND test_celix_netstring)
setup_target_for_coverage(test_celix_netstring SCAN_DIR ..)

if (EI_TESTS)

add_executable(test_celix_netstring_with_ei src/CelixNetstringErrorInjectionTestSuite.cc)

target_include_directories(test_celix_netstring_with_ei PRIVATE src)

target_link_libraries(test_celix_netstring_with_ei
PRIVATE
netstring_cut
Celix::stdio_ei
Celix::malloc_ei
GTest::gtest
GTest::gtest_main)

add_test(NAME run_test_celix_netstring_with_ei COMMAND test_celix_netstring_with_ei)
setup_target_for_coverage(test_celix_netstring_with_ei SCAN_DIR ..)

endif ()
Loading
Loading