Skip to content
Merged
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
52 changes: 43 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,34 @@ if(PROJECT_IS_TOP_LEVEL)
fetchcontent_makeavailable(${dep_name})
endforeach()

set(UNITY_CONFIG_INCLUDE_DIR
${CMAKE_CURRENT_SOURCE_DIR}/unity_config/include/unity)
set(UNITY_CONFIG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/unity/unity_config)
add_subdirectory(${unity_SOURCE_DIR} unity)
target_include_directories(
unity PUBLIC $<BUILD_INTERFACE:${UNITY_CONFIG_INCLUDE_DIR}>
$<INSTALL_INTERFACE:unity_config/include/unity>)
file(GLOB_RECURSE UNITY_CFG_SRCS CONFIGURE_DEPENDS "unity_config/*.c")
$<INSTALL_INTERFACE:unity/unity_config>)

file(GLOB_RECURSE GG_TEST_SRCS CONFIGURE_DEPENDS "unity/gg_test/*.c")
add_library(gg-test ${GG_TEST_SRCS})

target_include_directories(gg-test SYSTEM INTERFACE unity/gg_test/include)
target_include_directories(gg-test PRIVATE unity/gg_test/include
priv_include)
target_compile_definitions(gg-test PRIVATE "GG_MODULE=(\"gg-test\")")

target_link_libraries(gg-test gg-sdk unity)

file(GLOB_RECURSE UNITY_CFG_SRCS CONFIGURE_DEPENDS "unity/unity_config/*.c")
add_library(unity-config STATIC ${UNITY_CFG_SRCS})
target_include_directories(unity-config PUBLIC unity_config/include/unity)
target_include_directories(unity-config SYSTEM
INTERFACE unity_config/include)
target_include_directories(unity-config PRIVATE priv_include
unity_config/include)
target_compile_definitions(unity PUBLIC "UNITY_INCLUDE_CONFIG_H=1")
target_compile_definitions(unity-config PUBLIC "UNITY_INCLUDE_CONFIG_H=1")
target_link_libraries(unity-config PRIVATE unity gg-sdk gg-ipc-mock)
target_compile_definitions(unity-config PRIVATE "GG_MODULE=(\"gg-unity\")")
target_link_libraries(unity-config PRIVATE unity gg-sdk)

set(aws-greengrass-component-sdk_C_TEST_DIR
${CMAKE_CURRENT_SOURCE_DIR}/test)
endif()

# Put outputs in build/bin and build/lib
Expand Down Expand Up @@ -277,6 +289,9 @@ if(PROJECT_IS_TOP_LEVEL)
endif()
get_filename_component(test_name ${test_dir} NAME_WLE)
file(GLOB_RECURSE TEST_SRCS CONFIGURE_DEPENDS ${test_dir}/*.c)
if(NOT "${test_dir}/main.c" IN_LIST TEST_SRCS)
list(APPEND TEST_SRCS test/main_ipc_overrides.c)
endif()
foreach(src ${TEST_SRCS})
set_property(
SOURCE ${src}
Expand All @@ -285,13 +300,32 @@ if(PROJECT_IS_TOP_LEVEL)
endforeach()
add_executable(c_${test_name}_tests ${TEST_SRCS})
target_link_libraries(
c_${test_name}_tests PRIVATE gg-sdk++ gg-sdk gg-ipc-mock unity-config
unity)
c_${test_name}_tests PRIVATE gg-sdk gg-ipc-mock unity-config unity
gg-test)
install(TARGETS c_${test_name}_tests)
add_test(c_${test_name}_tests
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/c_${test_name}_tests)
endforeach()

set_target_properties(gg-sdk PROPERTIES EXPORT_COMPILE_COMMANDS OFF)

add_executable(gg-sdk-test ${SRCS} test/main_no_overrides.c)
target_compile_options(gg-sdk-test PRIVATE -pthread -fno-strict-aliasing
-std=gnu11 -Wno-missing-braces)
target_compile_definitions(gg-sdk-test PRIVATE _GNU_SOURCE)
target_include_directories(gg-sdk-test PRIVATE include priv_include)
target_include_directories(gg-sdk-test SYSTEM INTERFACE include)

target_compile_definitions(gg-sdk-test PRIVATE "GG_MODULE=(\"gg-sdk\")")

target_compile_definitions(gg-sdk-test
PUBLIC GG_LOG_LEVEL=GG_LOG_${choose_level})
target_compile_definitions(gg-sdk-test PUBLIC GG_TESTING)

target_link_libraries(gg-sdk-test PRIVATE unity-config unity gg-test)
add_test(gg-sdk-test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/gg-sdk-test)
endif()

endif()

if(BUILD_CPP)
Expand Down
8 changes: 6 additions & 2 deletions cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ if(aws-greengrass-component-sdk_IS_TOP_LEVEL)
endif()
get_filename_component(test_name ${test_dir} NAME_WLE)
file(GLOB_RECURSE TEST_SRCS CONFIGURE_DEPENDS ${test_dir}/*.cpp)
if(NOT "${test_dir}/main.cpp" IN_LIST TEST_SRCS)
list(APPEND TEST_SRCS
${aws-greengrass-component-sdk_C_TEST_DIR}/main_ipc_overrides.c)
endif()
foreach(src ${TEST_SRCS})
set_property(
SOURCE ${src}
Expand All @@ -71,8 +75,8 @@ if(aws-greengrass-component-sdk_IS_TOP_LEVEL)
endforeach()
add_executable(cpp_${test_name}_tests ${TEST_SRCS})
target_link_libraries(
cpp_${test_name}_tests PRIVATE gg-sdk++ gg-sdk gg-ipc-mock unity-config
unity)
cpp_${test_name}_tests PRIVATE gg-sdk++ gg-sdk gg-ipc-mock gg-test
unity-config unity)
install(TARGETS cpp_${test_name}_tests)
add_test(cpp_${test_name}_tests
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/cpp_${test_name}_tests)
Expand Down
23 changes: 8 additions & 15 deletions cpp/test/client/connect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
extern "C" {
#include <gg/ipc/mock.h>
#include <gg/ipc/packet_sequences.h>
#include <gg/process_wait.h>
#include <gg/test.h>
#include <sys/types.h>
#include <unistd.h>
#include <unity.h>
}

#define GG_TEST_ASSERT_OK(expr) TEST_ASSERT_EQUAL(GG_ERR_OK, (expr))
#define GG_TEST_ASSERT_BAD(expr) TEST_ASSERT_NOT_EQUAL(GG_ERR_OK, (expr))
GgError gg_process_wait(pid_t pid) noexcept;
}

namespace {
std::string_view as_view(gg::Buffer buf) noexcept {
Expand All @@ -39,9 +38,7 @@ namespace tests {
TEST_PASS();
}

GG_TEST_ASSERT_OK(
gg_test_expect_packet_sequence(seq, 30, server_handle)
);
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 30));

GG_TEST_ASSERT_OK(gg_process_wait(pid));
}
Expand Down Expand Up @@ -76,11 +73,9 @@ namespace tests {
TEST_PASS();
}

GG_TEST_ASSERT_OK(gg_test_accept_client(1, server_handle));
GG_TEST_ASSERT_OK(gg_test_accept_client(1));

GG_TEST_ASSERT_OK(
gg_test_expect_packet_sequence(seq, 5, server_handle)
);
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 5));

GG_TEST_ASSERT_OK(gg_process_wait(pid));
}
Expand All @@ -101,12 +96,10 @@ namespace tests {
TEST_PASS();
}

GG_TEST_ASSERT_OK(
gg_test_expect_packet_sequence(seq, 30, server_handle)
);
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 30));

/// TODO: verify Classic behavior
GG_TEST_ASSERT_OK(gg_test_disconnect(server_handle));
GG_TEST_ASSERT_OK(gg_test_disconnect());

GG_TEST_ASSERT_OK(gg_process_wait(pid));
}
Expand Down
7 changes: 0 additions & 7 deletions cpp/test/client/main.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
./mock
./samples
./test
./unity_config
./unity
./cpp/CMakeLists.txt
./cpp/include
./cpp/priv_include
Expand Down
19 changes: 9 additions & 10 deletions mock/gg/ipc/mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ typedef enum {
/// containing the socket path and setting appropriate IPC environment
/// variables. The IPC mock will not accept connections until
/// gg_test_expect_packet_sequence() is called.
NULL_TERMINATED_STRING_ARG(1) NONNULL(3) NULL_TERMINATED_STRING_ARG(4)
NULL_TERMINATED_STRING_ARG(1) NULL_TERMINATED_STRING_ARG(3)
GgError gg_test_setup_ipc(
const char *socket_path_prefix,
mode_t mode,
int *handle,
const char *auth_token
const char *socket_path_prefix, mode_t mode, const char *auth_token
);

/// Retrieves the socket path created by the IPC mock.
Expand Down Expand Up @@ -70,23 +67,25 @@ typedef struct {

/// Blocks until client process connects.
/// The client timeout is the time in seconds to wait for
GgError gg_test_accept_client(int client_timeout, int handle);
GgError gg_test_accept_client(int client_timeout);

/// Blocks until all packets in the packet sequence are validated or until any
/// are invalidated. Returns GG_ERR_OK if all packets were successfully
/// sent/received. The client timeout is the time in seconds to wait for the
/// next client packet before failing with GG_ERR_TIMEOUT.
GgError gg_test_expect_packet_sequence(
GgipcPacketSequence sequence, int client_timeout, int handle
GgipcPacketSequence sequence, int client_timeout
);

/// Hangs up on the client
GgError gg_test_disconnect(int handle);
GgError gg_test_disconnect(void);

/// Blocks until client closes socket. If any data is received on the socket, or
/// if the socket is not closed inside the timeout, returns a failure.
GgError gg_test_wait_for_client_disconnect(int client_timeout, int handle);
GgError gg_test_wait_for_client_disconnect(int client_timeout);

void gg_test_close(int handle);
/// Closes all file descriptors opened by the mock without waiting for client
/// disconnect.
void gg_test_close(void);

#endif
10 changes: 5 additions & 5 deletions mock/gg/ipc/mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ typedef enum : int8_t {
/// Sets up the IPC mock for this thread, opening a socket at path with mode,
/// and setting IPC environment variables. The IPC mock will not accept
/// connections until gg_test_expect_packet_sequence() is called.
NULL_TERMINATED_STRING_ARG(1) NONNULL(3) NULL_TERMINATED_STRING_ARG(4)
NULL_TERMINATED_STRING_ARG(1) NULL_TERMINATED_STRING_ARG(3)
GgError gg_test_setup_ipc(
const char *path, mode_t mode, int *handle, const char *auth_token
const char *path, mode_t mode, const char *auth_token
) noexcept;

typedef struct {
Expand All @@ -44,13 +44,13 @@ typedef struct {
/// sent/received. The client timeout is the time in seconds to wait for the
/// next client packet before failing with GG_ERR_TIMEOUT.
GgError gg_test_expect_packet_sequence(
GgipcPacketSequence sequence, int client_timeout, int handle
GgipcPacketSequence sequence, int client_timeout
) noexcept;

/// Hangs up on the client
GgError gg_test_disconnect(int handle) noexcept;
GgError gg_test_disconnect(void) noexcept;

void gg_test_close(int handle) noexcept;
void gg_test_close(void) noexcept;
}

#endif
27 changes: 8 additions & 19 deletions mock/mock.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ static GgError gg_socket_open(GgBuffer path, mode_t mode, int *socket_fd) {
}

GgError gg_test_setup_ipc(
const char *socket_path_prefix,
mode_t mode,
int *handle,
const char *auth_token
const char *socket_path_prefix, mode_t mode, const char *auth_token
) {
GgError ret = GG_ERR_OK;
GgByteVec auth_vec = gg_byte_vec_init(GG_BUF(ipc_auth_token));
Expand Down Expand Up @@ -208,8 +205,6 @@ GgError gg_test_setup_ipc(
cleanup_epollfd = -1;
sock_fd = socket_fd;

*handle = 1;

// test setup code should be called before creating any threads
// NOLINTBEGIN(concurrency-mt-unsafe)
int setenv_ret = setenv(
Expand Down Expand Up @@ -437,8 +432,7 @@ static GgError gg_test_recv_packet(const GgipcPacket *packet, int client) {
return GG_ERR_OK;
}

GgError gg_test_accept_client(int client_timeout, int handle) {
assert(handle > 0);
GgError gg_test_accept_client(int client_timeout) {
if (client_timeout <= 0) {
client_timeout = 5;
}
Expand Down Expand Up @@ -479,16 +473,14 @@ GgError gg_test_accept_client(int client_timeout, int handle) {
}

GgError gg_test_expect_packet_sequence(
GgipcPacketSequence sequence, int client_timeout, int handle
GgipcPacketSequence sequence, int client_timeout
) {
assert(handle > 0);

if (client_timeout <= 0) {
client_timeout = 5;
}

if (client_fd < 0) {
GgError ret = gg_test_accept_client(client_timeout, handle);
GgError ret = gg_test_accept_client(client_timeout);
if (ret != GG_ERR_OK) {
return ret;
}
Expand Down Expand Up @@ -518,8 +510,7 @@ GgError gg_test_expect_packet_sequence(
return GG_ERR_OK;
}

GgError gg_test_disconnect(int handle) {
assert(handle > 0);
GgError gg_test_disconnect(void) {
if (client_fd < 0) {
return GG_ERR_NOENTRY;
}
Expand All @@ -529,8 +520,7 @@ GgError gg_test_disconnect(int handle) {
return gg_close(old_client_fd);
}

GgError gg_test_wait_for_client_disconnect(int client_timeout, int handle) {
assert(handle > 0);
GgError gg_test_wait_for_client_disconnect(int client_timeout) {
if (client_fd < 0) {
return GG_ERR_NOENTRY;
}
Expand All @@ -552,7 +542,7 @@ GgError gg_test_wait_for_client_disconnect(int client_timeout, int handle) {
return GG_ERR_FAILURE;
}

return gg_test_disconnect(handle);
return gg_test_disconnect();
}

static void remove_temp_files(void) {
Expand All @@ -578,8 +568,7 @@ static void remove_temp_files(void) {
ipc_socket_path_buf = (GgBuffer) { 0 };
}

void gg_test_close(int handle) {
assert(handle > 0);
void gg_test_close(void) {
if (client_fd >= 0) {
(void) gg_close(client_fd);
client_fd = -1;
Expand Down
Loading