Ashley Mills / Mbed 2 deprecated VodafoneTestSuite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

Revision:
66:6b00a764e549
Child:
69:4fc3b0ad12c7
diff -r ef3357b6ce97 -r 6b00a764e549 Tests/Test01.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Tests/Test01.cpp	Thu Nov 01 11:19:47 2012 +0000
@@ -0,0 +1,108 @@
+#include "Test01.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("internet","web","web")==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() { }
\ No newline at end of file