Skip to content
This repository was archived by the owner on Jan 6, 2023. It is now read-only.

Commit b8568a3

Browse files
josuedhgphmccarty
authored andcommitted
Creating testing binary to test basic behoviour of fuse and daemon binaries
- creating a fake socket check if fuse is sending the request properly to the socket service. - make a request to the server socket while clr_debug_daemon is running to check that it is responding propertly and also that it is creating cache files - change makefile.am to compile binaries without add them to the installation process Signed-off-by: Josue David Hernandez Gutierrez <josue.d.hernandez.gutierrez@intel.com>
1 parent 0cc9c4b commit b8568a3

4 files changed

Lines changed: 291 additions & 0 deletions

File tree

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
*.o
33
clr_debug_fuse
44
clr_debug_daemon
5+
test-driver
6+
testing_fuse
7+
testing_daemon
58
.deps/
69
.dirstamp
710
Makefile
@@ -29,3 +32,8 @@ config.guess
2932
config.sub
3033
libtool
3134
ltmain.sh
35+
test-suite.log
36+
testing_fuse.log
37+
testing_fuse.trs
38+
testing_daemon.log
39+
testing_daemon.trs

Makefile.am

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,12 @@ clr_debug_daemon_LDADD = ${curl_LIBS} libnica.la ${LIBSYSTEMD_LIBS}
4545
systemdsystemunit_DATA = clr_debug_fuse.service clr_debug_daemon.service clr_debug_daemon.socket
4646

4747
tmpfiles_DATA = debuginfo.conf
48+
49+
# Functional Testing
50+
noinst_PROGRAMS = testing_fuse testing_daemon
51+
52+
testing_fuse_SOURCES = tests/testing_fuse.c
53+
testing_fuse_CFLAGS = $(AM_CFLAGS)
54+
55+
testing_daemon_SOURCES = tests/testing_daemon.c
56+
testing_daemon_CFLAGS = $(AM_CFLAGS)

tests/testing_daemon.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#define _GNU_SOURCE
2+
3+
#include <stdio.h>
4+
#include <string.h>
5+
#include <stddef.h>
6+
#include <sys/types.h>
7+
#include <sys/socket.h>
8+
#include <sys/un.h>
9+
#include <signal.h>
10+
#include <unistd.h>
11+
#include <errno.h>
12+
13+
#include "config.h"
14+
15+
#define BUFFER_LENGTH 16
16+
17+
int get_server_socket(void)
18+
{
19+
int sockfd;
20+
struct sockaddr_un serveraddr;
21+
fputs("Creating socket\n", stderr);
22+
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
23+
if (sockfd < 0) {
24+
fputs("Fail creating socket\n", stderr);
25+
return -1;
26+
}
27+
memset(&serveraddr, 0, sizeof(serveraddr));
28+
serveraddr.sun_family = AF_UNIX;
29+
strcpy(serveraddr.sun_path, SOCKET_PATH);
30+
31+
fputs("Connecting to server\n", stderr);
32+
if (connect(sockfd, (struct sockaddr*)&serveraddr,
33+
offsetof(struct sockaddr_un, sun_path) + strlen(SOCKET_PATH) + 1) < 0) {
34+
fprintf(stderr, "Fail connecting to server: %s\n", strerror(errno));
35+
close(sockfd);
36+
return -1;
37+
}
38+
return sockfd;
39+
}
40+
41+
42+
int testing_daemon(void)
43+
{
44+
int sockfd;
45+
char buffer[BUFFER_LENGTH];
46+
47+
if((sockfd = get_server_socket()) < 0) {
48+
return -1;
49+
}
50+
51+
fputs("Sending message to server\n", stderr);
52+
memset(buffer, 0, sizeof(buffer));
53+
strcpy(buffer, "0:lib:/lib");
54+
if(send(sockfd, buffer, sizeof(buffer), 0) < 0) {
55+
fputs("Fail sending message to socket service\n", stderr);
56+
close(sockfd);
57+
return -1;
58+
}
59+
60+
fputs("Receiving message from server\n", stderr);
61+
memset(buffer, 0, sizeof(buffer));
62+
if (recv(sockfd, &buffer[0],
63+
BUFFER_LENGTH, 0) < 0) {
64+
fputs("Failing receiving message from socket service\n", stderr);
65+
close(sockfd);
66+
return -1;
67+
}
68+
69+
close(sockfd);
70+
return strcmp(buffer, "ok") && access("/var/cache/debuginfo/lib/lib", F_OK) != -1;
71+
}
72+
73+
int main(void)
74+
{
75+
int res = 0;
76+
pid_t p;
77+
p = fork();
78+
if (p == 0) {
79+
if (freopen("/dev/null", "w", stderr) == NULL)
80+
return -1;
81+
char *args[] = {"./clr_debug_daemon", NULL};
82+
execvp("./clr_debug_daemon", args);
83+
} else if (p > 0) {
84+
sleep(1);
85+
res = testing_daemon();
86+
kill(p, SIGKILL);
87+
} else {
88+
fputs("error executing fork\n", stderr);
89+
}
90+
unlink(SOCKET_PATH);
91+
return res;
92+
}
93+
/*
94+
* Editor modelines - https://www.wireshark.org/tools/modelines.html
95+
*
96+
* Local variables:
97+
* c-basic-offset: 8
98+
* tab-width: 8
99+
* indent-tabs-mode: nil
100+
* End:
101+
*
102+
* vi: set shiftwidth=8 tabstop=8 expandtab:
103+
* :indentSize=8:tabSize=8:noTabs=true:
104+
*/

tests/testing_fuse.c

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#define _GNU_SOURCE
2+
3+
#include <errno.h>
4+
#include <grp.h>
5+
#include <linux/capability.h>
6+
#include <linux/limits.h>
7+
#include <malloc.h>
8+
#include <pwd.h>
9+
#include <signal.h>
10+
#include <stddef.h>
11+
#include <stdio.h>
12+
#include <stdlib.h>
13+
#include <string.h>
14+
#include <sys/prctl.h>
15+
#include <sys/socket.h>
16+
#include <sys/stat.h>
17+
#include <sys/types.h>
18+
#include <sys/un.h>
19+
#include <sys/wait.h>
20+
#include <unistd.h>
21+
#include <fcntl.h>
22+
23+
#include "config.h"
24+
25+
#define TIMEOUT 600
26+
#define MAX_CONNECTIONS 16
27+
28+
int check_file_request(int fd, char *expected_str)
29+
{
30+
char buf[PATH_MAX + 8];
31+
int clientsock;
32+
malloc_trim(0);
33+
fputs("Accepting connection\n", stderr);
34+
clientsock = accept(fd, NULL, NULL);
35+
if (clientsock < 0)
36+
return -1;
37+
38+
memset(buf, 0, sizeof(buf));
39+
fputs("Reading socket content\n", stderr);
40+
if (read(clientsock, buf, PATH_MAX + 8) < 0)
41+
return -1;
42+
close(clientsock);
43+
return strncmp(buf, expected_str, strlen(expected_str));
44+
}
45+
46+
int get_service_socket(void)
47+
{
48+
int sockfd;
49+
struct sockaddr_un sun;
50+
signal(SIGPIPE, SIG_IGN);
51+
fputs("Creating socket\n", stderr);
52+
sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
53+
if (sockfd < 0) {
54+
fputs("Fail creating socket\n", stderr);
55+
return -1;
56+
}
57+
58+
printf("Socket path %s\n", SOCKET_PATH);
59+
sun.sun_family = AF_UNIX;
60+
strcpy(sun.sun_path, SOCKET_PATH);
61+
62+
fputs("Binding socket\n", stderr);
63+
if (bind(sockfd, (struct sockaddr*)&sun,
64+
offsetof(struct sockaddr_un, sun_path) + strlen(SOCKET_PATH) + 1) < 0) {
65+
fprintf(stderr, "fail bind: %s\n", strerror(errno));
66+
close(sockfd);
67+
return -1;
68+
}
69+
70+
if (listen(sockfd, 16) < 0) {
71+
fputs("Fail listening socket \n", stderr);
72+
close(sockfd);
73+
return -1;
74+
}
75+
return sockfd;
76+
}
77+
78+
79+
int testing_fuse(void)
80+
{
81+
int sockfd;
82+
uid_t gdb_user = 0;
83+
gid_t gdb_group = 0;
84+
struct passwd *passwdentry;
85+
pid_t p2;
86+
int test_result = -1;
87+
umask(0);
88+
passwdentry = getpwnam("gdbinfo");
89+
if (passwdentry) {
90+
gdb_user = passwdentry->pw_uid;
91+
gdb_group = passwdentry->pw_gid;
92+
}
93+
endpwent();
94+
95+
if (prctl(PR_SET_DUMPABLE, 0) != 0) {
96+
fputs("fail prctl\n", stderr);
97+
}
98+
99+
if (geteuid() == 0) {
100+
if (prctl(PR_CAPBSET_DROP, CAP_SYS_ADMIN) !=0) {
101+
fprintf(stderr, "fail geteuid: %s\n", strerror(errno));
102+
}
103+
}
104+
105+
if ((sockfd = get_service_socket()) < 0) {
106+
return 1;
107+
}
108+
109+
if (setgid(gdb_group)) {
110+
fputs("fail setgid\n", stderr);
111+
}
112+
113+
if (setgroups(1, &gdb_group)) {
114+
fputs("fail setgroups\n", stderr);
115+
}
116+
117+
if (setuid(gdb_user)) {
118+
fputs("fail setuid\n", stderr);
119+
}
120+
121+
p2 = fork();
122+
if (p2 > 0) {
123+
test_result = check_file_request(sockfd, "0:lib:/tmp");
124+
} else if (p2 == 0) {
125+
if (freopen("/dev/null", "w", stderr) == NULL)
126+
exit(1);
127+
char *args[] = {"/usr/bin/cat", "/usr/lib/debug/tmp", NULL};
128+
execvp("/usr/bin/cat", args);
129+
} else {
130+
fputs("error executing fork\n", stderr);
131+
return 1;
132+
}
133+
134+
wait(NULL);
135+
close(sockfd);
136+
return test_result;
137+
}
138+
139+
int main(void)
140+
{
141+
int res = 0;
142+
pid_t p;
143+
p = fork();
144+
if (p == 0) {
145+
if (freopen("/dev/null", "w", stderr) == NULL)
146+
return -1;
147+
char *args[] = {"./clr_debug_fuse", NULL};
148+
execvp("./clr_debug_fuse", args);
149+
} else if (p > 0) {
150+
res = testing_fuse();
151+
kill(p, SIGKILL);
152+
} else {
153+
fputs("error executing fork\n", stderr);
154+
return 1;
155+
}
156+
unlink(SOCKET_PATH);
157+
return res;
158+
}
159+
/*
160+
* Editor modelines - https://www.wireshark.org/tools/modelines.html
161+
*
162+
* Local variables:
163+
* c-basic-offset: 8
164+
* tab-width: 8
165+
* indent-tabs-mode: nil
166+
* End:
167+
*
168+
* vi: set shiftwidth=8 tabstop=8 expandtab:
169+
* :indentSize=8:tabSize=8:noTabs=true:
170+
*/

0 commit comments

Comments
 (0)