Skip to content

Commit 719e1cd

Browse files
committed
Add test case for file descriptors
1 parent e58cf09 commit 719e1cd

File tree

5 files changed

+208
-2
lines changed

5 files changed

+208
-2
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ $(B)/libencrypt.so: libencrypt.c
188188

189189

190190
tests: $(B)/memcr
191-
$(MAKE) -C tests CC=$(CC) MEMCR=../$< COMPRESS_LZ4=$(COMPRESS_LZ4) CHECKSUM_MD5=$(CHECKSUM_MD5) ENCRYPT=$(ENCRYPT)
191+
$(MAKE) -C tests CC=$(CC) MEMCR=../$< COMPRESS_LZ4=$(COMPRESS_LZ4) COMPRESS_ZSTD=$(COMPRESS_ZSTD) CHECKSUM_MD5=$(CHECKSUM_MD5) ENCRYPT=$(ENCRYPT)
192192

193193
ifeq ($(ENCRYPT), 1)
194194
tests: $(B)/libencrypt.so

tests/run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ TIME_START=$(date +%s%N)
3131
. ./run_ok_test.sh
3232
. ./run_corrupt_test.sh
3333
. ./run_signal_test.sh
34+
. ./run_fd_test.sh
3435

3536
TIME_END=$(date +%s%N)
3637
TIME_ELAPSED_MS=$(((TIME_END - TIME_START) / 1000000))

tests/run_fd_test.sh

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#!/bin/sh
2+
3+
if [ "$0" = "$_" ]; then
4+
echo "this script should not be called directly"
5+
exit 1;
6+
fi
7+
8+
NAME=./run_fd_test.sh
9+
10+
TEST_CNT=0
11+
TEST_PIPE=./test-pipe
12+
13+
do_memcr_test()
14+
{
15+
if [ -z "$1" ]; then
16+
MEMCR_ENV=""
17+
else
18+
MEMCR_ENV="$1 "
19+
fi
20+
21+
MEMCR_CMD="$DO$MEMCR_ENV$MEMCR -d /tmp $2"
22+
TEST=$3
23+
24+
TEST_CNT=$((TEST_CNT + 1))
25+
echo "${WHITE}[test $TEST_CNT] $MEMCR_CMD -p .. for $TEST${NOFMT}"
26+
27+
mkfifo $TEST_PIPE
28+
29+
# start the test
30+
./"$TEST" $TEST_PIPE &
31+
TPID=$!
32+
33+
# wait for test to be ready
34+
cat $TEST_PIPE
35+
rm $TEST_PIPE
36+
37+
# memcr
38+
$MEMCR_CMD -p $TPID
39+
RET=$?
40+
if [ $RET -ne 0 ]; then
41+
echo "${RED}[test $TEST_CNT] failed, memcr exit code is $RET${NOFMT}"
42+
kill $TPID
43+
return 1
44+
fi
45+
46+
# stop the test
47+
kill -USR1 $TPID
48+
wait $TPID
49+
RET=$?
50+
if [ $RET -ne 0 ]; then
51+
echo "${RED}[test $TEST_CNT] failed, ${TEST} exit code is $RET${NOFMT}"
52+
return 1
53+
fi
54+
55+
echo "${GREEN}[test $TEST_CNT] passed${NOFMT}"
56+
return 0
57+
}
58+
59+
# remove stale test pipe (if any)
60+
rm -f $TEST_PIPE
61+
62+
TESTS="test-fd"
63+
64+
echo "${BOLD}[+] $NAME start${NOFMT}"
65+
66+
# basic tests
67+
for OPT in "" "--proc-mem" "--rss-file" "--proc-mem --rss-file"; do
68+
for TEST in $TESTS; do
69+
do_memcr_test "" "-n $OPT" "$TEST" || exit 1
70+
done
71+
done
72+
73+
echo "${BOLD}[+] $NAME $TEST_CNT tests passed${NOFMT}"
74+
75+
TESTS_DONE=$((TESTS_DONE + TEST_CNT))

tests/test-fd.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright (C) 2025 Mariusz Kozłowski
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, version 2
7+
* of the license.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this library; if not, write to the Free Software Foundation,
16+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
19+
#include <stdio.h>
20+
#include <stdlib.h>
21+
#include <sys/types.h>
22+
#include <sys/stat.h>
23+
#include <fcntl.h>
24+
#include <unistd.h>
25+
#include <string.h>
26+
#include <signal.h>
27+
#include <assert.h>
28+
29+
#define PFX "[test-fd] "
30+
31+
#define FD_NUM 1000
32+
33+
static volatile sig_atomic_t signalled;
34+
35+
static void sighandler(int num)
36+
{
37+
signalled = num;
38+
}
39+
40+
static void notify_ready(const char *pipe)
41+
{
42+
int ret;
43+
int fd;
44+
char msg[64];
45+
46+
fd = open(pipe, O_WRONLY);
47+
assert(fd >= 0);
48+
49+
snprintf(msg, sizeof(msg), PFX "pid %d ready\n", getpid());
50+
ret = write(fd, msg, strlen(msg));
51+
assert(ret == strlen(msg));
52+
53+
close(fd);
54+
}
55+
56+
int main(int argc, char *argv[])
57+
{
58+
int ret;
59+
int flags[FD_NUM];
60+
61+
if (argc < 2)
62+
return 1;
63+
64+
signal(SIGUSR1, sighandler);
65+
66+
printf(PFX "pid %d\n", getpid());
67+
68+
for (int i = 0; i < FD_NUM - 3; i++) {
69+
ret = open("/dev/null", O_RDWR);
70+
if (ret == -1) {
71+
perror(PFX "open");
72+
return 1;
73+
}
74+
}
75+
76+
for (int fd = 0; fd < FD_NUM; fd++) {
77+
ret = fcntl(fd, F_GETFD);
78+
if (ret == -1) {
79+
perror(PFX "fcntl");
80+
return 1;
81+
}
82+
83+
flags[fd] = ret;
84+
}
85+
86+
printf(PFX "waiting for SIGUSR1\n");
87+
88+
notify_ready(argv[1]);
89+
90+
while (!signalled)
91+
usleep(10 * 1000);
92+
93+
printf(PFX "signalled (%s)\n", strsignal(signalled));
94+
95+
/* test that the fds are still open and have the same flags */
96+
97+
for (int fd = 0; fd < FD_NUM; fd++) {
98+
ret = fcntl(fd, F_GETFD);
99+
100+
if (ret != flags[fd]) {
101+
printf(PFX "not ok, %d != %d for fd %d\n", ret, flags[fd], fd);
102+
return 1;
103+
}
104+
}
105+
106+
for (int fd = 3; fd < FD_NUM; fd++)
107+
close(fd);
108+
109+
printf(PFX "ok\n");
110+
111+
return 0;
112+
}

tests/test-malloc.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
/*
2+
* Copyright (C) 2024-2025 Mariusz Kozłowski
3+
*
4+
* This program is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU General Public License
6+
* as published by the Free Software Foundation, version 2
7+
* of the license.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12+
* General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this library; if not, write to the Free Software Foundation,
16+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
17+
*/
18+
119
#include <stdio.h>
220
#include <stdlib.h>
321
#include <sys/types.h>
@@ -79,4 +97,4 @@ int main(int argc, char *argv[])
7997

8098
free(memb);
8199
return 0;
82-
}
100+
}

0 commit comments

Comments
 (0)