Ashley Mills / Mbed 2 deprecated VodafoneTestSuite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

Committer:
ashleymills
Date:
Thu Nov 01 11:19:47 2012 +0000
Revision:
66:6b00a764e549
Child:
69:4fc3b0ad12c7
Renamed tests in sequential order to make it easier to manage and cleaner to follow.

Who changed what in which revision?

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