ublox-at-cellular-interface-ext
Dependencies: ublox-at-cellular-interface
TESTS/unit_tests/http/main.cpp@20:737d307fd4d6, 2019-03-22 (annotated)
- Committer:
- SanaMasood
- Date:
- Fri Mar 22 12:13:25 2019 +0500
- Revision:
- 20:737d307fd4d6
- Parent:
- 19:ee06f3b2b078
- Child:
- 28:4427f2e6bbab
Assert updated with the returned status code instead of URL
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobMeades | 0:0b75e22c9231 | 1 | #include "mbed.h" |
RobMeades | 0:0b75e22c9231 | 2 | #include "greentea-client/test_env.h" |
RobMeades | 0:0b75e22c9231 | 3 | #include "unity.h" |
RobMeades | 0:0b75e22c9231 | 4 | #include "utest.h" |
RobMeades | 0:0b75e22c9231 | 5 | #include "UbloxATCellularInterfaceExt.h" |
RobMeades | 0:0b75e22c9231 | 6 | #include "UDPSocket.h" |
rob.meades@u-blox.com | 5:9fd89567f769 | 7 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:0b75e22c9231 | 8 | #include "mbed_trace.h" |
RobMeades | 0:0b75e22c9231 | 9 | #define TRACE_GROUP "TEST" |
rob.meades@u-blox.com | 5:9fd89567f769 | 10 | #else |
rob.meades@u-blox.com | 5:9fd89567f769 | 11 | #define tr_debug(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 5:9fd89567f769 | 12 | #define tr_info(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 5:9fd89567f769 | 13 | #define tr_warn(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 5:9fd89567f769 | 14 | #define tr_error(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 5:9fd89567f769 | 15 | #endif |
RobMeades | 0:0b75e22c9231 | 16 | |
RobMeades | 0:0b75e22c9231 | 17 | using namespace utest::v1; |
RobMeades | 0:0b75e22c9231 | 18 | |
RobMeades | 0:0b75e22c9231 | 19 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 20 | // COMPILE-TIME MACROS |
RobMeades | 0:0b75e22c9231 | 21 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 22 | |
RobMeades | 0:0b75e22c9231 | 23 | // These macros can be overridden with an mbed_app.json file and |
RobMeades | 0:0b75e22c9231 | 24 | // contents of the following form: |
RobMeades | 0:0b75e22c9231 | 25 | // |
RobMeades | 0:0b75e22c9231 | 26 | //{ |
RobMeades | 0:0b75e22c9231 | 27 | // "config": { |
RobMeades | 0:0b75e22c9231 | 28 | // "apn": { |
RobMeades | 0:0b75e22c9231 | 29 | // "value": "\"my_apn\"" |
RobMeades | 0:0b75e22c9231 | 30 | // } |
RobMeades | 0:0b75e22c9231 | 31 | //} |
RobMeades | 0:0b75e22c9231 | 32 | |
rob.meades@u-blox.com | 11:3631f62bb359 | 33 | // Whether debug trace is on |
rob.meades@u-blox.com | 11:3631f62bb359 | 34 | #ifndef MBED_CONF_APP_DEBUG_ON |
rob.meades@u-blox.com | 11:3631f62bb359 | 35 | # define MBED_CONF_APP_DEBUG_ON false |
rob.meades@u-blox.com | 11:3631f62bb359 | 36 | #endif |
rob.meades@u-blox.com | 11:3631f62bb359 | 37 | |
RobMeades | 0:0b75e22c9231 | 38 | // The credentials of the SIM in the board. |
RobMeades | 0:0b75e22c9231 | 39 | #ifndef MBED_CONF_APP_DEFAULT_PIN |
rob.meades@u-blox.com | 11:3631f62bb359 | 40 | // Note: if PIN is enabled on your SIM, you must define the PIN |
rob.meades@u-blox.com | 11:3631f62bb359 | 41 | // for your SIM jere (e.g. using mbed_app.json to do so). |
rob.meades@u-blox.com | 11:3631f62bb359 | 42 | # define MBED_CONF_APP_DEFAULT_PIN "0000" |
RobMeades | 0:0b75e22c9231 | 43 | #endif |
RobMeades | 0:0b75e22c9231 | 44 | |
RobMeades | 0:0b75e22c9231 | 45 | // Network credentials. |
RobMeades | 0:0b75e22c9231 | 46 | #ifndef MBED_CONF_APP_APN |
RobMeades | 0:0b75e22c9231 | 47 | # define MBED_CONF_APP_APN NULL |
RobMeades | 0:0b75e22c9231 | 48 | #endif |
RobMeades | 0:0b75e22c9231 | 49 | #ifndef MBED_CONF_APP_USERNAME |
RobMeades | 0:0b75e22c9231 | 50 | # define MBED_CONF_APP_USERNAME NULL |
RobMeades | 0:0b75e22c9231 | 51 | #endif |
RobMeades | 0:0b75e22c9231 | 52 | #ifndef MBED_CONF_APP_PASSWORD |
RobMeades | 0:0b75e22c9231 | 53 | # define MBED_CONF_APP_PASSWORD NULL |
RobMeades | 0:0b75e22c9231 | 54 | #endif |
RobMeades | 0:0b75e22c9231 | 55 | |
RobMeades | 0:0b75e22c9231 | 56 | // The time to wait for a HTTP command to complete |
fahim.alavi@u-blox.com | 19:ee06f3b2b078 | 57 | #ifndef MBED_CONF_APP_HTTP_TIMEOUT |
fahim.alavi@u-blox.com | 19:ee06f3b2b078 | 58 | #define HTTP_TIMEOUT 10000 |
fahim.alavi@u-blox.com | 19:ee06f3b2b078 | 59 | #else |
fahim.alavi@u-blox.com | 19:ee06f3b2b078 | 60 | #define HTTP_TIMEOUT MBED_CONF_APP_HTTP_TIMEOUT |
fahim.alavi@u-blox.com | 19:ee06f3b2b078 | 61 | #endif |
RobMeades | 0:0b75e22c9231 | 62 | |
RobMeades | 0:0b75e22c9231 | 63 | // The HTTP echo server, as described in the |
RobMeades | 0:0b75e22c9231 | 64 | // first answer here: |
RobMeades | 0:0b75e22c9231 | 65 | // http://stackoverflow.com/questions/5725430/http-test-server-that-accepts-get-post-calls |
RobMeades | 0:0b75e22c9231 | 66 | // !!! IMPORTANT: this test relies on that server behaving in the same way forever !!! |
RobMeades | 0:0b75e22c9231 | 67 | #define HTTP_ECHO_SERVER "httpbin.org" |
RobMeades | 0:0b75e22c9231 | 68 | |
RobMeades | 0:0b75e22c9231 | 69 | // The size of the test file |
RobMeades | 0:0b75e22c9231 | 70 | #define TEST_FILE_SIZE 100 |
RobMeades | 0:0b75e22c9231 | 71 | |
RobMeades | 0:0b75e22c9231 | 72 | // The maximum number of HTTP profiles |
RobMeades | 0:0b75e22c9231 | 73 | #define MAX_PROFILES 4 |
RobMeades | 0:0b75e22c9231 | 74 | |
RobMeades | 0:0b75e22c9231 | 75 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 76 | // PRIVATE VARIABLES |
RobMeades | 0:0b75e22c9231 | 77 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 78 | |
rob.meades@u-blox.com | 5:9fd89567f769 | 79 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:0b75e22c9231 | 80 | // Lock for debug prints |
RobMeades | 0:0b75e22c9231 | 81 | static Mutex mtx; |
rob.meades@u-blox.com | 5:9fd89567f769 | 82 | #endif |
RobMeades | 0:0b75e22c9231 | 83 | |
RobMeades | 0:0b75e22c9231 | 84 | // An instance of the cellular interface |
RobMeades | 0:0b75e22c9231 | 85 | static UbloxATCellularInterfaceExt *pDriver = |
RobMeades | 0:0b75e22c9231 | 86 | new UbloxATCellularInterfaceExt(MDMTXD, MDMRXD, |
RobMeades | 0:0b75e22c9231 | 87 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
rob.meades@u-blox.com | 11:3631f62bb359 | 88 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:0b75e22c9231 | 89 | // A few buffers for general use |
fahim.alavi@u-blox.com | 18:071ae6c6e581 | 90 | static char buf[2048]; |
RobMeades | 0:0b75e22c9231 | 91 | static char buf1[sizeof(buf)]; |
RobMeades | 0:0b75e22c9231 | 92 | |
RobMeades | 0:0b75e22c9231 | 93 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 94 | // PRIVATE FUNCTIONS |
RobMeades | 0:0b75e22c9231 | 95 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 96 | |
rob.meades@u-blox.com | 5:9fd89567f769 | 97 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:0b75e22c9231 | 98 | // Locks for debug prints |
RobMeades | 0:0b75e22c9231 | 99 | static void lock() |
RobMeades | 0:0b75e22c9231 | 100 | { |
RobMeades | 0:0b75e22c9231 | 101 | mtx.lock(); |
RobMeades | 0:0b75e22c9231 | 102 | } |
RobMeades | 0:0b75e22c9231 | 103 | |
RobMeades | 0:0b75e22c9231 | 104 | static void unlock() |
RobMeades | 0:0b75e22c9231 | 105 | { |
RobMeades | 0:0b75e22c9231 | 106 | mtx.unlock(); |
RobMeades | 0:0b75e22c9231 | 107 | } |
rob.meades@u-blox.com | 5:9fd89567f769 | 108 | #endif |
RobMeades | 0:0b75e22c9231 | 109 | |
RobMeades | 0:0b75e22c9231 | 110 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 111 | // TESTS |
RobMeades | 0:0b75e22c9231 | 112 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 113 | |
RobMeades | 0:0b75e22c9231 | 114 | // Test HTTP commands |
RobMeades | 0:0b75e22c9231 | 115 | void test_http_cmd() { |
RobMeades | 0:0b75e22c9231 | 116 | int profile; |
RobMeades | 0:0b75e22c9231 | 117 | char * pData; |
RobMeades | 0:0b75e22c9231 | 118 | |
RobMeades | 0:0b75e22c9231 | 119 | TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:0b75e22c9231 | 120 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:0b75e22c9231 | 121 | |
RobMeades | 0:0b75e22c9231 | 122 | profile = pDriver->httpAllocProfile(); |
RobMeades | 0:0b75e22c9231 | 123 | TEST_ASSERT(profile >= 0); |
RobMeades | 0:0b75e22c9231 | 124 | |
RobMeades | 0:0b75e22c9231 | 125 | pDriver->httpSetTimeout(profile, HTTP_TIMEOUT); |
RobMeades | 0:0b75e22c9231 | 126 | |
RobMeades | 0:0b75e22c9231 | 127 | // Set up the server to talk to |
RobMeades | 0:0b75e22c9231 | 128 | TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, HTTP_ECHO_SERVER)); |
RobMeades | 0:0b75e22c9231 | 129 | |
RobMeades | 0:0b75e22c9231 | 130 | // Check HTTP head request |
RobMeades | 0:0b75e22c9231 | 131 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 132 | TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_HEAD, |
RobMeades | 0:0b75e22c9231 | 133 | "/headers", |
RobMeades | 0:0b75e22c9231 | 134 | NULL, NULL, 0, NULL, |
RobMeades | 0:0b75e22c9231 | 135 | buf, sizeof (buf)) == NULL); |
RobMeades | 0:0b75e22c9231 | 136 | tr_debug("Received: %s", buf); |
RobMeades | 0:0b75e22c9231 | 137 | TEST_ASSERT(strstr(buf, "Content-Length:") != NULL); |
RobMeades | 0:0b75e22c9231 | 138 | |
RobMeades | 0:0b75e22c9231 | 139 | // Check HTTP get request |
RobMeades | 0:0b75e22c9231 | 140 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 141 | TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET, |
RobMeades | 0:0b75e22c9231 | 142 | "/get", |
RobMeades | 0:0b75e22c9231 | 143 | NULL, NULL, 0, NULL, |
RobMeades | 0:0b75e22c9231 | 144 | buf, sizeof (buf)) == NULL); |
RobMeades | 0:0b75e22c9231 | 145 | tr_debug("Received: %s", buf); |
SanaMasood | 20:737d307fd4d6 | 146 | TEST_ASSERT(strstr(buf, "HTTP/1.1 200 OK") != NULL); |
RobMeades | 0:0b75e22c9231 | 147 | |
RobMeades | 0:0b75e22c9231 | 148 | // Check HTTP delete request |
RobMeades | 0:0b75e22c9231 | 149 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 150 | TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_DELETE, |
RobMeades | 0:0b75e22c9231 | 151 | "/delete", |
RobMeades | 0:0b75e22c9231 | 152 | NULL, NULL, 0, NULL, |
RobMeades | 0:0b75e22c9231 | 153 | buf, sizeof (buf)) == NULL); |
RobMeades | 0:0b75e22c9231 | 154 | tr_debug("Received: %s", buf); |
SanaMasood | 20:737d307fd4d6 | 155 | TEST_ASSERT(strstr(buf, "HTTP/1.1 200 OK") != NULL); |
RobMeades | 0:0b75e22c9231 | 156 | |
RobMeades | 0:0b75e22c9231 | 157 | // Check HTTP put request (this will fail as the echo server doesn't support it) |
RobMeades | 0:0b75e22c9231 | 158 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 159 | TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_PUT, |
RobMeades | 0:0b75e22c9231 | 160 | "/put", |
RobMeades | 0:0b75e22c9231 | 161 | NULL, NULL, 0, NULL, |
RobMeades | 0:0b75e22c9231 | 162 | buf, sizeof (buf)) != NULL); |
RobMeades | 0:0b75e22c9231 | 163 | |
RobMeades | 0:0b75e22c9231 | 164 | // Check HTTP post request with data |
RobMeades | 0:0b75e22c9231 | 165 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 166 | TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_POST_DATA, |
RobMeades | 0:0b75e22c9231 | 167 | "/post", |
fahim.alavi@u-blox.com | 18:071ae6c6e581 | 168 | NULL, "formData=0123456789", 0, NULL, |
RobMeades | 0:0b75e22c9231 | 169 | buf, sizeof (buf)) == NULL); |
RobMeades | 0:0b75e22c9231 | 170 | tr_debug("Received: %s", buf); |
SanaMasood | 20:737d307fd4d6 | 171 | TEST_ASSERT(strstr(buf, "HTTP/1.1 200 OK") != NULL); |
RobMeades | 0:0b75e22c9231 | 172 | |
RobMeades | 0:0b75e22c9231 | 173 | // Check HTTP post request with a file, also checking that writing the response |
RobMeades | 0:0b75e22c9231 | 174 | // to a named file works |
RobMeades | 0:0b75e22c9231 | 175 | for (int x = 0; x < TEST_FILE_SIZE; x++) { |
RobMeades | 0:0b75e22c9231 | 176 | buf[x] = (x % 10) + 0x30; |
RobMeades | 0:0b75e22c9231 | 177 | } |
RobMeades | 0:0b75e22c9231 | 178 | pDriver->delFile("post_test.txt"); |
RobMeades | 0:0b75e22c9231 | 179 | TEST_ASSERT(pDriver->writeFile("post_test.txt", buf, TEST_FILE_SIZE) == TEST_FILE_SIZE); |
RobMeades | 0:0b75e22c9231 | 180 | |
RobMeades | 0:0b75e22c9231 | 181 | // This may fail if rsp.txt doesn't happen to be sitting around from a previous run |
RobMeades | 0:0b75e22c9231 | 182 | // so don't check the return value |
RobMeades | 0:0b75e22c9231 | 183 | pDriver->delFile("rsp.txt"); |
RobMeades | 0:0b75e22c9231 | 184 | |
RobMeades | 0:0b75e22c9231 | 185 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 186 | TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_POST_FILE, |
RobMeades | 0:0b75e22c9231 | 187 | "/post", |
RobMeades | 0:0b75e22c9231 | 188 | "rsp.txt", "post_test.txt", |
RobMeades | 0:0b75e22c9231 | 189 | UbloxATCellularInterfaceExt::HTTP_CONTENT_TEXT, NULL, |
RobMeades | 0:0b75e22c9231 | 190 | buf, sizeof (buf)) == NULL); |
RobMeades | 0:0b75e22c9231 | 191 | tr_debug("Received: %s", buf); |
RobMeades | 0:0b75e22c9231 | 192 | // Find the data in the response and check it |
RobMeades | 0:0b75e22c9231 | 193 | pData = strstr(buf, "\"data\": \""); |
RobMeades | 0:0b75e22c9231 | 194 | TEST_ASSERT(pData != NULL); |
RobMeades | 0:0b75e22c9231 | 195 | pData += 9; |
RobMeades | 0:0b75e22c9231 | 196 | for (int x = 0; x < TEST_FILE_SIZE; x++) { |
RobMeades | 0:0b75e22c9231 | 197 | TEST_ASSERT(*(pData + x) == (x % 10) + 0x30); |
RobMeades | 0:0b75e22c9231 | 198 | } |
RobMeades | 0:0b75e22c9231 | 199 | |
RobMeades | 0:0b75e22c9231 | 200 | // Also check that rsp.txt exists and is the same as buf |
RobMeades | 0:0b75e22c9231 | 201 | pDriver->readFile("rsp.txt", buf1, sizeof (buf1)); |
RobMeades | 0:0b75e22c9231 | 202 | memcmp(buf1, buf, sizeof (buf1)); |
RobMeades | 0:0b75e22c9231 | 203 | TEST_ASSERT(pDriver->delFile("rsp.txt")); |
RobMeades | 0:0b75e22c9231 | 204 | TEST_ASSERT(!pDriver->delFile("rsp.txt")); // Should fail |
RobMeades | 0:0b75e22c9231 | 205 | |
RobMeades | 0:0b75e22c9231 | 206 | TEST_ASSERT(pDriver->httpFreeProfile(profile)); |
RobMeades | 0:0b75e22c9231 | 207 | TEST_ASSERT(pDriver->disconnect() == 0); |
RobMeades | 0:0b75e22c9231 | 208 | // Wait for printfs to leave the building or the test result string gets messed up |
RobMeades | 0:0b75e22c9231 | 209 | wait_ms(500); |
RobMeades | 0:0b75e22c9231 | 210 | } |
RobMeades | 0:0b75e22c9231 | 211 | |
RobMeades | 0:0b75e22c9231 | 212 | // Test HTTP with TLS |
RobMeades | 0:0b75e22c9231 | 213 | void test_http_tls() { |
RobMeades | 0:0b75e22c9231 | 214 | int profile; |
RobMeades | 0:0b75e22c9231 | 215 | SocketAddress address; |
RobMeades | 0:0b75e22c9231 | 216 | |
RobMeades | 0:0b75e22c9231 | 217 | TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:0b75e22c9231 | 218 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:0b75e22c9231 | 219 | |
RobMeades | 0:0b75e22c9231 | 220 | profile = pDriver->httpAllocProfile(); |
RobMeades | 0:0b75e22c9231 | 221 | TEST_ASSERT(profile >= 0); |
RobMeades | 0:0b75e22c9231 | 222 | |
RobMeades | 0:0b75e22c9231 | 223 | pDriver->httpSetTimeout(profile, HTTP_TIMEOUT); |
RobMeades | 0:0b75e22c9231 | 224 | |
RobMeades | 0:0b75e22c9231 | 225 | // Set up the server to talk to and TLS, using the IP address this time just for variety |
RobMeades | 0:0b75e22c9231 | 226 | TEST_ASSERT(pDriver->gethostbyname("amazon.com", &address) == 0); |
RobMeades | 0:0b75e22c9231 | 227 | TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_IP_ADDRESS, address.get_ip_address())); |
RobMeades | 0:0b75e22c9231 | 228 | TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_SECURE, "1")); |
RobMeades | 0:0b75e22c9231 | 229 | |
RobMeades | 0:0b75e22c9231 | 230 | // Check HTTP get request |
RobMeades | 0:0b75e22c9231 | 231 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 232 | TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET, |
RobMeades | 0:0b75e22c9231 | 233 | "/", |
RobMeades | 0:0b75e22c9231 | 234 | NULL, NULL, 0, NULL, |
RobMeades | 0:0b75e22c9231 | 235 | buf, sizeof (buf)) == NULL); |
RobMeades | 0:0b75e22c9231 | 236 | tr_debug("Received: %s", buf); |
RobMeades | 0:0b75e22c9231 | 237 | // This is what amazon.com returns if TLS is set |
fahim.alavi@u-blox.com | 18:071ae6c6e581 | 238 | TEST_ASSERT(strstr(buf, "503 Service Temporarily Unavailable") != NULL); |
RobMeades | 0:0b75e22c9231 | 239 | |
RobMeades | 0:0b75e22c9231 | 240 | // Reset the profile and check that this now fails |
RobMeades | 0:0b75e22c9231 | 241 | TEST_ASSERT(pDriver->httpResetProfile(profile)); |
RobMeades | 0:0b75e22c9231 | 242 | TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_IP_ADDRESS, address.get_ip_address())); |
RobMeades | 0:0b75e22c9231 | 243 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 244 | TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET, |
RobMeades | 0:0b75e22c9231 | 245 | "/", |
RobMeades | 0:0b75e22c9231 | 246 | NULL, NULL, 0, NULL, |
RobMeades | 0:0b75e22c9231 | 247 | buf, sizeof (buf)) == NULL); |
RobMeades | 0:0b75e22c9231 | 248 | tr_debug("Received: %s", buf); |
RobMeades | 0:0b75e22c9231 | 249 | // This is what amazon.com returns if TLS is NOT set |
RobMeades | 0:0b75e22c9231 | 250 | TEST_ASSERT(strstr(buf, "301 Moved Permanently") != NULL); |
RobMeades | 0:0b75e22c9231 | 251 | |
RobMeades | 0:0b75e22c9231 | 252 | TEST_ASSERT(pDriver->httpFreeProfile(profile)); |
RobMeades | 0:0b75e22c9231 | 253 | TEST_ASSERT(pDriver->disconnect() == 0); |
RobMeades | 0:0b75e22c9231 | 254 | // Wait for printfs to leave the building or the test result string gets messed up |
RobMeades | 0:0b75e22c9231 | 255 | wait_ms(500); |
RobMeades | 0:0b75e22c9231 | 256 | } |
RobMeades | 0:0b75e22c9231 | 257 | |
RobMeades | 0:0b75e22c9231 | 258 | // Allocate max profiles |
RobMeades | 0:0b75e22c9231 | 259 | void test_alloc_profiles() { |
RobMeades | 0:0b75e22c9231 | 260 | int profiles[MAX_PROFILES]; |
RobMeades | 0:0b75e22c9231 | 261 | |
RobMeades | 0:0b75e22c9231 | 262 | TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:0b75e22c9231 | 263 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:0b75e22c9231 | 264 | |
RobMeades | 0:0b75e22c9231 | 265 | // Allocate first profile and use it |
RobMeades | 0:0b75e22c9231 | 266 | profiles[0] = pDriver->httpAllocProfile(); |
RobMeades | 0:0b75e22c9231 | 267 | TEST_ASSERT(profiles[0] >= 0); |
fahim.alavi@u-blox.com | 18:071ae6c6e581 | 268 | TEST_ASSERT(pDriver->httpSetPar(profiles[0], UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, "raw.githubusercontent.com")); |
fahim.alavi@u-blox.com | 18:071ae6c6e581 | 269 | TEST_ASSERT(pDriver->httpSetPar(profiles[0], UbloxATCellularInterfaceExt::HTTP_SECURE, "1")); |
RobMeades | 0:0b75e22c9231 | 270 | |
RobMeades | 0:0b75e22c9231 | 271 | // Check HTTP get request |
RobMeades | 0:0b75e22c9231 | 272 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 273 | TEST_ASSERT(pDriver->httpCommand(profiles[0], UbloxATCellularInterfaceExt::HTTP_GET, |
fahim.alavi@u-blox.com | 18:071ae6c6e581 | 274 | "/u-blox/mbed-os/master/features/cellular/mbed_lib.json", |
RobMeades | 0:0b75e22c9231 | 275 | NULL, NULL, 0, NULL, |
RobMeades | 0:0b75e22c9231 | 276 | buf, sizeof (buf)) == NULL); |
RobMeades | 0:0b75e22c9231 | 277 | tr_debug("Received: %s", buf); |
fahim.alavi@u-blox.com | 18:071ae6c6e581 | 278 | TEST_ASSERT(strstr(buf, "Radio access technology to use. Value in integer: GSM=0, GSM_COMPACT=1, UTRAN=2, EGPRS=3, HSDPA=4, HSUPA=5, HSDPA_HSUPA=6, E_UTRAN=7, CATM1=8 ,NB1=9") != NULL); |
RobMeades | 0:0b75e22c9231 | 279 | |
RobMeades | 0:0b75e22c9231 | 280 | // Check that we stop being able to get profiles at the max number |
RobMeades | 0:0b75e22c9231 | 281 | for (int x = 1; x < sizeof (profiles) / sizeof (profiles[0]); x++) { |
RobMeades | 0:0b75e22c9231 | 282 | profiles[x] = pDriver->httpAllocProfile(); |
RobMeades | 0:0b75e22c9231 | 283 | TEST_ASSERT(profiles[0] >= 0); |
RobMeades | 0:0b75e22c9231 | 284 | } |
RobMeades | 0:0b75e22c9231 | 285 | TEST_ASSERT(pDriver->httpAllocProfile() < 0); |
RobMeades | 0:0b75e22c9231 | 286 | |
RobMeades | 0:0b75e22c9231 | 287 | // Now use the last one and check that it doesn't affect the first one |
RobMeades | 0:0b75e22c9231 | 288 | TEST_ASSERT(pDriver->httpSetPar(profiles[sizeof (profiles) / sizeof (profiles[0]) - 1], |
RobMeades | 0:0b75e22c9231 | 289 | UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, HTTP_ECHO_SERVER)); |
RobMeades | 0:0b75e22c9231 | 290 | |
RobMeades | 0:0b75e22c9231 | 291 | // Check HTTP head request on last profile |
RobMeades | 0:0b75e22c9231 | 292 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 293 | TEST_ASSERT(pDriver->httpCommand(profiles[sizeof (profiles) / sizeof (profiles[0]) - 1], |
RobMeades | 0:0b75e22c9231 | 294 | UbloxATCellularInterfaceExt::HTTP_HEAD, |
RobMeades | 0:0b75e22c9231 | 295 | "/headers", |
RobMeades | 0:0b75e22c9231 | 296 | NULL, NULL, 0, NULL, |
RobMeades | 0:0b75e22c9231 | 297 | buf, sizeof (buf)) == NULL); |
RobMeades | 0:0b75e22c9231 | 298 | tr_debug("Received: %s", buf); |
RobMeades | 0:0b75e22c9231 | 299 | TEST_ASSERT(strstr(buf, "Content-Length:") != NULL); |
RobMeades | 0:0b75e22c9231 | 300 | |
RobMeades | 0:0b75e22c9231 | 301 | // Check HTTP get request on first profile once more |
RobMeades | 0:0b75e22c9231 | 302 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:0b75e22c9231 | 303 | TEST_ASSERT(pDriver->httpCommand(profiles[0], UbloxATCellularInterfaceExt::HTTP_GET, |
fahim.alavi@u-blox.com | 18:071ae6c6e581 | 304 | "/u-blox/mbed-os/master/features/cellular/mbed_lib.json", |
RobMeades | 0:0b75e22c9231 | 305 | NULL, NULL, 0, NULL, |
RobMeades | 0:0b75e22c9231 | 306 | buf, sizeof (buf)) == NULL); |
RobMeades | 0:0b75e22c9231 | 307 | tr_debug("Received: %s", buf); |
fahim.alavi@u-blox.com | 18:071ae6c6e581 | 308 | TEST_ASSERT(strstr(buf, "Radio access technology to use. Value in integer: GSM=0, GSM_COMPACT=1, UTRAN=2, EGPRS=3, HSDPA=4, HSUPA=5, HSDPA_HSUPA=6, E_UTRAN=7, CATM1=8 ,NB1=9") != NULL); |
RobMeades | 0:0b75e22c9231 | 309 | |
RobMeades | 0:0b75e22c9231 | 310 | // Free the profiles again |
RobMeades | 0:0b75e22c9231 | 311 | for (int x = 0; x < sizeof (profiles) / sizeof (profiles[0]); x++) { |
RobMeades | 0:0b75e22c9231 | 312 | TEST_ASSERT(pDriver->httpFreeProfile(profiles[x])); |
RobMeades | 0:0b75e22c9231 | 313 | } |
RobMeades | 0:0b75e22c9231 | 314 | |
RobMeades | 0:0b75e22c9231 | 315 | TEST_ASSERT(pDriver->disconnect() == 0); |
RobMeades | 0:0b75e22c9231 | 316 | // Wait for printfs to leave the building or the test result string gets messed up |
RobMeades | 0:0b75e22c9231 | 317 | wait_ms(500); |
RobMeades | 0:0b75e22c9231 | 318 | } |
RobMeades | 0:0b75e22c9231 | 319 | |
RobMeades | 0:0b75e22c9231 | 320 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 321 | // TEST ENVIRONMENT |
RobMeades | 0:0b75e22c9231 | 322 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 323 | |
RobMeades | 0:0b75e22c9231 | 324 | // Setup the test environment |
RobMeades | 0:0b75e22c9231 | 325 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
RobMeades | 0:0b75e22c9231 | 326 | // Setup Greentea with a timeout |
RobMeades | 0:0b75e22c9231 | 327 | GREENTEA_SETUP(540, "default_auto"); |
RobMeades | 0:0b75e22c9231 | 328 | return verbose_test_setup_handler(number_of_cases); |
RobMeades | 0:0b75e22c9231 | 329 | } |
RobMeades | 0:0b75e22c9231 | 330 | |
RobMeades | 0:0b75e22c9231 | 331 | // Test cases |
RobMeades | 0:0b75e22c9231 | 332 | Case cases[] = { |
RobMeades | 0:0b75e22c9231 | 333 | Case("HTTP commands", test_http_cmd), |
rob.meades@u-blox.com | 11:3631f62bb359 | 334 | #ifndef TARGET_UBLOX_C027 |
rob.meades@u-blox.com | 11:3631f62bb359 | 335 | // C027 doesn't support TLS |
RobMeades | 0:0b75e22c9231 | 336 | Case("HTTP with TLS", test_http_tls), |
rob.meades@u-blox.com | 11:3631f62bb359 | 337 | #endif |
RobMeades | 0:0b75e22c9231 | 338 | Case("Alloc max profiles", test_alloc_profiles) |
RobMeades | 0:0b75e22c9231 | 339 | }; |
RobMeades | 0:0b75e22c9231 | 340 | |
RobMeades | 0:0b75e22c9231 | 341 | Specification specification(test_setup, cases); |
RobMeades | 0:0b75e22c9231 | 342 | |
RobMeades | 0:0b75e22c9231 | 343 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 344 | // MAIN |
RobMeades | 0:0b75e22c9231 | 345 | // ---------------------------------------------------------------- |
RobMeades | 0:0b75e22c9231 | 346 | |
RobMeades | 0:0b75e22c9231 | 347 | int main() { |
rob.meades@u-blox.com | 5:9fd89567f769 | 348 | |
rob.meades@u-blox.com | 5:9fd89567f769 | 349 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:0b75e22c9231 | 350 | mbed_trace_init(); |
RobMeades | 0:0b75e22c9231 | 351 | |
RobMeades | 0:0b75e22c9231 | 352 | mbed_trace_mutex_wait_function_set(lock); |
RobMeades | 0:0b75e22c9231 | 353 | mbed_trace_mutex_release_function_set(unlock); |
rob.meades@u-blox.com | 5:9fd89567f769 | 354 | #endif |
RobMeades | 0:0b75e22c9231 | 355 | |
RobMeades | 0:0b75e22c9231 | 356 | // Run tests |
RobMeades | 0:0b75e22c9231 | 357 | return !Harness::run(specification); |
RobMeades | 0:0b75e22c9231 | 358 | } |
RobMeades | 0:0b75e22c9231 | 359 | |
RobMeades | 0:0b75e22c9231 | 360 | // End Of File |
RobMeades | 0:0b75e22c9231 | 361 |