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_coap_security_handler.c Source File

test_coap_security_handler.c

00001 /*
00002  * Copyright (c) 2015-2016 ARM Limited. All Rights Reserved.
00003  */
00004 #include "test_coap_security_handler.h"
00005 #include "coap_security_handler.h"
00006 #include <string.h>
00007 #include "nsdynmemLIB_stub.h"
00008 #include "mbedtls_stub.h"
00009 #include "mbedtls/ssl.h"
00010 
00011 static int send_to_socket(int8_t socket_id, uint8_t *address_ptr, uint16_t port, const unsigned char *buf, size_t len)
00012 {
00013 
00014 }
00015 
00016 static int receive_from_socket(int8_t socket_id, unsigned char *buf, size_t len)
00017 {
00018 
00019 }
00020 
00021 static void start_timer_callback(int8_t timer_id, uint32_t int_ms, uint32_t fin_ms)
00022 {
00023 
00024 }
00025 
00026 static int timer_status_callback(int8_t timer_id)
00027 {
00028 
00029 }
00030 
00031 bool test_thread_security_create()
00032 {
00033     uint8_t buf[16];
00034     if( NULL != coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, NULL) )
00035         return false;
00036 
00037     if( NULL != coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback) )
00038         return false;
00039 
00040     nsdynmemlib_stub.returnCounter = 1;
00041     mbedtls_stub.expected_int = -1;
00042     if( NULL != coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback) )
00043         return false;
00044 
00045     mbedtls_stub.expected_int = 0;
00046     nsdynmemlib_stub.returnCounter = 2;
00047     mbedtls_stub.crt_expected_int = -1;
00048     if( NULL != coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback) )
00049         return false;
00050 
00051     nsdynmemlib_stub.returnCounter = 2;
00052     mbedtls_stub.crt_expected_int = 0;
00053     coap_security_t *handle = coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00054     if( NULL == handle )
00055         return false;
00056 
00057     coap_security_destroy(handle);
00058 
00059     return true;
00060 }
00061 
00062 bool test_thread_security_destroy()
00063 {
00064     uint8_t buf[16];
00065     nsdynmemlib_stub.returnCounter = 2;
00066     mbedtls_stub.crt_expected_int = 0;
00067     coap_security_t *handle = coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00068     if( NULL == handle )
00069         return false;
00070 
00071     coap_security_destroy(handle);
00072     return true;
00073 }
00074 
00075 bool test_coap_security_handler_connect()
00076 {
00077     uint8_t buf[16];
00078     nsdynmemlib_stub.returnCounter = 2;
00079     mbedtls_stub.crt_expected_int = 0;
00080     coap_security_t *handle = coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00081     if( NULL == handle )
00082         return false;
00083 
00084     unsigned char pw = "pwd";
00085     coap_security_keys_t keys;
00086     keys._priv = &pw;
00087     keys._priv_len = 3;
00088     if( -1 != coap_security_handler_connect_non_blocking(NULL, true, DTLS, keys, 0, 1) )
00089         return false;
00090     mbedtls_stub.useCounter = true;
00091     mbedtls_stub.counter = 0;
00092     mbedtls_stub.retArray[0] = -1;
00093     mbedtls_stub.retArray[1] = -1;
00094     mbedtls_stub.retArray[2] = -1;
00095     mbedtls_stub.retArray[3] = -1;
00096     mbedtls_stub.retArray[4] = -1;
00097     mbedtls_stub.retArray[5] = MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
00098     mbedtls_stub.retArray[6] = -1;
00099     mbedtls_stub.retArray[7] = -1;
00100 
00101     if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
00102         return false;
00103 
00104     mbedtls_stub.counter = 0;
00105     mbedtls_stub.retArray[0] = 0;
00106     if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
00107         return false;
00108 
00109     mbedtls_stub.counter = 0;
00110 //    mbedtls_stub.retArray[0] = 0;
00111     mbedtls_stub.retArray[1] = 0;
00112     if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
00113         return false;
00114 
00115     simple_cookie_t c;
00116     memset(&c, 0, sizeof(simple_cookie_t));
00117     mbedtls_stub.cookie_obj = &c;
00118     memset(&mbedtls_stub.cookie_value, 1, 8);
00119     mbedtls_stub.cookie_len = 2;
00120     mbedtls_stub.counter = 0;
00121 //    mbedtls_stub.retArray[0] = 0;
00122 //    mbedtls_stub.retArray[1] = 0;
00123     mbedtls_stub.retArray[2] = 0;
00124     if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
00125         return false;
00126 
00127     c.len = 8;
00128     memset(&c.value, 1, 8);
00129     mbedtls_stub.cookie_obj = &c;
00130     memset(&mbedtls_stub.cookie_value, 1, 8);
00131 
00132     mbedtls_stub.cookie_len = 8;
00133     mbedtls_stub.counter = 0;
00134 //    mbedtls_stub.retArray[0] = 0;
00135 //    mbedtls_stub.retArray[1] = 0;
00136 //    mbedtls_stub.retArray[2] = 0;
00137     mbedtls_stub.retArray[3] = 0;
00138     if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
00139         return false;
00140 
00141     mbedtls_stub.counter = 0;
00142 //    mbedtls_stub.retArray[0] = 0;
00143 //    mbedtls_stub.retArray[1] = 0;
00144 //    mbedtls_stub.retArray[2] = 0;
00145 //    mbedtls_stub.retArray[3] = 0;
00146     mbedtls_stub.retArray[4] = 0;
00147     if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
00148         return false;
00149 
00150     mbedtls_stub.counter = 0;
00151 //    mbedtls_stub.retArray[0] = 0;
00152 //    mbedtls_stub.retArray[1] = 0;
00153 //    mbedtls_stub.retArray[2] = 0;
00154 //    mbedtls_stub.retArray[3] = 0;
00155 //    mbedtls_stub.retArray[4] = 0;
00156     mbedtls_stub.retArray[6] = 0;
00157     mbedtls_stub.retArray[7] = 0;
00158     if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
00159         return false;
00160 
00161     mbedtls_stub.counter = 0;
00162     mbedtls_stub.retArray[5] = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
00163 
00164     if( -1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
00165         return false;
00166 
00167     mbedtls_stub.counter = 0;
00168     mbedtls_stub.retArray[5] = HANDSHAKE_FINISHED_VALUE;
00169 
00170     if( 1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1) )
00171         return false;
00172 
00173     coap_security_destroy(handle);
00174     return true;
00175 }
00176 
00177 bool test_coap_security_handler_continue_connecting()
00178 {
00179     uint8_t buf[16];
00180     nsdynmemlib_stub.returnCounter = 2;
00181     mbedtls_stub.crt_expected_int = 0;
00182     coap_security_t *handle = coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00183     if( NULL == handle )
00184         return false;
00185 
00186     mbedtls_stub.useCounter = true;
00187     mbedtls_stub.counter = 0;
00188     mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
00189     mbedtls_stub.retArray[1] = -1;
00190     mbedtls_stub.retArray[2] = -1;
00191 
00192     if( -1 != coap_security_handler_continue_connecting(handle) )
00193         return false;
00194 
00195     mbedtls_stub.counter = 0;
00196     mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
00197     mbedtls_stub.retArray[1] = 0;
00198     mbedtls_stub.retArray[2] = 0;
00199 
00200     if( 1 != coap_security_handler_continue_connecting(handle) )
00201         return false;
00202 
00203     mbedtls_stub.counter = 0;
00204     mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
00205 
00206     if( MBEDTLS_ERR_SSL_BAD_HS_FINISHED != coap_security_handler_continue_connecting(handle) )
00207         return false;
00208 
00209     mbedtls_stub.counter = 0;
00210     mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_WANT_READ;
00211 
00212     if( 1 != coap_security_handler_continue_connecting(handle) )
00213         return false;
00214 
00215     mbedtls_stub.counter = 0;
00216     mbedtls_stub.retArray[0] = HANDSHAKE_FINISHED_VALUE_RETURN_ZERO;
00217 
00218     if( 0 != coap_security_handler_continue_connecting(handle) )
00219         return false;
00220 
00221     coap_security_destroy(handle);
00222     return true;
00223 }
00224 
00225 bool test_coap_security_handler_send_message()
00226 {
00227     uint8_t buf[16];
00228     nsdynmemlib_stub.returnCounter = 2;
00229     mbedtls_stub.crt_expected_int = 0;
00230     coap_security_t *handle = coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00231     if( NULL == handle )
00232         return false;
00233 
00234     if( -1 != coap_security_handler_send_message(NULL, NULL, 0))
00235         return false;
00236 
00237     mbedtls_stub.expected_int = 6;
00238     unsigned char cbuf[6];
00239     if( 6 != coap_security_handler_send_message(handle, &cbuf, 6))
00240         return false;
00241 
00242     coap_security_destroy(handle);
00243     return true;
00244 }
00245 
00246 bool test_thread_security_send_close_alert()
00247 {
00248     uint8_t buf[16];
00249     nsdynmemlib_stub.returnCounter = 2;
00250     mbedtls_stub.crt_expected_int = 0;
00251     coap_security_t *handle = coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00252     if( NULL == handle )
00253         return false;
00254 
00255     if( -1 != coap_security_send_close_alert(NULL))
00256         return false;
00257 
00258     mbedtls_stub.expected_int = 0;
00259     if( 0 != coap_security_send_close_alert(handle))
00260         return false;
00261 
00262     coap_security_destroy(handle);
00263     return true;
00264 }
00265 
00266 bool test_coap_security_handler_read()
00267 {
00268     uint8_t buf[16];
00269     nsdynmemlib_stub.returnCounter = 2;
00270     mbedtls_stub.crt_expected_int = 0;
00271     coap_security_t *handle = coap_security_create(1,2,&buf,12,ECJPAKE,&send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00272     if( NULL == handle )
00273         return false;
00274 
00275     if( -1 != coap_security_handler_read(NULL, NULL, 0))
00276         return false;
00277 
00278     mbedtls_stub.expected_int = 6;
00279     unsigned char cbuf[6];
00280     if( 6 != coap_security_handler_read(handle, &cbuf, 6))
00281         return false;
00282 
00283     coap_security_destroy(handle);
00284     return true;
00285 }
00286