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

test_coap_message_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_message_handler.h"
00018 #include <string.h>
00019 #include "coap_message_handler.h"
00020 #include "sn_coap_protocol_stub.h"
00021 #include "nsdynmemLIB_stub.h"
00022 #include "sn_coap_builder_stub.h"
00023 #include "sn_coap_parser_stub.h"
00024 #include "socket_api.h"
00025 #include "coap_message_handler.c"
00026 
00027 int retCounter = 0;
00028 int retValue = 0;
00029 int transaction_cb = 0;
00030 
00031 static void *test_own_alloc(uint16_t size)
00032 {
00033     if (retCounter > 0) {
00034         retCounter--;
00035         return malloc(size);
00036     }
00037     return NULL;
00038 }
00039 
00040 static void test_own_free(void *ptr)
00041 {
00042     if (ptr) {
00043         free(ptr);
00044     }
00045 }
00046 
00047 static uint8_t coap_tx_function(uint8_t *data_ptr, uint16_t data_len, sn_nsdl_addr_s *address_ptr, void *param)
00048 {
00049     return 0;
00050 }
00051 
00052 int resp_recv(int8_t service_id, uint16_t msg_id, sn_coap_hdr_s *response_ptr)
00053 {
00054     return retValue;
00055 }
00056 
00057 int16_t process_cb(int8_t a, sn_coap_hdr_s *b, coap_transaction_t *c)
00058 {
00059     return retValue;
00060 }
00061 
00062 static int transaction_recv_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *response_ptr)
00063 {
00064     transaction_cb = 1;
00065     return 1;
00066 }
00067 
00068 bool test_coap_message_handler_init()
00069 {
00070     if (NULL != coap_message_handler_init(NULL, NULL, NULL)) {
00071         return false;
00072     }
00073     if (NULL != coap_message_handler_init(&test_own_alloc, NULL, NULL)) {
00074         return false;
00075     }
00076     if (NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, NULL)) {
00077         return false;
00078     }
00079     if (NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function)) {
00080         return false;
00081     }
00082     retCounter = 1;
00083     sn_coap_protocol_stub.expectedCoap = NULL;
00084     if (NULL != coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function)) {
00085         return false;
00086     }
00087     retCounter = 1;
00088     sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s));
00089     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00090     nsdynmemlib_stub.returnCounter = 1;
00091     coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
00092     if (NULL == handle) {
00093         return false;
00094     }
00095     free(sn_coap_protocol_stub.expectedCoap);
00096     sn_coap_protocol_stub.expectedCoap = NULL;
00097     free(handle);
00098     return true;
00099 }
00100 
00101 bool test_coap_message_handler_destroy()
00102 {
00103     if (-1 != coap_message_handler_destroy(NULL)) {
00104         return false;
00105     }
00106     retCounter = 1;
00107     sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s));
00108     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00109     nsdynmemlib_stub.returnCounter = 1;
00110     coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
00111 
00112     if (0 != coap_message_handler_destroy(handle)) {
00113         return false;
00114     }
00115 
00116     free(sn_coap_protocol_stub.expectedCoap);
00117     return true;
00118 }
00119 
00120 bool test_coap_message_handler_find_transaction()
00121 {
00122     if (NULL != coap_message_handler_find_transaction(NULL, 0)) {
00123         return false;
00124     }
00125     retCounter = 1;
00126     sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s));
00127     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00128     nsdynmemlib_stub.returnCounter = 1;
00129     coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
00130 
00131     uint8_t buf[16];
00132     memset(&buf, 1, 16);
00133     char uri[3];
00134     uri[0] = "r";
00135     uri[1] = "s";
00136     uri[2] = "\0";
00137 
00138     sn_coap_builder_stub.expectedUint16 = 1;
00139     nsdynmemlib_stub.returnCounter = 3;
00140     if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) {
00141         return false;
00142     }
00143 
00144     if (NULL == coap_message_handler_find_transaction(&buf, 24)) {
00145         return false;
00146     }
00147 
00148     free(sn_coap_protocol_stub.expectedCoap);
00149     sn_coap_protocol_stub.expectedCoap = NULL;
00150     coap_message_handler_destroy(handle);
00151     return true;
00152 }
00153 
00154 bool test_coap_message_handler_coap_msg_process()
00155 {
00156     uint8_t buf[16];
00157     memset(&buf, 1, 16);
00158     bool ret_val = false;
00159     /*Handler is null*/
00160     if (-1 != coap_message_handler_coap_msg_process(NULL, 0, 61131, buf, 22, ns_in6addr_any, NULL, 0, NULL)) {
00161         goto exit;
00162     }
00163 
00164     retCounter = 1;
00165     sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s));
00166     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00167     nsdynmemlib_stub.returnCounter = 1;
00168     coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
00169 
00170     sn_coap_protocol_stub.expectedHeader = NULL;
00171     /* Coap parse returns null */
00172     if (-1 != coap_message_handler_coap_msg_process(handle, 0, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
00173         goto exit;
00174     }
00175 
00176     sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00177     memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00178     sn_coap_protocol_stub.expectedHeader->coap_status = 66;
00179     nsdynmemlib_stub.returnCounter = 1;
00180     /* Coap library responds */
00181     if (-1 != coap_message_handler_coap_msg_process(handle, 0, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
00182         goto exit;
00183     }
00184 
00185     sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00186     memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00187     sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK;
00188     sn_coap_protocol_stub.expectedHeader->msg_code = 1;
00189     retValue = 0;
00190     /* request received */
00191     nsdynmemlib_stub.returnCounter = 1;
00192     if (0 != coap_message_handler_coap_msg_process(handle, 0, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
00193         goto exit;
00194     }
00195 
00196     sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00197     memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00198     sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK;
00199     sn_coap_protocol_stub.expectedHeader->msg_code = 1;
00200     nsdynmemlib_stub.returnCounter = 1;
00201     retValue = -1;
00202     if (0 != coap_message_handler_coap_msg_process(handle, 0, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
00203         goto exit;
00204     }
00205 
00206     sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00207     memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00208     sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK;
00209     sn_coap_protocol_stub.expectedHeader->msg_code = 333;
00210     nsdynmemlib_stub.returnCounter = 1;
00211 
00212     if (-1 != coap_message_handler_coap_msg_process(handle, 0, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
00213         goto exit;
00214     }
00215 
00216     sn_coap_protocol_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00217     memset(sn_coap_protocol_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00218     sn_coap_protocol_stub.expectedHeader->coap_status = COAP_STATUS_OK;
00219     sn_coap_protocol_stub.expectedHeader->msg_code = 333;
00220 
00221     char uri[3];
00222     uri[0] = "r";
00223     uri[1] = "s";
00224     uri[2] = "\0";
00225 
00226     sn_coap_builder_stub.expectedUint16 = 1;
00227     nsdynmemlib_stub.returnCounter = 3;
00228     if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) {
00229         goto exit;
00230     }
00231 
00232     sn_coap_protocol_stub.expectedHeader->msg_id = 2;
00233 
00234     if (-1 != coap_message_handler_coap_msg_process(handle, 0, 61131, buf, 22, ns_in6addr_any, NULL, 0, process_cb)) {
00235         goto exit;
00236     }
00237 
00238     ret_val = true;
00239 exit:
00240     free(sn_coap_protocol_stub.expectedCoap);
00241     sn_coap_protocol_stub.expectedCoap = NULL;
00242     coap_message_handler_destroy(handle);
00243     return ret_val;
00244 }
00245 
00246 bool test_coap_message_handler_request_send()
00247 {
00248     retCounter = 1;
00249     sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s));
00250     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00251     nsdynmemlib_stub.returnCounter = 1;
00252     coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
00253 
00254     uint8_t buf[16];
00255     memset(&buf, 1, 16);
00256     char uri[3] = "rs";
00257 
00258     if (0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) {
00259         return false;
00260     }
00261 
00262     sn_coap_builder_stub.expectedUint16 = 1;
00263     nsdynmemlib_stub.returnCounter = 1;
00264     if (0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) {
00265         return false;
00266     }
00267 
00268     sn_coap_builder_stub.expectedUint16 = 1;
00269     nsdynmemlib_stub.returnCounter = 3;
00270     if (0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) {
00271         return false;
00272     }
00273 
00274     sn_coap_builder_stub.expectedUint16 = 1;
00275     nsdynmemlib_stub.returnCounter = 3;
00276     if (0 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, NULL)) {
00277         return false;
00278     }
00279 
00280     sn_coap_builder_stub.expectedUint16 = 1;
00281     nsdynmemlib_stub.returnCounter = 3;
00282     if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) {
00283         return false;
00284     }
00285 
00286     /* Clear all transactions */
00287     if (0 != coap_message_handler_exec(handle, 0xffffffff)) {
00288         return false;
00289     }
00290 
00291     sn_coap_protocol_stub.expectedInt16 = -4;
00292     nsdynmemlib_stub.returnCounter = 3;
00293     if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb)) {
00294         return false;
00295     }
00296 
00297     transaction_cb = 0;
00298     sn_coap_protocol_stub.expectedInt8 = 0;
00299     if (0 != coap_message_handler_exec(handle, 12)) {
00300         return false;
00301     }
00302 
00303     if (transaction_cb != 1) {
00304         return false;
00305     }
00306 
00307     sn_coap_protocol_stub.expectedInt16 = -2;
00308     nsdynmemlib_stub.returnCounter = 3;
00309     if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &transaction_recv_cb)) {
00310         return false;
00311     }
00312 
00313     transaction_cb = 0;
00314     if (0 != coap_message_handler_exec(handle, 2)) {
00315         return false;
00316     }
00317     if (transaction_cb != 1) {
00318         return false;
00319     }
00320 
00321 
00322     free(sn_coap_protocol_stub.expectedCoap);
00323     sn_coap_protocol_stub.expectedCoap = NULL;
00324     coap_message_handler_destroy(handle);
00325     return true;
00326 }
00327 
00328 bool test_coap_message_handler_request_delete()
00329 {
00330     retCounter = 1;
00331     sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s));
00332     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00333     nsdynmemlib_stub.returnCounter = 1;
00334     coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
00335 
00336     uint8_t buf[16];
00337     memset(&buf, 1, 16);
00338     char uri[3];
00339     uri[0] = "r";
00340     uri[1] = "s";
00341     uri[2] = "\0";
00342     if (0 == coap_message_handler_request_delete(NULL, 1, 1)) {
00343         return false;
00344     }
00345 
00346     if (0 == coap_message_handler_request_delete(handle, 1, 1)) {
00347         return false;
00348     }
00349 
00350     sn_coap_builder_stub.expectedUint16 = 1;
00351     nsdynmemlib_stub.returnCounter = 3;
00352     if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) {
00353         return false;
00354     }
00355 
00356     if (0 != coap_message_handler_request_delete(handle, 1, 2)) {
00357         return false;
00358     }
00359 
00360     free(sn_coap_protocol_stub.expectedCoap);
00361     sn_coap_protocol_stub.expectedCoap = NULL;
00362     coap_message_handler_destroy(handle);
00363     return true;
00364 }
00365 
00366 bool test_coap_message_handler_request_delete_by_service_id()
00367 {
00368     retCounter = 1;
00369     sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s));
00370     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00371     nsdynmemlib_stub.returnCounter = 1;
00372     coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
00373     coap_service_handle = handle;
00374 
00375     uint8_t buf[16];
00376     memset(&buf, 1, 16);
00377     char uri[3] = "rs";
00378 
00379     if (0 == coap_message_handler_request_delete_by_service_id(NULL, 1)) {
00380         return false;
00381     }
00382 
00383     if (0 != coap_message_handler_request_delete_by_service_id(handle, 1)) {
00384         return false;
00385     }
00386 
00387     sn_coap_builder_stub.expectedUint16 = 1;
00388     nsdynmemlib_stub.returnCounter = 3;
00389     if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) {
00390         return false;
00391     }
00392 
00393     if (0 != coap_message_handler_request_delete_by_service_id(handle, 3)) {
00394         return false;
00395     }
00396 
00397     free(sn_coap_protocol_stub.expectedCoap);
00398     sn_coap_protocol_stub.expectedCoap = NULL;
00399     coap_message_handler_destroy(handle);
00400     coap_service_handle = NULL;
00401     return true;
00402 }
00403 
00404 bool test_coap_message_handler_response_send()
00405 {
00406     if (-1 != coap_message_handler_response_send(NULL, 2, 0, NULL, 1, 3, NULL, 0)) {
00407         return false;
00408     }
00409 
00410     retCounter = 1;
00411     sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s));
00412     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00413     nsdynmemlib_stub.returnCounter = 1;
00414     coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
00415     sn_coap_hdr_s *header = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00416     memset(header, 0, sizeof(sn_coap_hdr_s));
00417 
00418     if (-2 != coap_message_handler_response_send(handle, 2, 0, header, 1, 3, NULL, 0)) {
00419         return false;
00420     }
00421 
00422     uint8_t buf[16];
00423     memset(&buf, 1, 16);
00424     char uri[3];
00425     uri[0] = "r";
00426     uri[1] = "s";
00427     uri[2] = "\0";
00428     sn_coap_builder_stub.expectedUint16 = 1;
00429     nsdynmemlib_stub.returnCounter = 3;
00430     if (2 != coap_message_handler_request_send(handle, 3, 0, buf, 24, 1, 2, &uri, 4, NULL, 0, &resp_recv)) {
00431         return false;
00432     }
00433 
00434     header->msg_id = 2;
00435     sn_coap_builder_stub.expectedUint16 = 2;
00436     coap_transaction_t *tx = coap_message_handler_find_transaction(&buf, 24);
00437     if (tx) {
00438         tx->client_request = false;
00439     }
00440     sn_coap_builder_stub.expectedHeader = NULL;
00441     if (-1 != coap_message_handler_response_send(handle, 2, 0, header, 1, 3, NULL, 0)) {
00442         return false;
00443     }
00444 
00445     sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00446     memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00447     nsdynmemlib_stub.returnCounter = 0;
00448     if (-1 != coap_message_handler_response_send(handle, 2, 0, header, 1, 3, NULL, 0)) {
00449         return false;
00450     }
00451 
00452     sn_coap_builder_stub.expectedHeader = (sn_coap_hdr_s *)malloc(sizeof(sn_coap_hdr_s));
00453     memset(sn_coap_builder_stub.expectedHeader, 0, sizeof(sn_coap_hdr_s));
00454     nsdynmemlib_stub.returnCounter = 1;
00455     if (0 != coap_message_handler_response_send(handle, 2, 0, header, 1, 3, NULL, 0)) {
00456         return false;
00457     }
00458 
00459     free(header);
00460     free(sn_coap_protocol_stub.expectedCoap);
00461     sn_coap_protocol_stub.expectedCoap = NULL;
00462     coap_message_handler_destroy(handle);
00463     return true;
00464 }
00465 
00466 bool test_coap_message_handler_exec()
00467 {
00468     /* Null as a parameter */
00469     if (-1 != coap_message_handler_exec(NULL, 0)) {
00470         return false;
00471     }
00472 
00473     retCounter = 1;
00474     sn_coap_protocol_stub.expectedCoap = (struct coap_s *)malloc(sizeof(struct coap_s));
00475     memset(sn_coap_protocol_stub.expectedCoap, 0, sizeof(struct coap_s));
00476     nsdynmemlib_stub.returnCounter = 1;
00477     coap_msg_handler_t *handle = coap_message_handler_init(&test_own_alloc, &test_own_free, &coap_tx_function);
00478 
00479     if (0 != coap_message_handler_exec(handle, 0)) {
00480         return false;
00481     }
00482 
00483     nsdynmemlib_stub.returnCounter = 1;
00484     coap_transaction_t *transact_ptr = transaction_create();
00485 
00486     /* Transaction not timed out*/
00487     if (0 != coap_message_handler_exec(handle, 0)) {
00488         return false;
00489     }
00490 
00491     if (transaction_cb != 0) {
00492         return false;
00493     }
00494 
00495     /* Timed out, no CB */
00496     if (0 != coap_message_handler_exec(handle, 300)) {
00497         return false;
00498     }
00499 
00500     if (transaction_cb != 0) {
00501         return false;
00502     }
00503 
00504     nsdynmemlib_stub.returnCounter = 1;
00505     transact_ptr = transaction_create();
00506     transact_ptr->resp_cb = transaction_recv_cb;
00507 
00508     /* Transaction not timed out */
00509     if (0 != coap_message_handler_exec(handle, 0)) {
00510         return false;
00511     }
00512 
00513     if (transaction_cb != 0) {
00514         return false;
00515     }
00516 
00517     /* Transaction timed out */
00518     if (0 != coap_message_handler_exec(handle, 300)) {
00519         return false;
00520     }
00521 
00522     if (transaction_cb == 0) {
00523         return false;
00524     }
00525 
00526     /* Teardown */
00527     free(sn_coap_protocol_stub.expectedCoap);
00528     sn_coap_protocol_stub.expectedCoap = NULL;
00529     coap_message_handler_destroy(handle);
00530     return true;
00531 }