Last active
November 30, 2017 19:05
-
-
Save jcttrll/95fbc34f3154d70d67a2871d278887d8 to your computer and use it in GitHub Desktop.
C mocking experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SECTIONS { | |
.text : { | |
PROVIDE(_real_puts = "printf@@GLIBC_2.2.5" ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "framework.h" | |
int mocks_active = 0; | |
void activate_mocks() { | |
mocks_active = 1; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void activate_mocks(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() { | |
test(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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