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