amq amq / ublox-at-cellular-interface-ext-add-priority

Dependencies:   ublox-at-cellular-interface

Fork of ublox-at-cellular-interface-ext by u-blox

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "greentea-client/test_env.h"
00003 #include "unity.h"
00004 #include "utest.h"
00005 #include "UbloxATCellularInterfaceExt.h"
00006 #include "UDPSocket.h"
00007 #ifdef FEATURE_COMMON_PAL
00008 #include "mbed_trace.h"
00009 #define TRACE_GROUP "TEST"
00010 #else
00011 #define tr_debug(format, ...) debug(format "\n", ## __VA_ARGS__)
00012 #define tr_info(format, ...)  debug(format "\n", ## __VA_ARGS__)
00013 #define tr_warn(format, ...)  debug(format "\n", ## __VA_ARGS__)
00014 #define tr_error(format, ...) debug(format "\n", ## __VA_ARGS__)
00015 #endif
00016 
00017 using namespace utest::v1;
00018 
00019 // ----------------------------------------------------------------
00020 // COMPILE-TIME MACROS
00021 // ----------------------------------------------------------------
00022 
00023 // These macros can be overridden with an mbed_app.json file and
00024 // contents of the following form:
00025 //
00026 //{
00027 //    "config": {
00028 //        "apn": {
00029 //            "value": "\"my_apn\""
00030 //        }
00031 //}
00032 
00033 // Whether debug trace is on
00034 #ifndef MBED_CONF_APP_DEBUG_ON
00035 # define MBED_CONF_APP_DEBUG_ON false
00036 #endif
00037 
00038 // The credentials of the SIM in the board.
00039 #ifndef MBED_CONF_APP_DEFAULT_PIN
00040 // Note: if PIN is enabled on your SIM, you must define the PIN
00041 // for your SIM jere (e.g. using mbed_app.json to do so).
00042 # define MBED_CONF_APP_DEFAULT_PIN "0000"
00043 #endif
00044 
00045 // Network credentials.
00046 #ifndef MBED_CONF_APP_APN
00047 # define MBED_CONF_APP_APN         NULL
00048 #endif
00049 #ifndef MBED_CONF_APP_USERNAME
00050 # define MBED_CONF_APP_USERNAME    NULL
00051 #endif
00052 #ifndef MBED_CONF_APP_PASSWORD
00053 # define MBED_CONF_APP_PASSWORD    NULL
00054 #endif
00055 
00056 // The time to wait for a HTTP command to complete
00057 #define HTTP_TIMEOUT  10000
00058 
00059 // The HTTP echo server, as described in the
00060 // first answer here:
00061 // http://stackoverflow.com/questions/5725430/http-test-server-that-accepts-get-post-calls
00062 // !!! IMPORTANT: this test relies on that server behaving in the same way forever !!!
00063 #define HTTP_ECHO_SERVER "httpbin.org"
00064 
00065 // The size of the test file
00066 #define TEST_FILE_SIZE 100
00067 
00068 // The maximum number of HTTP profiles
00069 #define MAX_PROFILES 4
00070 
00071 // ----------------------------------------------------------------
00072 // PRIVATE VARIABLES
00073 // ----------------------------------------------------------------
00074 
00075 #ifdef FEATURE_COMMON_PAL
00076 // Lock for debug prints
00077 static Mutex mtx;
00078 #endif
00079 
00080 // An instance of the cellular interface
00081 static UbloxATCellularInterfaceExt *pDriver =
00082        new UbloxATCellularInterfaceExt(MDMTXD, MDMRXD,
00083                                        MBED_CONF_UBLOX_CELL_BAUD_RATE,
00084                                        MBED_CONF_APP_DEBUG_ON);
00085 // A few buffers for general use
00086 static char buf[1024];
00087 static char buf1[sizeof(buf)];
00088 
00089 // ----------------------------------------------------------------
00090 // PRIVATE FUNCTIONS
00091 // ----------------------------------------------------------------
00092 
00093 #ifdef FEATURE_COMMON_PAL
00094 // Locks for debug prints
00095 static void lock()
00096 {
00097     mtx.lock();
00098 }
00099 
00100 static void unlock()
00101 {
00102     mtx.unlock();
00103 }
00104 #endif
00105 
00106 // ----------------------------------------------------------------
00107 // TESTS
00108 // ----------------------------------------------------------------
00109 
00110 // Test HTTP commands
00111 void test_http_cmd() {
00112     int profile;
00113     char * pData;
00114 
00115     TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00116                                  MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00117 
00118     profile = pDriver->httpAllocProfile();
00119     TEST_ASSERT(profile >= 0);
00120 
00121     pDriver->httpSetTimeout(profile, HTTP_TIMEOUT);
00122 
00123     // Set up the server to talk to
00124     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, HTTP_ECHO_SERVER));
00125 
00126     // Check HTTP head request
00127     memset(buf, 0, sizeof (buf));
00128     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_HEAD,
00129                                      "/headers",
00130                                      NULL, NULL, 0, NULL,
00131                                      buf, sizeof (buf)) == NULL);
00132     tr_debug("Received: %s", buf);
00133     TEST_ASSERT(strstr(buf, "Content-Length:") != NULL);
00134 
00135     // Check HTTP get request
00136     memset(buf, 0, sizeof (buf));
00137     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET,
00138                                      "/get",
00139                                      NULL, NULL, 0, NULL,
00140                                      buf, sizeof (buf)) == NULL);
00141     tr_debug("Received: %s", buf);
00142     TEST_ASSERT(strstr(buf, "\"http://httpbin.org/get\"") != NULL);
00143 
00144     // Check HTTP delete request
00145     memset(buf, 0, sizeof (buf));
00146     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_DELETE,
00147                                      "/delete",
00148                                      NULL, NULL, 0, NULL,
00149                                      buf, sizeof (buf)) == NULL);
00150     tr_debug("Received: %s", buf);
00151     TEST_ASSERT(strstr(buf, "\"http://httpbin.org/delete\"") != NULL);
00152 
00153     // Check HTTP put request (this will fail as the echo server doesn't support it)
00154     memset(buf, 0, sizeof (buf));
00155     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_PUT,
00156                                      "/put",
00157                                      NULL, NULL, 0, NULL,
00158                                      buf, sizeof (buf)) != NULL);
00159 
00160     // Check HTTP post request with data
00161     memset(buf, 0, sizeof (buf));
00162     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_POST_DATA,
00163                                      "/post",
00164                                      NULL, NULL, 0, NULL,
00165                                      buf, sizeof (buf)) == NULL);
00166     tr_debug("Received: %s", buf);
00167     TEST_ASSERT(strstr(buf, "\"http://httpbin.org/post\"") != NULL);
00168 
00169     // Check HTTP post request with a file, also checking that writing the response
00170     // to a named file works
00171     for (int x = 0; x < TEST_FILE_SIZE; x++) {
00172         buf[x] = (x % 10) + 0x30;
00173     }
00174     pDriver->delFile("post_test.txt");
00175     TEST_ASSERT(pDriver->writeFile("post_test.txt", buf, TEST_FILE_SIZE) == TEST_FILE_SIZE);
00176 
00177     // This may fail if rsp.txt doesn't happen to be sitting around from a previous run
00178     // so don't check the return value
00179     pDriver->delFile("rsp.txt");
00180 
00181     memset(buf, 0, sizeof (buf));
00182     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_POST_FILE,
00183                                      "/post",
00184                                      "rsp.txt", "post_test.txt",
00185                                      UbloxATCellularInterfaceExt::HTTP_CONTENT_TEXT, NULL,
00186                                      buf, sizeof (buf)) == NULL);
00187     tr_debug("Received: %s", buf);
00188     // Find the data in the response and check it
00189     pData = strstr(buf, "\"data\": \"");
00190     TEST_ASSERT(pData != NULL);
00191     pData += 9;
00192     for (int x = 0; x < TEST_FILE_SIZE; x++) {
00193         TEST_ASSERT(*(pData + x) == (x % 10) + 0x30);
00194     }
00195 
00196     // Also check that rsp.txt exists and is the same as buf
00197     pDriver->readFile("rsp.txt", buf1, sizeof (buf1));
00198     memcmp(buf1, buf, sizeof (buf1));
00199     TEST_ASSERT(pDriver->delFile("rsp.txt"));
00200     TEST_ASSERT(!pDriver->delFile("rsp.txt")); // Should fail
00201 
00202     TEST_ASSERT(pDriver->httpFreeProfile(profile));
00203     TEST_ASSERT(pDriver->disconnect() == 0);
00204     // Wait for printfs to leave the building or the test result string gets messed up
00205     wait_ms(500);
00206 }
00207 
00208 // Test HTTP with TLS
00209 void test_http_tls() {
00210     int profile;
00211     SocketAddress address;
00212 
00213     TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00214                                  MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00215 
00216     profile = pDriver->httpAllocProfile();
00217     TEST_ASSERT(profile >= 0);
00218 
00219     pDriver->httpSetTimeout(profile, HTTP_TIMEOUT);
00220 
00221     // Set up the server to talk to and TLS, using the IP address this time just for variety
00222     TEST_ASSERT(pDriver->gethostbyname("amazon.com", &address) == 0);
00223     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_IP_ADDRESS, address.get_ip_address()));
00224     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_SECURE, "1"));
00225 
00226     // Check HTTP get request
00227     memset(buf, 0, sizeof (buf));
00228     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET,
00229                                      "/",
00230                                      NULL, NULL, 0, NULL,
00231                                      buf, sizeof (buf)) == NULL);
00232     tr_debug("Received: %s", buf);
00233     // This is what amazon.com returns if TLS is set
00234     TEST_ASSERT(strstr(buf, "302 MovedTemporarily") != NULL);
00235 
00236     // Reset the profile and check that this now fails
00237     TEST_ASSERT(pDriver->httpResetProfile(profile));
00238     TEST_ASSERT(pDriver->httpSetPar(profile, UbloxATCellularInterfaceExt::HTTP_IP_ADDRESS, address.get_ip_address()));
00239     memset(buf, 0, sizeof (buf));
00240     TEST_ASSERT(pDriver->httpCommand(profile, UbloxATCellularInterfaceExt::HTTP_GET,
00241                                      "/",
00242                                      NULL, NULL, 0, NULL,
00243                                      buf, sizeof (buf)) == NULL);
00244     tr_debug("Received: %s", buf);
00245     // This is what amazon.com returns if TLS is NOT set
00246     TEST_ASSERT(strstr(buf, "301 Moved Permanently") != NULL);
00247 
00248     TEST_ASSERT(pDriver->httpFreeProfile(profile));
00249     TEST_ASSERT(pDriver->disconnect() == 0);
00250     // Wait for printfs to leave the building or the test result string gets messed up
00251     wait_ms(500);
00252 }
00253 
00254 // Allocate max profiles
00255 void test_alloc_profiles() {
00256     int profiles[MAX_PROFILES];
00257 
00258     TEST_ASSERT(pDriver->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN,
00259                                  MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0);
00260 
00261     // Allocate first profile and use it
00262     profiles[0] = pDriver->httpAllocProfile();
00263     TEST_ASSERT(profiles[0] >= 0);
00264     TEST_ASSERT(pDriver->httpSetPar(profiles[0], UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, "developer.mbed.org"));
00265 
00266     // Check HTTP get request
00267     memset(buf, 0, sizeof (buf));
00268     TEST_ASSERT(pDriver->httpCommand(profiles[0], UbloxATCellularInterfaceExt::HTTP_GET,
00269                                      "/media/uploads/mbed_official/hello.txt",
00270                                      NULL, NULL, 0, NULL,
00271                                      buf, sizeof (buf)) == NULL);
00272     tr_debug("Received: %s", buf);
00273     TEST_ASSERT(strstr(buf, "Hello world!") != NULL);
00274 
00275     // Check that we stop being able to get profiles at the max number
00276     for (int x = 1; x < sizeof (profiles) / sizeof (profiles[0]); x++) {
00277         profiles[x] = pDriver->httpAllocProfile();
00278         TEST_ASSERT(profiles[0] >= 0);
00279     }
00280     TEST_ASSERT(pDriver->httpAllocProfile() < 0);
00281 
00282     // Now use the last one and check that it doesn't affect the first one
00283     TEST_ASSERT(pDriver->httpSetPar(profiles[sizeof (profiles) / sizeof (profiles[0]) - 1],
00284                                     UbloxATCellularInterfaceExt::HTTP_SERVER_NAME, HTTP_ECHO_SERVER));
00285 
00286     // Check HTTP head request on last profile
00287     memset(buf, 0, sizeof (buf));
00288     TEST_ASSERT(pDriver->httpCommand(profiles[sizeof (profiles) / sizeof (profiles[0]) - 1],
00289                                      UbloxATCellularInterfaceExt::HTTP_HEAD,
00290                                      "/headers",
00291                                      NULL, NULL, 0, NULL,
00292                                      buf, sizeof (buf)) == NULL);
00293     tr_debug("Received: %s", buf);
00294     TEST_ASSERT(strstr(buf, "Content-Length:") != NULL);
00295 
00296     // Check HTTP get request on first profile once more
00297     memset(buf, 0, sizeof (buf));
00298     TEST_ASSERT(pDriver->httpCommand(profiles[0], UbloxATCellularInterfaceExt::HTTP_GET,
00299                                      "/media/uploads/mbed_official/hello.txt",
00300                                      NULL, NULL, 0, NULL,
00301                                      buf, sizeof (buf)) == NULL);
00302     tr_debug("Received: %s", buf);
00303     TEST_ASSERT(strstr(buf, "Hello world!") != NULL);
00304 
00305     // Free the profiles again
00306     for (int x = 0; x < sizeof (profiles) / sizeof (profiles[0]); x++) {
00307         TEST_ASSERT(pDriver->httpFreeProfile(profiles[x]));
00308     }
00309 
00310     TEST_ASSERT(pDriver->disconnect() == 0);
00311     // Wait for printfs to leave the building or the test result string gets messed up
00312     wait_ms(500);
00313 }
00314 
00315 // ----------------------------------------------------------------
00316 // TEST ENVIRONMENT
00317 // ----------------------------------------------------------------
00318 
00319 // Setup the test environment
00320 utest::v1::status_t test_setup(const size_t number_of_cases) {
00321     // Setup Greentea with a timeout
00322     GREENTEA_SETUP(540, "default_auto");
00323     return verbose_test_setup_handler(number_of_cases);
00324 }
00325 
00326 // Test cases
00327 Case cases[] = {
00328     Case("HTTP commands", test_http_cmd),
00329 #ifndef TARGET_UBLOX_C027
00330     // C027 doesn't support TLS
00331     Case("HTTP with TLS", test_http_tls),
00332 #endif
00333     Case("Alloc max profiles", test_alloc_profiles)
00334 };
00335 
00336 Specification specification(test_setup, cases);
00337 
00338 // ----------------------------------------------------------------
00339 // MAIN
00340 // ----------------------------------------------------------------
00341 
00342 int main() {
00343 
00344 #ifdef FEATURE_COMMON_PAL
00345     mbed_trace_init();
00346 
00347     mbed_trace_mutex_wait_function_set(lock);
00348     mbed_trace_mutex_release_function_set(unlock);
00349 #endif
00350     
00351     // Run tests
00352     return !Harness::run(specification);
00353 }
00354 
00355 // End Of File
00356