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