Ram Gandikota
/
IOTMetronome
FRDM K64F Metronome
mbed-client/test/mbedclient/utest/nsdlaccesshelper/test_nsdlaccesshelper.cpp@0:dbad57390bd1, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:37:05 2017 +0000
- Revision:
- 0:dbad57390bd1
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:dbad57390bd1 | 1 | /* |
ram54288 | 0:dbad57390bd1 | 2 | * Copyright (c) 2015 ARM Limited. All rights reserved. |
ram54288 | 0:dbad57390bd1 | 3 | * SPDX-License-Identifier: Apache-2.0 |
ram54288 | 0:dbad57390bd1 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
ram54288 | 0:dbad57390bd1 | 5 | * not use this file except in compliance with the License. |
ram54288 | 0:dbad57390bd1 | 6 | * You may obtain a copy of the License at |
ram54288 | 0:dbad57390bd1 | 7 | * |
ram54288 | 0:dbad57390bd1 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ram54288 | 0:dbad57390bd1 | 9 | * |
ram54288 | 0:dbad57390bd1 | 10 | * Unless required by applicable law or agreed to in writing, software |
ram54288 | 0:dbad57390bd1 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
ram54288 | 0:dbad57390bd1 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ram54288 | 0:dbad57390bd1 | 13 | * See the License for the specific language governing permissions and |
ram54288 | 0:dbad57390bd1 | 14 | * limitations under the License. |
ram54288 | 0:dbad57390bd1 | 15 | */ |
ram54288 | 0:dbad57390bd1 | 16 | #include "CppUTest/TestHarness.h" |
ram54288 | 0:dbad57390bd1 | 17 | #include "test_nsdlaccesshelper.h" |
ram54288 | 0:dbad57390bd1 | 18 | #include "common_stub.h" |
ram54288 | 0:dbad57390bd1 | 19 | #include "m2mnsdlinterface_stub.h" |
ram54288 | 0:dbad57390bd1 | 20 | #include "m2mnsdlobserver.h" |
ram54288 | 0:dbad57390bd1 | 21 | #include "m2mconnectionobserver.h" |
ram54288 | 0:dbad57390bd1 | 22 | |
ram54288 | 0:dbad57390bd1 | 23 | class TestObserver : public M2MNsdlObserver, |
ram54288 | 0:dbad57390bd1 | 24 | public M2MConnectionObserver |
ram54288 | 0:dbad57390bd1 | 25 | { |
ram54288 | 0:dbad57390bd1 | 26 | |
ram54288 | 0:dbad57390bd1 | 27 | public: |
ram54288 | 0:dbad57390bd1 | 28 | TestObserver(){} |
ram54288 | 0:dbad57390bd1 | 29 | virtual ~TestObserver(){} |
ram54288 | 0:dbad57390bd1 | 30 | void coap_message_ready(uint8_t *, |
ram54288 | 0:dbad57390bd1 | 31 | uint16_t, |
ram54288 | 0:dbad57390bd1 | 32 | sn_nsdl_addr_s *){ |
ram54288 | 0:dbad57390bd1 | 33 | message_ready = true; |
ram54288 | 0:dbad57390bd1 | 34 | } |
ram54288 | 0:dbad57390bd1 | 35 | |
ram54288 | 0:dbad57390bd1 | 36 | void client_registered(M2MServer *){ |
ram54288 | 0:dbad57390bd1 | 37 | registered = true; |
ram54288 | 0:dbad57390bd1 | 38 | } |
ram54288 | 0:dbad57390bd1 | 39 | |
ram54288 | 0:dbad57390bd1 | 40 | void registration_updated(const M2MServer &){ |
ram54288 | 0:dbad57390bd1 | 41 | |
ram54288 | 0:dbad57390bd1 | 42 | } |
ram54288 | 0:dbad57390bd1 | 43 | |
ram54288 | 0:dbad57390bd1 | 44 | void registration_error(uint8_t, bool retry = false){ |
ram54288 | 0:dbad57390bd1 | 45 | register_error = true; |
ram54288 | 0:dbad57390bd1 | 46 | } |
ram54288 | 0:dbad57390bd1 | 47 | |
ram54288 | 0:dbad57390bd1 | 48 | void client_unregistered(){ |
ram54288 | 0:dbad57390bd1 | 49 | unregistered = true; |
ram54288 | 0:dbad57390bd1 | 50 | } |
ram54288 | 0:dbad57390bd1 | 51 | |
ram54288 | 0:dbad57390bd1 | 52 | void bootstrap_done(M2MSecurity *sec){ |
ram54288 | 0:dbad57390bd1 | 53 | if(sec) { |
ram54288 | 0:dbad57390bd1 | 54 | boot_done = true; |
ram54288 | 0:dbad57390bd1 | 55 | } |
ram54288 | 0:dbad57390bd1 | 56 | } |
ram54288 | 0:dbad57390bd1 | 57 | |
ram54288 | 0:dbad57390bd1 | 58 | void bootstrap_wait(M2MSecurity *sec){ |
ram54288 | 0:dbad57390bd1 | 59 | if(sec) { |
ram54288 | 0:dbad57390bd1 | 60 | boot_wait = true; |
ram54288 | 0:dbad57390bd1 | 61 | } |
ram54288 | 0:dbad57390bd1 | 62 | } |
ram54288 | 0:dbad57390bd1 | 63 | void bootstrap_error(){ |
ram54288 | 0:dbad57390bd1 | 64 | boot_error = true; |
ram54288 | 0:dbad57390bd1 | 65 | } |
ram54288 | 0:dbad57390bd1 | 66 | |
ram54288 | 0:dbad57390bd1 | 67 | void coap_data_processed(){ |
ram54288 | 0:dbad57390bd1 | 68 | data_processed = true; |
ram54288 | 0:dbad57390bd1 | 69 | } |
ram54288 | 0:dbad57390bd1 | 70 | |
ram54288 | 0:dbad57390bd1 | 71 | void value_updated(M2MBase *){ |
ram54288 | 0:dbad57390bd1 | 72 | } |
ram54288 | 0:dbad57390bd1 | 73 | |
ram54288 | 0:dbad57390bd1 | 74 | void data_available(uint8_t*, |
ram54288 | 0:dbad57390bd1 | 75 | uint16_t, |
ram54288 | 0:dbad57390bd1 | 76 | const M2MConnectionObserver::SocketAddress &) { |
ram54288 | 0:dbad57390bd1 | 77 | |
ram54288 | 0:dbad57390bd1 | 78 | } |
ram54288 | 0:dbad57390bd1 | 79 | |
ram54288 | 0:dbad57390bd1 | 80 | void socket_error(uint8_t, bool) { |
ram54288 | 0:dbad57390bd1 | 81 | |
ram54288 | 0:dbad57390bd1 | 82 | } |
ram54288 | 0:dbad57390bd1 | 83 | |
ram54288 | 0:dbad57390bd1 | 84 | void address_ready(const M2MConnectionObserver::SocketAddress &, |
ram54288 | 0:dbad57390bd1 | 85 | M2MConnectionObserver::ServerType, |
ram54288 | 0:dbad57390bd1 | 86 | const uint16_t) { |
ram54288 | 0:dbad57390bd1 | 87 | |
ram54288 | 0:dbad57390bd1 | 88 | } |
ram54288 | 0:dbad57390bd1 | 89 | |
ram54288 | 0:dbad57390bd1 | 90 | void data_sent() { |
ram54288 | 0:dbad57390bd1 | 91 | |
ram54288 | 0:dbad57390bd1 | 92 | } |
ram54288 | 0:dbad57390bd1 | 93 | |
ram54288 | 0:dbad57390bd1 | 94 | bool register_error; |
ram54288 | 0:dbad57390bd1 | 95 | bool boot_error; |
ram54288 | 0:dbad57390bd1 | 96 | bool boot_wait; |
ram54288 | 0:dbad57390bd1 | 97 | bool boot_done; |
ram54288 | 0:dbad57390bd1 | 98 | bool registered; |
ram54288 | 0:dbad57390bd1 | 99 | bool data_processed; |
ram54288 | 0:dbad57390bd1 | 100 | bool unregistered; |
ram54288 | 0:dbad57390bd1 | 101 | bool message_ready; |
ram54288 | 0:dbad57390bd1 | 102 | |
ram54288 | 0:dbad57390bd1 | 103 | }; |
ram54288 | 0:dbad57390bd1 | 104 | |
ram54288 | 0:dbad57390bd1 | 105 | Test_NsdlAccessHelper::Test_NsdlAccessHelper() |
ram54288 | 0:dbad57390bd1 | 106 | { |
ram54288 | 0:dbad57390bd1 | 107 | observer = new TestObserver(); |
ram54288 | 0:dbad57390bd1 | 108 | } |
ram54288 | 0:dbad57390bd1 | 109 | |
ram54288 | 0:dbad57390bd1 | 110 | Test_NsdlAccessHelper::~Test_NsdlAccessHelper() |
ram54288 | 0:dbad57390bd1 | 111 | { |
ram54288 | 0:dbad57390bd1 | 112 | delete observer; |
ram54288 | 0:dbad57390bd1 | 113 | observer = NULL; |
ram54288 | 0:dbad57390bd1 | 114 | |
ram54288 | 0:dbad57390bd1 | 115 | clear_list(); |
ram54288 | 0:dbad57390bd1 | 116 | } |
ram54288 | 0:dbad57390bd1 | 117 | |
ram54288 | 0:dbad57390bd1 | 118 | void Test_NsdlAccessHelper::test_nsdl_c_callback() |
ram54288 | 0:dbad57390bd1 | 119 | { |
ram54288 | 0:dbad57390bd1 | 120 | CHECK(__nsdl_c_callback(NULL,NULL,NULL,SN_NSDL_PROTOCOL_HTTP) == 0 ); |
ram54288 | 0:dbad57390bd1 | 121 | |
ram54288 | 0:dbad57390bd1 | 122 | m2mnsdlinterface_stub::int_value = 1; |
ram54288 | 0:dbad57390bd1 | 123 | m2mnsdlinterface_stub::void_value = malloc(1); |
ram54288 | 0:dbad57390bd1 | 124 | __nsdl_interface_list.clear(); |
ram54288 | 0:dbad57390bd1 | 125 | __nsdl_interface_list.push_back(new M2MNsdlInterface(*observer)); |
ram54288 | 0:dbad57390bd1 | 126 | common_stub::coap_header = (sn_coap_hdr_s *) malloc(sizeof(sn_coap_hdr_s)); |
ram54288 | 0:dbad57390bd1 | 127 | memset(common_stub::coap_header, 0, sizeof(sn_coap_hdr_s)); |
ram54288 | 0:dbad57390bd1 | 128 | common_stub::coap_header->options_list_ptr = (sn_coap_options_list_s *) malloc(sizeof(sn_coap_options_list_s)); |
ram54288 | 0:dbad57390bd1 | 129 | common_stub::coap_header->options_list_ptr->block1 = 0x0101; |
ram54288 | 0:dbad57390bd1 | 130 | |
ram54288 | 0:dbad57390bd1 | 131 | CHECK(__nsdl_c_callback((nsdl_s*)m2mnsdlinterface_stub::void_value, |
ram54288 | 0:dbad57390bd1 | 132 | common_stub::coap_header,NULL,SN_NSDL_PROTOCOL_HTTP) == 1 ); |
ram54288 | 0:dbad57390bd1 | 133 | free(common_stub::coap_header->options_list_ptr); |
ram54288 | 0:dbad57390bd1 | 134 | free(common_stub::coap_header); |
ram54288 | 0:dbad57390bd1 | 135 | free(m2mnsdlinterface_stub::void_value); |
ram54288 | 0:dbad57390bd1 | 136 | clear_list(); |
ram54288 | 0:dbad57390bd1 | 137 | } |
ram54288 | 0:dbad57390bd1 | 138 | |
ram54288 | 0:dbad57390bd1 | 139 | void Test_NsdlAccessHelper::test_nsdl_c_memory_alloc() |
ram54288 | 0:dbad57390bd1 | 140 | { |
ram54288 | 0:dbad57390bd1 | 141 | void *ptr = __nsdl_c_memory_alloc(6); |
ram54288 | 0:dbad57390bd1 | 142 | CHECK(ptr != NULL); |
ram54288 | 0:dbad57390bd1 | 143 | free(ptr); |
ram54288 | 0:dbad57390bd1 | 144 | ptr = NULL; |
ram54288 | 0:dbad57390bd1 | 145 | ptr = __nsdl_c_memory_alloc(UINT16_MAX+1); |
ram54288 | 0:dbad57390bd1 | 146 | CHECK(ptr == NULL); |
ram54288 | 0:dbad57390bd1 | 147 | free(ptr); |
ram54288 | 0:dbad57390bd1 | 148 | } |
ram54288 | 0:dbad57390bd1 | 149 | |
ram54288 | 0:dbad57390bd1 | 150 | void Test_NsdlAccessHelper::test_nsdl_c_memory_free() |
ram54288 | 0:dbad57390bd1 | 151 | { |
ram54288 | 0:dbad57390bd1 | 152 | void* ptr = malloc(7); |
ram54288 | 0:dbad57390bd1 | 153 | __nsdl_c_memory_free(ptr); |
ram54288 | 0:dbad57390bd1 | 154 | |
ram54288 | 0:dbad57390bd1 | 155 | CHECK(ptr != NULL); |
ram54288 | 0:dbad57390bd1 | 156 | |
ram54288 | 0:dbad57390bd1 | 157 | ptr = NULL; |
ram54288 | 0:dbad57390bd1 | 158 | //No need to check anything, since memory leak is the test |
ram54288 | 0:dbad57390bd1 | 159 | } |
ram54288 | 0:dbad57390bd1 | 160 | |
ram54288 | 0:dbad57390bd1 | 161 | void Test_NsdlAccessHelper::test_nsdl_c_send_to_server() |
ram54288 | 0:dbad57390bd1 | 162 | { |
ram54288 | 0:dbad57390bd1 | 163 | CHECK(__nsdl_c_send_to_server(NULL, SN_NSDL_PROTOCOL_HTTP, NULL, 0, NULL) == 0); |
ram54288 | 0:dbad57390bd1 | 164 | |
ram54288 | 0:dbad57390bd1 | 165 | m2mnsdlinterface_stub::int_value = 1; |
ram54288 | 0:dbad57390bd1 | 166 | m2mnsdlinterface_stub::void_value = malloc(1); |
ram54288 | 0:dbad57390bd1 | 167 | __nsdl_interface_list.clear(); |
ram54288 | 0:dbad57390bd1 | 168 | __nsdl_interface_list.push_back(new M2MNsdlInterface(*observer)); |
ram54288 | 0:dbad57390bd1 | 169 | |
ram54288 | 0:dbad57390bd1 | 170 | CHECK(__nsdl_c_send_to_server((nsdl_s*)m2mnsdlinterface_stub::void_value, SN_NSDL_PROTOCOL_HTTP, NULL, 0, NULL) == 1); |
ram54288 | 0:dbad57390bd1 | 171 | free(m2mnsdlinterface_stub::void_value); |
ram54288 | 0:dbad57390bd1 | 172 | clear_list(); |
ram54288 | 0:dbad57390bd1 | 173 | } |
ram54288 | 0:dbad57390bd1 | 174 | |
ram54288 | 0:dbad57390bd1 | 175 | void Test_NsdlAccessHelper::test_nsdl_c_received_from_server() |
ram54288 | 0:dbad57390bd1 | 176 | { |
ram54288 | 0:dbad57390bd1 | 177 | CHECK( 0 == __nsdl_c_received_from_server(NULL, NULL, NULL)); |
ram54288 | 0:dbad57390bd1 | 178 | |
ram54288 | 0:dbad57390bd1 | 179 | m2mnsdlinterface_stub::int_value = 1; |
ram54288 | 0:dbad57390bd1 | 180 | m2mnsdlinterface_stub::void_value = malloc(1); |
ram54288 | 0:dbad57390bd1 | 181 | __nsdl_interface_list.clear(); |
ram54288 | 0:dbad57390bd1 | 182 | __nsdl_interface_list.push_back(new M2MNsdlInterface(*observer)); |
ram54288 | 0:dbad57390bd1 | 183 | common_stub::coap_header = (sn_coap_hdr_s *) malloc(sizeof(sn_coap_hdr_s)); |
ram54288 | 0:dbad57390bd1 | 184 | memset(common_stub::coap_header, 0, sizeof(sn_coap_hdr_s)); |
ram54288 | 0:dbad57390bd1 | 185 | common_stub::coap_header->options_list_ptr = (sn_coap_options_list_s *) malloc(sizeof(sn_coap_options_list_s)); |
ram54288 | 0:dbad57390bd1 | 186 | common_stub::coap_header->options_list_ptr->block1 = 0x0101; |
ram54288 | 0:dbad57390bd1 | 187 | CHECK( 1 == __nsdl_c_received_from_server((nsdl_s*)m2mnsdlinterface_stub::void_value, common_stub::coap_header, NULL)); |
ram54288 | 0:dbad57390bd1 | 188 | free(common_stub::coap_header->options_list_ptr); |
ram54288 | 0:dbad57390bd1 | 189 | free(common_stub::coap_header); |
ram54288 | 0:dbad57390bd1 | 190 | free(m2mnsdlinterface_stub::void_value); |
ram54288 | 0:dbad57390bd1 | 191 | clear_list(); |
ram54288 | 0:dbad57390bd1 | 192 | } |
ram54288 | 0:dbad57390bd1 | 193 | |
ram54288 | 0:dbad57390bd1 | 194 | void Test_NsdlAccessHelper::test_socket_malloc() |
ram54288 | 0:dbad57390bd1 | 195 | { |
ram54288 | 0:dbad57390bd1 | 196 | void *ptr = __socket_malloc(NULL, 6); |
ram54288 | 0:dbad57390bd1 | 197 | CHECK(ptr != NULL); |
ram54288 | 0:dbad57390bd1 | 198 | |
ram54288 | 0:dbad57390bd1 | 199 | free(ptr); |
ram54288 | 0:dbad57390bd1 | 200 | } |
ram54288 | 0:dbad57390bd1 | 201 | |
ram54288 | 0:dbad57390bd1 | 202 | void Test_NsdlAccessHelper::test_socket_free() |
ram54288 | 0:dbad57390bd1 | 203 | { |
ram54288 | 0:dbad57390bd1 | 204 | void* ptr = malloc(7); |
ram54288 | 0:dbad57390bd1 | 205 | __socket_free(NULL, ptr); |
ram54288 | 0:dbad57390bd1 | 206 | |
ram54288 | 0:dbad57390bd1 | 207 | ptr = NULL; |
ram54288 | 0:dbad57390bd1 | 208 | //No need to check anything, since memory leak is the test |
ram54288 | 0:dbad57390bd1 | 209 | } |
ram54288 | 0:dbad57390bd1 | 210 | |
ram54288 | 0:dbad57390bd1 | 211 | void Test_NsdlAccessHelper::test_mutex_claim() |
ram54288 | 0:dbad57390bd1 | 212 | { |
ram54288 | 0:dbad57390bd1 | 213 | __connection_handler = new M2MConnectionHandler(*observer, |
ram54288 | 0:dbad57390bd1 | 214 | NULL, |
ram54288 | 0:dbad57390bd1 | 215 | M2MInterface::UDP, |
ram54288 | 0:dbad57390bd1 | 216 | M2MInterface::LwIP_IPv4); |
ram54288 | 0:dbad57390bd1 | 217 | __mutex_claim(); |
ram54288 | 0:dbad57390bd1 | 218 | |
ram54288 | 0:dbad57390bd1 | 219 | delete __connection_handler; |
ram54288 | 0:dbad57390bd1 | 220 | } |
ram54288 | 0:dbad57390bd1 | 221 | |
ram54288 | 0:dbad57390bd1 | 222 | void Test_NsdlAccessHelper::test_mutex_release() |
ram54288 | 0:dbad57390bd1 | 223 | { |
ram54288 | 0:dbad57390bd1 | 224 | __connection_handler = new M2MConnectionHandler(*observer, |
ram54288 | 0:dbad57390bd1 | 225 | NULL, |
ram54288 | 0:dbad57390bd1 | 226 | M2MInterface::UDP, |
ram54288 | 0:dbad57390bd1 | 227 | M2MInterface::LwIP_IPv4); |
ram54288 | 0:dbad57390bd1 | 228 | __mutex_release(); |
ram54288 | 0:dbad57390bd1 | 229 | |
ram54288 | 0:dbad57390bd1 | 230 | delete __connection_handler; |
ram54288 | 0:dbad57390bd1 | 231 | } |
ram54288 | 0:dbad57390bd1 | 232 | |
ram54288 | 0:dbad57390bd1 | 233 | void Test_NsdlAccessHelper::clear_list() |
ram54288 | 0:dbad57390bd1 | 234 | { |
ram54288 | 0:dbad57390bd1 | 235 | M2MNsdlInterfaceList::const_iterator it; |
ram54288 | 0:dbad57390bd1 | 236 | it = __nsdl_interface_list.begin(); |
ram54288 | 0:dbad57390bd1 | 237 | int size = __nsdl_interface_list.size(); |
ram54288 | 0:dbad57390bd1 | 238 | if (!__nsdl_interface_list.empty()) { |
ram54288 | 0:dbad57390bd1 | 239 | for (int i = 0; i < size; i++) { |
ram54288 | 0:dbad57390bd1 | 240 | delete __nsdl_interface_list[i]; |
ram54288 | 0:dbad57390bd1 | 241 | __nsdl_interface_list.erase(i); |
ram54288 | 0:dbad57390bd1 | 242 | } |
ram54288 | 0:dbad57390bd1 | 243 | __nsdl_interface_list.clear(); |
ram54288 | 0:dbad57390bd1 | 244 | } |
ram54288 | 0:dbad57390bd1 | 245 | } |