Skip to content

Commit c0666d0

Browse files
authored
Merge pull request #537 from xuzhenbao/rsa_shm_unit_test
Add unit tests for rsa_shm and rsa_json_rpc
2 parents 19529d7 + 8f1f2e2 commit c0666d0

File tree

86 files changed

+5592
-1068
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+5592
-1068
lines changed

bundles/remote_services/discovery_zeroconf/gtest/CMakeLists.txt

+25-23
Original file line numberDiff line numberDiff line change
@@ -40,31 +40,33 @@ target_compile_definitions(integration_test_discovery_zeroconf PRIVATE -DDISCOVE
4040
add_test(NAME run_integration_test_discovery_zeroconf COMMAND integration_test_discovery_zeroconf)
4141
setup_target_for_coverage(integration_test_discovery_zeroconf SCAN_DIR ..)
4242

43-
####unit test
44-
add_executable(unit_test_discovery_zeroconf
45-
src/DiscoveryZeroconfAnnouncerTestSuite.cc
46-
src/DiscoveryZeroconfWatcherTestSuite.cc
47-
src/DiscoveryZeroconfActivatorTestSuite.cc
48-
)
43+
if (LINKER_WRAP_SUPPORTED)
44+
####unit test
45+
add_executable(unit_test_discovery_zeroconf
46+
src/DiscoveryZeroconfAnnouncerTestSuite.cc
47+
src/DiscoveryZeroconfWatcherTestSuite.cc
48+
src/DiscoveryZeroconfActivatorTestSuite.cc
49+
)
4950

50-
celix_deprecated_utils_headers(unit_test_discovery_zeroconf)
51+
celix_deprecated_utils_headers(unit_test_discovery_zeroconf)
5152

52-
target_link_libraries(unit_test_discovery_zeroconf PRIVATE
53-
rsa_discovery_zeroconf_cut
54-
Celix::rsa_common
55-
Celix::c_rsa_spi
56-
Celix::framework
57-
Celix::threads_ei
58-
Celix::eventfd_ei
59-
Celix::bundle_ctx_ei
60-
Celix::mdnsresponder_ei
61-
Celix::malloc_ei
62-
GTest::gtest
63-
GTest::gtest_main
64-
)
53+
target_link_libraries(unit_test_discovery_zeroconf PRIVATE
54+
rsa_discovery_zeroconf_cut
55+
Celix::rsa_common
56+
Celix::c_rsa_spi
57+
Celix::framework
58+
Celix::threads_ei
59+
Celix::eventfd_ei
60+
Celix::bundle_ctx_ei
61+
Celix::mdnsresponder_ei
62+
Celix::malloc_ei
63+
GTest::gtest
64+
GTest::gtest_main
65+
)
6566

66-
target_compile_definitions(unit_test_discovery_zeroconf PRIVATE -DMDNSD="${DNSSD_LIB_DIRS}/../bin/mdnsd")
67+
target_compile_definitions(unit_test_discovery_zeroconf PRIVATE -DMDNSD="${DNSSD_LIB_DIRS}/../bin/mdnsd")
6768

6869

69-
add_test(NAME run_unit_test_discovery_zeroconf COMMAND sudo $<TARGET_FILE:unit_test_discovery_zeroconf>)
70-
setup_target_for_coverage(unit_test_discovery_zeroconf SCAN_DIR ..)
70+
add_test(NAME run_unit_test_discovery_zeroconf COMMAND sudo $<TARGET_FILE:unit_test_discovery_zeroconf>)
71+
setup_target_for_coverage(unit_test_discovery_zeroconf SCAN_DIR ..)
72+
endif ()

bundles/remote_services/remote_service_admin_shm_v2/rsa_shm/CMakeLists.txt

+25-16
Original file line numberDiff line numberDiff line change
@@ -17,39 +17,48 @@
1717

1818
find_package(libuuid REQUIRED)
1919

20+
set(RSA_SHM_SRC
21+
src/rsa_shm_impl.c
22+
src/rsa_shm_activator.c
23+
src/rsa_shm_server.c
24+
src/rsa_shm_client.c
25+
src/rsa_shm_export_registration.c
26+
src/rsa_shm_import_registration.c
27+
)
28+
29+
set(RSA_SHM_DEPS
30+
Celix::c_rsa_spi
31+
Celix::rsa_common
32+
Celix::log_helper
33+
Celix::framework
34+
Celix::thpool
35+
Celix::shm_pool
36+
libuuid::libuuid
37+
)
38+
2039
add_celix_bundle(rsa_shm
2140
VERSION 1.0.0
2241
SYMBOLIC_NAME "apache_celix_remote_service_admin_shm_v2"
2342
NAME "Apache Celix Remote Service Admin SHM V2"
2443
GROUP "Celix/RSA"
2544
SOURCES
26-
src/rsa_shm_impl.c
27-
src/rsa_shm_activator.c
28-
src/rsa_shm_server.c
29-
src/rsa_shm_client.c
30-
src/rsa_shm_export_registration.c
31-
src/rsa_shm_import_registration.c
45+
${RSA_SHM_SRC}
3246
)
3347

3448
celix_deprecated_utils_headers(rsa_shm)
3549
celix_deprecated_framework_headers(rsa_shm)
3650
target_include_directories(rsa_shm PRIVATE src)
3751

38-
39-
target_link_libraries(rsa_shm PRIVATE
40-
Celix::c_rsa_spi
41-
Celix::rsa_common
42-
Celix::log_helper
43-
Celix::framework
44-
Celix::thpool
45-
Celix::shm_pool
46-
libuuid::libuuid
47-
)
52+
target_link_libraries(rsa_shm PRIVATE ${RSA_SHM_DEPS})
4853

4954
install_celix_bundle(rsa_shm EXPORT celix COMPONENT rsa)
5055
add_library(Celix::rsa_shm ALIAS rsa_shm)
5156

5257
if (ENABLE_TESTING)
58+
add_library(rsa_shm_cut STATIC ${RSA_SHM_SRC})
59+
celix_deprecated_utils_headers(rsa_shm_cut)
60+
target_include_directories(rsa_shm_cut PUBLIC src)
61+
target_link_libraries(rsa_shm_cut PUBLIC ${RSA_SHM_DEPS})
5362
add_subdirectory(gtest)
5463
endif()
5564

bundles/remote_services/remote_service_admin_shm_v2/rsa_shm/gtest/CMakeLists.txt

+56-28
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,73 @@
1515
# specific language governing permissions and limitations
1616
# under the License.
1717

18-
find_package(libuuid REQUIRED)
19-
20-
add_executable(test_rsa_shm
21-
src/RsaShmActivatorTestSuite.cc
22-
src/RsaShmTestSuite.cc
23-
src/RsaShmClientServerTestSuite.cc
18+
####integration test
19+
add_executable(integration_test_rsa_shm
20+
src/RsaShmIntegrationTestSuite.cc
2421
)
2522

26-
target_include_directories(test_rsa_shm PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../src)
27-
celix_deprecated_utils_headers(test_rsa_shm)
28-
celix_deprecated_framework_headers(test_rsa_shm)
23+
celix_deprecated_utils_headers(integration_test_rsa_shm)
24+
celix_deprecated_framework_headers(integration_test_rsa_shm)
2925

30-
target_link_libraries(test_rsa_shm PRIVATE
26+
target_link_libraries(integration_test_rsa_shm PRIVATE
3127
Celix::c_rsa_spi
32-
calculator_api
33-
Celix::shell_api
3428
Celix::framework
35-
Celix::dfi
36-
libuuid::libuuid
3729
GTest::gtest
3830
GTest::gtest_main
3931
)
4032

4133
celix_get_bundle_file(Celix::rsa_shm RSA_SHM_BUNDLE_FILE)
42-
target_compile_definitions(test_rsa_shm PRIVATE -DRSA_SHM_BUNDLE="${RSA_SHM_BUNDLE_FILE}")
43-
celix_get_bundle_file(Celix::rsa_json_rpc RSA_RPC_BUNDLE_FILE)
44-
target_compile_definitions(test_rsa_shm PRIVATE -DRSA_RPC_BUNDLE="${RSA_RPC_BUNDLE_FILE}")
45-
celix_get_bundle_file(calculator_shell CONSUMER_BUNDLE_FILE)
46-
target_compile_definitions(test_rsa_shm PRIVATE -DCONSUMER_BUNDLE="${CONSUMER_BUNDLE_FILE}")
47-
celix_get_bundle_file(calculator PROVIDER_BUNDLE_FILE)
48-
target_compile_definitions(test_rsa_shm PRIVATE -DPROVIDER_BUNDLE="${PROVIDER_BUNDLE_FILE}")
49-
celix_get_bundle_file(Celix::rsa_topology_manager RSA_TOPOLOGY_MANAGER_BUNDLE_FILE)
50-
celix_get_bundle_file(Celix::rsa_discovery RSA_DISCOVERY_BUNDLE_FILE)
34+
target_compile_definitions(integration_test_rsa_shm PRIVATE -DRSA_SHM_BUNDLE="${RSA_SHM_BUNDLE_FILE}")
35+
36+
37+
add_celix_bundle_dependencies(integration_test_rsa_shm Celix::rsa_shm)
38+
39+
add_test(NAME run_integration_test_rsa_shm COMMAND integration_test_rsa_shm)
40+
setup_target_for_coverage(integration_test_rsa_shm SCAN_DIR ..)
41+
42+
if (LINKER_WRAP_SUPPORTED)
43+
####unit test
44+
add_executable(unit_test_rsa_shm
45+
src/RsaShmImplUnitTestSuite.cc
46+
src/RsaShmExportRegistrationUnitTestSuite.cc
47+
src/RsaShmImportRegistrationUnitTestSuite.cc
48+
src/RsaShmClientServerUnitTestSuite.cc
49+
src/RsaShmActivatorUnitTestSuite.cc
50+
src/thpool_ei.cc
51+
)
52+
53+
target_link_libraries(unit_test_rsa_shm PRIVATE
54+
rsa_shm_cut
55+
Celix::c_rsa_spi
56+
Celix::rsa_common
57+
Celix::log_helper
58+
Celix::framework
59+
Celix::threads_ei
60+
Celix::bundle_ctx_ei
61+
Celix::malloc_ei
62+
Celix::asprintf_ei
63+
Celix::utils_ei
64+
Celix::socket_ei
65+
Celix::stdio_ei
66+
Celix::pthread_ei
67+
Celix::log_helper_ei
68+
GTest::gtest
69+
GTest::gtest_main
70+
)
71+
72+
target_link_options(unit_test_rsa_shm PRIVATE
73+
LINKER:--wrap,thpool_init
74+
LINKER:--wrap,thpool_add_work
75+
)
5176

52-
configure_file(resources/client.properties.in client.properties)
53-
configure_file(resources/server.properties.in server.properties)
77+
target_compile_definitions(unit_test_rsa_shm PRIVATE -DRESOURCES_DIR="${CMAKE_CURRENT_LIST_DIR}/resources")
78+
celix_get_bundle_file(Celix::rsa_json_rpc RSA_JSON_RPC_BUNDLE_FILE)
79+
target_compile_definitions(unit_test_rsa_shm PRIVATE -DRSA_JSON_RPC_BUNDLE="${RSA_JSON_RPC_BUNDLE_FILE}")
5480

81+
add_celix_bundle_dependencies(unit_test_rsa_shm Celix::rsa_json_rpc)
5582

56-
add_celix_bundle_dependencies(test_rsa_shm Celix::rsa_shm Celix::rsa_json_rpc calculator_shell calculator)
83+
celix_deprecated_utils_headers(unit_test_rsa_shm)
5784

58-
add_test(NAME run_test_rsa_shm COMMAND test_rsa_shm)
59-
setup_target_for_coverage(test_rsa_shm SCAN_DIR ..)
85+
add_test(NAME run_unit_test_rsa_shm COMMAND unit_test_rsa_shm)
86+
setup_target_for_coverage(unit_test_rsa_shm SCAN_DIR ..)
87+
endif ()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
:header
2+
type=interface
3+
name=calculator
4+
version=1.0.0
5+
:annotations
6+
classname=org.test.Calculator
7+
:types
8+
:methods
9+
add(DD)D=add(#am=handle;PDD#am=pre;*D)N
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#include "rsa_shm_impl.h"
21+
#include "remote_service_admin.h"
22+
#include "celix_api.h"
23+
#include "celix_log_helper_ei.h"
24+
#include "malloc_ei.h"
25+
#include "celix_bundle_context_ei.h"
26+
#include <gtest/gtest.h>
27+
28+
class RsaShmActivatorUnitTestSuite : public ::testing::Test {
29+
public:
30+
RsaShmActivatorUnitTestSuite() {
31+
auto* props = celix_properties_create();
32+
celix_properties_set(props, CELIX_FRAMEWORK_FRAMEWORK_STORAGE_CLEAN_NAME, "onFirstInit");
33+
celix_properties_set(props, OSGI_FRAMEWORK_FRAMEWORK_STORAGE, ".rsa_shm_cache");
34+
auto* fwPtr = celix_frameworkFactory_createFramework(props);
35+
auto* ctxPtr = celix_framework_getFrameworkContext(fwPtr);
36+
fw = std::shared_ptr<celix_framework_t>{fwPtr, [](auto* f) {celix_frameworkFactory_destroyFramework(f);}};
37+
ctx = std::shared_ptr<celix_bundle_context_t>{ctxPtr, [](auto*){/*nop*/}};
38+
}
39+
40+
~RsaShmActivatorUnitTestSuite() {
41+
celix_ei_expect_celix_logHelper_create(nullptr, 0, nullptr);
42+
celix_ei_expect_calloc(nullptr, 0, nullptr);
43+
celix_ei_expect_celix_bundleContext_registerServiceAsync(nullptr, 0, 0);
44+
}
45+
46+
47+
std::shared_ptr<celix_framework_t> fw{};
48+
std::shared_ptr<celix_bundle_context_t> ctx{};
49+
};
50+
51+
TEST_F(RsaShmActivatorUnitTestSuite, RsaShmActivatorStart) {
52+
void *userData = nullptr;
53+
auto status = celix_bundleActivator_create(ctx.get(), &userData);
54+
EXPECT_EQ(status, CELIX_SUCCESS);
55+
status = celix_bundleActivator_start(userData, ctx.get());
56+
EXPECT_EQ(status, CELIX_SUCCESS);
57+
58+
bool found = celix_bundleContext_findService(ctx.get(), OSGI_RSA_REMOTE_SERVICE_ADMIN);
59+
EXPECT_TRUE(found);
60+
61+
status = celix_bundleActivator_stop(userData, ctx.get());
62+
EXPECT_EQ(status, CELIX_SUCCESS);
63+
status = celix_bundleActivator_destroy(userData, ctx.get());
64+
EXPECT_EQ(status, CELIX_SUCCESS);
65+
}
66+
67+
TEST_F(RsaShmActivatorUnitTestSuite, RsaShmActivatorStartWithLogHelperError) {
68+
celix_ei_expect_celix_logHelper_create(nullptr, 0, nullptr);
69+
70+
void *userData = nullptr;
71+
auto status = celix_bundleActivator_create(ctx.get(), &userData);
72+
EXPECT_EQ(status, CELIX_SUCCESS);
73+
celix_ei_expect_celix_logHelper_create((void*)&celix_bundleActivator_start, 1, nullptr);
74+
status = celix_bundleActivator_start(userData, ctx.get());
75+
EXPECT_EQ(status, CELIX_BUNDLE_EXCEPTION);
76+
77+
status = celix_bundleActivator_destroy(userData, ctx.get());
78+
EXPECT_EQ(status, CELIX_SUCCESS);
79+
}
80+
81+
TEST_F(RsaShmActivatorUnitTestSuite, RsaShmActivatorStartWithCreatingRsaError) {
82+
void *userData = nullptr;
83+
auto status = celix_bundleActivator_create(ctx.get(), &userData);
84+
EXPECT_EQ(status, CELIX_SUCCESS);
85+
celix_ei_expect_calloc((void*)&rsaShm_create, 0, nullptr);
86+
status = celix_bundleActivator_start(userData, ctx.get());
87+
EXPECT_EQ(status, CELIX_ENOMEM);
88+
89+
status = celix_bundleActivator_destroy(userData, ctx.get());
90+
EXPECT_EQ(status, CELIX_SUCCESS);
91+
}
92+
93+
TEST_F(RsaShmActivatorUnitTestSuite, RsaShmActivatorStartWithRegisteringRsaServiceError) {
94+
void *userData = nullptr;
95+
auto status = celix_bundleActivator_create(ctx.get(), &userData);
96+
EXPECT_EQ(status, CELIX_SUCCESS);
97+
celix_ei_expect_celix_bundleContext_registerServiceAsync((void*)&celix_bundleActivator_start, 1, -1);
98+
status = celix_bundleActivator_start(userData, ctx.get());
99+
EXPECT_EQ(status, CELIX_BUNDLE_EXCEPTION);
100+
101+
status = celix_bundleActivator_destroy(userData, ctx.get());
102+
EXPECT_EQ(status, CELIX_SUCCESS);
103+
}

0 commit comments

Comments
 (0)