New test suite for pam_panic_pw
This commit is contained in:
parent
d1412beb45
commit
18a70f2382
1
.gitignore
vendored
1
.gitignore
vendored
@ -34,4 +34,5 @@ src/pam_panic_pw/man/*
|
||||
!src/pam_panic_pw/man/Makefile.am
|
||||
!src/pam_panic_pw/man/man1/
|
||||
stamp-h1
|
||||
test/pwfile
|
||||
test/test
|
||||
|
@ -1,8 +1,11 @@
|
||||
AM_CFLAGS = -DTEST
|
||||
bin_PROGRAMS = test
|
||||
test_SOURCES = ../src/pam_panic/pam_panic_authdevice.c ../src/pam_panic/pam_panic_reject.c test.c
|
||||
test_LDFLAGS = -lpam -lcunit
|
||||
test_SOURCES = ../src/pam_panic/pam_panic_authdevice.c ../src/pam_panic/pam_panic_reject.c ../src/pam_panic_pw/pam_panic_pw.c test.c
|
||||
test_LDFLAGS = -lpam -lcrypt -lcunit
|
||||
|
||||
all:
|
||||
@printf "Running test...\n"
|
||||
./test
|
||||
@printf "Cleaning up...\n"
|
||||
$(RM) ./pwfile
|
||||
|
||||
|
59
test/test.c
59
test/test.c
@ -6,12 +6,14 @@ DATE : 2018-05-11T04:13:51+02:00
|
||||
LICENSE : GNU-GPLv3
|
||||
*/
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <unistd.h>
|
||||
#include <security/pam_modules.h>
|
||||
|
||||
#include "../lib/gettext.h"
|
||||
#include "../src/pam_panic/pam_panic_authdevice.h"
|
||||
#include "../src/pam_panic/pam_panic_reject.h"
|
||||
#include "../src/pam_panic_pw/pam_panic_pw.h"
|
||||
#include <CUnit/Basic.h>
|
||||
|
||||
#define STATE_GOOD 0
|
||||
@ -23,9 +25,15 @@ LICENSE : GNU-GPLv3
|
||||
#define STATE_REJ_POW 2
|
||||
#define STATE_REJ_NA 3
|
||||
|
||||
#define STATE_PPP_WRITEPASSWORDS 0
|
||||
|
||||
#define GOODUUID "./good"
|
||||
#define BADUUID "./bad"
|
||||
|
||||
#define PASSWORDFILE "./pwfile"
|
||||
#define GOODPASSWORD "yip"
|
||||
#define BADPASSWORD "69"
|
||||
|
||||
char* gU = GOODUUID;
|
||||
char* bU = BADUUID;
|
||||
|
||||
@ -85,6 +93,49 @@ void test_rejectNA(void) {
|
||||
}
|
||||
|
||||
|
||||
// pam_panic_pw tests
|
||||
void test_writePassword(void) {
|
||||
|
||||
char encpw[2][99];
|
||||
|
||||
strcpy(encpw[0], crypt(GOODPASSWORD, "$6$somesalt"));
|
||||
strcpy(encpw[1], crypt(BADPASSWORD, "$6$somesalt"));
|
||||
|
||||
int ret = writePasswords(encpw, PASSWORDFILE);
|
||||
CU_ASSERT_EQUAL(ret, STATE_PPP_WRITEPASSWORDS);
|
||||
}
|
||||
|
||||
void test_passwordCheckFromFile(void) {
|
||||
|
||||
int ret;
|
||||
char buf[2][256];
|
||||
char* line = NULL;
|
||||
|
||||
FILE *f = fopen(PASSWORDFILE, "r");
|
||||
size_t len = 0;
|
||||
ssize_t read;
|
||||
|
||||
if(f){
|
||||
int i = 0;
|
||||
while(read = getline(&line, &len, f)){
|
||||
strncpy(buf[i], line, len);
|
||||
if(++i > 1)
|
||||
break;
|
||||
}
|
||||
if(ferror(f))
|
||||
CU_FAIL("Some error occured with the password file.");
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
|
||||
ret = strcmp(strtok(buf[0], "\n"), crypt(GOODPASSWORD, buf[0])) == 0;
|
||||
CU_ASSERT_TRUE(ret);
|
||||
|
||||
ret = strcmp(strtok(buf[1], "\n"), crypt(BADPASSWORD, buf[1])) == 0;
|
||||
CU_ASSERT_TRUE(ret);
|
||||
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
|
||||
@ -94,14 +145,16 @@ int main(void) {
|
||||
// init CUnit test registry
|
||||
CU_pSuite pSuiteDevice = NULL;
|
||||
CU_pSuite pSuiteReject = NULL;
|
||||
CU_pSuite pSuitePasswordWrite = NULL;
|
||||
if (CUE_SUCCESS != CU_initialize_registry())
|
||||
return CU_get_error();
|
||||
|
||||
// Make suits
|
||||
pSuiteDevice = CU_add_suite("Suite pam_panic_authdevice", init_suite, clean_suite);
|
||||
pSuiteReject = CU_add_suite("Suite pam_panic_reject", init_suite, clean_suite);
|
||||
pSuitePasswordWrite = CU_add_suite("Suite pam_panic_pw", init_suite, clean_suite);
|
||||
if (pSuiteDevice == NULL
|
||||
|| pSuiteReject == NULL) {
|
||||
|| pSuiteReject == NULL || pSuitePasswordWrite == NULL) {
|
||||
CU_cleanup_registry();
|
||||
return CU_get_error();
|
||||
}
|
||||
@ -115,6 +168,8 @@ int main(void) {
|
||||
|| (NULL == CU_add_test(pSuiteReject, "Reject: Reboot?", test_rejectReboot))
|
||||
|| (NULL == CU_add_test(pSuiteReject, "Reject: Poweroff?", test_rejectPoweroff))
|
||||
|| (NULL == CU_add_test(pSuiteReject, "Reject: Nothing?", test_rejectNA))
|
||||
|| (NULL == CU_add_test(pSuitePasswordWrite, "pam_panic_pw: Write password?", test_writePassword))
|
||||
|| (NULL == CU_add_test(pSuitePasswordWrite, "pam_panic_pw: Passwords from file work?", test_passwordCheckFromFile))
|
||||
) {
|
||||
CU_cleanup_registry();
|
||||
return CU_get_error();
|
||||
|
Loading…
Reference in New Issue
Block a user