Browse code
rethink fake sys calls
Greg Wiley authored on 06/04/2017 20:31:16
Showing 13 changed files
Showing 13 changed files
- .gitignore
- Makefile.in
- dc_syslog.c
- dc_syslog.h
- dual_control_test.c
- logging.c
- logging_test
- logging_test.c
- test_support.h
- testutil.h
- token.c
- token_test
- token_test.c
... | ... |
@@ -1,7 +1,7 @@ |
1 | 1 |
MODULEFLAGS = -fPIC -fno-stack-protector |
2 |
-MODULEOBJS = dual_control.o logging.o dc_syslog.o token.o |
|
3 |
-HEADERS = logging.h dc_syslog.h |
|
4 |
- |
|
2 |
+MODULEOBJS = dual_control.o logging.o token.o |
|
3 |
+TESTOBJS = $(patsubst %.o, t_%.o, $(MODULEOBJS)) |
|
4 |
+HEADERS = logging.h test_support.h token.h |
|
5 | 5 |
MODULELIB = pam_dual_control.so |
6 | 6 |
UNAME_S := $(shell uname -s) |
7 | 7 |
|
... | ... |
@@ -15,6 +15,9 @@ dual_control.a: $(MODULEOBJS) |
15 | 15 |
$(MODULEOBJS): %.o: %.c $(HEADERS) |
16 | 16 |
$(CC) -c $(CFLAGS) $(CPPFLAGS) $(MODULEFLAGS) $< -o $@ |
17 | 17 |
|
18 |
+$(TESTOBJS): t_%.o: %.c $(HEADERS) |
|
19 |
+ $(CC) -c $(CFLAGS) $(CPPFLAGS) $(MODULEFLAGS) -D UNIT_TEST $< -o $@ |
|
20 |
+ |
|
18 | 21 |
.PHONY: clean |
19 | 22 |
clean: |
20 | 23 |
@rm -f *.o *.a |
... | ... |
@@ -24,17 +27,15 @@ clean: |
24 | 27 |
distclean: clean |
25 | 28 |
@rm -f Makefile config.h |
26 | 29 |
|
27 |
-dual_control_test: dual_control_test.c dual_control.o |
|
30 |
+dual_control_test: dual_control_test.c t_dual_control.o |
|
28 | 31 |
$(CC) $(CFLAGS) $(CPPFLAGS) -lpam -o $@ $^ |
29 | 32 |
|
30 |
-logging_test: logging_test.c logging.o |
|
33 |
+logging_test: logging_test.c t_logging.o |
|
31 | 34 |
$(CC) $(CFLAGS) $(CPPFLAGS) -lpam -o $@ $^ |
32 | 35 |
|
33 |
-token_test: token_test.c token.o |
|
36 |
+token_test: token_test.c t_token.o |
|
34 | 37 |
$(CC) $(CFLAGS) $(CPPFLAGS) -lpam -o $@ $^ |
35 | 38 |
|
36 |
- |
|
37 |
- |
|
38 | 39 |
.PHONY: test |
39 | 40 |
test: dual_control_test logging_test token_test |
40 | 41 |
@./dual_control_test |
41 | 42 |
deleted file mode 100644 |
... | ... |
@@ -1,17 +0,0 @@ |
1 |
-#include <stdarg.h> |
|
2 |
-#include "dc_syslog.h" |
|
3 |
- |
|
4 |
-void dc_openlog(const char *ident, int logopt, int facility) { |
|
5 |
- openlog(ident, logopt, facility); |
|
6 |
-} |
|
7 |
- |
|
8 |
-void dc_syslog(int priority, const char *format, ...) { |
|
9 |
- va_list varargs; |
|
10 |
- va_start(varargs, format); |
|
11 |
- vsyslog(priority, format, varargs); |
|
12 |
- va_end(varargs); |
|
13 |
-} |
|
14 |
-void dc_closelog(void) { |
|
15 |
- closelog(); |
|
16 |
-} |
|
17 |
- |
... | ... |
@@ -1,19 +1,21 @@ |
1 |
+#include <syslog.h> |
|
2 |
+ |
|
1 | 3 |
#include "logging.h" |
2 |
-#include "dc_syslog.h" |
|
4 |
+#include "test_support.h" |
|
3 | 5 |
|
4 | 6 |
|
5 | 7 |
static const char program_name[] = "pam_dual_control"; |
6 | 8 |
|
7 | 9 |
void log_success() { |
8 |
- dc_openlog(program_name, 0, LOG_AUTHPRIV); |
|
9 |
- dc_syslog(LOG_NOTICE, "dual control succeeded"); |
|
10 |
- dc_closelog(); |
|
10 |
+ openlog(program_name, 0, LOG_AUTHPRIV); |
|
11 |
+ syslog(LOG_NOTICE, "dual control succeeded"); |
|
12 |
+ closelog(); |
|
11 | 13 |
} |
12 | 14 |
|
13 | 15 |
void log_failure() { |
14 |
- dc_openlog(program_name, 0, LOG_AUTHPRIV); |
|
15 |
- dc_syslog(LOG_NOTICE, "dual control failed"); |
|
16 |
- dc_closelog(); |
|
16 |
+ openlog(program_name, 0, LOG_AUTHPRIV); |
|
17 |
+ syslog(LOG_NOTICE, "dual control failed"); |
|
18 |
+ closelog(); |
|
17 | 19 |
} |
18 | 20 |
|
19 | 21 |
|
... | ... |
@@ -1,27 +1,26 @@ |
1 | 1 |
#include <stdio.h> |
2 | 2 |
#include <string.h> |
3 |
+#include <syslog.h> |
|
3 | 4 |
|
4 |
-#include "dc_syslog.h" |
|
5 | 5 |
#include "logging.h" |
6 |
- |
|
7 |
-#include "testutil.h" |
|
6 |
+#include "test_util.h" |
|
8 | 7 |
|
9 | 8 |
int logged_priority = -1000; |
10 | 9 |
const char *logged_message = ""; |
11 |
-void dc_syslog(int priority, const char *message, ...) { |
|
10 |
+void fake_syslog(int priority, const char *message, ...) { |
|
12 | 11 |
logged_priority = priority; |
13 | 12 |
logged_message = message; |
14 | 13 |
} |
15 | 14 |
|
16 | 15 |
int close_log_invoked = 0; |
17 |
-void dc_closelog(void) { |
|
16 |
+void fake_closelog(void) { |
|
18 | 17 |
close_log_invoked = 1; |
19 | 18 |
} |
20 | 19 |
|
21 | 20 |
int opened_facility = -1000; |
22 | 21 |
const char *opened_program_name = ""; |
23 | 22 |
int opened_logopt = -1000; |
24 |
-void dc_openlog(const char *ident, int logopt, int facility) { |
|
23 |
+void fake_openlog(const char *ident, int logopt, int facility) { |
|
25 | 24 |
opened_facility = facility; |
26 | 25 |
opened_program_name = ident; |
27 | 26 |
opened_logopt = logopt; |
28 | 27 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,23 @@ |
1 |
+/* Include this in a module that will be under test coverage |
|
2 |
+ * |
|
3 |
+ */ |
|
4 |
+ |
|
5 |
+#ifndef _TEST_SUPPORT_H |
|
6 |
+#define _TEST_SUPPORT_H |
|
7 |
+ |
|
8 |
+void fake_openlog(const char *ident, int logopt, int facility); |
|
9 |
+void fake_syslog(int priority, const char *format, ...); |
|
10 |
+void fake_closelog(void); |
|
11 |
+ |
|
12 |
+/* |
|
13 |
+ * replace C library functions with fake counterparts when UINT_TEST symbol |
|
14 |
+ * is defined |
|
15 |
+ */ |
|
16 |
+#ifdef UNIT_TEST |
|
17 |
+#define openlog(IDENT, LOGOPT, FACILITY) fake_openlog(IDENT, LOGOPT, FACILITY) |
|
18 |
+#define syslog(PRIORITY, ...) fake_syslog(PRIORITY, __VA_ARGS__) |
|
19 |
+#define closelog() fake_closelog() |
|
20 |
+#endif |
|
21 |
+ |
|
22 |
+#endif |
|
23 |
+ |