Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Test01.cpp Source File

Test01.cpp

00001 #include "Test01.h"
00002 #include "TestHelper.h"
00003 
00004 class HTTPFileValidator : public IHTTPDataIn {
00005 public:
00006     HTTPFileValidator(int sockfd) {
00007        _fileIsValid = false;
00008        _bytesRead = 0;
00009     }
00010     
00011     bool isValid() {
00012        return _fileIsValid;
00013     }
00014     
00015     int bytesRead() {
00016        return _bytesRead;
00017     }
00018 
00019 protected:
00020     //IHTTPDataIn
00021     virtual int write(const char* buf, size_t len) {
00022         int i = 0;
00023         // do nothing if file already found invalid
00024         if(!_fileIsValid)
00025            return len;
00026            
00027         // check that received characters are in correct sequence
00028         for(i=0; i<len; i++) {
00029            if(buf[i]!=_expectedChar) {
00030               _fileIsValid = false;
00031               break;
00032            }
00033            _expectedChar++;
00034            if(_expectedChar==256) {
00035               _expectedChar = 0;
00036            }
00037         }
00038         _bytesRead += i;
00039         
00040         return len;
00041     }
00042     virtual void writeReset() {
00043        _fileIsValid = true;
00044        _expectedChar = 0;
00045        _bytesRead = 0;  
00046     }
00047     virtual void setDataType(const char* type) {}
00048     virtual void setIsChunked(bool chunked) {}
00049     virtual void setDataLen(size_t len) {}
00050     bool _fileIsValid;
00051     char _expectedChar;
00052     int _bytesRead;
00053 };
00054 
00055 
00056 Test01::Test01(VodafoneUSBModem *m) : VodafoneTestCase(m) {}
00057       
00058 void Test01::setupTest() {}
00059    
00060 bool Test01::executeTest() {
00061    HTTPClient http;
00062    char urlBuffer[125];
00063    bool outcome = true;
00064    LOG("Description: %s",gTest01Description);
00065    LOG("Connecting to internet");
00066    if(_modem->connect(APN,APN_USERNAME,APN_PASSWORD)==0) {
00067       LOG("Connected to internet");
00068    } else {
00069       LOG("Failed to connect to internet");
00070       _modem->disconnect();
00071       return false;
00072    }
00073    
00074    // retrieve files whose sizes are successive powers of 2 from 128 bytes upto 1MB
00075    int bytesToRead = 128;
00076    HTTPFileValidator fileValidator(0);
00077    Timer t;
00078    for(int i=0; i<14; i++) {
00079       sprintf(urlBuffer,"http://www.m2mthings.com/test%d.txt",bytesToRead);
00080       LOGX("Doing HTTP GET for %s ... ",urlBuffer);
00081       
00082       // read the file
00083       t.reset();
00084       t.start();
00085       if(http.get(urlBuffer, &fileValidator)!=0) {
00086          LOG("ERROR reading file from website");
00087          outcome = false;
00088          t.stop();
00089          break;
00090       }
00091       t.stop();
00092       
00093       // check that all received bytes were valid, and that the total number of bytes read is as expected
00094       if(fileValidator.isValid()&&fileValidator.bytesRead()==bytesToRead) {
00095          LOG("OK. (%f seconds, %f kb/s)",t.read(),((float)bytesToRead/1000.0)/t.read());
00096       } else {
00097          LOG("ERROR in file validation after %f seconds and %d bytes read.",t.read(),fileValidator.bytesRead());
00098          outcome = false;
00099          break;
00100       }
00101       
00102       bytesToRead *= 2;
00103    }
00104    
00105    _modem->disconnect();
00106    return outcome;
00107 }
00108       
00109 void Test01::endTest() { }