Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

Committer:
ashleymills
Date:
Fri Oct 12 10:03:37 2012 +0000
Revision:
53:54b2d3a0c7bf
Parent:
44:6d0ac4747f5b
Child:
60:7efce4a3c26f
File download test, now tests files between 128 bytes and 1MB in successive powers of 2.

Who changed what in which revision?

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