|
| 1 | +/* |
| 2 | + * Copyright (c) 2018, Arm Limited and affiliates. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +#include "gtest/gtest.h" |
| 18 | +#include <string.h> |
| 19 | + |
| 20 | +#include "CellularContext.h" |
| 21 | +#include "CellularDevice_stub.h" |
| 22 | +#include "ControlPlane_netif_stub.h" |
| 23 | +#include "myCellularDevice.h" |
| 24 | + |
| 25 | +using namespace mbed; |
| 26 | + |
| 27 | +// AStyle ignored as the definition is not clear due to preprocessor usage |
| 28 | +// *INDENT-OFF* |
| 29 | +class TestCellularContext : public testing::Test { |
| 30 | +protected: |
| 31 | + |
| 32 | + void SetUp() |
| 33 | + { |
| 34 | + CellularDevice_stub::create_in_get_default = true; |
| 35 | + } |
| 36 | + |
| 37 | + void TearDown() |
| 38 | + { |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +class testContext : public CellularContext |
| 43 | +{ |
| 44 | +public: |
| 45 | + |
| 46 | + testContext(CellularDevice *dev = NULL) |
| 47 | + { |
| 48 | + _device = dev; |
| 49 | + _cp_netif = new ControlPlane_netif_stub(); |
| 50 | + } |
| 51 | + |
| 52 | + ~testContext() |
| 53 | + { |
| 54 | + delete _cp_netif; |
| 55 | + } |
| 56 | + int get_retry_count() |
| 57 | + { |
| 58 | + return _retry_count; |
| 59 | + } |
| 60 | + CellularContext::AuthenticationType get_auth_type() |
| 61 | + { |
| 62 | + return _authentication_type; |
| 63 | + } |
| 64 | + nsapi_error_t set_blocking(bool blocking) |
| 65 | + { |
| 66 | + _is_blocking = blocking; |
| 67 | + return NSAPI_ERROR_OK; |
| 68 | + } |
| 69 | + |
| 70 | + NetworkStack *get_stack() |
| 71 | + { |
| 72 | + return NULL; |
| 73 | + } |
| 74 | + |
| 75 | + const char *get_ip_address() |
| 76 | + { |
| 77 | + return NULL; |
| 78 | + } |
| 79 | + virtual void attach(mbed::Callback<void(nsapi_event_t, intptr_t)> status_cb) |
| 80 | + { |
| 81 | + _status_cb = status_cb; |
| 82 | + } |
| 83 | + virtual nsapi_error_t connect() |
| 84 | + { |
| 85 | + return NSAPI_ERROR_OK; |
| 86 | + } |
| 87 | + |
| 88 | + virtual nsapi_error_t disconnect() |
| 89 | + { |
| 90 | + return NSAPI_ERROR_OK; |
| 91 | + } |
| 92 | + |
| 93 | + virtual void set_plmn(const char *plmn) |
| 94 | + { |
| 95 | + } |
| 96 | + virtual void set_sim_pin(const char *sim_pin) |
| 97 | + { |
| 98 | + } |
| 99 | + virtual nsapi_error_t connect(const char *sim_pin, const char *apn = 0, const char *uname = 0, |
| 100 | + const char *pwd = 0) |
| 101 | + { |
| 102 | + return NSAPI_ERROR_OK; |
| 103 | + } |
| 104 | + virtual void set_credentials(const char *apn, const char *uname = 0, const char *pwd = 0) |
| 105 | + { |
| 106 | + } |
| 107 | + virtual const char *get_netmask() |
| 108 | + { |
| 109 | + return NULL; |
| 110 | + } |
| 111 | + virtual const char *get_gateway() |
| 112 | + { |
| 113 | + return NULL; |
| 114 | + } |
| 115 | + virtual bool is_connected() |
| 116 | + { |
| 117 | + return false; |
| 118 | + } |
| 119 | + nsapi_error_t set_device_ready() |
| 120 | + { |
| 121 | + return NSAPI_ERROR_OK; |
| 122 | + } |
| 123 | + nsapi_error_t set_sim_ready() |
| 124 | + { |
| 125 | + return NSAPI_ERROR_OK; |
| 126 | + } |
| 127 | + nsapi_error_t register_to_network() |
| 128 | + { |
| 129 | + return NSAPI_ERROR_OK; |
| 130 | + } |
| 131 | + nsapi_error_t attach_to_network() |
| 132 | + { |
| 133 | + return NSAPI_ERROR_OK; |
| 134 | + } |
| 135 | + nsapi_error_t get_rate_control(CellularContext::RateControlExceptionReports &reports, |
| 136 | + CellularContext::RateControlUplinkTimeUnit &time_unit, int &uplink_rate) |
| 137 | + { |
| 138 | + return NSAPI_ERROR_OK; |
| 139 | + } |
| 140 | + nsapi_error_t get_pdpcontext_params(pdpContextList_t ¶ms_list) |
| 141 | + { |
| 142 | + return NSAPI_ERROR_OK; |
| 143 | + } |
| 144 | + nsapi_error_t get_apn_backoff_timer(int &backoff_timer) |
| 145 | + { |
| 146 | + return NSAPI_ERROR_OK; |
| 147 | + } |
| 148 | + void set_file_handle(FileHandle *fh) |
| 149 | + { |
| 150 | + |
| 151 | + } |
| 152 | +#if (DEVICE_SERIAL && DEVICE_INTERRUPTIN) || defined(DOXYGEN_ONLY) |
| 153 | + virtual void set_file_handle(UARTSerial *serial, PinName dcd_pin = NC, bool active_high = false) |
| 154 | + { |
| 155 | + |
| 156 | + } |
| 157 | +#endif |
| 158 | + ControlPlane_netif *get_cp_netif() |
| 159 | + { |
| 160 | + return _cp_netif; |
| 161 | + } |
| 162 | + void cellular_callback(nsapi_event_t ev, intptr_t ptr) |
| 163 | + { |
| 164 | + |
| 165 | + } |
| 166 | + void enable_hup(bool enable) |
| 167 | + { |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + void cp_data_received() |
| 172 | + { |
| 173 | + CellularContext::cp_data_received(); |
| 174 | + } |
| 175 | + |
| 176 | + virtual void do_connect() |
| 177 | + { |
| 178 | + } |
| 179 | + |
| 180 | + void set_cell_callback_data(cell_callback_data_t &cb_data) |
| 181 | + { |
| 182 | + _cb_data = cb_data; |
| 183 | + } |
| 184 | + |
| 185 | + void do_connect_with_retry() |
| 186 | + { |
| 187 | + CellularContext::do_connect_with_retry(); |
| 188 | + } |
| 189 | +}; |
| 190 | + |
| 191 | +static int network_cb_count = 0; |
| 192 | +static void network_cb(nsapi_event_t ev, intptr_t intptr) |
| 193 | +{ |
| 194 | + network_cb_count++; |
| 195 | +} |
| 196 | + |
| 197 | +// *INDENT-ON* |
| 198 | +TEST_F(TestCellularContext, test_create_delete) |
| 199 | +{ |
| 200 | + CellularDevice_stub::create_in_get_default = false; |
| 201 | + CellularContext *ctx = CellularContext::get_default_instance(); |
| 202 | + EXPECT_TRUE(ctx == NULL); |
| 203 | + |
| 204 | + ctx = CellularContext::get_default_nonip_instance(); |
| 205 | + EXPECT_TRUE(ctx == NULL); |
| 206 | + |
| 207 | + CellularDevice_stub::create_in_get_default = true; |
| 208 | + ctx = CellularContext::get_default_instance(); |
| 209 | + EXPECT_TRUE(ctx != NULL); |
| 210 | + |
| 211 | + ctx = CellularContext::get_default_nonip_instance(); |
| 212 | + EXPECT_TRUE(ctx != NULL); |
| 213 | +} |
| 214 | + |
| 215 | +TEST_F(TestCellularContext, get_device) |
| 216 | +{ |
| 217 | + CellularContext *ctx = CellularContext::get_default_instance(); |
| 218 | + EXPECT_TRUE(ctx != NULL); |
| 219 | + |
| 220 | + CellularDevice *dev = ctx->get_device(); |
| 221 | + EXPECT_TRUE(dev != NULL); |
| 222 | +} |
| 223 | + |
| 224 | +TEST_F(TestCellularContext, get_cid) |
| 225 | +{ |
| 226 | + CellularContext *ctx = CellularContext::get_default_instance(); |
| 227 | + EXPECT_TRUE(ctx != NULL); |
| 228 | + |
| 229 | + int cid = ctx->get_cid(); |
| 230 | + ASSERT_EQ(cid, -1); |
| 231 | +} |
| 232 | + |
| 233 | +TEST_F(TestCellularContext, set_authentication_type) |
| 234 | +{ |
| 235 | + testContext *ctx = new testContext(); |
| 236 | + EXPECT_TRUE(ctx != NULL); |
| 237 | + |
| 238 | + ASSERT_EQ(ctx->get_auth_type(), CellularContext::CHAP); |
| 239 | + ctx->set_authentication_type(CellularContext::PAP); |
| 240 | + ASSERT_EQ(ctx->get_auth_type(), CellularContext::PAP); |
| 241 | + |
| 242 | + delete ctx; |
| 243 | +} |
| 244 | + |
| 245 | +TEST_F(TestCellularContext, cp_data_received) |
| 246 | +{ |
| 247 | + testContext *ctx = new testContext(); |
| 248 | + EXPECT_TRUE(ctx != NULL); |
| 249 | + |
| 250 | + ControlPlane_netif_stub *netif = (ControlPlane_netif_stub *)ctx->get_cp_netif(); |
| 251 | + EXPECT_TRUE(!netif->cp_data_received_called); |
| 252 | + ctx->cp_data_received(); |
| 253 | + EXPECT_TRUE(netif->cp_data_received_called); |
| 254 | + |
| 255 | + delete ctx; |
| 256 | +} |
| 257 | + |
| 258 | +TEST_F(TestCellularContext, do_connect_with_retry) |
| 259 | +{ |
| 260 | + testContext *ctx = new testContext(); |
| 261 | + EXPECT_TRUE(ctx != NULL); |
| 262 | + ctx->attach(network_cb); |
| 263 | + |
| 264 | + cell_callback_data_t cb_data; |
| 265 | + cb_data.final_try = true; |
| 266 | + ctx->set_cell_callback_data(cb_data); |
| 267 | + ctx->do_connect_with_retry(); |
| 268 | + ASSERT_EQ(network_cb_count, 0); |
| 269 | + |
| 270 | + cb_data.error = NSAPI_ERROR_OK; |
| 271 | + cb_data.final_try = false; |
| 272 | + ctx->set_cell_callback_data(cb_data); |
| 273 | + ctx->do_connect_with_retry(); |
| 274 | + |
| 275 | + CellularDevice_stub::retry_array_length = 2; |
| 276 | + cb_data.error = NSAPI_ERROR_DEVICE_ERROR; |
| 277 | + cb_data.final_try = false; |
| 278 | + ctx->set_cell_callback_data(cb_data); |
| 279 | + ctx->do_connect_with_retry(); |
| 280 | + ASSERT_EQ(ctx->get_retry_count(), 2); |
| 281 | + |
| 282 | + delete ctx; |
| 283 | +} |
| 284 | + |
| 285 | +TEST_F(TestCellularContext, do_connect_with_retry_async) |
| 286 | +{ |
| 287 | + myCellularDevice *dev = new myCellularDevice(0); |
| 288 | + testContext *ctx = new testContext(dev); |
| 289 | + EXPECT_TRUE(ctx != NULL); |
| 290 | + ctx->attach(network_cb); |
| 291 | + ASSERT_EQ(NSAPI_ERROR_OK, ctx->set_blocking(false)); |
| 292 | + |
| 293 | + cell_callback_data_t cb_data; |
| 294 | + CellularDevice_stub::retry_array_length = 2; |
| 295 | + cb_data.error = NSAPI_ERROR_DEVICE_ERROR; |
| 296 | + cb_data.final_try = false; |
| 297 | + ctx->set_cell_callback_data(cb_data); |
| 298 | + ctx->do_connect_with_retry(); |
| 299 | + ASSERT_EQ(ctx->get_retry_count(), 1); |
| 300 | + |
| 301 | + delete ctx; |
| 302 | + delete dev; |
| 303 | +} |
| 304 | + |
| 305 | + |
| 306 | + |
| 307 | + |
0 commit comments