/** * 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 #include #include #include #include #include #include #include #include #include "../src/ParodusInternal.h" #include "../src/config.h" /*----------------------------------------------------------------------------*/ /* File Scoped Variables */ /*----------------------------------------------------------------------------*/ ParodusMsg *ParodusMsgQ; bool LastReasonStatus; /*----------------------------------------------------------------------------*/ /* Mocks */ /*----------------------------------------------------------------------------*/ nopoll_bool nopoll_base64_encode(const char *content,int length,char *output, int *output_size) { UNUSED(content); UNUSED(length); UNUSED(output_size); parStrncpy(output, "AWYFUJHUDUDKJDDRDKUIIKORE\nSFJLIRRSHLOUTDESTDJJITTESLOIUHJGDRS\nGIUY&%WSJ", (size_t) *output_size); function_called(); return (nopoll_bool)(intptr_t)mock(); } char *get_global_reconnect_reason() { function_called(); return (char *)(intptr_t)mock(); } /*----------------------------------------------------------------------------*/ /* Tests */ /*----------------------------------------------------------------------------*/ void test_getWebpaConveyHeader() { ParodusCfg cfg; memset(&cfg, 0, sizeof(ParodusCfg)); parStrncpy(cfg.hw_model, "TG1682", sizeof(cfg.hw_model)); parStrncpy(cfg.hw_serial_number, "Fer23u948590", sizeof(cfg.hw_serial_number)); parStrncpy(cfg.hw_manufacturer , "ARRISGroup,Inc.", sizeof(cfg.hw_manufacturer)); parStrncpy(cfg.hw_mac , "123567892366", sizeof(cfg.hw_mac)); parStrncpy(cfg.hw_last_reboot_reason , "unknown", sizeof(cfg.hw_last_reboot_reason)); parStrncpy(cfg.fw_name , "2.364s2", sizeof(cfg.fw_name)); parStrncpy(cfg.webpa_path_url , "/api/v2/device", sizeof(cfg.webpa_path_url)); parStrncpy(cfg.webpa_url , "localhost", sizeof(cfg.webpa_url)); parStrncpy(cfg.webpa_interface_used , "eth0", sizeof(cfg.webpa_interface_used)); parStrncpy(cfg.webpa_protocol , "WebPA-1.6", sizeof(cfg.webpa_protocol)); parStrncpy(cfg.webpa_uuid , "1234567-345456546", sizeof(cfg.webpa_uuid)); cfg.flags = 0; cfg.boot_time = 423457; cfg.webpa_ping_timeout = 30; cfg.webpa_backoff_max = 255; set_parodus_cfg(&cfg); will_return(get_global_reconnect_reason, (intptr_t)"Ping-Miss"); expect_function_call(get_global_reconnect_reason); will_return(nopoll_base64_encode, nopoll_true); expect_function_call(nopoll_base64_encode); getWebpaConveyHeader(); } void err_getWebpaConveyHeader() { ParodusCfg cfg; memset(&cfg, 0, sizeof(ParodusCfg)); set_parodus_cfg(&cfg); will_return(get_global_reconnect_reason, (intptr_t)NULL); expect_function_call(get_global_reconnect_reason); will_return(nopoll_base64_encode, nopoll_false); expect_function_call(nopoll_base64_encode); getWebpaConveyHeader(); } /* * Test function to verify timespec_diff function * Verifys when the time diff is positive ie stop time > start time */ void test_timespec_diff() { struct timespec start, stop, diff; int time_taken_ms; clock_gettime(CLOCK_REALTIME, &start); sleep(1); clock_gettime(CLOCK_REALTIME, &stop); timespec_diff(&start, &stop, &diff); time_taken_ms = diff.tv_sec * 1000 + (diff.tv_nsec / 1000000); assert_true(time_taken_ms >= 0); } /* * Test function to verify timespec_diff function * Verifys when the time diff is negative ie stop time < start time */ void test_timespec_diff1() { struct timespec start, stop, diff; int time_taken_ms; clock_gettime(CLOCK_REALTIME, &start); sleep(1); clock_gettime(CLOCK_REALTIME, &stop); timespec_diff(&stop, &start, &diff); time_taken_ms = diff.tv_sec * 1000 + (diff.tv_nsec / 1000000); assert_true(time_taken_ms <= 0); } /*----------------------------------------------------------------------------*/ /* External Functions */ /*----------------------------------------------------------------------------*/ int main(void) { const struct CMUnitTest tests[] = { cmocka_unit_test(test_getWebpaConveyHeader), cmocka_unit_test(err_getWebpaConveyHeader), cmocka_unit_test(test_timespec_diff), cmocka_unit_test(test_timespec_diff1), }; return cmocka_run_group_tests(tests, NULL, NULL); }