Skip to content

Instantly share code, notes, and snippets.

@jcttrll
Last active November 30, 2017 19:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcttrll/95fbc34f3154d70d67a2871d278887d8 to your computer and use it in GitHub Desktop.
Save jcttrll/95fbc34f3154d70d67a2871d278887d8 to your computer and use it in GitHub Desktop.
C mocking experiment
gcc -c main.c -o main.o
gcc -c framework.c -o framework.o
gcc -c test.c -o test.o
gcc -c mock.c -o mock.o && ar rcs mock.a mock.o; rm mock.o
ld custom.ld test.o main.o framework.o \
-L. --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker \
/lib64/ld-linux-x86-64.so.2 \
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crt1.o \
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crti.o \
/usr/lib64/gcc/x86_64-suse-linux/4.8/crtbegin.o \
-L/usr/lib64/gcc/x86_64-suse-linux/4.8 \
-L/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64 \
-L/lib/../lib64 \
-L/usr/lib/../lib64 \
-L/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../x86_64-suse-linux/lib \
-L/usr/lib64/gcc/x86_64-suse-linux/4.8/../../.. \
-l:mock.a \
-lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed \
/usr/lib64/gcc/x86_64-suse-linux/4.8/crtend.o \
/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crtn.o \
-o main
SECTIONS {
.text : {
PROVIDE(_real_puts = "printf@@GLIBC_2.2.5" );
}
}
#include "framework.h"
int mocks_active = 0;
void activate_mocks() {
mocks_active = 1;
}
void activate_mocks();
int main() {
test();
return 0;
}
#include <stdio.h>
#undef puts
extern int mocks_active;
int puts(const char *s) {
if (mocks_active) {
return printf("Mock\n");
} else {
return _real_puts(s);
}
}
#include <stdio.h>
#include "framework.h"
void test() {
puts("Hello\n");
activate_mocks();
puts("Hello\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment