Skip to content

Commit 862a35c

Browse files
committed
Make initializing IPC mock optional in unity
1 parent 5f45359 commit 862a35c

File tree

18 files changed

+139
-149
lines changed

18 files changed

+139
-149
lines changed

CMakeLists.txt

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,34 @@ if(PROJECT_IS_TOP_LEVEL)
4646
fetchcontent_makeavailable(${dep_name})
4747
endforeach()
4848

49-
set(UNITY_CONFIG_INCLUDE_DIR
50-
${CMAKE_CURRENT_SOURCE_DIR}/unity_config/include/unity)
49+
set(UNITY_CONFIG_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/unity/unity_config)
5150
add_subdirectory(${unity_SOURCE_DIR} unity)
5251
target_include_directories(
5352
unity PUBLIC $<BUILD_INTERFACE:${UNITY_CONFIG_INCLUDE_DIR}>
54-
$<INSTALL_INTERFACE:unity_config/include/unity>)
55-
file(GLOB_RECURSE UNITY_CFG_SRCS CONFIGURE_DEPENDS "unity_config/*.c")
53+
$<INSTALL_INTERFACE:unity/unity_config>)
54+
55+
file(GLOB_RECURSE GG_TEST_SRCS CONFIGURE_DEPENDS "unity/gg_test/*.c")
56+
add_library(gg-test ${GG_TEST_SRCS})
57+
58+
target_include_directories(gg-test SYSTEM INTERFACE unity/gg_test/include)
59+
target_include_directories(gg-test PRIVATE unity/gg_test/include
60+
priv_include)
61+
target_compile_definitions(gg-test PRIVATE "GG_MODULE=(\"gg-test\")")
62+
63+
target_link_libraries(gg-test gg-sdk unity)
64+
65+
file(GLOB_RECURSE UNITY_CFG_SRCS CONFIGURE_DEPENDS "unity/unity_config/*.c")
5666
add_library(unity-config STATIC ${UNITY_CFG_SRCS})
5767
target_include_directories(unity-config PUBLIC unity_config/include/unity)
58-
target_include_directories(unity-config SYSTEM
59-
INTERFACE unity_config/include)
6068
target_include_directories(unity-config PRIVATE priv_include
6169
unity_config/include)
6270
target_compile_definitions(unity PUBLIC "UNITY_INCLUDE_CONFIG_H=1")
6371
target_compile_definitions(unity-config PUBLIC "UNITY_INCLUDE_CONFIG_H=1")
64-
target_link_libraries(unity-config PRIVATE unity gg-sdk gg-ipc-mock)
72+
target_compile_definitions(unity-config PRIVATE "GG_MODULE=(\"gg-unity\")")
73+
target_link_libraries(unity-config PRIVATE unity gg-sdk)
74+
75+
set(aws-greengrass-component-sdk_C_TEST_DIR
76+
${CMAKE_CURRENT_SOURCE_DIR}/test)
6577
endif()
6678

6779
# Put outputs in build/bin and build/lib
@@ -277,30 +289,41 @@ if(PROJECT_IS_TOP_LEVEL)
277289
endif()
278290
get_filename_component(test_name ${test_dir} NAME_WLE)
279291
file(GLOB_RECURSE TEST_SRCS CONFIGURE_DEPENDS ${test_dir}/*.c)
292+
if(NOT "${test_dir}/main.c" IN_LIST TEST_SRCS)
293+
list(APPEND TEST_SRCS test/main_ipc_overrides.c)
294+
endif()
280295
foreach(src ${TEST_SRCS})
281296
set_property(
282297
SOURCE ${src}
283298
APPEND_STRING
284299
PROPERTY COMPILE_FLAGS "-frandom-seed=${src}")
285300
endforeach()
286-
if(NOT "${test_dir}/main.c" IN_LIST TEST_SRCS)
287-
list(APPEND TEST_SRCS test/default_main.c)
288-
endif()
289301
add_executable(c_${test_name}_tests ${TEST_SRCS})
290302
target_link_libraries(
291-
c_${test_name}_tests PRIVATE gg-sdk gg-ipc-mock unity-config
292-
unity)
303+
c_${test_name}_tests PRIVATE gg-sdk gg-ipc-mock unity-config unity
304+
gg-test)
293305
install(TARGETS c_${test_name}_tests)
294306
add_test(c_${test_name}_tests
295307
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/c_${test_name}_tests)
296308
endforeach()
297309
endif()
298310

299-
target_compile_definitions(gg-sdk PUBLIC GG_TESTING)
300-
target_link_libraries(gg-sdk PRIVATE unity-config unity)
311+
set_target_properties(gg-sdk PROPERTIES EXPORT_COMPILE_COMMANDS OFF)
312+
313+
add_executable(gg-sdk-test ${SRCS} test/main_no_overrides.c)
314+
target_compile_options(gg-sdk-test PRIVATE -pthread -fno-strict-aliasing
315+
-std=gnu11 -Wno-missing-braces)
316+
target_compile_definitions(gg-sdk-test PRIVATE _GNU_SOURCE)
317+
target_include_directories(gg-sdk-test PRIVATE include priv_include)
318+
target_include_directories(gg-sdk-test SYSTEM INTERFACE include)
319+
320+
target_compile_definitions(gg-sdk-test PRIVATE "GG_MODULE=(\"gg-sdk\")")
321+
322+
target_compile_definitions(gg-sdk-test
323+
PUBLIC GG_LOG_LEVEL=GG_LOG_${choose_level})
324+
target_compile_definitions(gg-sdk-test PUBLIC GG_TESTING)
301325

302-
add_executable(gg-sdk-test test/default_main.c)
303-
target_link_libraries(gg-sdk-test PRIVATE unity-config unity gg-sdk)
326+
target_link_libraries(gg-sdk-test PRIVATE unity-config unity gg-test)
304327
add_test(gg-sdk-test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/gg-sdk-test)
305328

306329
endif()

cpp/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ if(aws-greengrass-component-sdk_IS_TOP_LEVEL)
6363
endif()
6464
get_filename_component(test_name ${test_dir} NAME_WLE)
6565
file(GLOB_RECURSE TEST_SRCS CONFIGURE_DEPENDS ${test_dir}/*.cpp)
66+
if(NOT "${test_dir}/main.cpp" IN_LIST TEST_SRCS)
67+
list(APPEND TEST_SRCS
68+
${aws-greengrass-component-sdk_C_TEST_DIR}/main_ipc_overrides.c)
69+
endif()
6670
foreach(src ${TEST_SRCS})
6771
set_property(
6872
SOURCE ${src}
@@ -71,8 +75,8 @@ if(aws-greengrass-component-sdk_IS_TOP_LEVEL)
7175
endforeach()
7276
add_executable(cpp_${test_name}_tests ${TEST_SRCS})
7377
target_link_libraries(
74-
cpp_${test_name}_tests PRIVATE gg-sdk++ gg-sdk gg-ipc-mock unity-config
75-
unity)
78+
cpp_${test_name}_tests PRIVATE gg-sdk++ gg-sdk gg-ipc-mock gg-test
79+
unity-config unity)
7680
install(TARGETS cpp_${test_name}_tests)
7781
add_test(cpp_${test_name}_tests
7882
${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/cpp_${test_name}_tests)

cpp/test/client/connect.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
extern "C" {
66
#include <gg/ipc/mock.h>
77
#include <gg/ipc/packet_sequences.h>
8-
#include <gg/process_wait.h>
98
#include <gg/test.h>
9+
#include <sys/types.h>
1010
#include <unistd.h>
1111
#include <unity.h>
12+
13+
GgError gg_process_wait(pid_t pid) noexcept;
1214
}
1315

1416
namespace {
@@ -36,9 +38,7 @@ namespace tests {
3638
TEST_PASS();
3739
}
3840

39-
GG_TEST_ASSERT_OK(
40-
gg_test_expect_packet_sequence(seq, 30, server_handle)
41-
);
41+
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 30));
4242

4343
GG_TEST_ASSERT_OK(gg_process_wait(pid));
4444
}
@@ -73,11 +73,9 @@ namespace tests {
7373
TEST_PASS();
7474
}
7575

76-
GG_TEST_ASSERT_OK(gg_test_accept_client(1, server_handle));
76+
GG_TEST_ASSERT_OK(gg_test_accept_client(1));
7777

78-
GG_TEST_ASSERT_OK(
79-
gg_test_expect_packet_sequence(seq, 5, server_handle)
80-
);
78+
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 5));
8179

8280
GG_TEST_ASSERT_OK(gg_process_wait(pid));
8381
}
@@ -98,12 +96,10 @@ namespace tests {
9896
TEST_PASS();
9997
}
10098

101-
GG_TEST_ASSERT_OK(
102-
gg_test_expect_packet_sequence(seq, 30, server_handle)
103-
);
99+
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 30));
104100

105101
/// TODO: verify Classic behavior
106-
GG_TEST_ASSERT_OK(gg_test_disconnect(server_handle));
102+
GG_TEST_ASSERT_OK(gg_test_disconnect());
107103

108104
GG_TEST_ASSERT_OK(gg_process_wait(pid));
109105
}

cpp/test/client/main.cpp

Lines changed: 0 additions & 7 deletions
This file was deleted.

mock/gg/ipc/mock.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,9 @@ typedef enum {
2323
/// containing the socket path and setting appropriate IPC environment
2424
/// variables. The IPC mock will not accept connections until
2525
/// gg_test_expect_packet_sequence() is called.
26-
NULL_TERMINATED_STRING_ARG(1) NONNULL(3) NULL_TERMINATED_STRING_ARG(4)
26+
NULL_TERMINATED_STRING_ARG(1) NULL_TERMINATED_STRING_ARG(3)
2727
GgError gg_test_setup_ipc(
28-
const char *socket_path_prefix,
29-
mode_t mode,
30-
int *handle,
31-
const char *auth_token
28+
const char *socket_path_prefix, mode_t mode, const char *auth_token
3229
);
3330

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

7168
/// Blocks until client process connects.
7269
/// The client timeout is the time in seconds to wait for
73-
GgError gg_test_accept_client(int client_timeout, int handle);
70+
GgError gg_test_accept_client(int client_timeout);
7471

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

8380
/// Hangs up on the client
84-
GgError gg_test_disconnect(int handle);
81+
GgError gg_test_disconnect(void);
8582

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

90-
void gg_test_close(int handle);
87+
/// Closes all file descriptors opened by the mock without waiting for client
88+
/// disconnect.
89+
void gg_test_close(void);
9190

9291
#endif

mock/gg/ipc/mock.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ typedef enum : int8_t {
1919
/// Sets up the IPC mock for this thread, opening a socket at path with mode,
2020
/// and setting IPC environment variables. The IPC mock will not accept
2121
/// connections until gg_test_expect_packet_sequence() is called.
22-
NULL_TERMINATED_STRING_ARG(1) NONNULL(3) NULL_TERMINATED_STRING_ARG(4)
22+
NULL_TERMINATED_STRING_ARG(1) NULL_TERMINATED_STRING_ARG(3)
2323
GgError gg_test_setup_ipc(
24-
const char *path, mode_t mode, int *handle, const char *auth_token
24+
const char *path, mode_t mode, const char *auth_token
2525
) noexcept;
2626

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

5050
/// Hangs up on the client
51-
GgError gg_test_disconnect(int handle) noexcept;
51+
GgError gg_test_disconnect(void) noexcept;
5252

53-
void gg_test_close(int handle) noexcept;
53+
void gg_test_close(void) noexcept;
5454
}
5555

5656
#endif

mock/mock.c

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,10 +148,7 @@ static GgError gg_socket_open(GgBuffer path, mode_t mode, int *socket_fd) {
148148
}
149149

150150
GgError gg_test_setup_ipc(
151-
const char *socket_path_prefix,
152-
mode_t mode,
153-
int *handle,
154-
const char *auth_token
151+
const char *socket_path_prefix, mode_t mode, const char *auth_token
155152
) {
156153
GgError ret = GG_ERR_OK;
157154
GgByteVec auth_vec = gg_byte_vec_init(GG_BUF(ipc_auth_token));
@@ -208,8 +205,6 @@ GgError gg_test_setup_ipc(
208205
cleanup_epollfd = -1;
209206
sock_fd = socket_fd;
210207

211-
*handle = 1;
212-
213208
// test setup code should be called before creating any threads
214209
// NOLINTBEGIN(concurrency-mt-unsafe)
215210
int setenv_ret = setenv(
@@ -437,8 +432,7 @@ static GgError gg_test_recv_packet(const GgipcPacket *packet, int client) {
437432
return GG_ERR_OK;
438433
}
439434

440-
GgError gg_test_accept_client(int client_timeout, int handle) {
441-
assert(handle > 0);
435+
GgError gg_test_accept_client(int client_timeout) {
442436
if (client_timeout <= 0) {
443437
client_timeout = 5;
444438
}
@@ -479,16 +473,14 @@ GgError gg_test_accept_client(int client_timeout, int handle) {
479473
}
480474

481475
GgError gg_test_expect_packet_sequence(
482-
GgipcPacketSequence sequence, int client_timeout, int handle
476+
GgipcPacketSequence sequence, int client_timeout
483477
) {
484-
assert(handle > 0);
485-
486478
if (client_timeout <= 0) {
487479
client_timeout = 5;
488480
}
489481

490482
if (client_fd < 0) {
491-
GgError ret = gg_test_accept_client(client_timeout, handle);
483+
GgError ret = gg_test_accept_client(client_timeout);
492484
if (ret != GG_ERR_OK) {
493485
return ret;
494486
}
@@ -518,8 +510,7 @@ GgError gg_test_expect_packet_sequence(
518510
return GG_ERR_OK;
519511
}
520512

521-
GgError gg_test_disconnect(int handle) {
522-
assert(handle > 0);
513+
GgError gg_test_disconnect(void) {
523514
if (client_fd < 0) {
524515
return GG_ERR_NOENTRY;
525516
}
@@ -529,8 +520,7 @@ GgError gg_test_disconnect(int handle) {
529520
return gg_close(old_client_fd);
530521
}
531522

532-
GgError gg_test_wait_for_client_disconnect(int client_timeout, int handle) {
533-
assert(handle > 0);
523+
GgError gg_test_wait_for_client_disconnect(int client_timeout) {
534524
if (client_fd < 0) {
535525
return GG_ERR_NOENTRY;
536526
}
@@ -552,7 +542,7 @@ GgError gg_test_wait_for_client_disconnect(int client_timeout, int handle) {
552542
return GG_ERR_FAILURE;
553543
}
554544

555-
return gg_test_disconnect(handle);
545+
return gg_test_disconnect();
556546
}
557547

558548
static void remove_temp_files(void) {
@@ -578,8 +568,7 @@ static void remove_temp_files(void) {
578568
ipc_socket_path_buf = (GgBuffer) { 0 };
579569
}
580570

581-
void gg_test_close(int handle) {
582-
assert(handle > 0);
571+
void gg_test_close(void) {
583572
if (client_fd >= 0) {
584573
(void) gg_close(client_fd);
585574
client_fd = -1;

test/client/connect.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ GG_TEST_DEFINE(connect_okay) {
3131
TEST_PASS();
3232
}
3333

34-
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 5, server_handle));
34+
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 5));
3535

3636
GG_TEST_ASSERT_OK(gg_process_wait(pid));
3737
}
@@ -57,7 +57,7 @@ GG_TEST_DEFINE(connect_with_token_okay) {
5757
TEST_PASS();
5858
}
5959

60-
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 5, server_handle));
60+
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 5));
6161

6262
GG_TEST_ASSERT_OK(gg_process_wait(pid));
6363
}
@@ -75,10 +75,10 @@ GG_TEST_DEFINE(connect_bad) {
7575
TEST_PASS();
7676
}
7777

78-
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 1, server_handle));
78+
GG_TEST_ASSERT_OK(gg_test_expect_packet_sequence(seq, 1));
7979

8080
/// TODO: verify Classic behavior
81-
GG_TEST_ASSERT_OK(gg_test_disconnect(server_handle));
81+
GG_TEST_ASSERT_OK(gg_test_disconnect());
8282

8383
GG_TEST_ASSERT_OK(gg_process_wait(pid));
8484
}
@@ -98,7 +98,7 @@ GG_TEST_DEFINE(connect_with_token_bad) {
9898
TEST_PASS();
9999
}
100100

101-
GG_TEST_ASSERT_BAD(gg_test_accept_client(1, server_handle));
101+
GG_TEST_ASSERT_BAD(gg_test_accept_client(1));
102102

103103
GG_TEST_ASSERT_OK(gg_process_wait(pid));
104104
}

0 commit comments

Comments
 (0)