Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

Tests/Test01.cpp

Committer:
ashleymills
Date:
2014-01-29
Revision:
74:e52ac9624f7f
Parent:
69:4fc3b0ad12c7

File content as of revision 74:e52ac9624f7f:

#include "Test01.h"
#include "TestHelper.h"

class HTTPFileValidator : public IHTTPDataIn {
public:
    HTTPFileValidator(int sockfd) {
       _fileIsValid = false;
       _bytesRead = 0;
    }
    
    bool isValid() {
       return _fileIsValid;
    }
    
    int bytesRead() {
       return _bytesRead;
    }

protected:
    //IHTTPDataIn
    virtual int write(const char* buf, size_t len) {
        int i = 0;
        // do nothing if file already found invalid
        if(!_fileIsValid)
           return len;
           
        // check that received characters are in correct sequence
        for(i=0; i<len; i++) {
           if(buf[i]!=_expectedChar) {
              _fileIsValid = false;
              break;
           }
           _expectedChar++;
           if(_expectedChar==256) {
              _expectedChar = 0;
           }
        }
        _bytesRead += i;
        
        return len;
    }
    virtual void writeReset() {
       _fileIsValid = true;
       _expectedChar = 0;
       _bytesRead = 0;  
    }
    virtual void setDataType(const char* type) {}
    virtual void setIsChunked(bool chunked) {}
    virtual void setDataLen(size_t len) {}
    bool _fileIsValid;
    char _expectedChar;
    int _bytesRead;
};


Test01::Test01(VodafoneUSBModem *m) : VodafoneTestCase(m) {}
      
void Test01::setupTest() {}
   
bool Test01::executeTest() {
   HTTPClient http;
   char urlBuffer[125];
   bool outcome = true;
   LOG("Description: %s",gTest01Description);
   LOG("Connecting to internet");
   if(_modem->connect(APN,APN_USERNAME,APN_PASSWORD)==0) {
      LOG("Connected to internet");
   } else {
      LOG("Failed to connect to internet");
      _modem->disconnect();
      return false;
   }
   
   // retrieve files whose sizes are successive powers of 2 from 128 bytes upto 1MB
   int bytesToRead = 128;
   HTTPFileValidator fileValidator(0);
   Timer t;
   for(int i=0; i<14; i++) {
      sprintf(urlBuffer,"http://www.m2mthings.com/test%d.txt",bytesToRead);
      LOGX("Doing HTTP GET for %s ... ",urlBuffer);
      
      // read the file
      t.reset();
      t.start();
      if(http.get(urlBuffer, &fileValidator)!=0) {
         LOG("ERROR reading file from website");
         outcome = false;
         t.stop();
         break;
      }
      t.stop();
      
      // check that all received bytes were valid, and that the total number of bytes read is as expected
      if(fileValidator.isValid()&&fileValidator.bytesRead()==bytesToRead) {
         LOG("OK. (%f seconds, %f kb/s)",t.read(),((float)bytesToRead/1000.0)/t.read());
      } else {
         LOG("ERROR in file validation after %f seconds and %d bytes read.",t.read(),fileValidator.bytesRead());
         outcome = false;
         break;
      }
      
      bytesToRead *= 2;
   }
   
   _modem->disconnect();
   return outcome;
}
      
void Test01::endTest() { }