Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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 
00050     if (NULL != coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback)) {
00051         return false;
00052     }
00053 
00054     nsdynmemlib_stub.returnCounter = 1;
00055     mbedtls_stub.expected_int = -1;
00056     if (NULL != coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback)) {
00057         return false;
00058     }
00059 
00060     mbedtls_stub.expected_int = 0;
00061     nsdynmemlib_stub.returnCounter = 2;
00062     mbedtls_stub.crt_expected_int = -1;
00063     if (NULL != coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback)) {
00064         return false;
00065     }
00066 
00067     nsdynmemlib_stub.returnCounter = 2;
00068     mbedtls_stub.crt_expected_int = 0;
00069     coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00070     if (NULL == handle) {
00071         return false;
00072     }
00073 
00074     coap_security_destroy(handle);
00075 
00076     return true;
00077 }
00078 
00079 bool test_thread_security_destroy()
00080 {
00081     nsdynmemlib_stub.returnCounter = 2;
00082     mbedtls_stub.crt_expected_int = 0;
00083     coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00084     if (NULL == handle) {
00085         return false;
00086     }
00087 
00088     coap_security_destroy(handle);
00089     return true;
00090 }
00091 
00092 bool test_coap_security_handler_connect()
00093 {
00094     nsdynmemlib_stub.returnCounter = 2;
00095     mbedtls_stub.crt_expected_int = 0;
00096     coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00097     if (NULL == handle) {
00098         return false;
00099     }
00100 
00101     unsigned char pw = "pwd";
00102     coap_security_keys_t keys;
00103     keys._key = &pw;
00104     keys._key_len = 3;
00105     if (-1 != coap_security_handler_connect_non_blocking(NULL, true, DTLS, keys, 0, 1)) {
00106         return false;
00107     }
00108     mbedtls_stub.useCounter = true;
00109     mbedtls_stub.counter = 0;
00110     mbedtls_stub.retArray[0] = -1;
00111     mbedtls_stub.retArray[1] = -1;
00112     mbedtls_stub.retArray[2] = -1;
00113     mbedtls_stub.retArray[3] = -1;
00114     mbedtls_stub.retArray[4] = -1;
00115     mbedtls_stub.retArray[5] = MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
00116     mbedtls_stub.retArray[6] = -1;
00117     mbedtls_stub.retArray[7] = -1;
00118 
00119     if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) {
00120         return false;
00121     }
00122 
00123     mbedtls_stub.counter = 0;
00124     mbedtls_stub.retArray[0] = 0;
00125     if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) {
00126         return false;
00127     }
00128 
00129     mbedtls_stub.counter = 0;
00130 //    mbedtls_stub.retArray[0] = 0;
00131     mbedtls_stub.retArray[1] = 0;
00132     if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) {
00133         return false;
00134     }
00135 
00136     simple_cookie_t c;
00137     memset(&c, 0, sizeof(simple_cookie_t));
00138     mbedtls_stub.cookie_obj = &c;
00139     memset(&mbedtls_stub.cookie_value, 1, 8);
00140     mbedtls_stub.cookie_len = 2;
00141     mbedtls_stub.counter = 0;
00142 //    mbedtls_stub.retArray[0] = 0;
00143 //    mbedtls_stub.retArray[1] = 0;
00144     mbedtls_stub.retArray[2] = 0;
00145     if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) {
00146         return false;
00147     }
00148 
00149     c.len = 8;
00150     memset(&c.value, 1, 8);
00151     mbedtls_stub.cookie_obj = &c;
00152     memset(&mbedtls_stub.cookie_value, 1, 8);
00153 
00154     mbedtls_stub.cookie_len = 8;
00155     mbedtls_stub.counter = 0;
00156 //    mbedtls_stub.retArray[0] = 0;
00157 //    mbedtls_stub.retArray[1] = 0;
00158 //    mbedtls_stub.retArray[2] = 0;
00159     mbedtls_stub.retArray[3] = 0;
00160     if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) {
00161         return false;
00162     }
00163 
00164     mbedtls_stub.counter = 0;
00165 //    mbedtls_stub.retArray[0] = 0;
00166 //    mbedtls_stub.retArray[1] = 0;
00167 //    mbedtls_stub.retArray[2] = 0;
00168 //    mbedtls_stub.retArray[3] = 0;
00169     mbedtls_stub.retArray[4] = 0;
00170     if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) {
00171         return false;
00172     }
00173 
00174     mbedtls_stub.counter = 0;
00175 //    mbedtls_stub.retArray[0] = 0;
00176 //    mbedtls_stub.retArray[1] = 0;
00177 //    mbedtls_stub.retArray[2] = 0;
00178 //    mbedtls_stub.retArray[3] = 0;
00179 //    mbedtls_stub.retArray[4] = 0;
00180     mbedtls_stub.retArray[6] = 0;
00181     mbedtls_stub.retArray[7] = 0;
00182     if (1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) {
00183         return false;
00184     }
00185 
00186     mbedtls_stub.counter = 0;
00187     mbedtls_stub.retArray[5] = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
00188 
00189     if (-1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) {
00190         return false;
00191     }
00192 
00193     mbedtls_stub.counter = 0;
00194     mbedtls_stub.retArray[5] = HANDSHAKE_FINISHED_VALUE;
00195 
00196     if (1 != coap_security_handler_connect_non_blocking(handle, true, DTLS, keys, 0, 1)) {
00197         return false;
00198     }
00199 
00200     coap_security_destroy(handle);
00201     return true;
00202 }
00203 
00204 bool test_coap_security_handler_continue_connecting()
00205 {
00206     nsdynmemlib_stub.returnCounter = 2;
00207     mbedtls_stub.crt_expected_int = 0;
00208     coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00209     if (NULL == handle) {
00210         return false;
00211     }
00212 
00213     mbedtls_stub.useCounter = true;
00214     mbedtls_stub.counter = 0;
00215     mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
00216     mbedtls_stub.retArray[1] = -1;
00217     mbedtls_stub.retArray[2] = -1;
00218 
00219     if (-1 != coap_security_handler_continue_connecting(handle)) {
00220         return false;
00221     }
00222 
00223     mbedtls_stub.counter = 0;
00224     mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED;
00225     mbedtls_stub.retArray[1] = 0;
00226     mbedtls_stub.retArray[2] = 0;
00227 
00228     if (1 != coap_security_handler_continue_connecting(handle)) {
00229         return false;
00230     }
00231 
00232     mbedtls_stub.counter = 0;
00233     mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_BAD_HS_FINISHED;
00234 
00235     if (MBEDTLS_ERR_SSL_BAD_HS_FINISHED != coap_security_handler_continue_connecting(handle)) {
00236         return false;
00237     }
00238 
00239     mbedtls_stub.counter = 0;
00240     mbedtls_stub.retArray[0] = MBEDTLS_ERR_SSL_WANT_READ;
00241 
00242     if (1 != coap_security_handler_continue_connecting(handle)) {
00243         return false;
00244     }
00245 
00246     mbedtls_stub.counter = 0;
00247     mbedtls_stub.retArray[0] = HANDSHAKE_FINISHED_VALUE_RETURN_ZERO;
00248 
00249     if (0 != coap_security_handler_continue_connecting(handle)) {
00250         return false;
00251     }
00252 
00253     coap_security_destroy(handle);
00254     return true;
00255 }
00256 
00257 bool test_coap_security_handler_send_message()
00258 {
00259     nsdynmemlib_stub.returnCounter = 2;
00260     mbedtls_stub.crt_expected_int = 0;
00261     coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00262     if (NULL == handle) {
00263         return false;
00264     }
00265 
00266     if (-1 != coap_security_handler_send_message(NULL, NULL, 0)) {
00267         return false;
00268     }
00269 
00270     mbedtls_stub.expected_int = 6;
00271     unsigned char cbuf[6];
00272     if (6 != coap_security_handler_send_message(handle, &cbuf, 6)) {
00273         return false;
00274     }
00275 
00276     coap_security_destroy(handle);
00277     return true;
00278 }
00279 
00280 bool test_thread_security_send_close_alert()
00281 {
00282     nsdynmemlib_stub.returnCounter = 2;
00283     mbedtls_stub.crt_expected_int = 0;
00284     coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00285     if (NULL == handle) {
00286         return false;
00287     }
00288 
00289     if (-1 != coap_security_send_close_alert(NULL)) {
00290         return false;
00291     }
00292 
00293     mbedtls_stub.expected_int = 0;
00294     if (0 != coap_security_send_close_alert(handle)) {
00295         return false;
00296     }
00297 
00298     coap_security_destroy(handle);
00299     return true;
00300 }
00301 
00302 bool test_coap_security_handler_read()
00303 {
00304     nsdynmemlib_stub.returnCounter = 2;
00305     mbedtls_stub.crt_expected_int = 0;
00306     coap_security_t *handle = coap_security_create(1, 2, NULL, ECJPAKE, &send_to_socket, &receive_from_socket, &start_timer_callback, &timer_status_callback);
00307     if (NULL == handle) {
00308         return false;
00309     }
00310 
00311     if (-1 != coap_security_handler_read(NULL, NULL, 0)) {
00312         return false;
00313     }
00314 
00315     mbedtls_stub.expected_int = 6;
00316     unsigned char cbuf[6];
00317     if (6 != coap_security_handler_read(handle, &cbuf, 6)) {
00318         return false;
00319     }
00320 
00321     coap_security_destroy(handle);
00322     return true;
00323 }
00324