Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

Tests/Test08.cpp

Committer:
ashleymills
Date:
2012-09-19
Revision:
47:85c30274cc9b
Parent:
46:d2c22206031a
Child:
51:15ba73d1cc45

File content as of revision 47:85c30274cc9b:

#include "Test08.h"

const char *gTest08DNSIn[5] = {
   "m2mthings.com",
   "google.co.uk",
   "example.com",
   "freebsd.org",
   "www.mbed.org",
};

const char *gTest08DNSOut[5] = {
   "109.74.199.96",
   "173.194.69.94",
   "192.0.43.10",
   "69.147.83.40",
   "217.140.101.20",
};

const int gTest08NumDNSVals = 5;

Test08::Test08(VodafoneUSBModem *m) : VodafoneTestCase(m) {
   _description = gTest08Description;
   _testCaseNumber = 8;
}
      
void Test08::setupTest() {}
   
bool Test08::executeTest() {
   bool outcome = true;
   LOG("Description: %s",gTest08Description);
   LOG("Connecting to internet");
   if(_modem->connect("internet","web","web")==0) {
      LOG("Connected to internet");
   } else {
      LOG("Failed to connect to internet");
      outcome = false;
   }
   struct hostent *server;
   do {
      while(1) {
         LOG("Getting host address");
         server = ::gethostbyname("m2mthings.com");
         if(server==NULL) {
            LOG("Failure getting host address!");
            outcome = false;
            //break;
         } else {
            LOG("got host address, length %d",server->h_length);
            break;
         }
      }
      
      if(server->h_length==4) {
         LOG("DNS lookup returned %d.%d.%d.%d",
            server->h_addr[0],
            server->h_addr[1],
            server->h_addr[2],
            server->h_addr[3]
         );
         outcome = true;
      } else {
         LOG("Only IPv4 addresses are supported.");
         outcome = false;
         break;
      }
   } while(0);
   
   // this is the real test
   
   char dnsOut[32];
   
   for(int i=0; i<gTest08NumDNSVals; i++) {
      LOG("Running DNS lookup for %s",gTest08DNSIn[i]);
      server = ::gethostbyname(gTest08DNSIn[i]);
      if(server==NULL) {
         LOG("Failure getting host address!");
         outcome = false;
         break;
      } else {
         LOG("Got host address, length %d",server->h_length);
      }
      
      if(server->h_length==4) {
         sprintf(dnsOut,"%d.%d.%d.%d",
            server->h_addr[0],
            server->h_addr[1],
            server->h_addr[2],
            server->h_addr[3]
         );
         LOG("DNS lookup returned %s",dnsOut);
      } else {
         LOG("Error. Only IPv4 addresses are supported.");
         outcome = false;
         break;
      }
      
      LOG("Expected %s, got %s",gTest08DNSOut[i],dnsOut);
      if(strcmp(dnsOut,gTest08DNSOut[i])!=0) {
         LOG("Mismatch. Fail.");
         outcome = false;
         break;
      } else {
         LOG("Match. OK.");
      }
   }
   
   _modem->disconnect();
   return outcome;
}
      
void Test08::endTest() { }