| 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/string_util.h" | |
| 6 #include "testing/gtest/include/gtest/gtest.h" | |
| 7 #include "third_party/WebKit/WebKit/chromium/public/WebCString.h" | |
| 8 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" | |
| 9 #include "third_party/WebKit/WebKit/chromium/public/WebURLRequest.h" | |
| 10 #include "webkit/glue/plugins/webplugin_impl.h" | |
| 11 | |
| 12 using WebKit::WebHTTPBody; | |
| 13 using WebKit::WebString; | |
| 14 using WebKit::WebURLRequest; | |
| 15 using webkit_glue::WebPluginImpl; | |
| 16 | |
| 17 namespace { | |
| 18 | |
| 19 class WebPluginImplTest : public testing::Test { | |
| 20 }; | |
| 21 | |
| 22 } | |
| 23 | |
| 24 static std::string GetHeader(const WebURLRequest& request, const char* name) { | |
| 25 std::string result; | |
| 26 TrimWhitespace( | |
| 27 request.httpHeaderField(WebString::fromUTF8(name)).utf8(), | |
| 28 TRIM_ALL, | |
| 29 &result); | |
| 30 return result; | |
| 31 } | |
| 32 | |
| 33 static std::string GetBodyText(const WebURLRequest& request) { | |
| 34 const WebHTTPBody& body = request.httpBody(); | |
| 35 if (body.isNull()) | |
| 36 return std::string(); | |
| 37 | |
| 38 std::string result; | |
| 39 size_t i = 0; | |
| 40 WebHTTPBody::Element element; | |
| 41 while (body.elementAt(i++, element)) { | |
| 42 if (element.type == WebHTTPBody::Element::TypeData) { | |
| 43 result.append(element.data.data(), element.data.size()); | |
| 44 } else { | |
| 45 NOTREACHED() << "unexpected element type encountered!"; | |
| 46 } | |
| 47 } | |
| 48 return result; | |
| 49 } | |
| 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>(strlen(ex1))); | |
| 62 EXPECT_EQ(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>(strlen(ex1))); | |
| 76 EXPECT_EQ(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>(strlen(ex1))); | |
| 87 EXPECT_EQ(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>(strlen(ex1))); | |
| 110 EXPECT_EQ(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>(strlen(ex1))); | |
| 120 EXPECT_EQ(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>(strlen(ex1))); | |
| 134 EXPECT_EQ(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>(strlen(ex1))); | |
| 148 EXPECT_EQ(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>(strlen(ex1))); | |
| 159 EXPECT_EQ(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>(strlen(ex1))); | |
| 169 EXPECT_EQ(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>(strlen(ex1))); | |
| 179 EXPECT_EQ(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>(strlen(ex1))); | |
| 191 EXPECT_EQ(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>(strlen(ex1))); | |
| 204 EXPECT_EQ(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_EQ(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 } | |
| OLD | NEW |