Ram Gandikota / Mbed OS ABCD
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers test_nsdlaccesshelper.cpp Source File

test_nsdlaccesshelper.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_nsdlaccesshelper.h"
00018 #include "common_stub.h"
00019 #include "m2mnsdlinterface_stub.h"
00020 #include "m2mnsdlobserver.h"
00021 #include "m2mconnectionobserver.h"
00022 
00023 class TestObserver : public M2MNsdlObserver,
00024                      public M2MConnectionObserver
00025 {
00026 
00027 public:
00028     TestObserver(){}
00029     virtual ~TestObserver(){}
00030     void coap_message_ready(uint8_t *,
00031                             uint16_t,
00032                             sn_nsdl_addr_s *){
00033         message_ready = true;
00034     }
00035 
00036     void client_registered(M2MServer *){
00037         registered = true;
00038     }
00039 
00040     void registration_updated(const M2MServer &){
00041 
00042     }
00043 
00044     void registration_error(uint8_t, bool retry = false){
00045         register_error = true;
00046     }
00047 
00048     void client_unregistered(){
00049         unregistered = true;
00050     }
00051 
00052     void bootstrap_done(M2MSecurity *sec){
00053         if(sec) {
00054             boot_done = true;
00055         }
00056     }
00057 
00058     void bootstrap_wait(M2MSecurity *sec){
00059         if(sec) {
00060             boot_wait = true;
00061         }
00062     }
00063     void bootstrap_error(){
00064         boot_error = true;
00065     }
00066 
00067     void coap_data_processed(){
00068         data_processed = true;
00069     }
00070 
00071     void value_updated(M2MBase *){
00072     }
00073 
00074     void data_available(uint8_t*,
00075                         uint16_t,
00076                         const M2MConnectionObserver::SocketAddress &) {
00077 
00078     }
00079 
00080     void socket_error(uint8_t, bool) {
00081 
00082     }
00083 
00084     void address_ready(const M2MConnectionObserver::SocketAddress &,
00085                                M2MConnectionObserver::ServerType,
00086                                const uint16_t) {
00087 
00088     }
00089 
00090     void data_sent() {
00091 
00092     }
00093 
00094     bool register_error;
00095     bool boot_error;
00096     bool boot_wait;
00097     bool boot_done;
00098     bool registered;
00099     bool data_processed;
00100     bool unregistered;
00101     bool message_ready;
00102 
00103 };
00104 
00105 Test_NsdlAccessHelper::Test_NsdlAccessHelper()
00106 {
00107     observer = new TestObserver();
00108 }
00109 
00110 Test_NsdlAccessHelper::~Test_NsdlAccessHelper()
00111 {
00112     delete observer;
00113     observer = NULL;
00114 
00115     clear_list();
00116 }
00117 
00118 void Test_NsdlAccessHelper::test_nsdl_c_callback()
00119 {
00120     CHECK(__nsdl_c_callback(NULL,NULL,NULL,SN_NSDL_PROTOCOL_HTTP) == 0 );
00121 
00122     m2mnsdlinterface_stub::int_value = 1;
00123     m2mnsdlinterface_stub::void_value = malloc(1);
00124     __nsdl_interface_list.clear();
00125     __nsdl_interface_list.push_back(new M2MNsdlInterface(*observer));
00126     common_stub::coap_header = (sn_coap_hdr_s *) malloc(sizeof(sn_coap_hdr_s));
00127     memset(common_stub::coap_header, 0, sizeof(sn_coap_hdr_s));
00128     common_stub::coap_header->options_list_ptr = (sn_coap_options_list_s *) malloc(sizeof(sn_coap_options_list_s));
00129     common_stub::coap_header->options_list_ptr->block1 = 0x0101;
00130 
00131     CHECK(__nsdl_c_callback((nsdl_s*)m2mnsdlinterface_stub::void_value,
00132                             common_stub::coap_header,NULL,SN_NSDL_PROTOCOL_HTTP) == 1 );
00133     free(common_stub::coap_header->options_list_ptr);
00134     free(common_stub::coap_header);
00135     free(m2mnsdlinterface_stub::void_value);
00136     clear_list();
00137 }
00138 
00139 void Test_NsdlAccessHelper::test_nsdl_c_memory_alloc()
00140 {
00141     void *ptr = __nsdl_c_memory_alloc(6);
00142     CHECK(ptr != NULL);
00143     free(ptr);
00144     ptr = NULL;
00145     ptr = __nsdl_c_memory_alloc(UINT16_MAX+1);
00146     CHECK(ptr == NULL);
00147     free(ptr);
00148 }
00149 
00150 void Test_NsdlAccessHelper::test_nsdl_c_memory_free()
00151 {
00152     void* ptr = malloc(7);
00153     __nsdl_c_memory_free(ptr);
00154 
00155     CHECK(ptr != NULL);
00156 
00157     ptr = NULL;
00158     //No need to check anything, since memory leak is the test
00159 }
00160 
00161 void Test_NsdlAccessHelper::test_nsdl_c_send_to_server()
00162 {
00163     CHECK(__nsdl_c_send_to_server(NULL, SN_NSDL_PROTOCOL_HTTP, NULL, 0, NULL) == 0);
00164 
00165     m2mnsdlinterface_stub::int_value = 1;
00166     m2mnsdlinterface_stub::void_value = malloc(1);
00167     __nsdl_interface_list.clear();
00168     __nsdl_interface_list.push_back(new M2MNsdlInterface(*observer));
00169 
00170     CHECK(__nsdl_c_send_to_server((nsdl_s*)m2mnsdlinterface_stub::void_value, SN_NSDL_PROTOCOL_HTTP, NULL, 0, NULL) == 1);
00171     free(m2mnsdlinterface_stub::void_value);
00172     clear_list();
00173 }
00174 
00175 void Test_NsdlAccessHelper::test_nsdl_c_received_from_server()
00176 {
00177     CHECK( 0 == __nsdl_c_received_from_server(NULL, NULL, NULL));
00178 
00179     m2mnsdlinterface_stub::int_value = 1;
00180     m2mnsdlinterface_stub::void_value = malloc(1);
00181     __nsdl_interface_list.clear();
00182     __nsdl_interface_list.push_back(new M2MNsdlInterface(*observer));
00183     common_stub::coap_header = (sn_coap_hdr_s *) malloc(sizeof(sn_coap_hdr_s));
00184     memset(common_stub::coap_header, 0, sizeof(sn_coap_hdr_s));
00185     common_stub::coap_header->options_list_ptr = (sn_coap_options_list_s *) malloc(sizeof(sn_coap_options_list_s));
00186     common_stub::coap_header->options_list_ptr->block1 = 0x0101;
00187     CHECK( 1 == __nsdl_c_received_from_server((nsdl_s*)m2mnsdlinterface_stub::void_value, common_stub::coap_header, NULL));
00188     free(common_stub::coap_header->options_list_ptr);
00189     free(common_stub::coap_header);
00190     free(m2mnsdlinterface_stub::void_value);
00191     clear_list();
00192 }
00193 
00194 void Test_NsdlAccessHelper::test_socket_malloc()
00195 {
00196     void *ptr = __socket_malloc(NULL, 6);
00197     CHECK(ptr != NULL);
00198 
00199     free(ptr);
00200 }
00201 
00202 void Test_NsdlAccessHelper::test_socket_free()
00203 {
00204     void* ptr = malloc(7);
00205     __socket_free(NULL, ptr);
00206 
00207     ptr = NULL;
00208     //No need to check anything, since memory leak is the test
00209 }
00210 
00211 void Test_NsdlAccessHelper::test_mutex_claim()
00212 {
00213     __connection_handler = new M2MConnectionHandler(*observer,
00214                                                     NULL,
00215                                                     M2MInterface::UDP,
00216                                                     M2MInterface::LwIP_IPv4);
00217     __mutex_claim();
00218 
00219     delete __connection_handler;
00220 }
00221 
00222 void Test_NsdlAccessHelper::test_mutex_release()
00223 {
00224     __connection_handler = new M2MConnectionHandler(*observer,
00225                                                     NULL,
00226                                                     M2MInterface::UDP,
00227                                                     M2MInterface::LwIP_IPv4);
00228     __mutex_release();
00229 
00230     delete __connection_handler;
00231 }
00232 
00233 void Test_NsdlAccessHelper::clear_list()
00234 {
00235     M2MNsdlInterfaceList::const_iterator it;
00236     it = __nsdl_interface_list.begin();
00237     int size = __nsdl_interface_list.size();
00238     if (!__nsdl_interface_list.empty()) {
00239         for (int i = 0; i < size; i++) {
00240                 delete __nsdl_interface_list[i];
00241                 __nsdl_interface_list.erase(i);
00242             }
00243         __nsdl_interface_list.clear();
00244     }
00245 }