Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(207)

Side by Side Diff: ppapi/tests/test_url_request.cc

Issue 9174020: PPAPI URLRequestInfo test: port NaCl version to ppapi_tests. Fix Var and Resource leaks. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ppapi/tests/test_url_request.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4 //
5 // Tests PPB_URLRequestInfo. 5 // Tests PPB_URLRequestInfo interface.
6
7 #include "ppapi/tests/test_url_request.h"
6 8
7 #include <string.h> 9 #include <string.h>
8 10 #include <string>
9 #include "native_client/src/shared/platform/nacl_check.h" 11
10 #include "native_client/tests/ppapi_test_lib/get_browser_interface.h" 12 #include "ppapi/c/dev/ppb_testing_dev.h"
11 #include "native_client/tests/ppapi_test_lib/test_interface.h" 13 #include "ppapi/cpp/completion_callback.h"
12 #include "native_client/tests/ppapi_test_lib/testable_callback.h" 14 #include "ppapi/cpp/instance.h"
13 15 #include "ppapi/cpp/var.h"
14 #include "ppapi/c/pp_errors.h" 16 #include "ppapi/tests/test_utils.h"
15 #include "ppapi/c/ppb_core.h" 17 #include "ppapi/tests/testing_instance.h"
16 #include "ppapi/c/ppb_file_io.h" 18
17 #include "ppapi/c/ppb_file_ref.h" 19 REGISTER_TEST_CASE(URLRequest);
18 #include "ppapi/c/ppb_file_system.h"
19 #include "ppapi/c/ppb_url_loader.h"
20 #include "ppapi/c/ppb_url_request_info.h"
21 #include "ppapi/c/ppb_url_response_info.h"
22 #include "ppapi/c/ppb_var.h"
23 20
24 namespace { 21 namespace {
22 // TODO(polina): move these to test_case.h/cc since other NaCl tests use them?
23
24 const PP_Resource kInvalidResource = 0;
25 const PP_Instance kInvalidInstance = 0;
26
27 // These should not exist.
28 // The bottom 2 bits are used to differentiate between different id types.
29 // 00 - module, 01 - instance, 10 - resource, 11 - var.
30 const PP_Instance kNotAnInstance = 0xFFFFF0;
31 const PP_Resource kNotAResource = 0xAAAAA0;
32 }
33
34 TestURLRequest::TestURLRequest(TestingInstance* instance)
35 : TestCase(instance),
36 ppb_url_request_interface_(NULL),
37 ppb_url_loader_interface_(NULL),
38 ppb_url_response_interface_(NULL),
39 ppb_core_interface_(NULL),
40 ppb_var_interface_(NULL),
41 url_loader_(kInvalidResource) {
42 }
43
44 bool TestURLRequest::Init() {
45 ppb_url_request_interface_ = static_cast<const PPB_URLRequestInfo*>(
46 pp::Module::Get()->GetBrowserInterface(PPB_URLREQUESTINFO_INTERFACE));
47 ppb_url_loader_interface_ = static_cast<const PPB_URLLoader*>(
48 pp::Module::Get()->GetBrowserInterface(PPB_URLLOADER_INTERFACE));
49 ppb_url_response_interface_ = static_cast<const PPB_URLResponseInfo*>(
50 pp::Module::Get()->GetBrowserInterface(PPB_URLRESPONSEINFO_INTERFACE));
51 ppb_core_interface_ = static_cast<const PPB_Core*>(
52 pp::Module::Get()->GetBrowserInterface(PPB_CORE_INTERFACE));
53 ppb_var_interface_ = static_cast<const PPB_Var*>(
54 pp::Module::Get()->GetBrowserInterface(PPB_VAR_INTERFACE));
55 if (!ppb_url_request_interface_)
56 instance_->AppendError("PPB_URLRequestInfo interface not available");
57 if (!ppb_url_response_interface_)
58 instance_->AppendError("PPB_URLResponseInfo interface not available");
59 if (!ppb_core_interface_)
60 instance_->AppendError("PPB_Core interface not available");
61 if (!ppb_var_interface_)
62 instance_->AppendError("PPB_Var interface not available");
63 if (!ppb_url_loader_interface_) {
64 instance_->AppendError("PPB_URLLoader interface not available");
65 } else {
66 url_loader_ = ppb_url_loader_interface_->Create(instance_->pp_instance());
67 if (url_loader_ == kInvalidResource)
68 instance_->AppendError("Failed to create URLLoader");
69 }
70 return EnsureRunningOverHTTP();
71 }
72
73 void TestURLRequest::RunTests(const std::string& filter) {
74 RUN_TEST(CreateAndIsURLRequestInfo, filter);
75 RUN_TEST(SetProperty, filter);
76 RUN_TEST(Stress, filter);
77 RUN_TEST(AppendDataToBody, filter);
78 }
79
80 PP_Var TestURLRequest::PP_MakeString(const char* s) {
81 return ppb_var_interface_->VarFromUtf8(s, strlen(s));
82 }
25 83
26 // Tests 84 // Tests
27 // PP_Resource Create(PP_Instance instance). 85 // PP_Resource Create(PP_Instance instance)
28 void TestCreate() { 86 // PP_Bool IsURLRequestInfo(PP_Resource resource)
29 PP_Resource url_request = kInvalidResource; 87 std::string TestURLRequest::TestCreateAndIsURLRequestInfo() {
30 88 // Create: Invalid / non-existent instance -> invalid resource.
31 // Invalid instance -> invalid resource. 89 ASSERT_EQ(ppb_url_request_interface_->Create(kInvalidInstance),
32 url_request = PPBURLRequestInfo()->Create(kInvalidInstance); 90 kInvalidResource);
33 EXPECT(url_request == kInvalidResource); 91 ASSERT_EQ(ppb_url_request_interface_->Create(kNotAnInstance),
34 92 kInvalidResource);
35 // Valid instance -> valid resource. 93
36 url_request = PPBURLRequestInfo()->Create(pp_instance()); 94 // Create: Valid instance -> valid resource.
37 EXPECT(url_request != kInvalidResource); 95 PP_Resource url_request = ppb_url_request_interface_->Create(
38 96 instance_->pp_instance());
39 PPBCore()->ReleaseResource(url_request); 97 ASSERT_NE(url_request, kInvalidResource);
40 TEST_PASSED; 98
41 } 99 // IsURLRequestInfo:
42
43 // Tests
44 // PP_Bool IsURLRequestInfo(PP_Resource resource).
45 void TestIsURLRequestInfo() {
46 const PPB_URLRequestInfo* ppb = PPBURLRequestInfo();
47
48 // Invalid / non-existent / non-URLRequestInfo resource -> false. 100 // Invalid / non-existent / non-URLRequestInfo resource -> false.
49 EXPECT(PP_FALSE == ppb->IsURLRequestInfo(kInvalidResource)); 101 ASSERT_NE(PP_TRUE,
50 EXPECT(PP_FALSE == ppb->IsURLRequestInfo(kNotAResource)); 102 ppb_url_request_interface_->IsURLRequestInfo(kInvalidResource));
51 PP_Resource url_loader = PPBURLLoader()->Create(pp_instance()); 103 ASSERT_NE(PP_TRUE,
52 EXPECT(url_loader != kInvalidResource); 104 ppb_url_request_interface_->IsURLRequestInfo(kNotAResource));
53 EXPECT(PP_FALSE == ppb->IsURLRequestInfo(url_loader)); 105 ASSERT_NE(PP_TRUE, ppb_url_request_interface_->IsURLRequestInfo(url_loader_));
54 PPBCore()->ReleaseResource(url_loader); 106
55 107 // IsURLRequestInfo: Current URLRequestInfo resource -> true.
56 // Current URLRequestInfo resource -> true. 108 std::string error;
57 PP_Resource url_request = ppb->Create(pp_instance()); 109 if (PP_FALSE == ppb_url_request_interface_->IsURLRequestInfo(url_request))
58 EXPECT(PP_TRUE == ppb->IsURLRequestInfo(url_request)); 110 error = "IsURLRequestInfo() failed with a current URLRequestInfo resource";
59 111
60 // Released URLRequestInfo resource -> false. 112 // IsURLRequestInfo: Released URLRequestInfo resource -> false.
61 PPBCore()->ReleaseResource(url_request); 113 ppb_core_interface_->ReleaseResource(url_request);
62 EXPECT(PP_FALSE == ppb->IsURLRequestInfo(url_request)); 114 ASSERT_NE(PP_TRUE, ppb_url_request_interface_->IsURLRequestInfo(url_request))
63 115
64 TEST_PASSED; 116 return error; // == PASS() if empty.
65 } 117 }
66 118
67 // Tests 119 // Tests
68 // PP_Bool SetProperty(PP_Resource request, 120 // PP_Bool SetProperty(PP_Resource request,
69 // PP_URLRequestProperty property, 121 // PP_URLRequestProperty property,
70 // struct PP_Var value); 122 // struct PP_Var value);
71 void TestSetProperty() { 123 std::string TestURLRequest::TestSetProperty() {
72 struct PropertyTestData { 124 struct PropertyTestData {
73 PropertyTestData(PP_URLRequestProperty _property, 125 PropertyTestData(PP_URLRequestProperty prop,
74 const std::string& _property_name, 126 const std::string& name,
75 PP_Var _var, PP_Bool _expected_value) : 127 PP_Var value, PP_Bool expected) :
76 property(_property), property_name(_property_name), 128 property(prop), property_name(name),
77 var(_var), expected_value(_expected_value) { 129 var(value), expected_value(expected) {
78 PPBVar()->AddRef(var); 130 // var has ref count of 1 on creation.
79 } 131 }
80 PP_URLRequestProperty property; 132 PP_URLRequestProperty property;
81 std::string property_name; 133 std::string property_name;
82 PP_Var var; 134 PP_Var var; // Instance owner is responsible for releasing this var.
83 PP_Bool expected_value; 135 PP_Bool expected_value;
84 }; 136 };
85 137
86 // All bool properties should accept PP_TRUE and PP_FALSE, while rejecting 138 // All bool properties should accept PP_TRUE and PP_FALSE, while rejecting
87 // all other variable types. 139 // all other variable types.
88 #define ADD_BOOL_PROPERTY(_prop_name) \ 140 #define TEST_BOOL(_name) \
89 PropertyTestData(ID_STR(_prop_name), PP_MakeBool(PP_TRUE), PP_TRUE), \ 141 PropertyTestData(ID_STR(_name), PP_MakeBool(PP_TRUE), PP_TRUE), \
90 PropertyTestData(ID_STR(_prop_name), PP_MakeBool(PP_FALSE), PP_TRUE), \ 142 PropertyTestData(ID_STR(_name), PP_MakeBool(PP_FALSE), PP_TRUE), \
91 PropertyTestData(ID_STR(_prop_name), PP_MakeUndefined(), PP_FALSE), \ 143 PropertyTestData(ID_STR(_name), PP_MakeUndefined(), PP_FALSE), \
92 PropertyTestData(ID_STR(_prop_name), PP_MakeNull(), PP_FALSE), \ 144 PropertyTestData(ID_STR(_name), PP_MakeNull(), PP_FALSE), \
93 PropertyTestData(ID_STR(_prop_name), PP_MakeInt32(0), PP_FALSE), \ 145 PropertyTestData(ID_STR(_name), PP_MakeInt32(0), PP_FALSE), \
94 PropertyTestData(ID_STR(_prop_name), PP_MakeDouble(0.0), PP_FALSE) 146 PropertyTestData(ID_STR(_name), PP_MakeDouble(0.0), PP_FALSE)
95 147
96 // These property types are always invalid for string properties. 148 // These property types are always invalid for string properties.
97 #define ADD_STRING_INVALID_PROPERTIES(_prop_name) \ 149 #define TEST_STRING_INVALID(_name) \
98 PropertyTestData(ID_STR(_prop_name), PP_MakeNull(), PP_FALSE), \ 150 PropertyTestData(ID_STR(_name), PP_MakeNull(), PP_FALSE), \
99 PropertyTestData(ID_STR(_prop_name), PP_MakeBool(PP_FALSE), PP_FALSE), \ 151 PropertyTestData(ID_STR(_name), PP_MakeBool(PP_FALSE), PP_FALSE), \
100 PropertyTestData(ID_STR(_prop_name), PP_MakeInt32(0), PP_FALSE), \ 152 PropertyTestData(ID_STR(_name), PP_MakeInt32(0), PP_FALSE), \
101 PropertyTestData(ID_STR(_prop_name), PP_MakeDouble(0.0), PP_FALSE) 153 PropertyTestData(ID_STR(_name), PP_MakeDouble(0.0), PP_FALSE)
102 154
103 #define ADD_INT_INVALID_PROPERTIES(_prop_name) \ 155 #define TEST_INT_INVALID(_name) \
104 PropertyTestData(ID_STR(_prop_name), PP_MakeUndefined(), PP_FALSE), \ 156 PropertyTestData(ID_STR(_name), PP_MakeUndefined(), PP_FALSE), \
105 PropertyTestData(ID_STR(_prop_name), PP_MakeNull(), PP_FALSE), \ 157 PropertyTestData(ID_STR(_name), PP_MakeNull(), PP_FALSE), \
106 PropertyTestData(ID_STR(_prop_name), PP_MakeBool(PP_FALSE), PP_FALSE), \ 158 PropertyTestData(ID_STR(_name), PP_MakeBool(PP_FALSE), PP_FALSE), \
107 PropertyTestData(ID_STR(_prop_name), PP_MakeString(""), PP_FALSE), \ 159 PropertyTestData(ID_STR(_name), PP_MakeString("notint"), PP_FALSE), \
108 PropertyTestData(ID_STR(_prop_name), PP_MakeDouble(0.0), PP_FALSE) 160 PropertyTestData(ID_STR(_name), PP_MakeDouble(0.0), PP_FALSE)
109 161
110 // SetProperty accepts plenty of invalid values (malformed urls, negative 162 // SetProperty accepts plenty of invalid values (malformed urls, negative
111 // thresholds, etc). Error checking is delayed until request opening (aka url 163 // thresholds, etc). Error checking is delayed until request opening (aka url
112 // loading). 164 // loading).
113 #define ID_STR(arg) arg, #arg 165 #define ID_STR(arg) arg, #arg
114 PropertyTestData test_data[] = { 166 PropertyTestData test_data[] = {
115 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_STREAMTOFILE), 167 TEST_BOOL(PP_URLREQUESTPROPERTY_STREAMTOFILE),
116 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS), 168 TEST_BOOL(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS),
117 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS), 169 TEST_BOOL(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS),
118 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS), 170 TEST_BOOL(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS),
119 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS), 171 TEST_BOOL(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS),
120 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS), 172 TEST_BOOL(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS),
121 ADD_STRING_INVALID_PROPERTIES(PP_URLREQUESTPROPERTY_URL), 173 TEST_STRING_INVALID(PP_URLREQUESTPROPERTY_URL),
122 ADD_STRING_INVALID_PROPERTIES(PP_URLREQUESTPROPERTY_METHOD), 174 TEST_STRING_INVALID(PP_URLREQUESTPROPERTY_METHOD),
123 ADD_STRING_INVALID_PROPERTIES(PP_URLREQUESTPROPERTY_HEADERS), 175 TEST_STRING_INVALID(PP_URLREQUESTPROPERTY_HEADERS),
124 ADD_STRING_INVALID_PROPERTIES( 176 TEST_STRING_INVALID(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL),
125 PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL), 177 TEST_STRING_INVALID(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING),
126 ADD_STRING_INVALID_PROPERTIES( 178 TEST_INT_INVALID(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD),
127 PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING), 179 TEST_INT_INVALID(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD),
128 ADD_INT_INVALID_PROPERTIES(
129 PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD),
130 ADD_INT_INVALID_PROPERTIES(
131 PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD),
132 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL), 180 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL),
133 PP_MakeString("http://www.google.com"), PP_TRUE), 181 PP_MakeString("http://www.google.com"), PP_TRUE),
134 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL), 182 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL),
135 PP_MakeString("foo.jpg"), PP_TRUE), 183 PP_MakeString("foo.jpg"), PP_TRUE),
136 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD), 184 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD),
137 PP_MakeString("GET"), PP_TRUE), 185 PP_MakeString("GET"), PP_TRUE),
138 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD), 186 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD),
139 PP_MakeString("POST"), PP_TRUE), 187 PP_MakeString("POST"), PP_TRUE),
140 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_HEADERS), 188 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_HEADERS),
141 PP_MakeString("Accept: text/plain"), PP_TRUE), 189 PP_MakeString("Accept: text/plain"), PP_TRUE),
142 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_HEADERS), 190 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_HEADERS),
143 PP_MakeString(""), PP_TRUE), 191 PP_MakeString(""), PP_TRUE),
144 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL), 192 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL),
145 PP_MakeString("http://www.google.com"), PP_TRUE), 193 PP_MakeString("http://www.google.com"), PP_TRUE),
146 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL), 194 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL),
147 PP_MakeString(""), PP_TRUE), 195 PP_MakeString(""), PP_TRUE),
148 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL), 196 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL),
149 PP_MakeUndefined(), PP_TRUE), 197 PP_MakeUndefined(), PP_TRUE),
150 PropertyTestData( 198 PropertyTestData(
151 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING), 199 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING),
152 PP_MakeString("base64"), PP_TRUE), 200 PP_MakeString("base64"), PP_TRUE),
153 PropertyTestData( 201 PropertyTestData(
154 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING), 202 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING),
155 PP_MakeString(""), PP_TRUE), 203 PP_MakeString(""), PP_TRUE),
156 PropertyTestData( 204 PropertyTestData(
157 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING), 205 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING),
158 PP_MakeUndefined(), PP_TRUE), 206 PP_MakeUndefined(), PP_TRUE),
159 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL), 207 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL),
160 PP_MakeUndefined(), PP_FALSE), 208 PP_MakeUndefined(), PP_FALSE),
161 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD), 209 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD),
162 PP_MakeUndefined(), PP_FALSE), 210 PP_MakeUndefined(), PP_FALSE),
163 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_HEADERS), 211 PropertyTestData(
212 ID_STR(PP_URLREQUESTPROPERTY_HEADERS),
164 PP_MakeString("Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA=="), 213 PP_MakeString("Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA=="),
165 PP_TRUE), 214 PP_TRUE),
166 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_HEADERS), 215 PropertyTestData(
216 ID_STR(PP_URLREQUESTPROPERTY_HEADERS),
167 PP_MakeString("Accept-Encoding: *\n" 217 PP_MakeString("Accept-Encoding: *\n"
168 "Accept-Charset: iso-8859-5, unicode-1-1;q=0.8"), 218 "Accept-Charset: iso-8859-5, unicode-1-1;q=0.8"),
169 PP_TRUE), 219 PP_TRUE),
170 PropertyTestData( 220 PropertyTestData(
171 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD), 221 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD),
172 PP_MakeInt32(0), PP_TRUE), 222 PP_MakeInt32(0), PP_TRUE),
173 PropertyTestData( 223 PropertyTestData(
174 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD), 224 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD),
175 PP_MakeInt32(100), PP_TRUE), 225 PP_MakeInt32(100), PP_TRUE),
176 PropertyTestData( 226 PropertyTestData(
177 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD), 227 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD),
178 PP_MakeInt32(0), PP_TRUE), 228 PP_MakeInt32(0), PP_TRUE),
179 PropertyTestData( 229 PropertyTestData(
180 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD), 230 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD),
181 PP_MakeInt32(100), PP_TRUE), 231 PP_MakeInt32(100), PP_TRUE),
182 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL), 232 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL),
183 PP_MakeString("::::::::::::"), PP_TRUE), 233 PP_MakeString("::::::::::::"), PP_TRUE),
184 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD), 234 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD),
185 PP_MakeString("INVALID"), PP_TRUE), 235 PP_MakeString("INVALID"), PP_TRUE),
186 PropertyTestData( 236 PropertyTestData(
187 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING), 237 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING),
188 PP_MakeString("invalid"), PP_TRUE), 238 PP_MakeString("invalid"), PP_TRUE),
189 PropertyTestData( 239 PropertyTestData(
190 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD), 240 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD),
191 PP_MakeInt32(-100), PP_TRUE), 241 PP_MakeInt32(-100), PP_TRUE),
192 PropertyTestData( 242 PropertyTestData(
193 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD), 243 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD),
194 PP_MakeInt32(-100), PP_TRUE), 244 PP_MakeInt32(-100), PP_TRUE),
195 245
196 }; 246 };
247 std::string error;
197 248
198 const PPB_URLRequestInfo* ppb = PPBURLRequestInfo(); 249 PP_Resource url_request = ppb_url_request_interface_->Create(
199 PP_Resource url_request = ppb->Create(pp_instance()); 250 instance_->pp_instance());
200 EXPECT(url_request != kInvalidResource); 251 if (url_request == kInvalidResource)
252 error = "Failed to create a URLRequestInfo";
201 253
254 // Loop over all test data even if we encountered an error to release vars.
202 for (size_t i = 0; 255 for (size_t i = 0;
203 i < sizeof(test_data)/sizeof(PropertyTestData); 256 i < sizeof(test_data) / sizeof(test_data[0]);
204 ++i) { 257 ++i) {
205 if (test_data[i].expected_value != 258 if (error.empty() && test_data[i].expected_value !=
206 ppb->SetProperty(url_request, 259 ppb_url_request_interface_->SetProperty(url_request,
207 test_data[i].property, 260 test_data[i].property,
208 test_data[i].var)) { 261 test_data[i].var)) {
209 nacl::string error_string = nacl::string("Setting property ") + 262 pp::Var var(pp::Var::DontManage(), test_data[i].var);
210 test_data[i].property_name.c_str() + " to " + 263 error = std::string("Setting property ") +
211 StringifyVar(test_data[i].var) + " did not return " + 264 test_data[i].property_name + " to " + var.DebugString() +
212 StringifyVar(PP_MakeBool(test_data[i].expected_value)); 265 " did not return " + (test_data[i].expected_value ? "True" : "False");
213 // PostTestMessage will signal test failure here. 266 error = test_data[i].property_name;
214 PostTestMessage(__FUNCTION__, error_string.c_str());
215 } 267 }
216 PPBVar()->Release(test_data[i].var); 268 ppb_var_interface_->Release(test_data[i].var);
217 } 269 }
218 270
219 PPBCore()->ReleaseResource(url_request); 271 ppb_core_interface_->ReleaseResource(url_request);
220 TEST_PASSED; 272 return error; // == PASS() if empty.
221 } 273 }
222 274
275 std::string TestURLRequest::LoadAndCompareBody(
276 PP_Resource url_request, const std::string& expected_body) {
277 TestCompletionCallback test_callback(instance_->pp_instance(), true);
278 pp::CompletionCallback callback =
279 static_cast<pp::CompletionCallback>(test_callback);
280 int32_t result = ppb_url_loader_interface_->Open(
281 url_loader_, url_request, callback.pp_completion_callback());
282 ASSERT_EQ(PP_OK_COMPLETIONPENDING, result);
283 result = test_callback.WaitForResult();
284 ASSERT_EQ(PP_OK, result);
223 285
224 void LoadAndCompareBody(PP_Resource request, 286 std::string error;
225 const nacl::string& expected_body) { 287 PP_Resource url_response =
226 PP_Resource url_loader = PPBURLLoader()->Create(pp_instance()); 288 ppb_url_loader_interface_->GetResponseInfo(url_loader_);
227 EXPECT(url_loader != kInvalidResource); 289 if (url_response == kInvalidResource) {
290 error = "PPB_URLLoader::GetResponseInfo() returned invalid resource";
291 } else {
292 PP_Var status = ppb_url_response_interface_->GetProperty(
293 url_response, PP_URLRESPONSEPROPERTY_STATUSCODE);
294 if (status.type != PP_VARTYPE_INT32 && status.value.as_int != 200)
295 error = ReportError("PPB_URLLoader::Open() status", status.value.as_int);
228 296
229 TestableCallback callback(pp_instance(), true); 297 std::string actual_body;
298 for (; error.empty();) { // Read the entire body in this loop.
299 const size_t kBufferSize = 32;
300 char buf[kBufferSize];
301 result = ppb_url_loader_interface_->ReadResponseBody(
302 url_loader_, buf, kBufferSize,
303 callback.pp_completion_callback());
304 if (PP_OK_COMPLETIONPENDING != result) {
305 error = ReportError("PPB_URLLoader::ReadResponseBody()", result);
306 break;
307 }
308 result = test_callback.WaitForResult();
309 if (result < PP_OK)
310 error = ReportError("PPB_URLLoader::ReadResponseBody()", result);
311 if (result <= PP_OK)
312 break;
313 actual_body.append(buf, result);
314 }
315 if (actual_body != expected_body)
316 error = "PPB_URLLoader::ReadResponseBody() read unexpected response";
317 }
318 ppb_core_interface_->ReleaseResource(url_response);
319 return error;
320 }
230 321
231 int32_t result = PPBURLLoader()->Open(url_loader, 322 // Tests
232 request, 323 // PP_Bool AppendDataToBody(
233 callback.GetCallback()); 324 // PP_Resource request, const void* data, uint32_t len);
234 EXPECT(PP_OK_COMPLETIONPENDING == result); 325 std::string TestURLRequest::TestAppendDataToBody() {
235 result = callback.WaitForResult(); 326 PP_Resource url_request = ppb_url_request_interface_->Create(
236 EXPECT(PP_OK == result); 327 instance_->pp_instance());
328 ASSERT_NE(url_request, kInvalidResource);
237 329
238 PP_Resource response = PPBURLLoader()->GetResponseInfo(url_loader); 330 std::string postdata("sample postdata");
239 EXPECT(response != kInvalidResource); 331 std::string error;
332 PP_Var post_string_var = PP_MakeString("POST");
333 PP_Var echo_string_var = PP_MakeString("/echo");
240 334
241 PP_Var status = 335 // NULL pointer causes a crash. In general PPAPI implementation does not
242 PPBURLResponseInfo()->GetProperty(response, 336 // test for NULL because they are just a special case of bad pointers that
243 PP_URLRESPONSEPROPERTY_STATUSCODE); 337 // are not detectable if set to point to an object that does not exist.
244 EXPECT_VAR_INT(status, 200);
245 338
246 nacl::string actual_body; 339 // Invalid resource should fail.
340 if (PP_TRUE == ppb_url_request_interface_->AppendDataToBody(
341 kInvalidResource, postdata.data(), postdata.length())) {
342 error = "AppendDataToBody() succeeded with invalid resource";
343 // Append data and POST to echoing web server.
344 } else if (PP_FALSE == ppb_url_request_interface_->SetProperty(
345 url_request, PP_URLREQUESTPROPERTY_METHOD, post_string_var)) {
346 error = "SetProperty(METHOD) failed\n";
347 } else if (PP_FALSE == ppb_url_request_interface_->SetProperty(
348 url_request, PP_URLREQUESTPROPERTY_URL, echo_string_var)) {
349 error = "SetProperty(URL) failed\n";
350 } else if (PP_FALSE == ppb_url_request_interface_->AppendDataToBody(
351 url_request, postdata.data(), postdata.length())) {
352 error = "AppendDataToBody() failed";
353 } else {
354 // Check for success.
355 error = LoadAndCompareBody(url_request, postdata);
356 }
247 357
248 // Read the entire body in this loop. 358 ppb_var_interface_->Release(post_string_var);
249 for (;;) { 359 ppb_var_interface_->Release(echo_string_var);
250 callback.Reset(); 360 ppb_core_interface_->ReleaseResource(url_request);
251 const size_t kBufferSize = 32; 361 return error; // == PASS() if empty.
252 char buf[kBufferSize]; 362 }
253 result = PPBURLLoader()->ReadResponseBody(url_loader, 363
254 buf, kBufferSize, 364 // TODO(elijahtaylor): add TestAppendFileToBody based on a broken disabled
255 callback.GetCallback()); 365 // version from a NaCl test - see crbug.com/110242 for details.
256 EXPECT(PP_OK_COMPLETIONPENDING == result); 366
257 result = callback.WaitForResult(); 367 // Allocates and manipulates a large number of resources.
258 EXPECT(result >= PP_OK); 368 std::string TestURLRequest::TestStress() {
259 if (result < 0) { 369 const int kManyResources = 500;
260 return; 370 PP_Resource url_request_info[kManyResources];
371
372 std::string error;
373 int num_created = kManyResources;
374 for (int i = 0; i < kManyResources; i++) {
375 url_request_info[i] = ppb_url_request_interface_->Create(
376 instance_->pp_instance());
377 if (url_request_info[i] == kInvalidResource) {
378 error = "Create() failed";
379 } else if (PP_FALSE == ppb_url_request_interface_->IsURLRequestInfo(
380 url_request_info[i])) {
381 error = "IsURLRequestInfo() failed";
382 } else if (PP_FALSE == ppb_url_request_interface_->SetProperty(
383 url_request_info[i],
384 PP_URLREQUESTPROPERTY_STREAMTOFILE,
385 PP_MakeBool(PP_FALSE))) {
386 error = "SetProperty() failed";
261 } 387 }
262 if (PP_OK == result) { 388 if (!error.empty()) {
389 num_created = i + 1;
263 break; 390 break;
264 } 391 }
265 actual_body.append(buf, result);
266 } 392 }
267 393 for (int i = 0; i < num_created; i++) {
268 EXPECT(actual_body == expected_body); 394 ppb_core_interface_->ReleaseResource(url_request_info[i]);
269 395 if (PP_TRUE ==
270 PPBCore()->ReleaseResource(response); 396 ppb_url_request_interface_->IsURLRequestInfo(url_request_info[i]))
271 PPBCore()->ReleaseResource(url_loader); 397 error = "IsURLREquestInfo() succeeded after release";
398 }
399 return error; // == PASS() if empty.
272 } 400 }
273
274 void SetupPOSTURLRequest(PP_Resource request) {
275 const PPB_URLRequestInfo* ppb = PPBURLRequestInfo();
276 EXPECT(PP_TRUE == ppb->SetProperty(request,
277 PP_URLREQUESTPROPERTY_METHOD,
278 PP_MakeString("POST")));
279 EXPECT(PP_TRUE == ppb->SetProperty(request,
280 PP_URLREQUESTPROPERTY_URL,
281 PP_MakeString("/echo")));
282 }
283
284 void TestAppendDataToBody() {
285 const PPB_URLRequestInfo* ppb = PPBURLRequestInfo();
286 PP_Resource url_request = ppb->Create(pp_instance());
287 EXPECT(url_request != kInvalidResource);
288 nacl::string postdata("sample postdata");
289
290 // Bad pointer passed in should fail to proxy.
291 EXPECT(PP_FALSE == ppb->AppendDataToBody(url_request,
292 NULL,
293 1));
294 // Invalid resource should fail.
295 EXPECT(PP_FALSE == ppb->AppendDataToBody(kInvalidResource,
296 postdata.data(),
297 postdata.length()));
298 // Append data and POST to echoing web server.
299 SetupPOSTURLRequest(url_request);
300 EXPECT(PP_TRUE == ppb->AppendDataToBody(url_request,
301 postdata.data(),
302 postdata.length()));
303 // Check for success.
304 LoadAndCompareBody(url_request, postdata);
305
306 PPBCore()->ReleaseResource(url_request);
307 TEST_PASSED;
308 }
309
310 // TODO(elijahtaylor): Revisit this function when it is enabled. Parts could be
311 // factored out and it could be smaller and more readable in general.
312 void TestAppendFileToBody() {
313 const int64_t kFileSystemSize = 1024;
314 const PPB_URLRequestInfo* ppb = PPBURLRequestInfo();
315 PP_Resource url_request = ppb->Create(pp_instance());
316 EXPECT(url_request != kInvalidResource);
317
318 // Creating a file reference PP_Resource is a multi-stage process.
319 // First, create and open a new file system.
320 const PPB_FileSystem* ppb_file_sys = PPBFileSystem();
321 PP_Resource file_sys =
322 ppb_file_sys->Create(pp_instance(),
323 PP_FILESYSTEMTYPE_EXTERNAL);
324 EXPECT(file_sys != kInvalidResource);
325
326 TestableCallback callback(pp_instance(), true);
327
328 int32_t result = ppb_file_sys->Open(file_sys,
329 kFileSystemSize,
330 callback.GetCallback());
331 EXPECT(PP_OK_COMPLETIONPENDING == result);
332 result = callback.WaitForResult();
333
334 EXPECT(PP_OK == result);
335
336 // Create a file reference, then tie it to a new writable file io resource.
337 PP_Resource file_ref = PPBFileRef()->Create(file_sys, "/foo");
338 EXPECT(file_ref != kInvalidResource);
339 PP_Resource file_io = PPBFileIO()->Create(pp_instance());
340 EXPECT(file_ref != kInvalidResource);
341
342 callback.Reset();
343 result = PPBFileIO()->Open(file_io,
344 file_ref,
345 PP_FILEOPENFLAG_WRITE | PP_FILEOPENFLAG_CREATE |
346 PP_FILEOPENFLAG_TRUNCATE,
347 callback.GetCallback());
348
349 EXPECT(PP_OK_COMPLETIONPENDING == result);
350 result = callback.WaitForResult();
351 EXPECT(PP_OK == result);
352
353 // Now we can write the contents of this string to the file io resource.
354 nacl::string postdata("sample postdata from file");
355 size_t total_written = 0;
356 for (;;) {
357 callback.Reset();
358 result = PPBFileIO()->Write(file_io,
359 total_written,
360 postdata.data() + total_written,
361 postdata.length() - total_written,
362 callback.GetCallback());
363
364 EXPECT(PP_OK_COMPLETIONPENDING == result);
365 result = callback.WaitForResult();
366
367 EXPECT(result >= PP_OK);
368 if (result < 0)
369 return;
370 if (result == PP_OK)
371 break;
372 total_written += result;
373 }
374 PPBFileIO()->Close(file_io);
375
376 const int64_t kReadUntilEOF = -1;
377 const PP_Time kDummyTimeValue = 5; // seconds since epoch
378
379 // Finally, set up the url_request to post to an echoing web server.
380 SetupPOSTURLRequest(url_request);
381 EXPECT(PP_TRUE == ppb->AppendFileToBody(url_request,
382 file_ref,
383 0, // start offset
384 postdata.length(),
385 0)); // expected last modified time
386 LOG_TO_BROWSER("testing ordinary 'AppendFileToBody'");
387 // Check for success.
388 LoadAndCompareBody(url_request, postdata);
389
390 // Use a new url_request for another POST request
391 PPBCore()->ReleaseResource(url_request);
392 url_request = ppb->Create(pp_instance());
393 EXPECT(url_request != kInvalidResource);
394
395 callback.Reset();
396 // Set the timestamps so we can test 'expected last modified time'
397 result = PPBFileIO()->Touch(file_ref,
398 kDummyTimeValue+1,
399 kDummyTimeValue,
400 callback.GetCallback());
401
402 SetupPOSTURLRequest(url_request);
403 EXPECT(PP_TRUE == ppb->AppendFileToBody(url_request,
404 file_ref,
405 0, // start offset
406 postdata.length(),
407 kDummyTimeValue));
408 LOG_TO_BROWSER("testing 'expected last modified time'");
409 LoadAndCompareBody(url_request, postdata);
410
411 PPBCore()->ReleaseResource(url_request);
412 url_request = ppb->Create(pp_instance());
413 EXPECT(url_request != kInvalidResource);
414
415 SetupPOSTURLRequest(url_request);
416 // Test for failure on expected last modified time, it won't fail until an
417 // actual request is made.
418 EXPECT(PP_TRUE == ppb->AppendFileToBody(url_request,
419 file_ref,
420 0, // start offset
421 kReadUntilEOF, // number of bytes
422 kDummyTimeValue - 1));
423 LOG_TO_BROWSER("testing 'expected last modified time' failure");
424 PP_Resource url_loader = PPBURLLoader()->Create(pp_instance());
425 EXPECT(url_loader != kInvalidResource);
426 callback.Reset();
427
428 result = PPBURLLoader()->Open(url_loader,
429 url_request,
430 callback.GetCallback());
431 EXPECT(PP_OK_COMPLETIONPENDING == result);
432 result = callback.WaitForResult();
433 EXPECT(PP_ERROR_FILECHANGED == result);
434
435 // Invalid Resource is failure.
436 EXPECT(PP_FALSE == ppb->AppendFileToBody(url_request,
437 kInvalidResource,
438 0, // start offset
439 1, // length
440 0)); // expected last modified time
441 // Test bad start offset.
442 EXPECT(PP_FALSE == ppb->AppendFileToBody(url_request,
443 file_ref,
444 -1, // start offset
445 1, // length
446 0)); // expected last modified time
447 // Test invalid length. (-1 means read until end of file, hence -2)
448 EXPECT(PP_FALSE == ppb->AppendFileToBody(url_request,
449 kInvalidResource,
450 0, // start offset
451 -2, // length
452 0)); // expected last modified time
453 PPBCore()->ReleaseResource(url_request);
454 PPBCore()->ReleaseResource(file_ref);
455 PPBCore()->ReleaseResource(file_sys);
456 TEST_PASSED;
457 }
458
459 // Allocates and manipulates a large number of resources.
460 void TestStress() {
461 const int kManyResources = 500;
462 PP_Resource url_request_info[kManyResources];
463 const PPB_URLRequestInfo* ppb = PPBURLRequestInfo();
464
465 for (int i = 0; i < kManyResources; i++) {
466 url_request_info[i] = ppb->Create(pp_instance());
467 EXPECT(url_request_info[i] != kInvalidResource);
468 EXPECT(PP_TRUE == ppb->IsURLRequestInfo(url_request_info[i]));
469 EXPECT(PP_TRUE == ppb->SetProperty(url_request_info[i],
470 PP_URLREQUESTPROPERTY_STREAMTOFILE,
471 PP_MakeBool(PP_FALSE)));
472 }
473 for (int i = 0; i < kManyResources; i++) {
474 PPBCore()->ReleaseResource(url_request_info[i]);
475 EXPECT(PP_FALSE == ppb->IsURLRequestInfo(url_request_info[i]));
476 }
477
478 TEST_PASSED;
479 }
480
481 } // namespace
482
483 void SetupTests() {
484 RegisterTest("TestCreate", TestCreate);
485 RegisterTest("TestIsURLRequestInfo", TestIsURLRequestInfo);
486 RegisterTest("TestSetProperty", TestSetProperty);
487 RegisterTest("TestAppendDataToBody", TestAppendDataToBody);
488 RegisterTest("TestAppendFileToBody", TestAppendFileToBody);
489 RegisterTest("TestStress", TestStress);
490 }
491
492 void SetupPluginInterfaces() {
493 // none
494 }
OLDNEW
« no previous file with comments | « ppapi/tests/test_url_request.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698