Skip to content

Commit

Permalink
Adding unit tests with the check framework: user_pass.c checking
Browse files Browse the repository at this point in the history
  • Loading branch information
Aorimn committed Feb 15, 2014
1 parent be5a23d commit 049c48c
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ read_bekfile
encryption
getmetadata
fuse

# Unit test files
*.gcno
*.gcda
*.gcov
24 changes: 20 additions & 4 deletions src/accesses/user_pass/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ INC = -I/usr/include -I../../
WFLAGS = -Wall -Werror -Wextra
CFLAGS = $(WFLAGS) $(DEFINES) $(INC)
LDFLAGS = $(LIB) -lpolarssl
SOURCES = *.c ../stretch_key.c
OBJECTS = $(patsubst %.c, %.o, $(wildcard *.c)) ../stretch_key.o ../../xstd/xstdio.o ../../xstd/xstdlib.o
MAIN_OBJECT =
SOURCES = user_pass.c ../stretch_key.c
OBJECTS = $(SOURCES:.c=.o) ../stretch_key.o \
../../xstd/xstdio.o ../../xstd/xstdlib.o \
../../ntfs/encoding.o
BIN = $(PROGNAME)


Expand All @@ -41,15 +44,28 @@ endif
.c.o :
$(CC) $(CFLAGS) -c -o $@ $<

all : MAIN_OBJECT = main.o
all : main.o
all : $(BIN)

check : MAIN_OBJECT = check_user_pass.o
check : check_user_pass.o
check : DEFINES += -D__CK_DOING_TESTS
check : CFLAGS += -fprofile-arcs -ftest-coverage
check : LDFLAGS += -lcheck
check : do_check

do_check : $(BIN)
./$(BIN)
gcov $(BIN).c

$(BIN) : $(OBJECTS)
@$(MAKE) DEBUG=$(DEBUG) -C ../../ common
$(CC) $(CFLAGS) -o $@ $^ ../../common.o $(LDFLAGS)
$(CC) $(CFLAGS) -o $@ $(MAIN_OBJECT) $^ ../../common.o $(LDFLAGS)

library : user_pass.o

clean :
rm -rf -- *.o $(BIN) *~ *.swp
rm -rf -- $(BIN) *.o *~ *.swp *.gcno *.gcda *.gcov
@$(MAKE) -C ../../ clean

135 changes: 135 additions & 0 deletions src/accesses/user_pass/check_user_pass.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
#include <check.h>
#include <stdio.h>
#include <unistd.h>

#include "user_pass.h"


static char* ck_password = "TestPassword123!";



// int prompt_up(uint8_t** up);
START_TEST (check_prompt_up)
{
int old_stdin = -1;
int old_stdout = -1;
int new_stdin[2] = {-1, -1};
int new_stdout[2] = {-1, -1};
uint8_t *up = NULL;
int ret = FALSE;

/* TODO check syscalls' return values */

/* Prepare file descriptors for testing */
pipe(new_stdin);
pipe(new_stdout);

old_stdin = dup(STDIN_FILENO);
old_stdout = dup(STDOUT_FILENO);

close(STDIN_FILENO);
close(STDOUT_FILENO);

dup2(new_stdin[0], STDIN_FILENO);
dup2(new_stdout[1], STDOUT_FILENO);

/* Write the password as if it comes from a user input */
if(fork() == 0)
{
write(new_stdin[1], ck_password, strlen(ck_password));
write(new_stdin[1], "\n", 1);
_exit(0);
}

/* Tested unit */
ret = prompt_up(&up);

/* Check unit outputs */
ck_assert_int_eq(ret, TRUE);
ck_assert_str_eq(up, ck_password);

/* Putting every file descriptors back to normal */
close(STDIN_FILENO);
close(STDOUT_FILENO);

close(new_stdin[0]);
close(new_stdin[1]);
close(new_stdout[0]);
close(new_stdout[1]);

dup(old_stdin);
dup(old_stdout);

close(old_stdin);
close(old_stdout);
}
END_TEST


// int user_key(const uint8_t *user_password, const uint8_t *salt, uint8_t *result_key);
START_TEST (check_user_key)
{
uint8_t *user_password = NULL;
uint8_t salt[16] = {0,};
uint8_t *result_key = NULL;
char good_key[] = {
'\x39', '\xf5', '\x3f', '\xaf', '\x64', '\x09', '\x97', '\x2b',
'\xb1', '\x2b', '\x8e', '\xb2', '\x44', '\xcb', '\x04', '\x40',
'\x63', '\x57', '\x5c', '\xe5', '\xca', '\x3f', '\xce', '\x7f',
'\xac', '\xc6', '\x8c', '\x66', '\x96', '\x2d', '\x94', '\xb6'
};
int ret = FALSE;

user_password = (uint8_t*) ck_password;

/* From function's documentation, size should be 32 */
result_key = xmalloc(32 * sizeof(char));
memset(result_key, 0, 32 * sizeof(char));

/* Tested unit */
ret = user_key(user_password, salt, result_key);

/* Check unit outputs */
ck_assert_int_eq(ret, TRUE);
if(memcmp(result_key, good_key, 32) != 0)
{
xfree(result_key);
ck_abort_msg("Found result key doesn't match what it should");
}

xfree(result_key);
}
END_TEST


Suite* user_pass_suite(void)
{
Suite *s = suite_create("User pass");

/* Core test case */
TCase *tc_core = tcase_create("Core");
tcase_add_test(tc_core, check_user_key);
tcase_add_test(tc_core, check_prompt_up);
suite_add_tcase(s, tc_core);

/* TODO add limits for more code coverage */

return s;
}


int main(void)
{
int number_failed;

xstdio_init(L_ERROR, NULL);

Suite *s = user_pass_suite();
SRunner *sr = srunner_create(s);
srunner_run_all(sr, CK_NORMAL);
number_failed = srunner_ntests_failed(sr);
srunner_free (sr);

return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}
14 changes: 13 additions & 1 deletion src/accesses/user_pass/user_pass.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,16 @@ static ssize_t my_getpass(char **lineptr, FILE *stream)
if(!lineptr || !stream)
return -1;

struct termios old, new;
size_t n = 0;
ssize_t nread;

/*
* If we're running tests under check, disable echoing off: this doesn't
* work on pipes
*/
#ifndef __CK_DOING_TESTS
struct termios old, new;

/* Turn echoing off and fail if we can't. */
if(tcgetattr(fileno(stream), &old) != 0)
return -1;
Expand All @@ -56,13 +62,16 @@ static ssize_t my_getpass(char **lineptr, FILE *stream)
new.c_lflag &= (tcflag_t)~ECHO;
if(tcsetattr(fileno(stream), TCSAFLUSH, &new) != 0)
return -1;
#endif

/* Read the password. */
nread = getline(lineptr, &n, stream);
xprintf(L_DEBUG, "New memory allocation at %p (%#" F_SIZE_T " byte allocated)\n", (void*)*lineptr, n);

#ifndef __CK_DOING_TESTS
/* Restore terminal. */
(void) tcsetattr(fileno(stream), TCSAFLUSH, &old);
#endif

return nread;
}
Expand Down Expand Up @@ -139,8 +148,11 @@ int prompt_up(uint8_t** up)
if(!up)
return FALSE;

/* There's no need for a prompt if we're doing tests */
#ifndef __CK_DOING_TESTS
printf("Enter the user password: ");
fflush(NULL);
#endif

*up = NULL;

Expand Down

0 comments on commit 049c48c

Please sign in to comment.