Vodafone Test Suite
Dependencies: mbed-rtos mbed HTTPClient VodafoneUSBModem
Diff: Tests/Test00.cpp
- Revision:
- 66:6b00a764e549
- Child:
- 67:a61d60fa342c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Tests/Test00.cpp Thu Nov 01 11:19:47 2012 +0000 @@ -0,0 +1,148 @@ +#include "Test00.h" +#include "socket.h" + +bool connectToSocket(char *ipAddress, int port, int *sockfd) { + *sockfd = -1; + // create the socket + if((*sockfd=socket(AF_INET,SOCK_STREAM,0))<0) { + LOG("Error opening socket\r\n"); + return false; + } else { + LOG("Socket open\r\n"); + } + + // create the socket address + sockaddr_in serverAddress; + std::memset(&serverAddress, 0, sizeof(struct sockaddr_in)); + serverAddress.sin_addr.s_addr = inet_addr(ipAddress); + serverAddress.sin_family = AF_INET; + serverAddress.sin_port = htons(port); + + // do socket connect + LOG("Connecting socket to %s:%d", inet_ntoa(serverAddress.sin_addr), ntohs(serverAddress.sin_port)); + if(connect(*sockfd, (const struct sockaddr *)&serverAddress, sizeof(serverAddress))<0) { + ::close(*sockfd); + LOG("Could not connect"); + return false; + } else { + LOG("Connection OK"); + } + return true; +} + +const char *gTest00DNSIn[5] = { + "m2mthings.com", + "kent.ac.uk", + "example.com", + "m2mcontroller.com", + "www.mbed.org", +}; + +const char *gTest00DNSOut[5] = { + "109.74.199.96", + "129.12.10.249", + "192.0.43.10", + "62.105.171.138", + "217.140.101.20", +}; + +const int gTest00NumDNSVals = 5; + +Test00::Test00(VodafoneUSBModem *m) : VodafoneTestCase(m) {} + +void Test00::setupTest() {} + +bool Test00::executeTest() { + bool outcome = true; + LOG("Description: %s",gTest00Description); + LOG("Connecting to internet"); + if(_modem->connect("internet","web","web")==0) { + LOG("Connected to internet"); + } else { + LOG("Failed to connect to internet"); + outcome = false; + } + + // try connecting to DNS server directly + int sockfd; + //if(connectToSocket("88.82.13.28",53,&sockfd)) { + + if(connectToSocket("109.74.199.96",80,&sockfd)) { + close(sockfd); + } + + + 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<gTest00NumDNSVals; i++) { + LOG("Running DNS lookup for %s",gTest00DNSIn[i]); + server = ::gethostbyname(gTest00DNSIn[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",gTest00DNSOut[i],dnsOut); + if(strcmp(dnsOut,gTest00DNSOut[i])!=0) { + LOG("Mismatch. Fail."); + outcome = false; + break; + } else { + LOG("Match. OK."); + } + } + + _modem->disconnect(); + return outcome; +} + +void Test00::endTest() { } \ No newline at end of file