Merge pull request #300 from dkg/improve-tests

Test getpid() against a library that invokes getpid() in its constructor
This commit is contained in:
Wolfgang Hommel
2021-02-24 17:54:30 +01:00
committed by GitHub
6 changed files with 51 additions and 9 deletions

5
.gitignore vendored
View File

@@ -2,9 +2,10 @@
*.so.1
timetest
test/getrandom_test
test/librandom.o
test/librandom.so
test/lib*.o
test/lib*.so
test/use_lib_random
test/use_lib_getpid
src/libfaketime.dylib.1
src/libfaketime.1.dylib

View File

@@ -22,23 +22,26 @@ test: timetest functest
functest:
./testframe.sh functests
getrandom_test: getrandom_test.c
%_test: %_test.c
${CC} -o $@ ${CFLAGS} $<
randomtest: getrandom_test use_lib_random
randomtest: getrandom_test use_lib_random librandom.so
./randomtest.sh
librandom.o: librandom.c
getpidtest: use_lib_getpid libgetpid.so
./pidtest.sh
lib%.o: lib%.c
${CC} -c -o $@ -fpic ${CFLAGS} $<
librandom.so: librandom.o
lib%.so: lib%.o
${CC} -o $@ -shared ${CFLAGS} $<
use_lib_random: use_lib_random.c librandom.so
${CC} -L. -o $@ ${CFLAGS} $< -lrandom
use_lib_%: use_lib_%.c lib%.so
${CC} -L. -o $@ ${CFLAGS} $< -l$*
clean:
@rm -f ${OBJ} timetest getrandom_test librandom.o librandom.so use_lib_random
@rm -f ${OBJ} timetest getrandom_test lib*.o lib*.so use_lib_random use_lib_getpid
distclean: clean
@echo

13
test/libgetpid.c Normal file
View File

@@ -0,0 +1,13 @@
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
void getpid_func() {
fprintf(stderr, " called getpid_func()\n");
}
static __attribute__((constructor)) void getpid_init() {
pid_t pid = getpid();
fprintf(stderr, " getpid() yielded %d\n", pid);
}

6
test/libgetpid.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef __LIBGETPID_H__
#define __LIBGETPID_H__
extern void getpid_func();
#endif

View File

@@ -17,3 +17,10 @@ if [ $output != 13 ]; then
printf >&2 'Failed to enforce a rigid response to getpid()\n'
exit 2
fi
printf 'testing shared object with getpid() in library constructor\n'
LD_LIBRARY_PATH=. ./use_lib_getpid
printf 'now with LD_PRELOAD and FAKETIME_FAKEPID\n'
FAKETIME_FAKEPID=25 LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_lib_getpid
printf 'now with LD_PRELOAD without FAKETIME_FAKEPID\n'
LD_PRELOAD="$FTPL" LD_LIBRARY_PATH=. ./use_lib_getpid

12
test/use_lib_getpid.c Normal file
View File

@@ -0,0 +1,12 @@
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include "libgetpid.h"
int main() {
pid_t pid;
getpid_func();
pid = getpid();
fprintf(stderr, " getpid() -> %d\n", pid);
return 0;
}