|
| 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 "CellularDevice.h" |
| 21 | +#include "FileHandle_stub.h" |
| 22 | +#include "myCellularDevice.h" |
| 23 | +#include "CellularStateMachine_stub.h" |
| 24 | +#include "CellularSIM.h" |
| 25 | + |
| 26 | +using namespace mbed; |
| 27 | + |
| 28 | +// AStyle ignored as the definition is not clear due to preprocessor usage |
| 29 | +// *INDENT-OFF* |
| 30 | +class TestCellularDevice : public testing::Test { |
| 31 | +protected: |
| 32 | + |
| 33 | + void SetUp() |
| 34 | + { |
| 35 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; |
| 36 | + CellularStateMachine_stub::get_current_target_state = STATE_INIT; |
| 37 | + CellularStateMachine_stub::get_current_current_state = STATE_INIT; |
| 38 | + CellularStateMachine_stub::bool_value = false; |
| 39 | + } |
| 40 | + |
| 41 | + void TearDown() |
| 42 | + { |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +// *INDENT-ON* |
| 47 | +TEST_F(TestCellularDevice, test_create_delete) |
| 48 | +{ |
| 49 | + FileHandle_stub fh1; |
| 50 | + |
| 51 | + CellularDevice *dev = new myCellularDevice(&fh1); |
| 52 | + EXPECT_TRUE(dev); |
| 53 | + delete dev; |
| 54 | + dev = NULL; |
| 55 | + |
| 56 | + CellularDevice *dev1 = CellularDevice::get_default_instance(); |
| 57 | + EXPECT_TRUE(dev1); |
| 58 | +} |
| 59 | + |
| 60 | +TEST_F(TestCellularDevice, test_set_sim_pin) |
| 61 | +{ |
| 62 | + FileHandle_stub fh1; |
| 63 | + CellularDevice *dev = new myCellularDevice(&fh1); |
| 64 | + EXPECT_TRUE(dev); |
| 65 | + |
| 66 | + // MAX_PIN_SIZE = 8; so let's try a longer one |
| 67 | + dev->set_sim_pin("123480375462074360276407"); |
| 68 | + dev->set_sim_pin(NULL); |
| 69 | + dev->set_sim_pin("1234"); |
| 70 | + delete dev; |
| 71 | +} |
| 72 | + |
| 73 | +TEST_F(TestCellularDevice, test_set_plmn) |
| 74 | +{ |
| 75 | + FileHandle_stub fh1; |
| 76 | + CellularDevice *dev = new myCellularDevice(&fh1); |
| 77 | + EXPECT_TRUE(dev); |
| 78 | + |
| 79 | + |
| 80 | + // MAX_PLMN_SIZE = 16; so let's try a longer one |
| 81 | + dev->set_plmn("123480327465023746502734602736073264076340"); |
| 82 | + dev->set_plmn("1234"); |
| 83 | + dev->set_plmn(NULL); |
| 84 | + delete dev; |
| 85 | +} |
| 86 | + |
| 87 | +TEST_F(TestCellularDevice, test_set_device_ready) |
| 88 | +{ |
| 89 | + FileHandle_stub fh1; |
| 90 | + CellularDevice *dev = new myCellularDevice(&fh1); |
| 91 | + EXPECT_TRUE(dev); |
| 92 | + |
| 93 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY; |
| 94 | + ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_NO_MEMORY); |
| 95 | + |
| 96 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; |
| 97 | + ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_OK); |
| 98 | + |
| 99 | + CellularStateMachine_stub::get_current_current_state = STATE_SIM_PIN; |
| 100 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; |
| 101 | + ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_ALREADY); |
| 102 | + |
| 103 | + CellularStateMachine_stub::bool_value = true; |
| 104 | + CellularStateMachine_stub::get_current_target_state = STATE_SIM_PIN; |
| 105 | + CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON; |
| 106 | + ASSERT_EQ(dev->set_device_ready(), NSAPI_ERROR_IN_PROGRESS); |
| 107 | + delete dev; |
| 108 | +} |
| 109 | + |
| 110 | +TEST_F(TestCellularDevice, test_set_sim_ready) |
| 111 | +{ |
| 112 | + FileHandle_stub fh1; |
| 113 | + CellularDevice *dev = new myCellularDevice(&fh1); |
| 114 | + EXPECT_TRUE(dev); |
| 115 | + |
| 116 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY; |
| 117 | + ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_NO_MEMORY); |
| 118 | + |
| 119 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; |
| 120 | + ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_OK); |
| 121 | + |
| 122 | + CellularStateMachine_stub::get_current_current_state = STATE_MANUAL_REGISTERING_NETWORK; |
| 123 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; |
| 124 | + ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_ALREADY); |
| 125 | + |
| 126 | + CellularStateMachine_stub::bool_value = true; |
| 127 | + CellularStateMachine_stub::get_current_target_state = STATE_MANUAL_REGISTERING_NETWORK; |
| 128 | + CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON; |
| 129 | + ASSERT_EQ(dev->set_sim_ready(), NSAPI_ERROR_IN_PROGRESS); |
| 130 | + delete dev; |
| 131 | +} |
| 132 | + |
| 133 | +TEST_F(TestCellularDevice, test_register_to_network) |
| 134 | +{ |
| 135 | + FileHandle_stub fh1; |
| 136 | + CellularDevice *dev = new myCellularDevice(&fh1); |
| 137 | + EXPECT_TRUE(dev); |
| 138 | + |
| 139 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY; |
| 140 | + ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_NO_MEMORY); |
| 141 | + |
| 142 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; |
| 143 | + ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_OK); |
| 144 | + |
| 145 | + CellularStateMachine_stub::get_current_current_state = STATE_ATTACHING_NETWORK; |
| 146 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; |
| 147 | + ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_ALREADY); |
| 148 | + |
| 149 | + CellularStateMachine_stub::bool_value = true; |
| 150 | + CellularStateMachine_stub::get_current_target_state = STATE_ATTACHING_NETWORK; |
| 151 | + CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON; |
| 152 | + ASSERT_EQ(dev->register_to_network(), NSAPI_ERROR_IN_PROGRESS); |
| 153 | + delete dev; |
| 154 | +} |
| 155 | + |
| 156 | +TEST_F(TestCellularDevice, test_attach_to_network) |
| 157 | +{ |
| 158 | + FileHandle_stub fh1; |
| 159 | + CellularDevice *dev = new myCellularDevice(&fh1); |
| 160 | + EXPECT_TRUE(dev); |
| 161 | + |
| 162 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_NO_MEMORY; |
| 163 | + ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_NO_MEMORY); |
| 164 | + |
| 165 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; |
| 166 | + ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_OK); |
| 167 | + |
| 168 | + CellularStateMachine_stub::get_current_current_state = STATE_ATTACHING_NETWORK; |
| 169 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; |
| 170 | + ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_ALREADY); |
| 171 | + |
| 172 | + CellularStateMachine_stub::bool_value = true; |
| 173 | + CellularStateMachine_stub::get_current_target_state = STATE_ATTACHING_NETWORK; |
| 174 | + CellularStateMachine_stub::get_current_current_state = STATE_POWER_ON; |
| 175 | + ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_IN_PROGRESS); |
| 176 | + delete dev; |
| 177 | +} |
| 178 | + |
| 179 | +TEST_F(TestCellularDevice, test_get_context_list) |
| 180 | +{ |
| 181 | + FileHandle_stub fh1; |
| 182 | + |
| 183 | + CellularDevice *dev = new myCellularDevice(&fh1); |
| 184 | + EXPECT_TRUE(dev); |
| 185 | + CellularContext *ctx = dev->create_context(); |
| 186 | + EXPECT_TRUE(dev->get_context_list()); |
| 187 | + delete dev; |
| 188 | + dev = NULL; |
| 189 | + |
| 190 | + dev = new myCellularDevice(&fh1); |
| 191 | + EXPECT_TRUE(dev); |
| 192 | + EXPECT_FALSE(dev->get_context_list()); |
| 193 | +} |
| 194 | + |
| 195 | +TEST_F(TestCellularDevice, test_stop) |
| 196 | +{ |
| 197 | + FileHandle_stub fh1; |
| 198 | + CellularDevice *dev = new myCellularDevice(&fh1); |
| 199 | + EXPECT_TRUE(dev); |
| 200 | + |
| 201 | + CellularStateMachine_stub::nsapi_error_value = NSAPI_ERROR_OK; |
| 202 | + ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_OK); |
| 203 | + |
| 204 | + dev->stop(); |
| 205 | + delete dev; |
| 206 | +} |
| 207 | + |
| 208 | +TEST_F(TestCellularDevice, test_cellular_callback) |
| 209 | +{ |
| 210 | + FileHandle_stub fh1; |
| 211 | + myCellularDevice *dev = new myCellularDevice(&fh1); |
| 212 | + EXPECT_TRUE(dev); |
| 213 | + |
| 214 | + ASSERT_EQ(dev->attach_to_network(), NSAPI_ERROR_OK); |
| 215 | + |
| 216 | + cell_callback_data_t data; |
| 217 | + dev->cellular_callback((nsapi_event_t)CellularRegistrationStatusChanged, (intptr_t)&data); |
| 218 | + |
| 219 | + data.error = NSAPI_ERROR_OK; |
| 220 | + dev->set_plmn("1234"); |
| 221 | + dev->cellular_callback((nsapi_event_t)CellularDeviceReady, (intptr_t)&data); |
| 222 | + |
| 223 | + dev->set_sim_pin("1234"); |
| 224 | + data.status_data = CellularSIM::SimStatePinNeeded; |
| 225 | + dev->cellular_callback((nsapi_event_t)CellularSIMStatusChanged, (intptr_t)&data); |
| 226 | + |
| 227 | + CellularContext *ctx = dev->create_context(); |
| 228 | + dev->cellular_callback(NSAPI_EVENT_CONNECTION_STATUS_CHANGE, NSAPI_STATUS_DISCONNECTED); |
| 229 | + |
| 230 | + delete dev; |
| 231 | +} |
0 commit comments