Skip to content

Commit 374d70d

Browse files
committed
Add test case for file descriptors
1 parent e58cf09 commit 374d70d

File tree

4 files changed

+172
-1
lines changed

4 files changed

+172
-1
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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/types.h>
4+
#include <sys/stat.h>
5+
#include <fcntl.h>
6+
#include <unistd.h>
7+
#include <string.h>
8+
#include <signal.h>
9+
#include <assert.h>
10+
11+
#define PFX "[test-fd] "
12+
13+
#define FD_NUM 1000
14+
15+
static volatile sig_atomic_t signalled;
16+
17+
static void sighandler(int num)
18+
{
19+
signalled = num;
20+
}
21+
22+
static void notify_ready(const char *pipe)
23+
{
24+
int ret;
25+
int fd;
26+
char msg[64];
27+
28+
fd = open(pipe, O_WRONLY);
29+
assert(fd >= 0);
30+
31+
snprintf(msg, sizeof(msg), PFX "pid %d ready\n", getpid());
32+
ret = write(fd, msg, strlen(msg));
33+
assert(ret == strlen(msg));
34+
35+
close(fd);
36+
}
37+
38+
int main(int argc, char *argv[])
39+
{
40+
int ret;
41+
42+
if (argc < 2)
43+
return 1;
44+
45+
signal(SIGUSR1, sighandler);
46+
47+
printf(PFX "pid %d\n", getpid());
48+
49+
int flags[FD_NUM];
50+
51+
for (int i = 0; i < FD_NUM - 3; i++) {
52+
ret = open("/dev/null", O_RDWR);
53+
if (ret == -1) {
54+
perror(PFX "open");
55+
return 1;
56+
}
57+
}
58+
59+
for (int fd = 0; fd < FD_NUM; fd++) {
60+
ret = fcntl(fd, F_GETFD);
61+
if (ret == -1) {
62+
perror(PFX "fcntl");
63+
return 1;
64+
}
65+
66+
flags[fd] = ret;
67+
}
68+
69+
printf(PFX "waiting for SIGUSR1\n");
70+
71+
notify_ready(argv[1]);
72+
73+
while (!signalled)
74+
usleep(10 * 1000);
75+
76+
printf(PFX "signalled (%s)\n", strsignal(signalled));
77+
78+
/* test that the fds are still open and have the same flags */
79+
80+
for (int fd = 0; fd < FD_NUM; fd++) {
81+
ret = fcntl(fd, F_GETFD);
82+
83+
if (ret != flags[fd]) {
84+
printf(PFX "not ok, %d != %d for fd %d\n", ret, flags[fd], fd);
85+
return 1;
86+
}
87+
}
88+
89+
for (int fd = 3; fd < FD_NUM; fd++)
90+
close(fd);
91+
92+
printf(PFX "ok\n");
93+
94+
return 0;
95+
}

0 commit comments

Comments
 (0)