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