1 /* 2 * Copyright 2014 Haiku, inc. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "DataTest.h" 8 9 #include <AutoDeleter.h> 10 #include <DataRequest.h> 11 #include <UrlProtocolRoster.h> 12 13 #include <cppunit/TestCaller.h> 14 15 16 using namespace BPrivate::Network; 17 18 19 // http://www-archive.mozilla.org/quality/networking/testing/datatests.html 20 21 22 DataTest::DataTest() 23 { 24 } 25 26 27 DataTest::~DataTest() 28 { 29 } 30 31 32 void 33 DataTest::SimpleTest() 34 { 35 _RunTest("data:,A%20brief%20note", "A brief note", strlen("A brief note")); 36 _RunTest("data:text/plain;charset=iso-8859-7,%b8%f7%fe", "\xb8\xf7\xfe", 3); 37 38 } 39 40 41 void 42 DataTest::EmptyTest() 43 { 44 _RunTest("data:", "", 0); 45 _RunTest("data:,", "", 0); 46 _RunTest("data:text/plain,", "", 0); 47 _RunTest("data:;base64,", "", 0); 48 _RunTest("data:;base64,", "", 0); 49 } 50 51 52 void 53 DataTest::InvalidTest() 54 { 55 _RunTest("data:test", "test", 4); 56 _RunTest("data:,;test", ";test", 5); 57 _RunTest("data:;,test", "test", 4); 58 _RunTest("data:,a,b", "a,b", 3); 59 _RunTest("data:;base64,hello", "", 0); 60 } 61 62 63 void 64 DataTest::Base64Test() 65 { 66 _RunTest("data:text/html;base64,VGhpcyBpcyBhIHRlc3QK", 67 "This is a test\n", 15); 68 69 static const char* kLarryUrl="data:image/gif;base64,R0lGODdhMAAwAPAAAAAAAP" 70 "///ywAAAAAMAAwAAAC8IyPqcvt3wCcDkiLc7C0qwyGHhSWpjQu5yqmCYsapyuvUUlvONm" 71 "OZtfzgFzByTB10QgxOR0TqBQejhRNzOfkVJ+5YiUqrXF5Y5lKh/DeuNcP5yLWGsEbtLiO" 72 "Spa/TPg7JpJHxyendzWTBfX0cxOnKPjgBzi4diinWGdkF8kjdfnycQZXZeYGejmJlZeGl" 73 "9i2icVqaNVailT6F5iJ90m6mvuTS4OK05M0vDk0Q4XUtwvKOzrcd3iq9uisF81M1OIcR7" 74 "lEewwcLp7tuNNkM3uNna3F2JQFo97Vriy/Xl4/f1cf5VWzXyym7PHhhx4dbgYKAAA7"; 75 76 static const unsigned char kLarryData[] = { 77 0x47, 0x49, 0x46, 0x38, 0x37, 0x61, 0x30, 0x00, 0x30, 0x00, 0xf0, 0x00, 78 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0x2c, 0x00, 0x00, 0x00, 0x00, 79 0x30, 0x00, 0x30, 0x00, 0x00, 0x02, 0xf0, 0x8c, 0x8f, 0xa9, 0xcb, 0xed, 80 0xdf, 0x00, 0x9c, 0x0e, 0x48, 0x8b, 0x73, 0xb0, 0xb4, 0xab, 0x0c, 0x86, 81 0x1e, 0x14, 0x96, 0xa6, 0x34, 0x2e, 0xe7, 0x2a, 0xa6, 0x09, 0x8b, 0x1a, 82 0xa7, 0x2b, 0xaf, 0x51, 0x49, 0x6f, 0x38, 0xd9, 0x8e, 0x66, 0xd7, 0xf3, 83 0x80, 0x5c, 0xc1, 0xc9, 0x30, 0x75, 0xd1, 0x08, 0x31, 0x39, 0x1d, 0x13, 84 0xa8, 0x14, 0x1e, 0x8e, 0x14, 0x4d, 0xcc, 0xe7, 0xe4, 0x54, 0x9f, 0xb9, 85 0x62, 0x25, 0x2a, 0xad, 0x71, 0x79, 0x63, 0x99, 0x4a, 0x87, 0xf0, 0xde, 86 0xb8, 0xd7, 0x0f, 0xe7, 0x22, 0xd6, 0x1a, 0xc1, 0x1b, 0xb4, 0xb8, 0x8e, 87 0x4a, 0x96, 0xbf, 0x4c, 0xf8, 0x3b, 0x26, 0x92, 0x47, 0xc7, 0x27, 0xa7, 88 0x77, 0x35, 0x93, 0x05, 0xf5, 0xf4, 0x73, 0x13, 0xa7, 0x28, 0xf8, 0xe0, 89 0x07, 0x38, 0xb8, 0x76, 0x28, 0xa7, 0x58, 0x67, 0x64, 0x17, 0xc9, 0x23, 90 0x75, 0xf9, 0xf2, 0x71, 0x06, 0x57, 0x65, 0xe6, 0x06, 0x7a, 0x39, 0x89, 91 0x95, 0x97, 0x86, 0x97, 0xd8, 0xb6, 0x89, 0xc5, 0x6a, 0x68, 0xd5, 0x5a, 92 0x8a, 0x54, 0xfa, 0x17, 0x98, 0x89, 0xf7, 0x49, 0xba, 0x9a, 0xfb, 0x93, 93 0x4b, 0x83, 0x8a, 0xd3, 0x93, 0x34, 0xbc, 0x39, 0x34, 0x43, 0x85, 0xd4, 94 0xb7, 0x0b, 0xca, 0x3b, 0x3a, 0xdc, 0x77, 0x78, 0xaa, 0xf6, 0xe8, 0xac, 95 0x17, 0xcd, 0x4c, 0xd4, 0xe2, 0x1c, 0x47, 0xb9, 0x44, 0x7b, 0x0c, 0x1c, 96 0x2e, 0x9e, 0xed, 0xb8, 0xd3, 0x64, 0x33, 0x7b, 0x8d, 0x9d, 0xad, 0xc5, 97 0xd8, 0x94, 0x05, 0xa3, 0xde, 0xd5, 0xae, 0x2c, 0xbf, 0x5e, 0x5e, 0x3f, 98 0x7f, 0x57, 0x1f, 0xe5, 0x55, 0xb3, 0x5f, 0x2c, 0xa6, 0xec, 0xf1, 0xe1, 99 0x87, 0x1e, 0x1d, 0x6e, 0x06, 0x0a, 0x00, 0x00, 0x3b 100 }; 101 static const size_t kLarrySize = 273; 102 103 _RunTest(kLarryUrl, (const char*)kLarryData, kLarrySize); 104 } 105 106 107 void 108 DataTest::CharsetTest() 109 { 110 _RunTest("data:text/plain,test", "test", 4); 111 _RunTest("data:text/plain;charset=US-ASCII,test", "test", 4); 112 _RunTest("data:;charset=UTF-8,Hello", "Hello", 5); 113 _RunTest("data:;charset=,test", "test", 4); 114 } 115 116 117 void 118 DataTest::UrlDecodeTest() 119 { 120 _RunTest("data:%20", " ", 1); 121 _RunTest("data:%2", "%2", 2); 122 _RunTest("data:%", "%", 1); 123 _RunTest("data:%-1", "%-1", 3); 124 _RunTest("data:%202", " 2", 2); 125 } 126 127 128 void 129 DataTest::DataReceived(BUrlRequest*, const char* data, off_t, ssize_t size) 130 { 131 fReceivedData.insert(fReceivedData.end(), data, data + size); 132 } 133 134 135 /* static */ void 136 DataTest::AddTests(BTestSuite& parent) 137 { 138 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("DataTest"); 139 140 suite.addTest(new CppUnit::TestCaller<DataTest>( 141 "DataTest::SimpleTest", &DataTest::SimpleTest)); 142 suite.addTest(new CppUnit::TestCaller<DataTest>( 143 "DataTest::EmptyTest", &DataTest::EmptyTest)); 144 suite.addTest(new CppUnit::TestCaller<DataTest>( 145 "DataTest::InvalidTest", &DataTest::InvalidTest)); 146 suite.addTest(new CppUnit::TestCaller<DataTest>( 147 "DataTest::CharsetTest", &DataTest::CharsetTest)); 148 suite.addTest(new CppUnit::TestCaller<DataTest>( 149 "DataTest::Base64Test", &DataTest::Base64Test)); 150 suite.addTest(new CppUnit::TestCaller<DataTest>( 151 "DataTest::UrlDecodeTest", &DataTest::UrlDecodeTest)); 152 153 parent.addTest("DataTest", &suite); 154 } 155 156 157 void 158 DataTest::_RunTest(BString url, const char* expected, size_t expectedLength) 159 { 160 NextSubTest(); 161 162 BUrl testUrl(url); 163 ObjectDeleter<BUrlRequest> requestDeleter( 164 BUrlProtocolRoster::MakeRequest(testUrl, this)); 165 BDataRequest* request = dynamic_cast<BDataRequest*>(requestDeleter.Get()); 166 CPPUNIT_ASSERT(request != NULL); 167 fReceivedData.clear(); 168 request->Run(); 169 170 while(request->IsRunning()) 171 snooze(1000); 172 173 CPPUNIT_ASSERT_EQUAL(expectedLength, fReceivedData.size()); 174 CPPUNIT_ASSERT(memcmp(&fReceivedData.front(), expected, expectedLength) == 0); 175 } 176 177