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);
std::string user ("user");
std::string token ("token");
|
a9690b1d |
//when
|
bc6c3d35 |
logger.log (PAM_SUCCESS, user, token);
|
a9690b1d |
//then
|
bc6c3d35 |
check (capture->facility == LOG_AUTHPRIV, "facility does not match");
check (capture->message == user + " " + token + " " + "success",
"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);
std::string user ("user");
std::string token ("token");
|
9526a1d3 |
//when
|
bc6c3d35 |
logger.log (PAM_AUTH_ERR, user, token);
|
9526a1d3 |
//then
|
bc6c3d35 |
check (capture->facility == LOG_AUTHPRIV, "facility does not match");
check (capture->message == user + " " + token + " " + "fail",
"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);
std::string user ("user");
std::string token ("token");
|
ebbc2787 |
//when
|
bc6c3d35 |
logger.log (PAM_SERVICE_ERR, user, token);
|
ebbc2787 |
//then
|
bc6c3d35 |
check (capture->facility == LOG_AUTH, "facility does not match");
check (capture->message == user + " pam returned error",
"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 |
RESET_VARS_START
RESET_VARS_END
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 |
/*
|
9fe53359 |
int logged_priority = -1000;
|
a4c373a6 |
const char *logged_message = "";
|
b017a4d2 |
void fake_syslog (int priority, const char *message, ...)
|
cdf7fd74 |
{
|
9fe53359 |
logged_priority = priority;
|
a4c373a6 |
logged_message = message;
|
6f45b32f |
}
|
4a5fbc12 |
int close_log_invoked = 0;
|
b017a4d2 |
void fake_closelog (void)
|
cdf7fd74 |
{
|
4a5fbc12 |
close_log_invoked = 1;
|
6f45b32f |
}
int opened_facility = -1000;
|
b9244c44 |
const char *opened_program_name = "";
|
08aca5aa |
int opened_logopt = -1000;
|
b017a4d2 |
void fake_openlog (const char *ident, int logopt, int facility)
|
cdf7fd74 |
{
|
6f45b32f |
opened_facility = facility;
|
b9244c44 |
opened_program_name = ident;
|
08aca5aa |
opened_logopt = logopt;
|
6f45b32f |
}
|
704ed597 |
RESET_VARS_START
logged_priority = -1000;
close_log_invoked = 0;
opened_facility = -1000;
const char *opened_program_name = "";
int opened_logopt = -1000;
RESET_VARS_END
|
cdf7fd74 |
int test_log_success()
{
|
6f45b32f |
// given
// when
log_success();
// then
|
b017a4d2 |
checkint (LOG_AUTHPRIV, opened_facility, "facility");
checkint (LOG_NOTICE, logged_priority, "priority");
checkint (0, opened_logopt, "logopt");
check (close_log_invoked, "log closed");
checkstr ("pam_dual_control", opened_program_name, "program name");
checkstr ("dual control succeeded", logged_message, "logged message");
|
f2d9dbb5 |
succeed();
|
6f45b32f |
}
|
cdf7fd74 |
int test_log_failure()
{
|
6120f050 |
//given
//when
log_failure();
//then
|
b017a4d2 |
checkint (LOG_AUTHPRIV, opened_facility, "facility");
checkint (LOG_NOTICE, logged_priority, "priority");
checkint (0, opened_logopt, "logopt");
check (close_log_invoked, "log closed");
checkstr ("pam_dual_control", opened_program_name, "program name");
checkstr ("dual control failed", logged_message, "logged message");
|
6120f050 |
succeed();
}
|
cdf7fd74 |
int test_runner()
{
|
b017a4d2 |
test (test_log_success);
test (test_log_failure);
|
9dd224b0 |
succeed();
}
|
6f45b32f |
|
b017a4d2 |
int main (int numargs, char **args)
|
cdf7fd74 |
{
|
9dd224b0 |
return !test_runner();
|
6f45b32f |
}
|
b5e563ba |
*/
|
704ed597 |
|