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

Side by Side Diff: content/renderer/npapi/webplugin_impl_unittest.cc

Issue 1853793003: Remove content/renderer/npapi (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 8 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
« no previous file with comments | « content/renderer/npapi/webplugin_impl.cc ('k') | content/renderer/render_frame_impl.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stddef.h>
6 #include <stdint.h>
7
8 #include "base/strings/string_util.h"
9 #include "content/renderer/npapi/webplugin_impl.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11 #include "third_party/WebKit/public/platform/WebString.h"
12 #include "third_party/WebKit/public/platform/WebURLRequest.h"
13
14 using blink::WebHTTPBody;
15 using blink::WebString;
16 using blink::WebURLRequest;
17
18 namespace content {
19
20 namespace {
21
22 std::string GetHeader(const WebURLRequest& request, const char* name) {
23 std::string result;
24 base::TrimWhitespaceASCII(
25 request.httpHeaderField(WebString::fromUTF8(name)).utf8(),
26 base::TRIM_ALL,
27 &result);
28 return result;
29 }
30
31 std::string GetBodyText(const WebURLRequest& request) {
32 const WebHTTPBody& body = request.httpBody();
33 if (body.isNull())
34 return std::string();
35
36 std::string result;
37 size_t i = 0;
38 WebHTTPBody::Element element;
39 while (body.elementAt(i++, element)) {
40 if (element.type == WebHTTPBody::Element::TypeData) {
41 result.append(element.data.data(), element.data.size());
42 } else {
43 NOTREACHED() << "unexpected element type encountered!";
44 }
45 }
46 return result;
47 }
48
49 } // namespace
50
51 // The Host functions for NPN_PostURL and NPN_PostURLNotify
52 // need to parse out some HTTP headers. Make sure it works
53 // with the following tests
54
55 TEST(WebPluginImplTest, PostParserSimple) {
56 // Test a simple case with headers & data
57 const char *ex1 = "foo: bar\nContent-length: 10\n\nabcdefghij";
58 WebURLRequest request;
59 request.initialize();
60 bool rv = WebPluginImpl::SetPostData(&request, ex1,
61 static_cast<uint32_t>(strlen(ex1)));
62 EXPECT_TRUE(rv);
63 EXPECT_EQ("bar", GetHeader(request, "foo"));
64 EXPECT_EQ(0U, GetHeader(request, "bar").length());
65 EXPECT_EQ(0U, GetHeader(request, "Content-length").length());
66 EXPECT_EQ("abcdefghij", GetBodyText(request));
67 }
68
69 TEST(WebPluginImplTest, PostParserLongHeader) {
70 // Test a simple case with long headers
71 const char *ex1 = "foo: 012345678901234567890123456789012345678901234567890123 4567890123456789012345678901234567890123456789\n\nabcdefghij";
72 WebURLRequest request;
73 request.initialize();
74 bool rv = WebPluginImpl::SetPostData(&request, ex1,
75 static_cast<uint32_t>(strlen(ex1)));
76 EXPECT_TRUE(rv);
77 EXPECT_EQ(100U, GetHeader(request, "foo").length());
78 }
79
80 TEST(WebPluginImplTest, PostParserManyHeaders) {
81 // Test a simple case with long headers
82 const char *ex1 = "h1:h1\nh2:h2\nh3:h3\nh4:h4\nh5:h5\nh6:h6\nh7:h7\nh8:h8\nh9: h9\nh10:h10\n\nbody";
83 WebURLRequest request;
84 request.initialize();
85 bool rv = WebPluginImpl::SetPostData(&request, ex1,
86 static_cast<uint32_t>(strlen(ex1)));
87 EXPECT_TRUE(rv);
88 EXPECT_EQ("h1", GetHeader(request, "h1"));
89 EXPECT_EQ("h2", GetHeader(request, "h2"));
90 EXPECT_EQ("h3", GetHeader(request, "h3"));
91 EXPECT_EQ("h4", GetHeader(request, "h4"));
92 EXPECT_EQ("h5", GetHeader(request, "h5"));
93 EXPECT_EQ("h6", GetHeader(request, "h6"));
94 EXPECT_EQ("h7", GetHeader(request, "h7"));
95 EXPECT_EQ("h8", GetHeader(request, "h8"));
96 EXPECT_EQ("h9", GetHeader(request, "h9"));
97 EXPECT_EQ("h10", GetHeader(request, "h10"));
98 EXPECT_EQ("body", GetBodyText(request));
99 }
100
101 TEST(WebPluginImplTest, PostParserDuplicateHeaders) {
102 // Test a simple case with long headers
103 // What value gets returned doesn't really matter. It shouldn't error
104 // out.
105 const char *ex1 = "h1:h1\nh1:h2\n\nbody";
106 WebURLRequest request;
107 request.initialize();
108 bool rv = WebPluginImpl::SetPostData(&request, ex1,
109 static_cast<uint32_t>(strlen(ex1)));
110 EXPECT_TRUE(rv);
111 }
112
113 TEST(WebPluginImplTest, PostParserNoHeaders) {
114 // Test a simple case with no headers but with data
115 const char *ex1 = "\nabcdefghij";
116 WebURLRequest request;
117 request.initialize();
118 bool rv = WebPluginImpl::SetPostData(&request, ex1,
119 static_cast<uint32_t>(strlen(ex1)));
120 EXPECT_TRUE(rv);
121 EXPECT_EQ(0U, GetHeader(request, "foo").length());
122 EXPECT_EQ(0U, GetHeader(request, "bar").length());
123 EXPECT_EQ(0U, GetHeader(request, "Content-length").length());
124 EXPECT_EQ("abcdefghij", GetBodyText(request));
125 }
126
127 TEST(WebPluginImplTest, PostParserNoBody) {
128 // Test a simple case with headers and no body
129 const char *ex1 = "Foo:bar\n\n";
130 WebURLRequest request;
131 request.initialize();
132 bool rv = WebPluginImpl::SetPostData(&request, ex1,
133 static_cast<uint32_t>(strlen(ex1)));
134 EXPECT_TRUE(rv);
135 EXPECT_EQ("bar", GetHeader(request, "foo"));
136 EXPECT_EQ(0U, GetHeader(request, "bar").length());
137 EXPECT_EQ(0U, GetHeader(request, "Content-length").length());
138 EXPECT_EQ(0U, GetBodyText(request).length());
139 }
140
141 TEST(WebPluginImplTest, PostParserBodyWithNewLines) {
142 // Test a simple case with headers and no body
143 const char *ex1 = "Foo:bar\n\n\n\nabcdefg\n\nabcdefg";
144 WebURLRequest request;
145 request.initialize();
146 bool rv = WebPluginImpl::SetPostData(&request, ex1,
147 static_cast<uint32_t>(strlen(ex1)));
148 EXPECT_TRUE(rv);
149 EXPECT_EQ(GetBodyText(request), "\n\nabcdefg\n\nabcdefg");
150 }
151
152 TEST(WebPluginImplTest, PostParserErrorNoBody) {
153 // Test with headers and no body
154 const char *ex1 = "Foo:bar\n";
155 WebURLRequest request;
156 request.initialize();
157 bool rv = WebPluginImpl::SetPostData(&request, ex1,
158 static_cast<uint32_t>(strlen(ex1)));
159 EXPECT_TRUE(rv);
160 }
161
162 TEST(WebPluginImplTest, PostParserErrorEmpty) {
163 // Test with an empty string
164 const char *ex1 = "";
165 WebURLRequest request;
166 request.initialize();
167 bool rv = WebPluginImpl::SetPostData(&request, ex1,
168 static_cast<uint32_t>(strlen(ex1)));
169 EXPECT_TRUE(rv);
170 }
171
172 TEST(WebPluginImplTest, PostParserEmptyName) {
173 // Test an error case with an empty header name field
174 const char *ex1 = "foo:bar\n:blat\n\nbody";
175 WebURLRequest request;
176 request.initialize();
177 bool rv = WebPluginImpl::SetPostData(&request, ex1,
178 static_cast<uint32_t>(strlen(ex1)));
179 EXPECT_TRUE(rv);
180 EXPECT_EQ("bar", GetHeader(request, "foo"));
181 EXPECT_EQ("body", GetBodyText(request));
182 }
183
184 TEST(WebPluginImplTest, PostParserEmptyValue) {
185 // Test an error case with an empty value field
186 const char *ex1 = "foo:bar\nbar:\n\nbody";
187 WebURLRequest request;
188 request.initialize();
189 bool rv = WebPluginImpl::SetPostData(&request, ex1,
190 static_cast<uint32_t>(strlen(ex1)));
191 EXPECT_TRUE(rv);
192 EXPECT_EQ("bar", GetHeader(request, "foo"));
193 EXPECT_EQ(0U, GetHeader(request, "bar").length());
194 EXPECT_EQ("body", GetBodyText(request));
195 }
196
197 TEST(WebPluginImplTest, PostParserCRLF) {
198 // Test an error case with an empty value field
199 const char *ex1 = "foo: bar\r\nbar:\r\n\r\nbody\r\n\r\nbody2";
200 WebURLRequest request;
201 request.initialize();
202 bool rv = WebPluginImpl::SetPostData(&request, ex1,
203 static_cast<uint32_t>(strlen(ex1)));
204 EXPECT_TRUE(rv);
205 EXPECT_EQ("bar", GetHeader(request, "foo"));
206 EXPECT_EQ(0U, GetHeader(request, "bar").length());
207 EXPECT_EQ("body\r\n\r\nbody2", GetBodyText(request));
208 }
209
210 TEST(WebPluginImplTest, PostParserBodyWithBinaryData) {
211 // Test a simple case with headers and binary data.
212 char ex1[33] = "foo: bar\nContent-length: 10\n\n";
213 unsigned int binary_data = 0xFFFFFFF0;
214 memcpy(ex1 + strlen("foo: bar\nContent-length: 10\n\n"), &binary_data,
215 sizeof(binary_data));
216
217 WebURLRequest request;
218 request.initialize();
219 bool rv = WebPluginImpl::SetPostData(&request, ex1,
220 sizeof(ex1)/sizeof(ex1[0]));
221 EXPECT_TRUE(rv);
222 EXPECT_EQ("bar", GetHeader(request, "foo"));
223 EXPECT_EQ(0U, GetHeader(request, "bar").length());
224 EXPECT_EQ(0U, GetHeader(request, "Content-length").length());
225
226 std::string body = GetBodyText(request);
227
228 EXPECT_EQ(0xF0, (unsigned char)body[0]);
229 EXPECT_EQ(0xFF, (unsigned char)body[1]);
230 EXPECT_EQ(0xFF, (unsigned char)body[2]);
231 EXPECT_EQ(0xFF, (unsigned char)body[3]);
232 }
233
234 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/npapi/webplugin_impl.cc ('k') | content/renderer/render_frame_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698