Vodafone Test Suite

Dependencies:   mbed-rtos mbed HTTPClient VodafoneUSBModem

Revision:
53:54b2d3a0c7bf
Parent:
44:6d0ac4747f5b
Child:
60:7efce4a3c26f
--- a/Tests/Test10.cpp	Thu Oct 11 10:00:19 2012 +0000
+++ b/Tests/Test10.cpp	Fri Oct 12 10:03:37 2012 +0000
@@ -1,46 +1,111 @@
-#include "Test10.h"
-
-Test10::Test10(VodafoneUSBModem *m) : VodafoneTestCase(m) {
-   _description = gTest10Description;
-   _testCaseNumber = 10;
-}
-      
-void Test10::setupTest() {}
-   
-bool Test10::executeTest() {
-   HTTPClient http;
-   char msgBuffer[125];
-   bool outcome = true;
-   LOG("Description: %s",gTest10Description);
-   LOG("Connecting to internet");
-   if(_modem->connect("internet","web","web")==0) {
-      LOG("Connected to internet");
-   } else {
-      LOG("Failed to connect to internet");
-      outcome = false;
-   }
-       
-   LOG("Doing HTTP GET for http://www.m2mthings.com/test100.txt");
-   if(http.get("http://www.m2mthings.com/test100.txt", msgBuffer, 125)==0) {
-      LOG("Got buffer");
-      char c = 0;
-      for(int i=0; i<100; i++) {
-         if(msgBuffer[i]!=c) {
-            LOG("Strings do not match at char %d (%x,%x)",i,c,msgBuffer[i]);
-            outcome = false;
-            break;
-         }
-         c++;
-         if(c==256)
-            c = 0;
-      }
-      LOG("All bytes match! PASS.");
-   } else {
-      LOG("HTTP get failure");
-      outcome = false;
-   }
-   _modem->disconnect();
-   return outcome;
-}
-      
+#include "Test10.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;
+};
+
+
+Test10::Test10(VodafoneUSBModem *m) : VodafoneTestCase(m) {
+   _description = gTest10Description;
+   _testCaseNumber = 10;
+}
+      
+void Test10::setupTest() {}
+   
+bool Test10::executeTest() {
+   HTTPClient http;
+   char urlBuffer[125];
+   bool outcome = true;
+   LOG("Description: %s",gTest10Description);
+   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 Test10::endTest() { }
\ No newline at end of file