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