Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Copyright (c) 2017, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "mbed.h"
00018 #include "greentea-client/test_env.h"
00019 #include "unity.h"
00020 #include "utest.h"
00021 #include "rtos.h"
00022 #include "rtc_api.h"
00023 
00024 #if !DEVICE_RTC
00025 #error [NOT_SUPPORTED] test not supported
00026 #endif
00027 
00028 using namespace utest::v1;
00029 
00030 /* On some boards RTC counter can not be
00031  * Initialised with 0 value in such case
00032  * drivers sets RTC counter to 1. */
00033 #define CUSTOM_TIME_0  1
00034 #define CUSTOM_TIME_1  1256729737
00035 #define CUSTOM_TIME_2  2147483637
00036 
00037 #define DELAY_10_SEC   10
00038 #define MS_PER_SEC     1000
00039 #define RTC_DELTA      1
00040 
00041 static volatile int rtc_enabled_ret;
00042 static volatile time_t rtc_time_val;
00043 static volatile bool rtc_read_called;
00044 static volatile bool rtc_write_called;
00045 static volatile bool rtc_init_called;
00046 static volatile bool rtc_isenabled_called;
00047 
00048 /* Stub of RTC read function. */
00049 static time_t read_rtc_stub(void)
00050 {
00051     rtc_read_called = true;
00052 
00053     return rtc_time_val;
00054 }
00055 
00056 /* Stub of RTC write function. */
00057 static void write_rtc_stub(time_t t)
00058 {
00059    rtc_write_called = true;
00060 
00061    rtc_time_val = t;
00062 }
00063 
00064 /* Stub of RTC init function. */
00065 static void init_rtc_stub(void)
00066 {
00067     rtc_init_called = true;
00068 }
00069 
00070 /* Stub of RTC isenabled function. */
00071 static int isenabled_rtc_stub(void)
00072 {
00073     rtc_isenabled_called = true;
00074 
00075     return rtc_enabled_ret;
00076 }
00077 
00078 /* This test verifies if attach_rtc provides availability to
00079  * connect specific RTC driver functions.
00080  *
00081  * This is unit test to verify if correct functions are used
00082  * to support RTC.
00083  *
00084  * Given specific RTC driver functions have been attached (stubs).
00085  * When set_time/time functions are called.
00086  * Then set_time/time functions use attached RTC functions.
00087  */
00088 void test_attach_RTC_stub_funtions()
00089 {
00090     time_t seconds = 0;
00091 
00092     /* Attache RTC read/write/init/isenabled stubs. */
00093     attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
00094 
00095     /* Init stub variables/set to unexpected. */
00096     rtc_write_called = false;
00097     rtc_init_called = false;
00098 
00099     /* Call set_time() function. We expect that init and write RTC stubs
00100      * will be executed.
00101      */
00102     set_time(CUSTOM_TIME_1);
00103 
00104     /* Verify results. */
00105     TEST_ASSERT_EQUAL(true, rtc_write_called);
00106     TEST_ASSERT_EQUAL(true, rtc_init_called);
00107 
00108     /* Init stub variables/set to unexpected. */
00109     rtc_time_val = CUSTOM_TIME_1;
00110     rtc_enabled_ret = true;
00111     rtc_isenabled_called = false;
00112     rtc_read_called = false;
00113 
00114     /* Call time() function. We expect that isenabled and read RTC stubs
00115      * are be executed.
00116      */
00117     time(NULL);
00118 
00119     /* Verify results. */
00120     TEST_ASSERT_EQUAL(true, rtc_isenabled_called);
00121     TEST_ASSERT_EQUAL(true, rtc_read_called);
00122 
00123     /* This part of the test can be executed only on RTC devices. */
00124 
00125     /* Restore env. */
00126     attach_rtc(rtc_read, rtc_write, rtc_init, rtc_isenabled);
00127 
00128     /* Set to unexpected. */
00129     rtc_write_called = false;
00130     rtc_init_called = false;
00131     rtc_isenabled_called = false;
00132     rtc_init_called = false;
00133 
00134     /* Set time. */
00135     set_time(CUSTOM_TIME_1);
00136 
00137     /* Get time. */
00138     seconds = time(NULL);
00139 
00140     /* Stub RTC functions should not be called now. */
00141     TEST_ASSERT_EQUAL(false, rtc_isenabled_called);
00142     TEST_ASSERT_EQUAL(false, rtc_init_called);
00143     TEST_ASSERT_EQUAL(false, rtc_write_called);
00144     TEST_ASSERT_EQUAL(false, rtc_init_called);
00145 
00146     /* Check if time has been successfully set and retrieved. */
00147     TEST_ASSERT_EQUAL(CUSTOM_TIME_1, seconds);
00148 }
00149 
00150 /* This test verifies if attach_rtc provides availability to
00151  * connect specific RTC driver functions.
00152  *
00153  * This is unit test to verify if correct functions are used
00154  * to support RTC.
00155  *
00156  * Given specific RTC driver functions have been attached (original).
00157  * When set_time/time functions are called.
00158  * Then set_time/time functions use attached RTC functions.
00159  */
00160 void test_attach_RTC_org_funtions()
00161 {
00162     time_t seconds = 0;
00163 
00164     /* Attache original driver functions. */
00165     attach_rtc(rtc_read, rtc_write, rtc_init, rtc_isenabled);
00166 
00167     /* Set to unexpected. */
00168     rtc_write_called = false;
00169     rtc_init_called = false;
00170     rtc_isenabled_called = false;
00171     rtc_init_called = false;
00172 
00173     /* Set time. */
00174     set_time(CUSTOM_TIME_1);
00175 
00176     /* Get time. */
00177     seconds = time(NULL);
00178 
00179     /* Stub RTC functions should not be called now. */
00180     TEST_ASSERT_EQUAL(false, rtc_isenabled_called);
00181     TEST_ASSERT_EQUAL(false, rtc_init_called);
00182     TEST_ASSERT_EQUAL(false, rtc_write_called);
00183     TEST_ASSERT_EQUAL(false, rtc_init_called);
00184 
00185     /* Check if time has been successfully set and retrieved. */
00186     TEST_ASSERT_EQUAL(CUSTOM_TIME_1, seconds);
00187 }
00188 
00189 /* This test verifies if time() function returns
00190  * current time when all RTC functions are
00191  * defined and RTC is enabled.
00192  *
00193  * Note: Stubs are used instead of original RTC functions.
00194  *
00195  * Given environment has RTC functions defined and RTC is enabled.
00196  * When time() functions is called.
00197  * Then current time is returned.
00198  */
00199 void test_time_RTC_func_defined_RTC_is_enabled()
00200 {
00201     time_t seconds = 0;
00202 
00203     /* Attache RTC read/write/init/isenabled stubs. */
00204     attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
00205 
00206     /* Simulate that RTC is enabled. */
00207     rtc_enabled_ret = true;
00208 
00209     /* Simulate current time. */
00210     rtc_time_val = CUSTOM_TIME_1;
00211 
00212     /* Try to get current time. */
00213     seconds = time(NULL);
00214 
00215     /* Check if expected value has been returned. */
00216     TEST_ASSERT_EQUAL(CUSTOM_TIME_1, seconds);
00217 }
00218 
00219 /* This test verifies if time() function resets time
00220  * when RTC functions are defined and RTC is disabled.
00221  *
00222  * Note: Stubs are used instead of original RTC functions.
00223  *
00224  * Given environment has RTC functions defined and RTC is disabled.
00225  * When time() functions is called.
00226  * Then function result is 0.
00227  */
00228 void test_time_RTC_func_defined_RTC_is_disabled()
00229 {
00230     time_t seconds = 0;
00231 
00232     /* Attache RTC read/write/init/isenabled stubs. */
00233     attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
00234 
00235     /* Simulate that RTC is disabled. */
00236     rtc_enabled_ret = false;
00237 
00238     /* Simulate current time. */
00239     rtc_time_val = CUSTOM_TIME_1;
00240 
00241     /* Try to get current time. */
00242     seconds = time(NULL);
00243 
00244     /* Check if expected value has been returned. */
00245     TEST_ASSERT_EQUAL(0, seconds);
00246 }
00247 
00248 /* This test verifies if time() function can be successfully
00249  * executed when isenabled RTC function is undefined.
00250  *
00251  * Note: Stubs are used instead of original RTC functions.
00252  *
00253  * Given environment has isenabled RTC function undefined.
00254  * When time() functions is called.
00255  * Then current time is returned.
00256  */
00257 void test_time_isenabled_RTC_func_undefined()
00258 {
00259     time_t seconds = 0;
00260 
00261     /* Attache RTC read/write/init stubs. */
00262     attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, NULL);
00263 
00264     /* Simulate current time. */
00265     rtc_time_val = CUSTOM_TIME_1;
00266 
00267     /* Try to get current time. */
00268     seconds = time(NULL);
00269 
00270     /* Check if expected value has been returned. */
00271     TEST_ASSERT_EQUAL(CUSTOM_TIME_1, seconds);
00272 }
00273 
00274 /* This test verifies if time() function returns -1 if
00275  * read RTC function is undefined.
00276  *
00277  * Note: Stubs are used instead of original RTC functions.
00278  *
00279  * Given environment has read RTC function undefined.
00280  * When time() functions is called.
00281  * Then -1 is returned.
00282  */
00283 void test_time_read_RTC_func_undefined()
00284 {
00285     time_t seconds = 0;
00286 
00287     /* Attache RTC write/init/isenabled stubs. */
00288     attach_rtc(NULL, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
00289 
00290     /* Simulate current time. */
00291     rtc_time_val = CUSTOM_TIME_1;
00292 
00293     /* Try to get current time. */
00294     seconds = time(NULL);
00295 
00296     /* Check if expected value has been returned. */
00297     TEST_ASSERT_EQUAL((time_t)-1, seconds);
00298 }
00299 
00300 /* This test verifies if time() function stores
00301  * the result in given time buffer (if specified).
00302  *
00303  * Note: Stubs are used instead original RTC functions.
00304  *       Other test cases calls time() routine with
00305  *       undefined time buffer.
00306  *
00307  * Given environment has all RTC function defined, RTC is enabled and time buffer is passed to time() function.
00308  * When time() functions is called.
00309  * Then current time is stored in the specified buffer.
00310  */
00311 void test_time_called_with_param()
00312 {
00313     time_t seconds = 0;
00314     time_t buffer = 0;
00315 
00316     /* Attache RTC read/write/init/isenabled stubs. */
00317     attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
00318 
00319     /* Simulate that RTC is enabled. */
00320     rtc_enabled_ret = true;
00321 
00322     /* Simulate current time. */
00323     rtc_time_val = CUSTOM_TIME_1;
00324 
00325     /* Try to get current time. */
00326     seconds = time(&buffer);
00327 
00328     /* Check if expected value has been returned. */
00329     TEST_ASSERT_EQUAL(CUSTOM_TIME_1, seconds);
00330     TEST_ASSERT_EQUAL(CUSTOM_TIME_1, buffer);
00331 }
00332 
00333 /* This test verifies if set_time() function inits the RTC
00334  * and writes current time if RTC functions are defined.
00335  *
00336  * Note: Stubs are used instead of original RTC functions.
00337  *
00338  * Given environment has RTC functions defined.
00339  * When set_time() functions is called.
00340  * Then function initialises RTC and sets RTC time.
00341  */
00342 void test_set_time_RTC_func_defined()
00343 {
00344     /* Attache RTC read/write/init/isenabled stubs. */
00345     attach_rtc(read_rtc_stub, write_rtc_stub, init_rtc_stub, isenabled_rtc_stub);
00346 
00347     /* Set to unexpected. */
00348     rtc_time_val = 123;
00349     rtc_init_called = false;
00350 
00351     /* Set current time. */
00352     rtc_time_val = 123;
00353 
00354     /* Set new RTC time. */
00355     set_time(CUSTOM_TIME_1);
00356 
00357     /* Check if RTC init has been performed and RTC time has been updated. */
00358     TEST_ASSERT_EQUAL(true, rtc_init_called);
00359     TEST_ASSERT_EQUAL(CUSTOM_TIME_1, time(NULL));
00360     TEST_ASSERT_EQUAL(CUSTOM_TIME_1, rtc_time_val);
00361 }
00362 
00363 /* This test verifies if set_time() function can be
00364  * successfully executed when init RTC function is undefined.
00365  *
00366  * Note: Stubs are used instead of original RTC functions.
00367  *
00368  * Given environment has init RTC function undefined.
00369  * When set_time() functions is called.
00370  * Then function sets RTC time.
00371  */
00372 void test_set_time_init_RTC_func_undefined()
00373 {
00374     /* Attache RTC read/write/isenabled stubs. */
00375     attach_rtc(read_rtc_stub, write_rtc_stub, NULL, isenabled_rtc_stub);
00376 
00377     /* Set to unexpected. */
00378     rtc_time_val = 123;
00379 
00380     /* Set new RTC time. */
00381     set_time(CUSTOM_TIME_1);
00382 
00383     /* Check if RTC time has been updated. */
00384     TEST_ASSERT_EQUAL(CUSTOM_TIME_1, time(NULL));
00385     TEST_ASSERT_EQUAL(CUSTOM_TIME_1, rtc_time_val);
00386 }
00387 
00388 /* This test verifies if set_time() function can be
00389  * successfully executed when write RTC function is undefined.
00390  *
00391  * Note: Stubs are used instead original RTC functions.
00392  *
00393  * Given environemt has write RTC function undefined.
00394  * When set_time() function is called.
00395  * Then function inits RTC and does not modify RTC time.
00396  */
00397 void test_set_time_write_RTC_func_undefined()
00398 {
00399     /* Attache RTC read/write/init/isenabled stubs. */
00400     attach_rtc(read_rtc_stub, NULL, init_rtc_stub, isenabled_rtc_stub);
00401 
00402     /* Set to unexpected. */
00403     rtc_time_val = 123;
00404     rtc_init_called = false;
00405 
00406     /* Set new RTC time. */
00407     set_time(CUSTOM_TIME_1);
00408 
00409     /* Check if RTC has been initialized and RTC time has not been updated. */
00410     TEST_ASSERT_EQUAL(true, rtc_init_called);
00411     TEST_ASSERT_EQUAL(123, time(NULL));
00412     TEST_ASSERT_EQUAL(123, rtc_time_val);
00413 }
00414 
00415 /* This test verifies if RTC time can be successfully set.
00416  *
00417  * Note: Original RTC functions are used in this test.
00418  *
00419  * Given environment has RTC available.
00420  * When set_time() functions is called.
00421  * Then RTC time is retrieved.
00422  */
00423 template<uint32_t timeValue>
00424 void test_functional_set()
00425 {
00426     /* Set original RTC functions. */
00427     attach_rtc(rtc_read, rtc_write, rtc_init, rtc_isenabled);
00428 
00429     /* Set new RTC time. */
00430     set_time(timeValue);
00431 
00432     /* Get current time and verify that new value has been set. */
00433     TEST_ASSERT_EQUAL(timeValue, time(NULL));
00434 }
00435 
00436 /* This test verifies if RTC counts seconds.
00437  *
00438  * Note: Original RTC functions are used in this test.
00439  *
00440  * Given RTC has time set.
00441  * When some time has passed (seconds).
00442  * Then RTC time is updated.
00443  */
00444 void test_functional_count()
00445 {
00446     time_t seconds = 0;
00447 
00448     /* Set original RTC functions. */
00449     attach_rtc(rtc_read, rtc_write, rtc_init, rtc_isenabled);
00450 
00451     /* Set new RTC time. */
00452     set_time(CUSTOM_TIME_2);
00453 
00454     /* Wait 10 sec. */
00455     wait_ms(DELAY_10_SEC * MS_PER_SEC);
00456 
00457     /* Get time. */
00458     seconds = time(NULL);
00459 
00460     /* Verify that RTC counts seconds. */
00461     TEST_ASSERT_UINT_WITHIN(RTC_DELTA, (unsigned int)seconds, CUSTOM_TIME_2 + DELAY_10_SEC);
00462 }
00463 
00464 utest::v1::status_t test_setup(const size_t number_of_cases)
00465 {
00466     GREENTEA_SETUP(20, "default_auto");
00467     return verbose_test_setup_handler(number_of_cases);
00468 }
00469 
00470 Case cases[] = {
00471     Case("Unit Test: attach stub RTC functions.", test_attach_RTC_stub_funtions),
00472     Case("Unit Test: attach original RTC functions.", test_attach_RTC_org_funtions),
00473 
00474     Case("Unit Test: time() - RTC functions are defined, RTC is enabled.", test_time_RTC_func_defined_RTC_is_enabled),
00475     Case("Unit Test: time() - RTC functions are defined, RTC is disabled.", test_time_RTC_func_defined_RTC_is_disabled),
00476     Case("Unit Test: time() - isenabled RTC function is undefined.", test_time_isenabled_RTC_func_undefined),
00477     Case("Unit Test: time() - read RTC function is undefined.", test_time_read_RTC_func_undefined),
00478     Case("Unit Test: time() - result is stored in given buffer.", test_time_called_with_param),
00479 
00480     Case("Unit Test: set_time() - RTC functions are defined.", test_set_time_RTC_func_defined),
00481     Case("Unit Test: set_time() - init RTC function is undefined.", test_set_time_init_RTC_func_undefined),
00482     Case("Unit Test: set_time() - write RTC function is undefined.", test_set_time_write_RTC_func_undefined),
00483 
00484     Case("Functional Test: set time - CUSTOM_TIME_0.", test_functional_set<CUSTOM_TIME_0>),
00485     Case("Functional Test: set time - CUSTOM_TIME_1.", test_functional_set<CUSTOM_TIME_1>),
00486     Case("Functional Test: set time - CUSTOM_TIME_2.", test_functional_set<CUSTOM_TIME_2>),
00487 
00488     Case("Functional Test: RTC counts seconds.", test_functional_count),
00489 };
00490 
00491 Specification specification(test_setup, cases);
00492 
00493 int main() {
00494     return !Harness::run(specification);
00495 }