joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_m2mconnectionsecuritypimpl_mbedtls.cpp Source File

test_m2mconnectionsecuritypimpl_mbedtls.cpp

00001 /*
00002  * Copyright (c) 2015 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "CppUTest/TestHarness.h"
00017 #include "test_m2mconnectionsecuritypimpl_mbedtls.h"
00018 #include "m2mtimerobserver.h"
00019 #include "m2msecurity.h"
00020 #include "m2msecurity_stub.h"
00021 #include "mbedtls_stub.h"
00022 #include "mbed-client/m2mconnectionhandler.h"
00023 #include "m2mtimer_stub.h"
00024 
00025 uint32_t get_random_number(void)
00026 {
00027     return time(NULL);
00028 }
00029 
00030 entropy_cb ent_cb;
00031 
00032 int ent_poll( void *, unsigned char *output, size_t len,
00033                            size_t *olen )
00034 {
00035     for(uint16_t i=0; i < len; i++){
00036         srand(time(NULL));
00037         output[i] = rand() % 256;
00038     }
00039     *olen = len;
00040 
00041     return( 0 );
00042 }
00043 
00044 class TestObserver : public M2MConnectionObserver {
00045 
00046 public:
00047     TestObserver(){}
00048     void data_available(uint8_t*,
00049                         uint16_t,
00050                         const M2MConnectionObserver::SocketAddress &){}
00051 
00052     void socket_error(uint8_t error_code, bool retry = true){}
00053 
00054     void address_ready(const M2MConnectionObserver::SocketAddress &,
00055                        M2MConnectionObserver::ServerType,
00056                        const uint16_t){}
00057 
00058     void data_sent(){}
00059 };
00060 
00061 Test_M2MConnectionSecurityPimpl::Test_M2MConnectionSecurityPimpl()
00062 {
00063     mbedtls_stub::clear();
00064     m2msecurity_stub::clear();
00065 }
00066 
00067 Test_M2MConnectionSecurityPimpl::~Test_M2MConnectionSecurityPimpl()
00068 {
00069 }
00070 
00071 void Test_M2MConnectionSecurityPimpl::test_constructor()
00072 {
00073     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00074     CHECK(impl._timer != NULL);
00075 }
00076 
00077 void Test_M2MConnectionSecurityPimpl::test_destructor()
00078 {
00079     M2MConnectionSecurityPimpl* impl = new M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00080     delete impl;
00081     //Memory leak detector will report an error if leaks
00082 }
00083 
00084 void Test_M2MConnectionSecurityPimpl::test_reset()
00085 {
00086     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00087     impl._init_done = true;
00088     impl.reset();
00089     CHECK(impl._init_done == false);
00090 }
00091 
00092 void Test_M2MConnectionSecurityPimpl::test_init()
00093 {
00094     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00095     CHECK( -1 == impl.init(NULL) );
00096 
00097     m2msecurity_stub::int_value = 5;
00098     M2MSecurity* sec = new M2MSecurity(M2MSecurity::Bootstrap);
00099     CHECK( -1 == impl.init(sec) );
00100 
00101     m2msecurity_stub::int_value = 5;
00102     m2msecurity_stub::has_value = true;
00103     mbedtls_stub::useCounter = true;
00104     mbedtls_stub::counter = 0;
00105     mbedtls_stub::retArray[0] = -1;
00106     m2msecurity_stub::int_value = M2MSecurity::Psk;
00107     CHECK( -1 == impl.init(sec) );
00108 
00109     mbedtls_stub::useCounter = true;
00110     mbedtls_stub::counter = 0;
00111     mbedtls_stub::retArray[0] = 0;
00112     m2msecurity_stub::int_value = 5;
00113     m2msecurity_stub::has_value = true;
00114     mbedtls_stub::expected_int = -1;
00115     mbedtls_stub::crt_expected_int = -1;
00116     CHECK( -1 == impl.init(sec) );
00117 
00118     mbedtls_stub::crt_expected_int = 0;
00119     mbedtls_stub::useCounter = true;
00120     mbedtls_stub::counter = 0;
00121     mbedtls_stub::retArray[0] = 0;
00122     mbedtls_stub::retArray[1] = 0;
00123     mbedtls_stub::retArray[2] = 0;
00124     m2msecurity_stub::int_value = M2MSecurity::Psk;
00125     CHECK( 0 == impl.init(sec) );
00126 
00127     mbedtls_stub::useCounter = true;
00128     mbedtls_stub::counter = 0;
00129     mbedtls_stub::retArray[0] = 0;
00130     mbedtls_stub::retArray[1] = -1;
00131     mbedtls_stub::retArray[2] = -1;
00132     m2msecurity_stub::int_value = M2MSecurity::Certificate;
00133     CHECK( -1 == impl.init(sec) );
00134 
00135     mbedtls_stub::useCounter = true;
00136     mbedtls_stub::counter = 0;
00137     mbedtls_stub::retArray[0] = 0;
00138     mbedtls_stub::retArray[1] = 0;
00139     m2msecurity_stub::int_value = M2MSecurity::Certificate;
00140     CHECK( -1 == impl.init(sec) );
00141 
00142     mbedtls_stub::useCounter = true;
00143     mbedtls_stub::counter = 0;
00144     mbedtls_stub::retArray[0] = 0;
00145     mbedtls_stub::retArray[1] = 0;
00146     mbedtls_stub::retArray[2] = 0;
00147     m2msecurity_stub::int_value = M2MSecurity::Certificate;
00148     CHECK( -1 == impl.init(sec) );
00149 
00150     mbedtls_stub::useCounter = true;
00151     mbedtls_stub::counter = 0;
00152     mbedtls_stub::retArray[0] = 0;
00153     mbedtls_stub::retArray[1] = 0;
00154     mbedtls_stub::retArray[2] = 0;
00155     mbedtls_stub::retArray[3] = 0;
00156     mbedtls_stub::retArray[4] = -1;
00157     m2msecurity_stub::int_value = M2MSecurity::Certificate;
00158     CHECK( -1 == impl.init(sec) );
00159 
00160     mbedtls_stub::useCounter = true;
00161     mbedtls_stub::counter = 0;
00162     mbedtls_stub::retArray[0] = 0;
00163     mbedtls_stub::retArray[1] = 0;
00164     mbedtls_stub::retArray[2] = 0;
00165     mbedtls_stub::retArray[3] = 0;
00166     mbedtls_stub::retArray[4] = 0;
00167     m2msecurity_stub::int_value = M2MSecurity::Certificate;
00168     CHECK( 0 == impl.init(sec) );
00169 
00170     mbedtls_stub::useCounter = true;
00171     mbedtls_stub::counter = 0;
00172     mbedtls_stub::retArray[0] = 0;
00173     mbedtls_stub::retArray[1] = 0;
00174     mbedtls_stub::retArray[2] = 0;
00175     m2msecurity_stub::int_value = 99;
00176     CHECK( -1 == impl.init(sec) );
00177 
00178     impl.set_random_number_callback(NULL);
00179 
00180     ent_cb.entropy_source_ptr = ent_poll;
00181     ent_cb.p_source = NULL;
00182     ent_cb.threshold = 128;
00183     ent_cb.strong = 0;
00184 
00185     m2msecurity_stub::int_value = 5;
00186     m2msecurity_stub::has_value = false;
00187     mbedtls_stub::useCounter = false;
00188     mbedtls_stub::expected_int = 0;
00189     mbedtls_stub::crt_expected_int = 0;
00190     mbedtls_stub::counter = 1;
00191     m2msecurity_stub::int_value = M2MSecurity::Certificate;
00192     CHECK( -1 == impl.init(sec) );
00193 
00194     impl.set_entropy_callback(ent_cb);
00195     m2msecurity_stub::int_value = 5;
00196     m2msecurity_stub::has_value = false;
00197     mbedtls_stub::useCounter = false;
00198     mbedtls_stub::expected_int = -1;
00199     mbedtls_stub::crt_expected_int = 0;
00200     mbedtls_stub::counter = 1;
00201     m2msecurity_stub::int_value = M2MSecurity::Certificate;
00202     CHECK( -1 == impl.init(sec) );
00203 
00204     delete sec;
00205     mbedtls_stub::clear();
00206 }
00207 
00208 void Test_M2MConnectionSecurityPimpl::test_connect()
00209 {
00210     mbedtls_stub::invalidate_timer = true;
00211     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00212     CHECK( -1 == impl.connect(NULL));
00213 
00214     impl._init_done = true;
00215     CHECK( -1 == impl.connect(NULL));
00216 
00217     m2msecurity_stub::int_value = 5;
00218     m2msecurity_stub::has_value = true;
00219 
00220     mbedtls_stub::useCounter = true;
00221     mbedtls_stub::counter = 0;
00222     mbedtls_stub::retArray[0] = 0;
00223     CHECK( -1 == impl.connect(NULL));
00224 
00225     mbedtls_stub::counter = 0;
00226     mbedtls_stub::retArray[0] = 0;
00227     CHECK( -1 == impl.connect(NULL));
00228 
00229     m2mtimer_stub::bool_value = true;
00230 
00231     TestObserver obs;
00232     M2MConnectionHandler* obj = new M2MConnectionHandler(obs, NULL, M2MInterface::NOT_SET,M2MInterface::LwIP_IPv4);
00233 
00234     mbedtls_stub::expected_uint32_value = 12;
00235     mbedtls_stub::counter = 0;
00236     mbedtls_stub::retArray[0] = 0;
00237     mbedtls_stub::retArray[1] = 0;
00238     mbedtls_stub::retArray[2] = MBEDTLS_ERR_SSL_WANT_READ;
00239     mbedtls_stub::retArray[3] = MBEDTLS_ERR_SSL_WANT_WRITE;
00240     mbedtls_stub::retArray[4] = 0;
00241     CHECK( -1 == impl.connect(obj));
00242 
00243     delete obj;
00244 
00245     mbedtls_stub::counter = 0;
00246     mbedtls_stub::expected_uint32_value = 0;
00247     CHECK( 0 == impl.connect(NULL));
00248 
00249     //Tests mbedtls_timing_get_delay() for intermediate case
00250     mbedtls_stub::counter = 0;
00251     m2mtimer_stub::total_bool_value = false;
00252     m2mtimer_stub::bool_value = true;
00253     mbedtls_stub::expected_uint32_value = 1;
00254 
00255     CHECK( -1 == impl.connect(obj));
00256 
00257     //Tests mbedtls_timing_get_delay() for cancelled case
00258     mbedtls_stub::invalidate_timer = true;
00259 
00260     mbedtls_stub::counter = 0;
00261     m2mtimer_stub::total_bool_value = true;
00262     CHECK( -1 == impl.connect(obj));
00263 
00264     //Tests mbedtls_timing_get_delay() for NULL case
00265     mbedtls_stub::counter = 0;
00266     delete(impl._timer);
00267     impl._timer = NULL;
00268     CHECK( -1 == impl.connect(obj));
00269 }
00270 
00271 void Test_M2MConnectionSecurityPimpl::test_start_connecting_non_blocking()
00272 {
00273     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00274     CHECK( -1 == impl.start_connecting_non_blocking(NULL));
00275 
00276     m2mtimer_stub::total_bool_value = false;
00277     impl._init_done = true;
00278     impl._sec_mode = M2MConnectionSecurity::TLS;
00279     CHECK( -1 == impl.start_connecting_non_blocking(NULL));
00280 
00281     m2msecurity_stub::int_value = 5;
00282     m2msecurity_stub::has_value = true;
00283 
00284     mbedtls_stub::useCounter = true;
00285     mbedtls_stub::counter = 0;
00286     mbedtls_stub::retArray[0] = 0;
00287     CHECK( -1 == impl.start_connecting_non_blocking(NULL));
00288 
00289     mbedtls_stub::counter = 0;
00290     mbedtls_stub::retArray[0] = 0;
00291     mbedtls_stub::retArray[1] = 0;
00292     CHECK( -1 == impl.start_connecting_non_blocking(NULL));
00293 
00294     TestObserver obs;
00295     M2MConnectionHandler* obj = new M2MConnectionHandler(obs, NULL, M2MInterface::NOT_SET, M2MInterface::LwIP_IPv4);
00296 
00297     m2mtimer_stub::total_bool_value = true;
00298     mbedtls_stub::expected_uint32_value = 12;
00299     mbedtls_stub::counter = 0;
00300     mbedtls_stub::retArray[0] = 0;
00301     mbedtls_stub::retArray[1] = 0;
00302     mbedtls_stub::retArray[2] = 0;
00303     mbedtls_stub::retArray[3] = 0;
00304     mbedtls_stub::retArray[4] = 0;
00305     CHECK( 1 == impl.start_connecting_non_blocking(obj));
00306 
00307     delete obj;
00308 
00309     mbedtls_stub::counter = 0;
00310     mbedtls_stub::expected_uint32_value = 0;
00311     mbedtls_stub::retArray[3] = -1;
00312     CHECK( -1 == impl.start_connecting_non_blocking(NULL));
00313 }
00314 
00315 void Test_M2MConnectionSecurityPimpl::test_continue_connecting()
00316 {
00317     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00318     impl._ssl.state = MBEDTLS_SSL_HELLO_REQUEST;
00319     mbedtls_stub::expected_int = MBEDTLS_ERR_SSL_WANT_READ;
00320     CHECK( M2MConnectionHandler::CONNECTION_ERROR_WANTS_READ == impl.continue_connecting());
00321 
00322     mbedtls_stub::expected_int = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST;
00323     impl._ssl.state = MBEDTLS_SSL_CLIENT_HELLO;
00324     CHECK( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST == impl.continue_connecting());
00325 
00326     mbedtls_stub::expected_int = -6;
00327     impl._ssl.state = MBEDTLS_SSL_HANDSHAKE_OVER;
00328     CHECK( -6 == impl.continue_connecting());
00329 
00330     mbedtls_stub::expected_int = 0;
00331     impl._ssl.state = MBEDTLS_SSL_HANDSHAKE_OVER;
00332     CHECK( 0 == impl.continue_connecting());
00333 }
00334 
00335 void Test_M2MConnectionSecurityPimpl::test_send_message()
00336 {
00337     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00338     unsigned char msg[6] = "hello";
00339     CHECK( -1 == impl.send_message(msg, 5) );
00340     impl._init_done = true;
00341 
00342     mbedtls_stub::useCounter = true;
00343     mbedtls_stub::counter = 0;
00344     mbedtls_stub::retArray[0] = MBEDTLS_ERR_SSL_WANT_READ;
00345     mbedtls_stub::retArray[1] = MBEDTLS_ERR_SSL_WANT_WRITE;
00346     mbedtls_stub::retArray[2] = 65;
00347     CHECK( 65 == impl.send_message(msg, 5) );
00348 }
00349 
00350 void Test_M2MConnectionSecurityPimpl::test_read()
00351 
00352 {
00353     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00354     unsigned char msg[50];
00355     CHECK( -1 == impl.read(msg, 49));
00356 
00357     impl._init_done = true;
00358     mbedtls_stub::useCounter = true;
00359     mbedtls_stub::counter = 0;
00360     mbedtls_stub::retArray[0] = MBEDTLS_ERR_SSL_WANT_READ;
00361     mbedtls_stub::retArray[1] = MBEDTLS_ERR_SSL_WANT_WRITE;
00362     mbedtls_stub::retArray[2] = 25;
00363     CHECK( 25 == impl.read(msg, 49));
00364 }
00365 
00366 void Test_M2MConnectionSecurityPimpl::test_timer_expired()
00367 {
00368     //This uses static function so we need to prepare test using connect() function.
00369     mbedtls_stub::invalidate_timer = false;
00370     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00371     impl._init_done = true;
00372     m2msecurity_stub::int_value = 5;
00373     m2msecurity_stub::has_value = true;
00374     mbedtls_stub::useCounter = true;
00375     mbedtls_stub::counter = 0;
00376     mbedtls_stub::retArray[0] = 0;
00377     mbedtls_stub::retArray[1] = 0;
00378     mbedtls_stub::retArray[3] = M2MConnectionHandler::CONNECTION_ERROR_WANTS_READ;
00379     impl.connect(NULL);
00380 
00381     void *bio = malloc(1);
00382     impl._ssl.p_bio = bio;
00383     impl.timer_expired(M2MTimerObserver::Dtls);
00384 
00385     // For testing blocking mode use case
00386     mbedtls_stub::useCounter = false;
00387     mbedtls_stub::expected_int = MBEDTLS_ERR_SSL_TIMEOUT;
00388 
00389     impl.timer_expired(M2MTimerObserver::Dtls);
00390     free(bio);
00391 }
00392 
00393 void Test_M2MConnectionSecurityPimpl::test_set_random_number_callback()
00394 {
00395     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00396     random_number_cb cb(&test_random_callback);
00397     impl.set_random_number_callback(cb);
00398 }
00399 
00400 void Test_M2MConnectionSecurityPimpl::test_set_entropy_callback()
00401 {
00402     M2MConnectionSecurityPimpl impl = M2MConnectionSecurityPimpl(M2MConnectionSecurity::TLS);
00403     impl.set_entropy_callback(ent_cb);
00404 }
00405 
00406 uint32_t test_random_callback(void)
00407 {
00408     return 1;
00409 }