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

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, 11 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
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_)
dmichael (off chromium) 2012/01/17 18:38:03 optional: You could just do ASSERT_TRUE(foo_interf
polina1 2012/01/26 02:56:26 Not sure why that's better for init, but in either
dmichael (off chromium) 2012/01/26 17:13:43 Nevermind, wasn't reading carefully enough. Those
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();
bbudge 2012/01/16 16:24:29 Shouldn't we return false here if we couldn't get
polina1 2012/01/26 02:56:26 Just setting the error does the trick. That's how
71 }
72
73 void TestURLRequest::RunTests(const std::string& filter) {
74 RUN_TEST(Create, filter);
75 RUN_TEST(IsURLRequestInfo, filter);
76 RUN_TEST(SetProperty, filter);
77 RUN_TEST(Stress, filter);
78 RUN_TEST(AppendDataToBody, filter);
79 }
80
81 PP_Var TestURLRequest::PP_MakeString(const char* s) {
82 return ppb_var_interface_->VarFromUtf8(s, strlen(s));
83 }
25 84
26 // Tests 85 // Tests
27 // PP_Resource Create(PP_Instance instance). 86 // PP_Resource Create(PP_Instance instance).
28 void TestCreate() { 87 std::string TestURLRequest::TestCreate() {
29 PP_Resource url_request = kInvalidResource; 88 // Invalid / non-existent instance -> invalid resource.
30 89 if (ppb_url_request_interface_->Create(kInvalidInstance) != kInvalidResource)
31 // Invalid instance -> invalid resource. 90 return "Create() passed with an invalid instance";
dmichael (off chromium) 2012/01/17 18:38:03 nit: I prefer ASSERT_* for checks, unless what the
polina1 2012/01/26 02:56:26 I think historically these tests were written not
dmichael (off chromium) 2012/01/26 17:13:43 Right, only do them where they make sense.
polina1 2012/01/26 21:26:59 Done.
32 url_request = PPBURLRequestInfo()->Create(kInvalidInstance); 91 if (ppb_url_request_interface_->Create(kNotAnInstance) != kInvalidResource)
33 EXPECT(url_request == kInvalidResource); 92 return "Create() passed with a non-existent instance";
34 93
35 // Valid instance -> valid resource. 94 // Valid instance -> valid resource.
36 url_request = PPBURLRequestInfo()->Create(pp_instance()); 95 PP_Resource url_request = ppb_url_request_interface_->Create(
37 EXPECT(url_request != kInvalidResource); 96 instance_->pp_instance());
38 97 if (url_request == kInvalidResource)
39 PPBCore()->ReleaseResource(url_request); 98 return "Create() failed with a valid instance";
40 TEST_PASSED; 99 ppb_core_interface_->ReleaseResource(url_request);
100
101 PASS();
41 } 102 }
42 103
43 // Tests 104 // Tests
44 // PP_Bool IsURLRequestInfo(PP_Resource resource). 105 // PP_Bool IsURLRequestInfo(PP_Resource resource).
45 void TestIsURLRequestInfo() { 106 std::string TestURLRequest::TestIsURLRequestInfo() {
46 const PPB_URLRequestInfo* ppb = PPBURLRequestInfo();
47
48 // Invalid / non-existent / non-URLRequestInfo resource -> false. 107 // Invalid / non-existent / non-URLRequestInfo resource -> false.
49 EXPECT(PP_FALSE == ppb->IsURLRequestInfo(kInvalidResource)); 108 if (PP_TRUE == ppb_url_request_interface_->IsURLRequestInfo(kInvalidResource))
50 EXPECT(PP_FALSE == ppb->IsURLRequestInfo(kNotAResource)); 109 return "IsURLRequestInfo() passed with an invalid resource";
51 PP_Resource url_loader = PPBURLLoader()->Create(pp_instance()); 110 if (PP_TRUE == ppb_url_request_interface_->IsURLRequestInfo(kNotAResource))
52 EXPECT(url_loader != kInvalidResource); 111 return "IsURLRequestInfo() passed with an invalid resource";
53 EXPECT(PP_FALSE == ppb->IsURLRequestInfo(url_loader)); 112 if (PP_TRUE == ppb_url_request_interface_->IsURLRequestInfo(url_loader_))
54 PPBCore()->ReleaseResource(url_loader); 113 return "IsURLRequest() passed with a non-URLRequestInfo resource";
55 114
56 // Current URLRequestInfo resource -> true. 115 // Current URLRequestInfo resource -> true.
57 PP_Resource url_request = ppb->Create(pp_instance()); 116 PP_Resource url_request = ppb_url_request_interface_->Create(
58 EXPECT(PP_TRUE == ppb->IsURLRequestInfo(url_request)); 117 instance_->pp_instance());
118 std::string error;
119 if (PP_FALSE == ppb_url_request_interface_->IsURLRequestInfo(url_request))
120 error = "IsURLRequestInfo() failed with a current URLRequestInfo resource";
59 121
60 // Released URLRequestInfo resource -> false. 122 // Released URLRequestInfo resource -> false.
61 PPBCore()->ReleaseResource(url_request); 123 ppb_core_interface_->ReleaseResource(url_request);
62 EXPECT(PP_FALSE == ppb->IsURLRequestInfo(url_request)); 124 if (PP_TRUE == ppb_url_request_interface_->IsURLRequestInfo(url_request))
63 125 error = "IsURLRequestInfo() passed with a released URLRequestInfo resource";
64 TEST_PASSED; 126
127 return error; // == PASS() if empty.
65 } 128 }
66 129
67 // Tests 130 // Tests
68 // PP_Bool SetProperty(PP_Resource request, 131 // PP_Bool SetProperty(PP_Resource request,
69 // PP_URLRequestProperty property, 132 // PP_URLRequestProperty property,
70 // struct PP_Var value); 133 // struct PP_Var value);
71 void TestSetProperty() { 134 std::string TestURLRequest::TestSetProperty() {
72 struct PropertyTestData {
73 PropertyTestData(PP_URLRequestProperty _property,
74 const std::string& _property_name,
75 PP_Var _var, PP_Bool _expected_value) :
76 property(_property), property_name(_property_name),
77 var(_var), expected_value(_expected_value) {
78 PPBVar()->AddRef(var);
79 }
80 PP_URLRequestProperty property;
81 std::string property_name;
82 PP_Var var;
83 PP_Bool expected_value;
84 };
85
86 // All bool properties should accept PP_TRUE and PP_FALSE, while rejecting 135 // All bool properties should accept PP_TRUE and PP_FALSE, while rejecting
87 // all other variable types. 136 // all other variable types.
88 #define ADD_BOOL_PROPERTY(_prop_name) \ 137 #define TEST_BOOL(_name) \
dmichael (off chromium) 2012/01/17 18:38:03 nit: I think the style guide says that all precomp
polina1 2012/01/26 02:56:26 Yeah, I know. I wanted to keep the diff with the o
dmichael (off chromium) 2012/01/26 17:13:43 How about just fixing the indentation so they are
polina1 2012/01/26 21:26:59 I shifted #define to the very left to be consisten
dmichael (off chromium) 2012/01/26 21:36:06 The style guide says "preprocessor directives", wh
89 PropertyTestData(ID_STR(_prop_name), PP_MakeBool(PP_TRUE), PP_TRUE), \ 138 PropertyTestData(this, ID_STR(_name), PP_MakeBool(PP_TRUE), PP_TRUE), \
90 PropertyTestData(ID_STR(_prop_name), PP_MakeBool(PP_FALSE), PP_TRUE), \ 139 PropertyTestData(this, ID_STR(_name), PP_MakeBool(PP_FALSE), PP_TRUE), \
91 PropertyTestData(ID_STR(_prop_name), PP_MakeUndefined(), PP_FALSE), \ 140 PropertyTestData(this, ID_STR(_name), PP_MakeUndefined(), PP_FALSE), \
92 PropertyTestData(ID_STR(_prop_name), PP_MakeNull(), PP_FALSE), \ 141 PropertyTestData(this, ID_STR(_name), PP_MakeNull(), PP_FALSE), \
93 PropertyTestData(ID_STR(_prop_name), PP_MakeInt32(0), PP_FALSE), \ 142 PropertyTestData(this, ID_STR(_name), PP_MakeInt32(0), PP_FALSE), \
94 PropertyTestData(ID_STR(_prop_name), PP_MakeDouble(0.0), PP_FALSE) 143 PropertyTestData(this, ID_STR(_name), PP_MakeDouble(0.0), PP_FALSE)
95 144
96 // These property types are always invalid for string properties. 145 // These property types are always invalid for string properties.
97 #define ADD_STRING_INVALID_PROPERTIES(_prop_name) \ 146 #define TEST_STRING_INVALID(_name) \
98 PropertyTestData(ID_STR(_prop_name), PP_MakeNull(), PP_FALSE), \ 147 PropertyTestData(this, ID_STR(_name), PP_MakeNull(), PP_FALSE), \
99 PropertyTestData(ID_STR(_prop_name), PP_MakeBool(PP_FALSE), PP_FALSE), \ 148 PropertyTestData(this, ID_STR(_name), PP_MakeBool(PP_FALSE), PP_FALSE), \
100 PropertyTestData(ID_STR(_prop_name), PP_MakeInt32(0), PP_FALSE), \ 149 PropertyTestData(this, ID_STR(_name), PP_MakeInt32(0), PP_FALSE), \
101 PropertyTestData(ID_STR(_prop_name), PP_MakeDouble(0.0), PP_FALSE) 150 PropertyTestData(this, ID_STR(_name), PP_MakeDouble(0.0), PP_FALSE)
102 151
103 #define ADD_INT_INVALID_PROPERTIES(_prop_name) \ 152 #define TEST_INT_INVALID(_name) \
104 PropertyTestData(ID_STR(_prop_name), PP_MakeUndefined(), PP_FALSE), \ 153 PropertyTestData(this, ID_STR(_name), PP_MakeUndefined(), PP_FALSE), \
105 PropertyTestData(ID_STR(_prop_name), PP_MakeNull(), PP_FALSE), \ 154 PropertyTestData(this, ID_STR(_name), PP_MakeNull(), PP_FALSE), \
106 PropertyTestData(ID_STR(_prop_name), PP_MakeBool(PP_FALSE), PP_FALSE), \ 155 PropertyTestData(this, ID_STR(_name), PP_MakeBool(PP_FALSE), PP_FALSE), \
107 PropertyTestData(ID_STR(_prop_name), PP_MakeString(""), PP_FALSE), \ 156 PropertyTestData(this, ID_STR(_name), PP_MakeString("notint"), PP_FALSE), \
108 PropertyTestData(ID_STR(_prop_name), PP_MakeDouble(0.0), PP_FALSE) 157 PropertyTestData(this, ID_STR(_name), PP_MakeDouble(0.0), PP_FALSE)
109 158
110 // SetProperty accepts plenty of invalid values (malformed urls, negative 159 // SetProperty accepts plenty of invalid values (malformed urls, negative
111 // thresholds, etc). Error checking is delayed until request opening (aka url 160 // thresholds, etc). Error checking is delayed until request opening (aka url
112 // loading). 161 // loading).
113 #define ID_STR(arg) arg, #arg 162 #define ID_STR(arg) arg, #arg
114 PropertyTestData test_data[] = { 163 PropertyTestData test_data[] = {
115 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_STREAMTOFILE), 164 TEST_BOOL(PP_URLREQUESTPROPERTY_STREAMTOFILE),
116 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS), 165 TEST_BOOL(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS),
117 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS), 166 TEST_BOOL(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS),
118 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS), 167 TEST_BOOL(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS),
119 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS), 168 TEST_BOOL(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS),
120 ADD_BOOL_PROPERTY(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS), 169 TEST_BOOL(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS),
121 ADD_STRING_INVALID_PROPERTIES(PP_URLREQUESTPROPERTY_URL), 170 TEST_STRING_INVALID(PP_URLREQUESTPROPERTY_URL),
122 ADD_STRING_INVALID_PROPERTIES(PP_URLREQUESTPROPERTY_METHOD), 171 TEST_STRING_INVALID(PP_URLREQUESTPROPERTY_METHOD),
123 ADD_STRING_INVALID_PROPERTIES(PP_URLREQUESTPROPERTY_HEADERS), 172 TEST_STRING_INVALID(PP_URLREQUESTPROPERTY_HEADERS),
124 ADD_STRING_INVALID_PROPERTIES( 173 TEST_STRING_INVALID(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL),
125 PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL), 174 TEST_STRING_INVALID(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING),
126 ADD_STRING_INVALID_PROPERTIES( 175 TEST_INT_INVALID(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD),
127 PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING), 176 TEST_INT_INVALID(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD),
128 ADD_INT_INVALID_PROPERTIES( 177 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_URL),
129 PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD),
130 ADD_INT_INVALID_PROPERTIES(
131 PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD),
132 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL),
133 PP_MakeString("http://www.google.com"), PP_TRUE), 178 PP_MakeString("http://www.google.com"), PP_TRUE),
134 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL), 179 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_URL),
135 PP_MakeString("foo.jpg"), PP_TRUE), 180 PP_MakeString("foo.jpg"), PP_TRUE),
136 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD), 181 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_METHOD),
137 PP_MakeString("GET"), PP_TRUE), 182 PP_MakeString("GET"), PP_TRUE),
138 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD), 183 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_METHOD),
139 PP_MakeString("POST"), PP_TRUE), 184 PP_MakeString("POST"), PP_TRUE),
140 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_HEADERS), 185 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_HEADERS),
141 PP_MakeString("Accept: text/plain"), PP_TRUE), 186 PP_MakeString("Accept: text/plain"), PP_TRUE),
142 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_HEADERS), 187 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_HEADERS),
143 PP_MakeString(""), PP_TRUE), 188 PP_MakeString(""), PP_TRUE),
144 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL), 189 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL),
145 PP_MakeString("http://www.google.com"), PP_TRUE), 190 PP_MakeString("http://www.google.com"), PP_TRUE),
146 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL), 191 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL),
147 PP_MakeString(""), PP_TRUE), 192 PP_MakeString(""), PP_TRUE),
148 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL), 193 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL),
149 PP_MakeUndefined(), PP_TRUE), 194 PP_MakeUndefined(), PP_TRUE),
150 PropertyTestData( 195 PropertyTestData(this,
151 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING), 196 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING),
152 PP_MakeString("base64"), PP_TRUE), 197 PP_MakeString("base64"), PP_TRUE),
153 PropertyTestData( 198 PropertyTestData(this,
154 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING), 199 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING),
155 PP_MakeString(""), PP_TRUE), 200 PP_MakeString(""), PP_TRUE),
156 PropertyTestData( 201 PropertyTestData(this,
157 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING), 202 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING),
158 PP_MakeUndefined(), PP_TRUE), 203 PP_MakeUndefined(), PP_TRUE),
159 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL), 204 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_URL),
160 PP_MakeUndefined(), PP_FALSE), 205 PP_MakeUndefined(), PP_FALSE),
161 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD), 206 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_METHOD),
162 PP_MakeUndefined(), PP_FALSE), 207 PP_MakeUndefined(), PP_FALSE),
163 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_HEADERS), 208 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_HEADERS),
164 PP_MakeString("Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA=="), 209 PP_MakeString("Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA=="),
165 PP_TRUE), 210 PP_TRUE),
166 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_HEADERS), 211 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_HEADERS),
167 PP_MakeString("Accept-Encoding: *\n" 212 PP_MakeString("Accept-Encoding: *\n"
168 "Accept-Charset: iso-8859-5, unicode-1-1;q=0.8"), 213 "Accept-Charset: iso-8859-5, unicode-1-1;q=0.8"),
169 PP_TRUE), 214 PP_TRUE),
170 PropertyTestData( 215 PropertyTestData(this,
171 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD), 216 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD),
172 PP_MakeInt32(0), PP_TRUE), 217 PP_MakeInt32(0), PP_TRUE),
173 PropertyTestData( 218 PropertyTestData(this,
174 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD), 219 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD),
175 PP_MakeInt32(100), PP_TRUE), 220 PP_MakeInt32(100), PP_TRUE),
176 PropertyTestData( 221 PropertyTestData(this,
177 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD), 222 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD),
178 PP_MakeInt32(0), PP_TRUE), 223 PP_MakeInt32(0), PP_TRUE),
179 PropertyTestData( 224 PropertyTestData(this,
180 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD), 225 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD),
181 PP_MakeInt32(100), PP_TRUE), 226 PP_MakeInt32(100), PP_TRUE),
182 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_URL), 227 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_URL),
183 PP_MakeString("::::::::::::"), PP_TRUE), 228 PP_MakeString("::::::::::::"), PP_TRUE),
184 PropertyTestData(ID_STR(PP_URLREQUESTPROPERTY_METHOD), 229 PropertyTestData(this, ID_STR(PP_URLREQUESTPROPERTY_METHOD),
185 PP_MakeString("INVALID"), PP_TRUE), 230 PP_MakeString("INVALID"), PP_TRUE),
186 PropertyTestData( 231 PropertyTestData(this,
187 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING), 232 ID_STR(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING),
188 PP_MakeString("invalid"), PP_TRUE), 233 PP_MakeString("invalid"), PP_TRUE),
189 PropertyTestData( 234 PropertyTestData(this,
190 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD), 235 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD),
191 PP_MakeInt32(-100), PP_TRUE), 236 PP_MakeInt32(-100), PP_TRUE),
192 PropertyTestData( 237 PropertyTestData(this,
193 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD), 238 ID_STR(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD),
194 PP_MakeInt32(-100), PP_TRUE), 239 PP_MakeInt32(-100), PP_TRUE),
195
196 }; 240 };
197 241 std::string error;
198 const PPB_URLRequestInfo* ppb = PPBURLRequestInfo(); 242
199 PP_Resource url_request = ppb->Create(pp_instance()); 243 PP_Resource url_request = ppb_url_request_interface_->Create(
200 EXPECT(url_request != kInvalidResource); 244 instance_->pp_instance());
201 245 if (url_request == kInvalidResource)
246 error = "Failed to create a URLRequestInfo";
247
248 // Loop over all test data even if we encountered an error to release vars.
202 for (size_t i = 0; 249 for (size_t i = 0;
203 i < sizeof(test_data)/sizeof(PropertyTestData); 250 i < sizeof(test_data) / sizeof(PropertyTestData);
dmichael (off chromium) 2012/01/17 18:38:03 nit: best practice is to use sizeof(test_data[0])
polina1 2012/01/26 02:56:26 Done.
204 ++i) { 251 ++i) {
205 if (test_data[i].expected_value != 252 if (error.empty() && test_data[i].expected_value !=
206 ppb->SetProperty(url_request, 253 ppb_url_request_interface_->SetProperty(url_request,
207 test_data[i].property, 254 test_data[i].property,
208 test_data[i].var)) { 255 test_data[i].var)) {
209 nacl::string error_string = nacl::string("Setting property ") + 256 pp::Var var(pp::Var::DontManage(), test_data[i].var);
210 test_data[i].property_name.c_str() + " to " + 257 error = std::string("Setting property ") +
211 StringifyVar(test_data[i].var) + " did not return " + 258 test_data[i].property_name + " to " + var.DebugString() +
212 StringifyVar(PP_MakeBool(test_data[i].expected_value)); 259 " did not return " + (test_data[i].expected_value ? "True" : "False");
213 // PostTestMessage will signal test failure here. 260 error = test_data[i].property_name;
dmichael (off chromium) 2012/01/17 18:38:03 FWIW, I think this is a perfect example where buil
polina1 2012/01/26 02:56:26 Done.
214 PostTestMessage(__FUNCTION__, error_string.c_str());
215 } 261 }
216 PPBVar()->Release(test_data[i].var); 262 ppb_var_interface_->Release(test_data[i].var);
dmichael (off chromium) 2012/01/17 18:38:03 Ah, you can ignore my prior comment about releasin
polina1 2012/01/26 02:56:26 I already responded to that comment before reading
217 } 263 }
218 264
219 PPBCore()->ReleaseResource(url_request); 265 ppb_core_interface_->ReleaseResource(url_request);
220 TEST_PASSED; 266 return error; // == PASS() if empty.
221 } 267 }
222 268
223 269 std::string TestURLRequest::LoadAndCompareBody(
224 void LoadAndCompareBody(PP_Resource request, 270 PP_Resource url_request, const std::string& expected_body) {
225 const nacl::string& expected_body) { 271 TestCompletionCallback test_callback(instance_->pp_instance(), true);
226 PP_Resource url_loader = PPBURLLoader()->Create(pp_instance()); 272 pp::CompletionCallback callback =
227 EXPECT(url_loader != kInvalidResource); 273 static_cast<pp::CompletionCallback>(test_callback);
228 274 int32_t result = ppb_url_loader_interface_->Open(
229 TestableCallback callback(pp_instance(), true); 275 url_loader_, url_request, callback.pp_completion_callback());
230 276 if (PP_OK_COMPLETIONPENDING != result)
231 int32_t result = PPBURLLoader()->Open(url_loader, 277 return ReportError("PPB_URLLoader::Open()", result);
232 request, 278 result = test_callback.WaitForResult();
233 callback.GetCallback()); 279 if (PP_OK != result)
234 EXPECT(PP_OK_COMPLETIONPENDING == result); 280 return ReportError("PPB_URLLoader::Open()", result);
235 result = callback.WaitForResult(); 281
236 EXPECT(PP_OK == result); 282 std::string error;
237 283 PP_Resource url_response =
238 PP_Resource response = PPBURLLoader()->GetResponseInfo(url_loader); 284 ppb_url_loader_interface_->GetResponseInfo(url_loader_);
239 EXPECT(response != kInvalidResource); 285 if (url_response == kInvalidResource) {
240 286 error = "PPB_URLLoader::GetResponseInfo() returned invalid resource";
241 PP_Var status = 287 } else {
242 PPBURLResponseInfo()->GetProperty(response, 288 PP_Var status = ppb_url_response_interface_->GetProperty(
243 PP_URLRESPONSEPROPERTY_STATUSCODE); 289 url_response, PP_URLRESPONSEPROPERTY_STATUSCODE);
244 EXPECT_VAR_INT(status, 200); 290 if (status.type != PP_VARTYPE_INT32 && status.value.as_int != 200)
245 291 error = ReportError("PPB_URLLoader::Open() status", status.value.as_int);
246 nacl::string actual_body; 292
247 293 std::string actual_body;
248 // Read the entire body in this loop. 294 for (; error.empty();) { // Read the entire body in this loop.
249 for (;;) { 295 const size_t kBufferSize = 32;
250 callback.Reset(); 296 char buf[kBufferSize];
251 const size_t kBufferSize = 32; 297 result = ppb_url_loader_interface_->ReadResponseBody(
252 char buf[kBufferSize]; 298 url_loader_, buf, kBufferSize,
253 result = PPBURLLoader()->ReadResponseBody(url_loader, 299 callback.pp_completion_callback());
254 buf, kBufferSize, 300 if (PP_OK_COMPLETIONPENDING != result) {
255 callback.GetCallback()); 301 error = ReportError("PPB_URLLoader::ReadResponseBody()", result);
256 EXPECT(PP_OK_COMPLETIONPENDING == result); 302 break;
257 result = callback.WaitForResult(); 303 }
258 EXPECT(result >= PP_OK); 304 result = test_callback.WaitForResult();
259 if (result < 0) { 305 if (result < PP_OK)
260 return; 306 error = ReportError("PPB_URLLoader::ReadResponseBody()", result);
307 if (result <= PP_OK)
308 break;
309 actual_body.append(buf, result);
261 } 310 }
262 if (PP_OK == result) { 311 if (actual_body != expected_body)
312 error = "PPB_URLLoader::ReadResponseBody() read unexpected response";
313 }
314 ppb_core_interface_->ReleaseResource(url_response);
315 return error;
316 }
317
318 // Tests
319 // PP_Bool AppendDataToBody(
320 // PP_Resource request, const void* data, uint32_t len);
321 std::string TestURLRequest::TestAppendDataToBody() {
322 PP_Resource url_request = ppb_url_request_interface_->Create(
323 instance_->pp_instance());
324 if (url_request == kInvalidResource)
325 return "Create() failed\n";
326
327 std::string postdata("sample postdata");
328 std::string error;
329 PP_Var post_string_var = PP_MakeString("POST");
330 PP_Var echo_string_var = PP_MakeString("/echo");
331
332 // NULL pointer causes a crash. In general PPAPI implementation does not
333 // test for NULL because they are just a special case of bad pointers that
334 // are not detectable if set to point to an object that does not exist.
335
336 // Invalid resource should fail.
337 if (PP_TRUE == ppb_url_request_interface_->AppendDataToBody(
338 kInvalidResource, postdata.data(), postdata.length())) {
339 error = "AppendDataToBody() succeeded with invalid resource";
340 // Append data and POST to echoing web server.
341 } else if (PP_FALSE == ppb_url_request_interface_->SetProperty(
342 url_request, PP_URLREQUESTPROPERTY_METHOD, post_string_var)) {
343 error = "SetProperty(METHOD) failed\n";
344 } else if (PP_FALSE == ppb_url_request_interface_->SetProperty(
345 url_request, PP_URLREQUESTPROPERTY_URL, echo_string_var)) {
346 error = "SetProperty(URL) failed\n";
347 } else if (PP_FALSE == ppb_url_request_interface_->AppendDataToBody(
348 url_request, postdata.data(), postdata.length())) {
349 error = "AppendDataToBody() failed";
350 } else {
351 // Check for success.
352 error = LoadAndCompareBody(url_request, postdata);
353 }
354
355 ppb_var_interface_->Release(post_string_var);
356 ppb_var_interface_->Release(echo_string_var);
357 ppb_core_interface_->ReleaseResource(url_request);
358 return error; // == PASS() if empty.
359 }
360
361 // TODO(elijahtaylor): add TestAppendFileToBody based on a broken disabled
362 // version from a NaCl test - see crbug.com/110242 for details.
363
364 // Allocates and manipulates a large number of resources.
365 std::string TestURLRequest::TestStress() {
366 const int kManyResources = 500;
367 PP_Resource url_request_info[kManyResources];
368
369 std::string error;
370 int num_created = kManyResources;
371 for (int i = 0; i < kManyResources; i++) {
372 url_request_info[i] = ppb_url_request_interface_->Create(
373 instance_->pp_instance());
374 if (url_request_info[i] == kInvalidResource) {
375 error = "Create() failed";
376 } else if (PP_FALSE == ppb_url_request_interface_->IsURLRequestInfo(
377 url_request_info[i])) {
378 error = "IsURLRequestInfo() failed";
379 } else if (PP_FALSE == ppb_url_request_interface_->SetProperty(
380 url_request_info[i],
381 PP_URLREQUESTPROPERTY_STREAMTOFILE,
382 PP_MakeBool(PP_FALSE))) {
383 error = "SetProperty() failed";
384 }
385 if (!error.empty()) {
386 num_created = i + 1;
263 break; 387 break;
264 } 388 }
265 actual_body.append(buf, result); 389 }
266 } 390 for (int i = 0; i < num_created; i++) {
267 391 ppb_core_interface_->ReleaseResource(url_request_info[i]);
268 EXPECT(actual_body == expected_body); 392 if (PP_TRUE ==
269 393 ppb_url_request_interface_->IsURLRequestInfo(url_request_info[i]))
270 PPBCore()->ReleaseResource(response); 394 error = "IsURLREquestInfo() succeeded after release";
271 PPBCore()->ReleaseResource(url_loader); 395 }
272 } 396 return error; // == PASS() if empty.
273 397 }
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
« ppapi/tests/test_url_request.h ('K') | « ppapi/tests/test_url_request.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698