text: /*
   * Copyright 2014 Haiku, inc.
   * Distributed under the terms of the MIT License.
   */
  
  
  #include "DataTest.h"
  
  #include <AutoDeleter.h>
  #include <DataRequest.h>
  #include <UrlProtocolRoster.h>
  
  #include <cppunit/TestCaller.h>
  
  
  using namespace BPrivate::Network;
  
  
  // http://www-archive.mozilla.org/quality/networking/testing/datatests.html
  
  
  DataTest::DataTest()
  {
  }
  
  
  DataTest::~DataTest()
  {
  }
  
  
  void
  DataTest::SimpleTest()
  {
  	_RunTest("data:,A%20brief%20note", "A brief note", strlen("A brief note"));
  	_RunTest("data:text/plain;charset=iso-8859-7,%b8%f7%fe", "\xb8\xf7\xfe", 3);
  
  }
  
  
  void
  DataTest::EmptyTest()
  {
  	_RunTest("data:", "", 0);
  	_RunTest("data:,", "", 0);
  	_RunTest("data:text/plain,", "", 0);
  	_RunTest("data:;base64,", "", 0);
  	_RunTest("data:;base64,", "", 0);
  }
  
  
  void
  DataTest::InvalidTest()
  {
  	_RunTest("data:test", "test", 4);
  	_RunTest("data:,;test", ";test", 5);
  	_RunTest("data:;,test", "test", 4);
  	_RunTest("data:,a,b", "a,b", 3);
  	_RunTest("data:;base64,hello", "", 0);
  }
  
  
  void
  DataTest::Base64Test()
  {
  	_RunTest("data:text/html;base64,VGhpcyBpcyBhIHRlc3QK",
  		"This is a test\n", 15);
  
  	static const char* kLarryUrl="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP"
  		"///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONm"
  		"OZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiO"
  		"Spa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl"
  		"9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7"
  		"lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7";
  
  	static const unsigned char kLarryData[] = {
  		0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x30, 0x00, 0x30, 0x00, 0xf0, 0x00,
  		0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00,
  		0x30, 0x00, 0x30, 0x00, 0x00, 0x02, 0xf0, 0x8c, 0x8f, 0xa9, 0xcb, 0xed,
  		0xdf, 0x00, 0x9c, 0x0e, 0x48, 0x8b, 0x73, 0xb0, 0xb4, 0xab, 0x0c, 0x86,
  		0x1e, 0x14, 0x96, 0xa6, 0x34, 0x2e, 0xe7, 0x2a, 0xa6, 0x09, 0x8b, 0x1a,
  		0xa7, 0x2b, 0xaf, 0x51, 0x49, 0x6f, 0x38, 0xd9, 0x8e, 0x66, 0xd7, 0xf3,
  		0x80, 0x5c, 0xc1, 0xc9, 0x30, 0x75, 0xd1, 0x08, 0x31, 0x39, 0x1d, 0x13,
  		0xa8, 0x14, 0x1e, 0x8e, 0x14, 0x4d, 0xcc, 0xe7, 0xe4, 0x54, 0x9f, 0xb9,
  		0x62, 0x25, 0x2a, 0xad, 0x71, 0x79, 0x63, 0x99, 0x4a, 0x87, 0xf0, 0xde,
  		0xb8, 0xd7, 0x0f, 0xe7, 0x22, 0xd6, 0x1a, 0xc1, 0x1b, 0xb4, 0xb8, 0x8e,
  		0x4a, 0x96, 0xbf, 0x4c, 0xf8, 0x3b, 0x26, 0x92, 0x47, 0xc7, 0x27, 0xa7,
  		0x77, 0x35, 0x93, 0x05, 0xf5, 0xf4, 0x73, 0x13, 0xa7, 0x28, 0xf8, 0xe0,
  		0x07, 0x38, 0xb8, 0x76, 0x28, 0xa7, 0x58, 0x67, 0x64, 0x17, 0xc9, 0x23,
  		0x75, 0xf9, 0xf2, 0x71, 0x06, 0x57, 0x65, 0xe6, 0x06, 0x7a, 0x39, 0x89,
  		0x95, 0x97, 0x86, 0x97, 0xd8, 0xb6, 0x89, 0xc5, 0x6a, 0x68, 0xd5, 0x5a,
  		0x8a, 0x54, 0xfa, 0x17, 0x98, 0x89, 0xf7, 0x49, 0xba, 0x9a, 0xfb, 0x93,
  		0x4b, 0x83, 0x8a, 0xd3, 0x93, 0x34, 0xbc, 0x39, 0x34, 0x43, 0x85, 0xd4,
  		0xb7, 0x0b, 0xca, 0x3b, 0x3a, 0xdc, 0x77, 0x78, 0xaa, 0xf6, 0xe8, 0xac,
  		0x17, 0xcd, 0x4c, 0xd4, 0xe2, 0x1c, 0x47, 0xb9, 0x44, 0x7b, 0x0c, 0x1c,
  		0x2e, 0x9e, 0xed, 0xb8, 0xd3, 0x64, 0x33, 0x7b, 0x8d, 0x9d, 0xad, 0xc5,
  		0xd8, 0x94, 0x05, 0xa3, 0xde, 0xd5, 0xae, 0x2c, 0xbf, 0x5e, 0x5e, 0x3f,
  		0x7f, 0x57, 0x1f, 0xe5, 0x55, 0xb3, 0x5f, 0x2c, 0xa6, 0xec, 0xf1, 0xe1,
  		0x87, 0x1e, 0x1d, 0x6e, 0x06, 0x0a, 0x00, 0x00, 0x3b
  	};
  	static const size_t kLarrySize = 273;
  
  	_RunTest(kLarryUrl, (const char*)kLarryData, kLarrySize);
  }
  
  
  void
  DataTest::CharsetTest()
  {
  	_RunTest("data:text/plain,test", "test", 4);
  	_RunTest("data:text/plain;charset=US-ASCII,test", "test", 4);
  	_RunTest("data:;charset=UTF-8,Hello", "Hello", 5);
  	_RunTest("data:;charset=,test", "test", 4);
  }
  
  
  void
  DataTest::UrlDecodeTest()
  {
  	_RunTest("data:%20", " ", 1);
  	_RunTest("data:%2", "%2", 2);
  	_RunTest("data:%", "%", 1);
  	_RunTest("data:%-1", "%-1", 3);
  	_RunTest("data:%202", " 2", 2);
  }
  
  
  void
  DataTest::DataReceived(BUrlRequest*, const char* data, off_t, ssize_t size)
  {
  	fReceivedData.insert(fReceivedData.end(), data, data + size);
  }
  
  
  /* static */ void
  DataTest::AddTests(BTestSuite& parent)
  {
  	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("DataTest");
  
  	suite.addTest(new CppUnit::TestCaller<DataTest>(
  		"DataTest::SimpleTest", &DataTest::SimpleTest));
  	suite.addTest(new CppUnit::TestCaller<DataTest>(
  		"DataTest::EmptyTest", &DataTest::EmptyTest));
  	suite.addTest(new CppUnit::TestCaller<DataTest>(
  		"DataTest::InvalidTest", &DataTest::InvalidTest));
  	suite.addTest(new CppUnit::TestCaller<DataTest>(
  		"DataTest::CharsetTest", &DataTest::CharsetTest));
  	suite.addTest(new CppUnit::TestCaller<DataTest>(
  		"DataTest::Base64Test", &DataTest::Base64Test));
  	suite.addTest(new CppUnit::TestCaller<DataTest>(
  		"DataTest::UrlDecodeTest", &DataTest::UrlDecodeTest));
  
  	parent.addTest("DataTest", &suite);
  }
  
  
  void
  DataTest::_RunTest(BString url, const char* expected, size_t expectedLength)
  {
  	NextSubTest();
  
  	BUrl testUrl(url);
  	ObjectDeleter<BUrlRequest> requestDeleter(
  		BUrlProtocolRoster::MakeRequest(testUrl, this));
  	BDataRequest* request = dynamic_cast<BDataRequest*>(requestDeleter.Get());
  	CPPUNIT_ASSERT(request != NULL);
  	fReceivedData.clear();
  	request->Run();
  
  	while(request->IsRunning())
  		snooze(1000);
  
  	CPPUNIT_ASSERT_EQUAL(expectedLength, fReceivedData.size());
  	CPPUNIT_ASSERT(memcmp(&fReceivedData.front(), expected, expectedLength) == 0);
  }
  
  