git.fiddlerwoaroof.com
logger_test.cc
7684972a
 /* Copyright (C) CJ Affiliate
  *
  * You may use, distribute and modify this code under  the
  * terms of the  GNU General Public License  version 2  or
  * later.
  *
  * You should have received a copy of the license with this
  * file. If not, you will find a copy in the "LICENSE" file
  * at https://github.com/cjdev/dual-control.
  */
 
1c7f8bf0
 #include <syslog.h>
a9690b1d
 #include <security/pam_modules.h>
6f45b32f
 
a9690b1d
 #include <iostream>
 
 #include "sys_syslog.h"
 #include "logger.h"
1c7f8bf0
 #include "test_util.h"
a26b1d7c
 
bc6c3d35
 class mock_syslog : public sys_syslog_ifc
 {
 public:
     int facility;
     std::string message;
     int priority;
     bool closed;
     std::string ident;
     mock_syslog() : closed (false), facility (-1000), priority (-1000) {}
     void openlog (const char *ident, int logopt, int facility)
     {
         this->facility = facility;
         this->ident = ident;
     }
     void vsyslog (int priority, const char *message, va_list args)
     {
         this->priority = priority;
         this->message = message;
     }
     void closelog()
     {
         this->closed = true;
     }
a9690b1d
 
 };
 
bc6c3d35
 int logs_success()
 {
a9690b1d
     //given
     mock_syslog *capture = new mock_syslog;
bc6c3d35
     sys_syslog::delegate test_delegate (capture);
     sys_syslog test_syslog (test_delegate);
     logger logger = logger::create (test_syslog);
47142d31
     std::string requester_user ("requester_user");
     std::string authorizer_user ("authorizer_user");
bc6c3d35
     std::string token ("token");
b14fb8c6
     std::string reason ("reason");
a9690b1d
 
     //when
3fc168fa
     logger.log (PAM_SUCCESS, requester_user, authorizer_user, token, reason);
a9690b1d
 
     //then
bc6c3d35
     check (capture->facility == LOG_AUTHPRIV, "facility does not match");
b14fb8c6
     check (capture->message == requester_user + " " + authorizer_user + " " +
            reason + " " +
8804b544
            "success",
bc6c3d35
            "message does not match");
     check (capture->priority == LOG_NOTICE, "priority does not match");
     check (capture->closed, "syslog not closed");
     check (capture->ident == "dual-control", "dual-control");
a9690b1d
     succeed();
 }
 
bc6c3d35
 int logs_failure()
 {
9526a1d3
     //given
     mock_syslog *capture = new mock_syslog;
bc6c3d35
     sys_syslog::delegate test_delegate (capture);
     sys_syslog test_syslog (test_delegate);
     logger logger = logger::create (test_syslog);
47142d31
     std::string requester ("requestuser");
     std::string authorizer ("authuser");
bc6c3d35
     std::string token ("token");
b14fb8c6
     std::string reason ("reason");
9526a1d3
 
     //when
3fc168fa
     logger.log (PAM_AUTH_ERR, requester, authorizer, token, reason);
9526a1d3
 
     //then
bc6c3d35
     check (capture->facility == LOG_AUTHPRIV, "facility does not match");
3fc168fa
     check (capture->message == requester + " " + authorizer + " " + reason + " "
706636a4
            + "fail",
bc6c3d35
            "message does not match");
     check (capture->priority == LOG_NOTICE, "priority does not match");
     check (capture->closed, "syslog not closed");
     check (capture->ident == "dual-control", "dual-control");
9526a1d3
     succeed();
 }
 
bc6c3d35
 int logs_pam_service_error()
 {
ebbc2787
     //given
     mock_syslog *capture = new mock_syslog;
bc6c3d35
     sys_syslog::delegate test_delegate (capture);
     sys_syslog test_syslog (test_delegate);
     logger logger = logger::create (test_syslog);
47142d31
     std::string requester ("user");
     std::string authorizer ("user");
bc6c3d35
     std::string token ("token");
b14fb8c6
     std::string reason ("reason");
ebbc2787
 
     //when
3fc168fa
     logger.log (PAM_SERVICE_ERR, requester, authorizer, token, reason);
ebbc2787
 
     //then
bc6c3d35
     check (capture->facility == LOG_AUTH, "facility does not match");
3fc168fa
     check (capture->message == requester + " " + authorizer + " " + reason +
706636a4
            " pam returned error",
bc6c3d35
            "message does not match");
     check (capture->priority == LOG_ERR, "priority does not match");
     check (capture->closed, "syslog not closed");
     check (capture->ident == "dual-control", "dual-control");
ebbc2787
     succeed();
 }
 
a9690b1d
 int run_tests()
 {
     test (logs_success);
9526a1d3
     test (logs_failure);
ebbc2787
     test (logs_pam_service_error);
a9690b1d
     succeed();
 }
 
 int main (int numargs, char **args)
 {
     return !run_tests();
 }
b5e563ba