Skip to content

Commit

Permalink
Unit tests on auth token request
Browse files Browse the repository at this point in the history
  • Loading branch information
sadhyama committed Feb 10, 2019
1 parent a5081a5 commit 7b03fad
Show file tree
Hide file tree
Showing 3 changed files with 314 additions and 263 deletions.
19 changes: 14 additions & 5 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ target_link_libraries (test_nopoll_handlers_fragment -lnopoll -lcunit -lcimplog
add_test(NAME test_connection COMMAND ${MEMORY_CHECK} ./test_connection)
set (CONN_SRC ../src/connection.c
../src/string_helpers.c ../src/mutex.c ../src/time.c
../src/config.c ../src/spin_thread.c ../src/heartBeat.c ../src/close_retry.c)
../src/config.c ../src/auth_token.c ../src/spin_thread.c ../src/heartBeat.c ../src/close_retry.c)
#set(CONN_SRC ../src/connection.c ${PARODUS_COMMON_SRC})
add_executable(test_connection test_connection.c ${CONN_SRC})
target_link_libraries (test_connection ${PARODUS_COMMON_LIBS} -lcmocka -lcurl -luuid)
Expand All @@ -141,7 +141,7 @@ target_link_libraries (test_connection ${PARODUS_COMMON_LIBS} -lcmocka -lcurl -l
add_test(NAME test_client_list COMMAND ${MEMORY_CHECK} ./test_client_list)
#add_executable(test_client_list test_client_list.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/downstream.c ../src/connection.c ../src/nopoll_handlers.c ../src/ParodusInternal.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ${PARODUS_COMMON_SRC})
#target_link_libraries (test_client_list ${PARODUS_COMMON_LIBS})
set(CLIST_SRC test_client_list.c ../src/client_list.c
set(CLIST_SRC test_client_list.c ../src/client_list.c ../src/auth_token.c
../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c
../src/downstream.c ../src/connection.c ../src/nopoll_handlers.c ../src/heartBeat.c ../src/close_retry.c
../src/ParodusInternal.c ../src/thread_tasks.c ../src/conn_interface.c
Expand All @@ -163,7 +163,7 @@ target_link_libraries (test_client_list ${PARODUS_COMMON_LIBS} -lcurl -luuid)
add_test(NAME test_service_alive COMMAND ${MEMORY_CHECK} ./test_service_alive)
#add_executable(test_service_alive test_service_alive.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/nopoll_handlers.c ../src/config.c ../src/connection.c ../src/ParodusInternal.c ../src/downstream.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ${PARODUS_COMMON_SRC})
#target_link_libraries (test_service_alive ${PARODUS_COMMON_LIBS})
set(SVA_SRC test_service_alive.c ../src/client_list.c ../src/service_alive.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/nopoll_handlers.c ../src/config.c ../src/connection.c ../src/ParodusInternal.c ../src/downstream.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ../src/heartBeat.c ../src/close_retry.c ${PARODUS_COMMON_SRC})
set(SVA_SRC test_service_alive.c ../src/client_list.c ../src/service_alive.c ../src/auth_token.c ../src/upstream.c ../src/networking.c ../src/nopoll_helpers.c ../src/nopoll_handlers.c ../src/config.c ../src/connection.c ../src/ParodusInternal.c ../src/downstream.c ../src/thread_tasks.c ../src/conn_interface.c ../src/partners_check.c ../src/heartBeat.c ../src/close_retry.c ${PARODUS_COMMON_SRC})
if (ENABLE_SESHAT)
set(SVA_SRC ${SVA_SRC} ../src/seshat_interface.c)
else()
Expand All @@ -178,12 +178,21 @@ target_link_libraries (test_service_alive ${PARODUS_COMMON_LIBS} -lcurl -luuid)
# test_config
#-------------------------------------------------------------------------------
add_test(NAME test_config COMMAND ${MEMORY_CHECK} ./test_config)
add_executable(test_config test_config.c ../src/config.c ../src/string_helpers.c)
add_executable(test_config test_config.c ../src/config.c ../src/auth_token.c ../src/string_helpers.c)
target_link_libraries (test_config -lcmocka
-Wl,--no-as-needed -lcimplog
-lcjson -lcjwt -ltrower-base64 -lssl -lcrypto -lrt -lm -lcurl -luuid
)

#-------------------------------------------------------------------------------
# test_auth_token
#-------------------------------------------------------------------------------
add_test(NAME test_auth_token COMMAND ${MEMORY_CHECK} ./test_auth_token)
add_executable(test_auth_token test_auth_token.c ../src/config.c ../src/auth_token.c ../src/string_helpers.c)
target_link_libraries (test_auth_token -lcmocka
-Wl,--no-as-needed -lcimplog
-lcjson -lcjwt -ltrower-base64 -lssl -lcrypto -lrt -lm -lcurl -luuid
)
#-------------------------------------------------------------------------------
# test_crud_interface
#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -285,7 +294,7 @@ set(TOKEN_SRC ../src/conn_interface.c ../src/config.c
../src/nopoll_handlers.c ../src/nopoll_helpers.c
../src/partners_check.c ../src/ParodusInternal.c
../src/upstream.c ../src/downstream.c
../src/networking.c
../src/networking.c ../src/auth_token.c
../src/thread_tasks.c ../src/time.c
../src/string_helpers.c ../src/mutex.c
../src/token.c ../src/heartBeat.c
Expand Down
299 changes: 299 additions & 0 deletions tests/test_auth_token.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,299 @@
/**
* Copyright 2010-2016 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <malloc.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>

#include <CUnit/Basic.h>

#include "../src/config.h"
#include "../src/auth_token.h"
#include "../src/ParodusInternal.h"

extern int requestNewAuthToken(char *newToken, size_t len, int r_count);

/*----------------------------------------------------------------------------*/
/* Mocks */
/*----------------------------------------------------------------------------*/
typedef void CURL;

typedef enum {
CURLINFO_RESPONSE_CODE = 2,
CURLINFO_TOTAL_TIME
} CURLINFO;

int curl_easy_perform(CURL *curl)
{
UNUSED(curl);
function_called();
return (int) mock();
}

int curl_easy_getinfo(CURL *curl, CURLINFO CURLINFO_RESPONSE_CODE, long response_code)
{
UNUSED(curl);
UNUSED(CURLINFO_RESPONSE_CODE);
UNUSED(response_code);
function_called();
return (int) mock();
}
/*----------------------------------------------------------------------------*/
/* Tests */
/*----------------------------------------------------------------------------*/
void getAuthToken_Null()
{
ParodusCfg cfg;
memset(&cfg,0,sizeof(cfg));
parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac));
cfg.client_cert_path = NULL;
getAuthToken(&cfg);
set_parodus_cfg(&cfg);
assert( cfg.client_cert_path == NULL);
}

void getAuthToken_MacNull()
{
ParodusCfg cfg;
memset(&cfg,0,sizeof(cfg));
cfg.client_cert_path = NULL;
getAuthToken(&cfg);
set_parodus_cfg(&cfg);
assert( cfg.client_cert_path == NULL);
}

void test_requestNewAuthToken_init_fail ()
{
char token[32];
ParodusCfg cfg;
int output = -1;
memset(&cfg,0,sizeof(cfg));

cfg.token_server_url = strdup("https://dev.comcast.net/token");
parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path));
parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number));
parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac));

set_parodus_cfg(&cfg);

will_return (curl_easy_perform, -1);
expect_function_calls (curl_easy_perform, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, -1);
expect_function_calls (curl_easy_getinfo, 1);

requestNewAuthToken (token, sizeof(token), 2);
assert_int_equal (output, -1);
free(cfg.token_server_url);
}


void test_requestNewAuthToken_failure ()
{
char token[32];
ParodusCfg cfg;
int output = -1;
memset(&cfg,0,sizeof(cfg));

cfg.token_server_url = strdup("https://dev.comcast.net/token");
parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path));
parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number));
parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac));
set_parodus_cfg(&cfg);

will_return (curl_easy_perform, -1);
expect_function_calls (curl_easy_perform, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, -1);
expect_function_calls (curl_easy_getinfo, 1);

requestNewAuthToken (token, sizeof(token), 2);
assert_int_equal (output, -1);
free(cfg.token_server_url);
}


void test_requestNewAuthToken ()
{
char token[1024];
ParodusCfg cfg;
int output = -1;
memset(&cfg,0,sizeof(cfg));

cfg.token_server_url = strdup("https://dev.comcast.net/token");
parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path));
parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used));
parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number));
parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac));
set_parodus_cfg(&cfg);

will_return (curl_easy_perform, 0);
expect_function_calls (curl_easy_perform, 1);
will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

output = requestNewAuthToken (token, sizeof(token), 1);
assert_int_equal (output, 0);
free(cfg.token_server_url);
}

void test_getAuthToken ()
{
ParodusCfg cfg;
memset(&cfg,0,sizeof(cfg));

cfg.token_server_url = strdup("https://dev.comcast.net/token");
cfg.client_cert_path = strdup("testcert");
parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path));
parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used));
parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number));
parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac));
set_parodus_cfg(&cfg);

/* To test curl failure case and retry on v4 mode */
will_return (curl_easy_perform, -1);
expect_function_calls (curl_easy_perform, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

/* To test curl failure case and retry on v6 mode */
will_return (curl_easy_perform, -1);
expect_function_calls (curl_easy_perform, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

/* To test curl success case */
will_return (curl_easy_perform, 0);
expect_function_calls (curl_easy_perform, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

getAuthToken(&cfg);

free(cfg.client_cert_path);
free(cfg.token_server_url);
}

void test_getAuthTokenFailure ()
{
ParodusCfg cfg;
memset(&cfg,0,sizeof(cfg));

cfg.token_server_url = strdup("https://dev.comcast.net/token");
cfg.client_cert_path = strdup("testcert");
parStrncpy(cfg.cert_path , "/etc/ssl/certs/ca-certificates.crt", sizeof(cfg.cert_path));
parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used));
parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number));
parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac));
set_parodus_cfg(&cfg);
will_return (curl_easy_perform, -1);
expect_function_calls (curl_easy_perform, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_perform, -1);
expect_function_calls (curl_easy_perform, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_perform, -1);
expect_function_calls (curl_easy_perform, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

will_return (curl_easy_getinfo, 0);
expect_function_calls (curl_easy_getinfo, 1);

getAuthToken(&cfg);

assert_string_equal( cfg.webpa_auth_token, "");

free(cfg.client_cert_path);
free(cfg.token_server_url);
}

void test_write_callback_fn ()
{
void *buffer;
size_t size = 1;
size_t nmemb =8;
int out_len=0;
struct token_data data;
data.size = 0;
buffer = strdup("response");

data.data = (char *) malloc(sizeof(char) * 5);
data.data[0] = '\0';

out_len = write_callback_fn(buffer, size, nmemb, &data);
assert_string_equal(data.data, buffer);
assert_int_equal( out_len, strlen(buffer));
free(data.data);
}

/*----------------------------------------------------------------------------*/
/* External Functions */
/*----------------------------------------------------------------------------*/

int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_requestNewAuthToken),
cmocka_unit_test(test_requestNewAuthToken_init_fail),
cmocka_unit_test(test_requestNewAuthToken_failure),
cmocka_unit_test(getAuthToken_Null),
cmocka_unit_test(getAuthToken_MacNull),
cmocka_unit_test(test_getAuthToken),
cmocka_unit_test(test_getAuthTokenFailure),
cmocka_unit_test(test_write_callback_fn),
};

return cmocka_run_group_tests(tests, NULL, NULL);
}
Loading

0 comments on commit 7b03fad

Please sign in to comment.