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_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_error(){
00059         boot_error = true;
00060     }
00061 
00062     void coap_data_processed(){
00063         data_processed = true;
00064     }
00065 
00066     void value_updated(M2MBase *){
00067     }
00068 
00069     void data_available(uint8_t*,
00070                         uint16_t,
00071                         const M2MConnectionObserver::SocketAddress &) {
00072 
00073     }
00074 
00075     void socket_error(uint8_t, bool) {
00076 
00077     }
00078 
00079     void address_ready(const M2MConnectionObserver::SocketAddress &,
00080                                M2MConnectionObserver::ServerType,
00081                                const uint16_t) {
00082 
00083     }
00084 
00085     void data_sent() {
00086 
00087     }
00088 
00089     bool register_error;
00090     bool boot_error;
00091     bool boot_done;
00092     bool registered;
00093     bool data_processed;
00094     bool unregistered;
00095     bool message_ready;
00096 
00097 };
00098 
00099 Test_NsdlAccessHelper::Test_NsdlAccessHelper()
00100 {
00101     observer = new TestObserver();
00102 }
00103 
00104 Test_NsdlAccessHelper::~Test_NsdlAccessHelper()
00105 {
00106     delete observer;
00107     observer = NULL;
00108 
00109     clear_list();
00110 }
00111 
00112 void Test_NsdlAccessHelper::test_nsdl_c_callback()
00113 {
00114     CHECK(__nsdl_c_callback(NULL,NULL,NULL,SN_NSDL_PROTOCOL_HTTP) == 0 );
00115 
00116     m2mnsdlinterface_stub::int_value = 1;
00117     m2mnsdlinterface_stub::void_value = malloc(1);
00118     __nsdl_interface_list.clear();
00119     __nsdl_interface_list.push_back(new M2MNsdlInterface(*observer));
00120     common_stub::coap_header = (sn_coap_hdr_s *) malloc(sizeof(sn_coap_hdr_s));
00121     memset(common_stub::coap_header, 0, sizeof(sn_coap_hdr_s));
00122     common_stub::coap_header->options_list_ptr = (sn_coap_options_list_s *) malloc(sizeof(sn_coap_options_list_s));
00123     common_stub::coap_header->options_list_ptr->block1_len = 2;
00124 
00125     CHECK(__nsdl_c_callback((nsdl_s*)m2mnsdlinterface_stub::void_value,
00126                             common_stub::coap_header,NULL,SN_NSDL_PROTOCOL_HTTP) == 1 );
00127     free(common_stub::coap_header->options_list_ptr);
00128     free(common_stub::coap_header);
00129     free(m2mnsdlinterface_stub::void_value);
00130     clear_list();
00131 }
00132 
00133 void Test_NsdlAccessHelper::test_nsdl_c_memory_alloc()
00134 {
00135     void *ptr = __nsdl_c_memory_alloc(6);
00136     CHECK(ptr != NULL);
00137     free(ptr);
00138     ptr = NULL;
00139     ptr = __nsdl_c_memory_alloc(UINT16_MAX+1);
00140     CHECK(ptr == NULL);
00141     free(ptr);
00142 }
00143 
00144 void Test_NsdlAccessHelper::test_nsdl_c_memory_free()
00145 {
00146     void* ptr = malloc(7);
00147     __nsdl_c_memory_free(ptr);
00148 
00149     CHECK(ptr != NULL);
00150 
00151     ptr = NULL;
00152     //No need to check anything, since memory leak is the test
00153 }
00154 
00155 void Test_NsdlAccessHelper::test_nsdl_c_send_to_server()
00156 {
00157     CHECK(__nsdl_c_send_to_server(NULL, SN_NSDL_PROTOCOL_HTTP, NULL, 0, NULL) == 0);
00158 
00159     m2mnsdlinterface_stub::int_value = 1;
00160     m2mnsdlinterface_stub::void_value = malloc(1);
00161     __nsdl_interface_list.clear();
00162     __nsdl_interface_list.push_back(new M2MNsdlInterface(*observer));
00163 
00164     CHECK(__nsdl_c_send_to_server((nsdl_s*)m2mnsdlinterface_stub::void_value, SN_NSDL_PROTOCOL_HTTP, NULL, 0, NULL) == 1);
00165     free(m2mnsdlinterface_stub::void_value);
00166     clear_list();
00167 }
00168 
00169 void Test_NsdlAccessHelper::test_nsdl_c_received_from_server()
00170 {
00171     CHECK( 0 == __nsdl_c_received_from_server(NULL, NULL, NULL));
00172 
00173     m2mnsdlinterface_stub::int_value = 1;
00174     m2mnsdlinterface_stub::void_value = malloc(1);
00175     __nsdl_interface_list.clear();
00176     __nsdl_interface_list.push_back(new M2MNsdlInterface(*observer));
00177     common_stub::coap_header = (sn_coap_hdr_s *) malloc(sizeof(sn_coap_hdr_s));
00178     memset(common_stub::coap_header, 0, sizeof(sn_coap_hdr_s));
00179     common_stub::coap_header->options_list_ptr = (sn_coap_options_list_s *) malloc(sizeof(sn_coap_options_list_s));
00180     common_stub::coap_header->options_list_ptr->block1_len = 2;
00181     CHECK( 1 == __nsdl_c_received_from_server((nsdl_s*)m2mnsdlinterface_stub::void_value, common_stub::coap_header, NULL));
00182     free(common_stub::coap_header->options_list_ptr);
00183     free(common_stub::coap_header);
00184     free(m2mnsdlinterface_stub::void_value);
00185     clear_list();
00186 }
00187 
00188 void Test_NsdlAccessHelper::test_socket_malloc()
00189 {
00190     void *ptr = __socket_malloc(NULL, 6);
00191     CHECK(ptr != NULL);
00192 
00193     free(ptr);
00194 }
00195 
00196 void Test_NsdlAccessHelper::test_socket_free()
00197 {
00198     void* ptr = malloc(7);
00199     __socket_free(NULL, ptr);
00200 
00201     ptr = NULL;
00202     //No need to check anything, since memory leak is the test
00203 }
00204 
00205 void Test_NsdlAccessHelper::test_mutex_claim()
00206 {
00207     __connection_handler = new M2MConnectionHandler(*observer,
00208                                                     NULL,
00209                                                     M2MInterface::UDP,
00210                                                     M2MInterface::LwIP_IPv4);
00211     __mutex_claim();
00212 
00213     delete __connection_handler;
00214 }
00215 
00216 void Test_NsdlAccessHelper::test_mutex_release()
00217 {
00218     __connection_handler = new M2MConnectionHandler(*observer,
00219                                                     NULL,
00220                                                     M2MInterface::UDP,
00221                                                     M2MInterface::LwIP_IPv4);
00222     __mutex_release();
00223 
00224     delete __connection_handler;
00225 }
00226 
00227 void Test_NsdlAccessHelper::clear_list()
00228 {
00229     M2MNsdlInterfaceList::const_iterator it;
00230     it = __nsdl_interface_list.begin();
00231     int size = __nsdl_interface_list.size();
00232     if (!__nsdl_interface_list.empty()) {
00233         for (int i = 0; i < size; i++) {
00234                 delete __nsdl_interface_list[i];
00235                 __nsdl_interface_list.erase(i);
00236             }
00237         __nsdl_interface_list.clear();
00238     }
00239 }