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