Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

Committer:
ashleymills
Date:
Wed Jan 29 16:34:38 2014 +0000
Revision:
74:e52ac9624f7f
Parent:
69:4fc3b0ad12c7
Updated dependencies to latest versions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ashleymills 66:6b00a764e549 1 #include "Test01.h"
ashleymills 69:4fc3b0ad12c7 2 #include "TestHelper.h"
ashleymills 66:6b00a764e549 3
ashleymills 66:6b00a764e549 4 class HTTPFileValidator : public IHTTPDataIn {
ashleymills 66:6b00a764e549 5 public:
ashleymills 66:6b00a764e549 6 HTTPFileValidator(int sockfd) {
ashleymills 66:6b00a764e549 7 _fileIsValid = false;
ashleymills 66:6b00a764e549 8 _bytesRead = 0;
ashleymills 66:6b00a764e549 9 }
ashleymills 66:6b00a764e549 10
ashleymills 66:6b00a764e549 11 bool isValid() {
ashleymills 66:6b00a764e549 12 return _fileIsValid;
ashleymills 66:6b00a764e549 13 }
ashleymills 66:6b00a764e549 14
ashleymills 66:6b00a764e549 15 int bytesRead() {
ashleymills 66:6b00a764e549 16 return _bytesRead;
ashleymills 66:6b00a764e549 17 }
ashleymills 66:6b00a764e549 18
ashleymills 66:6b00a764e549 19 protected:
ashleymills 66:6b00a764e549 20 //IHTTPDataIn
ashleymills 66:6b00a764e549 21 virtual int write(const char* buf, size_t len) {
ashleymills 66:6b00a764e549 22 int i = 0;
ashleymills 66:6b00a764e549 23 // do nothing if file already found invalid
ashleymills 66:6b00a764e549 24 if(!_fileIsValid)
ashleymills 66:6b00a764e549 25 return len;
ashleymills 66:6b00a764e549 26
ashleymills 66:6b00a764e549 27 // check that received characters are in correct sequence
ashleymills 66:6b00a764e549 28 for(i=0; i<len; i++) {
ashleymills 66:6b00a764e549 29 if(buf[i]!=_expectedChar) {
ashleymills 66:6b00a764e549 30 _fileIsValid = false;
ashleymills 66:6b00a764e549 31 break;
ashleymills 66:6b00a764e549 32 }
ashleymills 66:6b00a764e549 33 _expectedChar++;
ashleymills 66:6b00a764e549 34 if(_expectedChar==256) {
ashleymills 66:6b00a764e549 35 _expectedChar = 0;
ashleymills 66:6b00a764e549 36 }
ashleymills 66:6b00a764e549 37 }
ashleymills 66:6b00a764e549 38 _bytesRead += i;
ashleymills 66:6b00a764e549 39
ashleymills 66:6b00a764e549 40 return len;
ashleymills 66:6b00a764e549 41 }
ashleymills 66:6b00a764e549 42 virtual void writeReset() {
ashleymills 66:6b00a764e549 43 _fileIsValid = true;
ashleymills 66:6b00a764e549 44 _expectedChar = 0;
ashleymills 66:6b00a764e549 45 _bytesRead = 0;
ashleymills 66:6b00a764e549 46 }
ashleymills 66:6b00a764e549 47 virtual void setDataType(const char* type) {}
ashleymills 66:6b00a764e549 48 virtual void setIsChunked(bool chunked) {}
ashleymills 66:6b00a764e549 49 virtual void setDataLen(size_t len) {}
ashleymills 66:6b00a764e549 50 bool _fileIsValid;
ashleymills 66:6b00a764e549 51 char _expectedChar;
ashleymills 66:6b00a764e549 52 int _bytesRead;
ashleymills 66:6b00a764e549 53 };
ashleymills 66:6b00a764e549 54
ashleymills 66:6b00a764e549 55
ashleymills 66:6b00a764e549 56 Test01::Test01(VodafoneUSBModem *m) : VodafoneTestCase(m) {}
ashleymills 66:6b00a764e549 57
ashleymills 66:6b00a764e549 58 void Test01::setupTest() {}
ashleymills 66:6b00a764e549 59
ashleymills 66:6b00a764e549 60 bool Test01::executeTest() {
ashleymills 66:6b00a764e549 61 HTTPClient http;
ashleymills 66:6b00a764e549 62 char urlBuffer[125];
ashleymills 66:6b00a764e549 63 bool outcome = true;
ashleymills 66:6b00a764e549 64 LOG("Description: %s",gTest01Description);
ashleymills 66:6b00a764e549 65 LOG("Connecting to internet");
ashleymills 69:4fc3b0ad12c7 66 if(_modem->connect(APN,APN_USERNAME,APN_PASSWORD)==0) {
ashleymills 66:6b00a764e549 67 LOG("Connected to internet");
ashleymills 66:6b00a764e549 68 } else {
ashleymills 66:6b00a764e549 69 LOG("Failed to connect to internet");
ashleymills 66:6b00a764e549 70 _modem->disconnect();
ashleymills 66:6b00a764e549 71 return false;
ashleymills 66:6b00a764e549 72 }
ashleymills 66:6b00a764e549 73
ashleymills 66:6b00a764e549 74 // retrieve files whose sizes are successive powers of 2 from 128 bytes upto 1MB
ashleymills 66:6b00a764e549 75 int bytesToRead = 128;
ashleymills 66:6b00a764e549 76 HTTPFileValidator fileValidator(0);
ashleymills 66:6b00a764e549 77 Timer t;
ashleymills 66:6b00a764e549 78 for(int i=0; i<14; i++) {
ashleymills 66:6b00a764e549 79 sprintf(urlBuffer,"http://www.m2mthings.com/test%d.txt",bytesToRead);
ashleymills 66:6b00a764e549 80 LOGX("Doing HTTP GET for %s ... ",urlBuffer);
ashleymills 66:6b00a764e549 81
ashleymills 66:6b00a764e549 82 // read the file
ashleymills 66:6b00a764e549 83 t.reset();
ashleymills 66:6b00a764e549 84 t.start();
ashleymills 66:6b00a764e549 85 if(http.get(urlBuffer, &fileValidator)!=0) {
ashleymills 66:6b00a764e549 86 LOG("ERROR reading file from website");
ashleymills 66:6b00a764e549 87 outcome = false;
ashleymills 66:6b00a764e549 88 t.stop();
ashleymills 66:6b00a764e549 89 break;
ashleymills 66:6b00a764e549 90 }
ashleymills 66:6b00a764e549 91 t.stop();
ashleymills 66:6b00a764e549 92
ashleymills 66:6b00a764e549 93 // check that all received bytes were valid, and that the total number of bytes read is as expected
ashleymills 66:6b00a764e549 94 if(fileValidator.isValid()&&fileValidator.bytesRead()==bytesToRead) {
ashleymills 66:6b00a764e549 95 LOG("OK. (%f seconds, %f kb/s)",t.read(),((float)bytesToRead/1000.0)/t.read());
ashleymills 66:6b00a764e549 96 } else {
ashleymills 66:6b00a764e549 97 LOG("ERROR in file validation after %f seconds and %d bytes read.",t.read(),fileValidator.bytesRead());
ashleymills 66:6b00a764e549 98 outcome = false;
ashleymills 66:6b00a764e549 99 break;
ashleymills 66:6b00a764e549 100 }
ashleymills 66:6b00a764e549 101
ashleymills 66:6b00a764e549 102 bytesToRead *= 2;
ashleymills 66:6b00a764e549 103 }
ashleymills 66:6b00a764e549 104
ashleymills 66:6b00a764e549 105 _modem->disconnect();
ashleymills 66:6b00a764e549 106 return outcome;
ashleymills 66:6b00a764e549 107 }
ashleymills 66:6b00a764e549 108
ashleymills 66:6b00a764e549 109 void Test01::endTest() { }