Skip to content

Commit 850f36a

Browse files
authored
Refactor Assuan code to make integration testing easier (#12)
Two facets to this change: * Move some server-specific startup code out of agent.c into a separate file so that it can be used in tests. * Compile most of the server and client code as a CMake 'object' library so that it can be used in tests.
1 parent be362bb commit 850f36a

File tree

4 files changed

+133
-60
lines changed

4 files changed

+133
-60
lines changed

CMakeLists.txt

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,32 +44,51 @@ set_target_properties(createrepo-cache PROPERTIES
4444
SOVERSION ${CRA_VERSION_MAJOR}
4545
VERSION ${CRA_VERSION})
4646

47-
# Agent
47+
# Client/Server
4848
configure_file(
4949
"src/${PROJECT_NAME}/common.h.in"
5050
"${CMAKE_CURRENT_BINARY_DIR}/gen/${PROJECT_NAME}/common.h"
5151
@ONLY)
52-
add_executable(${PROJECT_NAME}
53-
src/${PROJECT_NAME}/agent.c
52+
add_library(createrepo-agent-lib OBJECT
5453
src/${PROJECT_NAME}/client.c
5554
src/${PROJECT_NAME}/command.c
56-
src/${PROJECT_NAME}/options.c)
57-
target_include_directories(${PROJECT_NAME} PRIVATE src "${CMAKE_CURRENT_BINARY_DIR}/gen")
55+
src/${PROJECT_NAME}/options.c
56+
src/${PROJECT_NAME}/server.c)
57+
target_include_directories(createrepo-agent-lib PUBLIC
58+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
59+
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/gen>)
60+
target_include_directories(createrepo-agent-lib SYSTEM PRIVATE
61+
${CREATEREPO_C_INCLUDE_DIRS})
62+
target_include_directories(createrepo-agent-lib SYSTEM PUBLIC
63+
${GLIB2_INCLUDE_DIRS}
64+
${GPG_ERROR_INCLUDE_DIRS}
65+
${LIBASSUAN_INCLUDE_DIRS})
66+
target_link_libraries(createrepo-agent-lib PRIVATE
67+
createrepo-cache
68+
${CREATEREPO_C_LIBRARIES})
69+
target_link_libraries(createrepo-agent-lib PUBLIC
70+
${GLIB2_LIBRARIES}
71+
${GPG_ERROR_LIBRARIES}
72+
${LIBASSUAN_LIBRARIES})
73+
target_compile_definitions(createrepo-agent-lib PRIVATE
74+
-DG_LOG_DOMAIN="CREATEREPO_AGENT")
75+
76+
# Executable
77+
add_executable(${PROJECT_NAME}
78+
src/${PROJECT_NAME}/agent.c)
5879
target_include_directories(${PROJECT_NAME} SYSTEM PRIVATE
5980
${CREATEREPO_C_INCLUDE_DIRS}
6081
${GLIB2_INCLUDE_DIRS}
6182
${GPG_ERROR_INCLUDE_DIRS}
6283
${GPGME_INCLUDE_DIRS}
6384
${LIBASSUAN_INCLUDE_DIRS})
6485
target_link_libraries(${PROJECT_NAME} PRIVATE
65-
createrepo-cache
86+
createrepo-agent-lib
6687
${CREATEREPO_C_LIBRARIES}
6788
${GLIB2_LIBRARIES}
6889
${GPG_ERROR_LIBRARIES}
6990
${GPGME_LIBRARIES}
7091
${LIBASSUAN_LIBRARIES})
71-
target_compile_definitions(${PROJECT_NAME} PRIVATE
72-
-DG_LOG_DOMAIN="CREATEREPO_AGENT")
7392
install(TARGETS ${PROJECT_NAME}
7493
RUNTIME DESTINATION bin
7594
COMPONENT bin)

src/createrepo-agent/agent.c

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
#include <signal.h>
1919
#include <stdlib.h>
2020
#include <string.h>
21-
#include <sys/socket.h>
22-
#include <sys/un.h>
2321

2422
#include <createrepo_c/createrepo_c.h>
2523
#include <glib.h>
@@ -29,56 +27,7 @@
2927
#include "createrepo-agent/command.h"
3028
#include "createrepo-agent/common.h"
3129
#include "createrepo-agent/options.h"
32-
33-
static gpg_error_t
34-
try_server(const char * name)
35-
{
36-
assuan_context_t ctx;
37-
gpg_error_t rc;
38-
39-
rc = assuan_new(&ctx);
40-
if (rc) {
41-
return rc;
42-
}
43-
44-
rc = assuan_socket_connect(ctx, name, ASSUAN_INVALID_PID, 0);
45-
46-
assuan_release(ctx);
47-
48-
return rc;
49-
}
50-
51-
static assuan_fd_t
52-
create_server_socket(const char * name)
53-
{
54-
struct sockaddr_un addr_un;
55-
struct sockaddr * addr = (struct sockaddr *)&addr_un;
56-
assuan_fd_t fd;
57-
int r_redirected;
58-
59-
addr_un.sun_family = AF_UNIX;
60-
61-
if (assuan_sock_set_sockaddr_un(name, addr, &r_redirected)) {
62-
return ASSUAN_INVALID_FD;
63-
}
64-
65-
fd = assuan_sock_new(addr_un.sun_family, SOCK_STREAM, 0);
66-
if (fd == ASSUAN_INVALID_FD) {
67-
return ASSUAN_INVALID_FD;
68-
}
69-
70-
if (assuan_sock_bind(fd, addr, sizeof(addr_un))) {
71-
assuan_sock_close(fd);
72-
return ASSUAN_INVALID_FD;
73-
}
74-
75-
if (listen(fd, SOMAXCONN)) {
76-
assuan_sock_close(fd);
77-
return ASSUAN_INVALID_FD;
78-
}
79-
80-
return fd;
81-
}
30+
#include "createrepo-agent/server.h"
8231

8332
void
8433
ignore_sigpipe()

src/createrepo-agent/server.c

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright 2025 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include <assuan.h>
16+
#include <sys/socket.h>
17+
#include <sys/un.h>
18+
19+
#include "createrepo-agent/command.h"
20+
#include "createrepo-agent/server.h"
21+
22+
gpg_error_t
23+
try_server(const char * name)
24+
{
25+
assuan_context_t ctx;
26+
gpg_error_t rc;
27+
28+
rc = assuan_new(&ctx);
29+
if (rc) {
30+
return rc;
31+
}
32+
33+
rc = assuan_socket_connect(ctx, name, ASSUAN_INVALID_PID, 0);
34+
35+
assuan_release(ctx);
36+
37+
return rc;
38+
}
39+
40+
assuan_fd_t
41+
create_server_socket(const char * name)
42+
{
43+
struct sockaddr_un addr_un;
44+
struct sockaddr * addr = (struct sockaddr *)&addr_un;
45+
assuan_fd_t fd;
46+
int r_redirected;
47+
48+
addr_un.sun_family = AF_UNIX;
49+
50+
if (assuan_sock_set_sockaddr_un(name, addr, &r_redirected)) {
51+
return ASSUAN_INVALID_FD;
52+
}
53+
54+
fd = assuan_sock_new(addr_un.sun_family, SOCK_STREAM, 0);
55+
if (fd == ASSUAN_INVALID_FD) {
56+
return ASSUAN_INVALID_FD;
57+
}
58+
59+
if (assuan_sock_bind(fd, addr, sizeof(addr_un))) {
60+
assuan_sock_close(fd);
61+
return ASSUAN_INVALID_FD;
62+
}
63+
64+
if (listen(fd, SOMAXCONN)) {
65+
assuan_sock_close(fd);
66+
return ASSUAN_INVALID_FD;
67+
}
68+
69+
return fd;
70+
}

src/createrepo-agent/server.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2025 Open Source Robotics Foundation, Inc.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef CREATEREPO_AGENT__SERVER_H_
16+
#define CREATEREPO_AGENT__SERVER_H_
17+
18+
#include <assuan.h>
19+
20+
#ifdef __cplusplus
21+
extern "C"
22+
{
23+
#endif
24+
25+
gpg_error_t
26+
try_server(const char * name);
27+
28+
assuan_fd_t
29+
create_server_socket(const char * name);
30+
31+
#ifdef __cplusplus
32+
}
33+
#endif
34+
35+
#endif // CREATEREPO_AGENT__SERVER_H_

0 commit comments

Comments
 (0)