Browse code
logger logs success
Greg Wiley authored on 26/04/2017 17:49:46
Showing 9 changed files
Showing 9 changed files
- Makefile.in
- dual_control.cc
- dual_control_test.cc
- logger.cc
- logger.h
- logging_test.cc
- logging.cc
- sys_syslog.cc
- sys_syslog.h
... | ... |
@@ -2,8 +2,9 @@ CXXFLAGS += -fPIC -fno-stack-protector -std=c++14 |
2 | 2 |
CFLAGS += -fPIC -fno-stack-protector |
3 | 3 |
|
4 | 4 |
OBJS = dual_control.o request.o dual_control_integrate.o validator.o conversation.o user.o \ |
5 |
- sys_unistd.o sys_pwd.o token.o sys_fstream.o |
|
6 |
-TESTS = dual_control_test validator_test conversation_test request_test user_test token_test |
|
5 |
+ sys_unistd.o sys_pwd.o token.o sys_fstream.o sys_syslog.o logger.o |
|
6 |
+TESTS = dual_control_test validator_test conversation_test request_test user_test token_test \ |
|
7 |
+ logger_test |
|
7 | 8 |
TESTOBJS = $(patsubst %,%.o,$(TESTS)) |
8 | 9 |
SRCS := $(OBJS:.o=.cc) $(TESTOBJS:.o=.cc) |
9 | 10 |
|
... | ... |
@@ -39,7 +39,7 @@ void use_conversation (dual_control_configuration &config, |
39 | 39 |
|
40 | 40 |
void use_logger (dual_control_configuration &config, logger_ifc *value) |
41 | 41 |
{ |
42 |
- config.logger = logger (share (value)); |
|
42 |
+ config.logger = logger (logger::delegate (value)); |
|
43 | 43 |
} |
44 | 44 |
|
45 | 45 |
class mock_logger : public logger_ifc |
46 | 46 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,36 @@ |
1 |
+/* Copyright (C) CJ Affiliate |
|
2 |
+ * |
|
3 |
+ * You may use, distribute and modify this code under the |
|
4 |
+ * terms of the GNU General Public License version 2 or |
|
5 |
+ * later. |
|
6 |
+ * |
|
7 |
+ * You should have received a copy of the license with this |
|
8 |
+ * file. If not, you will find a copy in the "LICENSE" file |
|
9 |
+ * at https://github.com/cjdev/dual-control. |
|
10 |
+ */ |
|
11 |
+ |
|
12 |
+#include <syslog.h> |
|
13 |
+ |
|
14 |
+#include "sys_syslog.h" |
|
15 |
+#include "logger.h" |
|
16 |
+ |
|
17 |
+namespace { |
|
18 |
+ class impl : public logger_ifc { |
|
19 |
+ private: |
|
20 |
+ sys_syslog syslog_; |
|
21 |
+ public: |
|
22 |
+ impl(const sys_syslog &sys_syslog) : syslog_(sys_syslog) {} |
|
23 |
+ void log (int result, const std::string &user_name, |
|
24 |
+ const std::string &token) { |
|
25 |
+ std::string message(user_name + " " + token + " success"); |
|
26 |
+ |
|
27 |
+ syslog_.openlog("", 0, LOG_AUTHPRIV); |
|
28 |
+ syslog_.syslog(LOG_NOTICE, message.c_str()); |
|
29 |
+ syslog_.closelog(); |
|
30 |
+ } |
|
31 |
+ }; |
|
32 |
+} |
|
33 |
+ |
|
34 |
+logger logger::create(const sys_syslog &sys_syslog) { |
|
35 |
+ return logger(delegate(new impl(sys_syslog))); |
|
36 |
+} |
... | ... |
@@ -15,6 +15,8 @@ |
15 | 15 |
#include <memory> |
16 | 16 |
#include <string> |
17 | 17 |
|
18 |
+#include "sys_syslog.h" |
|
19 |
+ |
|
18 | 20 |
class logger_ifc |
19 | 21 |
{ |
20 | 22 |
public: |
... | ... |
@@ -25,17 +27,20 @@ public: |
25 | 27 |
|
26 | 28 |
class logger : public logger_ifc |
27 | 29 |
{ |
30 |
+ public: |
|
31 |
+ typedef std::shared_ptr<logger_ifc> delegate; |
|
28 | 32 |
private: |
29 |
- std::shared_ptr<logger_ifc> delegate_; |
|
33 |
+ delegate delegate_; |
|
30 | 34 |
public: |
31 |
- logger (const std::shared_ptr<logger_ifc> &delegate) : delegate_ |
|
35 |
+ logger (const delegate &delegate) : delegate_ |
|
32 | 36 |
(delegate) {} |
33 |
- logger() : logger (std::shared_ptr<logger_ifc> (new logger_ifc)) {} |
|
37 |
+ logger() : logger (delegate (new logger_ifc)) {} |
|
34 | 38 |
void log (int result, const std::string &user_name, |
35 | 39 |
const std::string &token) |
36 | 40 |
{ |
37 | 41 |
delegate_->log (result, user_name, token); |
38 | 42 |
} |
43 |
+ static logger create(const sys_syslog &sys_syslog); |
|
39 | 44 |
}; |
40 | 45 |
|
41 | 46 |
#endif |
42 | 47 |
similarity index 62% |
43 | 48 |
rename from logging_test.cc |
44 | 49 |
rename to logger_test.cc |
... | ... |
@@ -9,13 +9,65 @@ |
9 | 9 |
* at https://github.com/cjdev/dual-control. |
10 | 10 |
*/ |
11 | 11 |
|
12 |
-#include <cstdio> |
|
13 |
-#include <cstring> |
|
14 | 12 |
#include <syslog.h> |
13 |
+#include <security/pam_modules.h> |
|
15 | 14 |
|
16 |
-// #include "logging.h" |
|
15 |
+#include <iostream> |
|
16 |
+ |
|
17 |
+#include "sys_syslog.h" |
|
18 |
+#include "logger.h" |
|
17 | 19 |
#include "test_util.h" |
18 | 20 |
|
21 |
+class mock_syslog : public sys_syslog_ifc { |
|
22 |
+ public: |
|
23 |
+ int facility; |
|
24 |
+ std::string message; |
|
25 |
+ int priority; |
|
26 |
+ void openlog(const char *ident, int logopt, int facility) { |
|
27 |
+ this->facility = facility; |
|
28 |
+ } |
|
29 |
+ void vsyslog(int priority, const char *message, va_list args) { |
|
30 |
+ this->priority = priority; |
|
31 |
+ this->message = message; |
|
32 |
+ } |
|
33 |
+ void closelog() |
|
34 |
+ {} |
|
35 |
+ |
|
36 |
+}; |
|
37 |
+ |
|
38 |
+ |
|
39 |
+int logs_success() { |
|
40 |
+ //given |
|
41 |
+ mock_syslog *capture = new mock_syslog; |
|
42 |
+ sys_syslog::delegate test_delegate(capture); |
|
43 |
+ sys_syslog test_syslog(test_delegate); |
|
44 |
+ logger logger = logger::create(test_syslog); |
|
45 |
+ std::string user("user"); |
|
46 |
+ std::string token("token"); |
|
47 |
+ |
|
48 |
+ //when |
|
49 |
+ logger.log(PAM_SUCCESS, user, token); |
|
50 |
+ |
|
51 |
+ //then |
|
52 |
+ check(capture->facility == LOG_AUTHPRIV, "facility does not match"); |
|
53 |
+ check(capture->message == user + " " + token + " " + "success", "message does not match"); |
|
54 |
+ check(capture->priority == LOG_NOTICE, "priority does not match"); |
|
55 |
+ succeed(); |
|
56 |
+} |
|
57 |
+ |
|
58 |
+RESET_VARS_START |
|
59 |
+RESET_VARS_END |
|
60 |
+ |
|
61 |
+int run_tests() |
|
62 |
+{ |
|
63 |
+ test (logs_success); |
|
64 |
+ succeed(); |
|
65 |
+} |
|
66 |
+ |
|
67 |
+int main (int numargs, char **args) |
|
68 |
+{ |
|
69 |
+ return !run_tests(); |
|
70 |
+} |
|
19 | 71 |
|
20 | 72 |
|
21 | 73 |
/* |
22 | 74 |
deleted file mode 100644 |
... | ... |
@@ -1,32 +0,0 @@ |
1 |
-/* Copyright (C) CJ Affiliate |
|
2 |
- * |
|
3 |
- * You may use, distribute and modify this code under the |
|
4 |
- * terms of the GNU General Public License version 2 or |
|
5 |
- * later. |
|
6 |
- * |
|
7 |
- * You should have received a copy of the license with this |
|
8 |
- * file. If not, you will find a copy in the "LICENSE" file |
|
9 |
- * at https://github.com/cjdev/dual-control. |
|
10 |
- */ |
|
11 |
- |
|
12 |
-#include <syslog.h> |
|
13 |
- |
|
14 |
-//#include "logging.h" |
|
15 |
-#include "test_support.h" |
|
16 |
- |
|
17 |
-static const char program_name[] = "pam_dual_control"; |
|
18 |
- |
|
19 |
-void log_success() |
|
20 |
-{ |
|
21 |
- openlog (program_name, 0, LOG_AUTHPRIV); |
|
22 |
- syslog (LOG_NOTICE, "dual control succeeded"); |
|
23 |
- closelog(); |
|
24 |
-} |
|
25 |
- |
|
26 |
-void log_failure() |
|
27 |
-{ |
|
28 |
- openlog (program_name, 0, LOG_AUTHPRIV); |
|
29 |
- syslog (LOG_NOTICE, "dual control failed"); |
|
30 |
- closelog(); |
|
31 |
-} |
|
32 |
- |
33 | 0 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,26 @@ |
1 |
+#include <syslog.h> |
|
2 |
+ |
|
3 |
+#include "sys_syslog.h" |
|
4 |
+ |
|
5 |
+namespace { |
|
6 |
+class impl : public sys_syslog_ifc { |
|
7 |
+ public: |
|
8 |
+ void openlog(const char *ident, int logopt, int facility) { |
|
9 |
+ ::openlog(ident, logopt, facility); |
|
10 |
+ } |
|
11 |
+ |
|
12 |
+ void vsyslog(int priority, const char *message, va_list args) { |
|
13 |
+ ::vsyslog(priority, message, args); |
|
14 |
+ } |
|
15 |
+ |
|
16 |
+ void closelog() { |
|
17 |
+ ::closelog(); |
|
18 |
+ } |
|
19 |
+}; |
|
20 |
+} |
|
21 |
+ |
|
22 |
+sys_syslog sys_syslog::create() { |
|
23 |
+ static sys_syslog singleton(sys_syslog::delegate(new impl)); |
|
24 |
+ return singleton; |
|
25 |
+} |
|
26 |
+ |
0 | 27 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,42 @@ |
1 |
+#ifndef SYS_SYSLOG_H |
|
2 |
+#define SYS_SYSLOG_H |
|
3 |
+ |
|
4 |
+#include <memory> |
|
5 |
+#include <cstdarg> |
|
6 |
+#include <syslog.h> |
|
7 |
+ |
|
8 |
+class sys_syslog_ifc { |
|
9 |
+ public: |
|
10 |
+ virtual void openlog(const char *ident, int logopt, int facility) {} |
|
11 |
+ virtual void vsyslog(int priority, const char *message, va_list args) {} |
|
12 |
+ virtual void closelog() {} |
|
13 |
+}; |
|
14 |
+ |
|
15 |
+class sys_syslog : public sys_syslog_ifc { |
|
16 |
+ public: |
|
17 |
+ typedef std::shared_ptr<sys_syslog_ifc> delegate; |
|
18 |
+ private: |
|
19 |
+ delegate delegate_; |
|
20 |
+ public: |
|
21 |
+ sys_syslog(const delegate &delegate) : delegate_(delegate) {} |
|
22 |
+ sys_syslog() : sys_syslog(delegate(new sys_syslog_ifc)) {} |
|
23 |
+ void openlog(const char *ident, int logopt, int facility) { |
|
24 |
+ delegate_->openlog(ident, logopt, facility); |
|
25 |
+ } |
|
26 |
+ void syslog(int priority, const char *format, ...) { |
|
27 |
+ va_list vargs; |
|
28 |
+ va_start(vargs, format); |
|
29 |
+ vsyslog(priority, format, vargs); |
|
30 |
+ va_end(vargs); |
|
31 |
+ } |
|
32 |
+ void vsyslog(int priority, const char *message, va_list vargs) { |
|
33 |
+ delegate_->vsyslog(priority, message, vargs); |
|
34 |
+ } |
|
35 |
+ void closelog() { |
|
36 |
+ delegate_->closelog(); |
|
37 |
+ } |
|
38 |
+ |
|
39 |
+ static sys_syslog create(); |
|
40 |
+}; |
|
41 |
+ |
|
42 |
+#endif |