1
0
mirror of https://github.com/Yubico/yubico-pam.git synced 2024-11-29 09:24:22 +01:00
yubico-pam/tests/util_test.c

102 lines
3.4 KiB
C
Raw Normal View History

2014-08-27 09:46:04 +02:00
/*
* Copyright (c) 2014 Yubico AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
2014-08-27 09:58:43 +02:00
#include <assert.h>
2014-08-27 10:17:40 +02:00
#include <string.h>
2014-08-27 10:22:29 +02:00
#include <stdlib.h>
2014-08-27 09:46:04 +02:00
#include "util.h"
2014-08-27 10:17:40 +02:00
void test_get_user_cfgfile_path(void) {
char *file = NULL;
2014-08-27 09:58:43 +02:00
int ret = get_user_cfgfile_path("/foo/bar", "test", "root", &file);
assert(ret == 1);
2014-08-27 09:58:43 +02:00
assert(strcmp(file, "/foo/bar/test") == 0);
free(file);
ret = get_user_cfgfile_path(NULL, "test", "root", &file);
assert(ret == 1);
assert(strcmp(file, "/root/.yubico/test") == 0);
free(file);
2014-08-27 09:58:43 +02:00
}
2014-08-27 10:17:40 +02:00
#if HAVE_CR
#define CHALLENGE1 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
#define RESPONSE1 "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
#define SALT1 "cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc"
#define CHALLENGE2 "dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"
#define RESPONSE2 "eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
void test_load_chalresp_state(void) {
int ret;
FILE *file = tmpfile();
CR_STATE state = {0};
fprintf(file, "v2:%s:%s:%s:%d:%d\n", CHALLENGE1, RESPONSE1, SALT1, 1000, 2);
rewind(file);
ret = load_chalresp_state(file, &state, true);
assert(ret == 1);
assert(state.iterations == 1000);
assert(state.slot == 2);
assert(state.challenge_len == 63);
assert(state.response_len == 20);
assert(state.salt_len == 32);
rewind(file);
memset(&state, 0, sizeof(state));
fprintf(file, "v1:%s:%s:%d\n", CHALLENGE2, RESPONSE2, 1);
rewind(file);
ret = load_chalresp_state(file, &state, true);
assert(ret == 1);
assert(state.slot == 1);
assert(state.challenge_len == 63);
assert(state.response_len == 20);
assert(state.salt_len == 0);
rewind(file);
/* slot 3 should fail.. */
fprintf(file, "v2:%s:%s:%s:%d:%d\n", CHALLENGE1, RESPONSE1, SALT1, 1000, 3);
rewind(file);
ret = load_chalresp_state(file, &state, true);
assert(ret == 0);
}
2014-08-27 09:58:43 +02:00
#endif /* HAVE_CR */
2014-08-27 09:46:04 +02:00
int
main (int argc, char **argv)
{
2014-08-27 09:58:43 +02:00
test_get_user_cfgfile_path();
2014-08-27 10:17:40 +02:00
#if HAVE_CR
test_load_chalresp_state();
2014-08-27 09:58:43 +02:00
#endif /* HAVE_CR */
2014-08-27 10:17:40 +02:00
return 0;
2014-08-27 09:46:04 +02:00
}