| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 #include <stddef.h> |
| 6 #include <stdint.h> |
| 7 |
| 5 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 6 #include "content/renderer/npapi/webplugin_impl.h" | 9 #include "content/renderer/npapi/webplugin_impl.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 8 #include "third_party/WebKit/public/platform/WebCString.h" | 11 #include "third_party/WebKit/public/platform/WebCString.h" |
| 9 #include "third_party/WebKit/public/platform/WebString.h" | 12 #include "third_party/WebKit/public/platform/WebString.h" |
| 10 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 13 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 11 | 14 |
| 12 using blink::WebHTTPBody; | 15 using blink::WebHTTPBody; |
| 13 using blink::WebString; | 16 using blink::WebString; |
| 14 using blink::WebURLRequest; | 17 using blink::WebURLRequest; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 49 // The Host functions for NPN_PostURL and NPN_PostURLNotify | 52 // The Host functions for NPN_PostURL and NPN_PostURLNotify |
| 50 // need to parse out some HTTP headers. Make sure it works | 53 // need to parse out some HTTP headers. Make sure it works |
| 51 // with the following tests | 54 // with the following tests |
| 52 | 55 |
| 53 TEST(WebPluginImplTest, PostParserSimple) { | 56 TEST(WebPluginImplTest, PostParserSimple) { |
| 54 // Test a simple case with headers & data | 57 // Test a simple case with headers & data |
| 55 const char *ex1 = "foo: bar\nContent-length: 10\n\nabcdefghij"; | 58 const char *ex1 = "foo: bar\nContent-length: 10\n\nabcdefghij"; |
| 56 WebURLRequest request; | 59 WebURLRequest request; |
| 57 request.initialize(); | 60 request.initialize(); |
| 58 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 61 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 59 static_cast<uint32>(strlen(ex1))); | 62 static_cast<uint32_t>(strlen(ex1))); |
| 60 EXPECT_TRUE(rv); | 63 EXPECT_TRUE(rv); |
| 61 EXPECT_EQ("bar", GetHeader(request, "foo")); | 64 EXPECT_EQ("bar", GetHeader(request, "foo")); |
| 62 EXPECT_EQ(0U, GetHeader(request, "bar").length()); | 65 EXPECT_EQ(0U, GetHeader(request, "bar").length()); |
| 63 EXPECT_EQ(0U, GetHeader(request, "Content-length").length()); | 66 EXPECT_EQ(0U, GetHeader(request, "Content-length").length()); |
| 64 EXPECT_EQ("abcdefghij", GetBodyText(request)); | 67 EXPECT_EQ("abcdefghij", GetBodyText(request)); |
| 65 } | 68 } |
| 66 | 69 |
| 67 TEST(WebPluginImplTest, PostParserLongHeader) { | 70 TEST(WebPluginImplTest, PostParserLongHeader) { |
| 68 // Test a simple case with long headers | 71 // Test a simple case with long headers |
| 69 const char *ex1 = "foo: 012345678901234567890123456789012345678901234567890123
4567890123456789012345678901234567890123456789\n\nabcdefghij"; | 72 const char *ex1 = "foo: 012345678901234567890123456789012345678901234567890123
4567890123456789012345678901234567890123456789\n\nabcdefghij"; |
| 70 WebURLRequest request; | 73 WebURLRequest request; |
| 71 request.initialize(); | 74 request.initialize(); |
| 72 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 75 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 73 static_cast<uint32>(strlen(ex1))); | 76 static_cast<uint32_t>(strlen(ex1))); |
| 74 EXPECT_TRUE(rv); | 77 EXPECT_TRUE(rv); |
| 75 EXPECT_EQ(100U, GetHeader(request, "foo").length()); | 78 EXPECT_EQ(100U, GetHeader(request, "foo").length()); |
| 76 } | 79 } |
| 77 | 80 |
| 78 TEST(WebPluginImplTest, PostParserManyHeaders) { | 81 TEST(WebPluginImplTest, PostParserManyHeaders) { |
| 79 // Test a simple case with long headers | 82 // Test a simple case with long headers |
| 80 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 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"; |
| 81 WebURLRequest request; | 84 WebURLRequest request; |
| 82 request.initialize(); | 85 request.initialize(); |
| 83 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 86 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 84 static_cast<uint32>(strlen(ex1))); | 87 static_cast<uint32_t>(strlen(ex1))); |
| 85 EXPECT_TRUE(rv); | 88 EXPECT_TRUE(rv); |
| 86 EXPECT_EQ("h1", GetHeader(request, "h1")); | 89 EXPECT_EQ("h1", GetHeader(request, "h1")); |
| 87 EXPECT_EQ("h2", GetHeader(request, "h2")); | 90 EXPECT_EQ("h2", GetHeader(request, "h2")); |
| 88 EXPECT_EQ("h3", GetHeader(request, "h3")); | 91 EXPECT_EQ("h3", GetHeader(request, "h3")); |
| 89 EXPECT_EQ("h4", GetHeader(request, "h4")); | 92 EXPECT_EQ("h4", GetHeader(request, "h4")); |
| 90 EXPECT_EQ("h5", GetHeader(request, "h5")); | 93 EXPECT_EQ("h5", GetHeader(request, "h5")); |
| 91 EXPECT_EQ("h6", GetHeader(request, "h6")); | 94 EXPECT_EQ("h6", GetHeader(request, "h6")); |
| 92 EXPECT_EQ("h7", GetHeader(request, "h7")); | 95 EXPECT_EQ("h7", GetHeader(request, "h7")); |
| 93 EXPECT_EQ("h8", GetHeader(request, "h8")); | 96 EXPECT_EQ("h8", GetHeader(request, "h8")); |
| 94 EXPECT_EQ("h9", GetHeader(request, "h9")); | 97 EXPECT_EQ("h9", GetHeader(request, "h9")); |
| 95 EXPECT_EQ("h10", GetHeader(request, "h10")); | 98 EXPECT_EQ("h10", GetHeader(request, "h10")); |
| 96 EXPECT_EQ("body", GetBodyText(request)); | 99 EXPECT_EQ("body", GetBodyText(request)); |
| 97 } | 100 } |
| 98 | 101 |
| 99 TEST(WebPluginImplTest, PostParserDuplicateHeaders) { | 102 TEST(WebPluginImplTest, PostParserDuplicateHeaders) { |
| 100 // Test a simple case with long headers | 103 // Test a simple case with long headers |
| 101 // What value gets returned doesn't really matter. It shouldn't error | 104 // What value gets returned doesn't really matter. It shouldn't error |
| 102 // out. | 105 // out. |
| 103 const char *ex1 = "h1:h1\nh1:h2\n\nbody"; | 106 const char *ex1 = "h1:h1\nh1:h2\n\nbody"; |
| 104 WebURLRequest request; | 107 WebURLRequest request; |
| 105 request.initialize(); | 108 request.initialize(); |
| 106 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 109 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 107 static_cast<uint32>(strlen(ex1))); | 110 static_cast<uint32_t>(strlen(ex1))); |
| 108 EXPECT_TRUE(rv); | 111 EXPECT_TRUE(rv); |
| 109 } | 112 } |
| 110 | 113 |
| 111 TEST(WebPluginImplTest, PostParserNoHeaders) { | 114 TEST(WebPluginImplTest, PostParserNoHeaders) { |
| 112 // Test a simple case with no headers but with data | 115 // Test a simple case with no headers but with data |
| 113 const char *ex1 = "\nabcdefghij"; | 116 const char *ex1 = "\nabcdefghij"; |
| 114 WebURLRequest request; | 117 WebURLRequest request; |
| 115 request.initialize(); | 118 request.initialize(); |
| 116 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 119 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 117 static_cast<uint32>(strlen(ex1))); | 120 static_cast<uint32_t>(strlen(ex1))); |
| 118 EXPECT_TRUE(rv); | 121 EXPECT_TRUE(rv); |
| 119 EXPECT_EQ(0U, GetHeader(request, "foo").length()); | 122 EXPECT_EQ(0U, GetHeader(request, "foo").length()); |
| 120 EXPECT_EQ(0U, GetHeader(request, "bar").length()); | 123 EXPECT_EQ(0U, GetHeader(request, "bar").length()); |
| 121 EXPECT_EQ(0U, GetHeader(request, "Content-length").length()); | 124 EXPECT_EQ(0U, GetHeader(request, "Content-length").length()); |
| 122 EXPECT_EQ("abcdefghij", GetBodyText(request)); | 125 EXPECT_EQ("abcdefghij", GetBodyText(request)); |
| 123 } | 126 } |
| 124 | 127 |
| 125 TEST(WebPluginImplTest, PostParserNoBody) { | 128 TEST(WebPluginImplTest, PostParserNoBody) { |
| 126 // Test a simple case with headers and no body | 129 // Test a simple case with headers and no body |
| 127 const char *ex1 = "Foo:bar\n\n"; | 130 const char *ex1 = "Foo:bar\n\n"; |
| 128 WebURLRequest request; | 131 WebURLRequest request; |
| 129 request.initialize(); | 132 request.initialize(); |
| 130 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 133 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 131 static_cast<uint32>(strlen(ex1))); | 134 static_cast<uint32_t>(strlen(ex1))); |
| 132 EXPECT_TRUE(rv); | 135 EXPECT_TRUE(rv); |
| 133 EXPECT_EQ("bar", GetHeader(request, "foo")); | 136 EXPECT_EQ("bar", GetHeader(request, "foo")); |
| 134 EXPECT_EQ(0U, GetHeader(request, "bar").length()); | 137 EXPECT_EQ(0U, GetHeader(request, "bar").length()); |
| 135 EXPECT_EQ(0U, GetHeader(request, "Content-length").length()); | 138 EXPECT_EQ(0U, GetHeader(request, "Content-length").length()); |
| 136 EXPECT_EQ(0U, GetBodyText(request).length()); | 139 EXPECT_EQ(0U, GetBodyText(request).length()); |
| 137 } | 140 } |
| 138 | 141 |
| 139 TEST(WebPluginImplTest, PostParserBodyWithNewLines) { | 142 TEST(WebPluginImplTest, PostParserBodyWithNewLines) { |
| 140 // Test a simple case with headers and no body | 143 // Test a simple case with headers and no body |
| 141 const char *ex1 = "Foo:bar\n\n\n\nabcdefg\n\nabcdefg"; | 144 const char *ex1 = "Foo:bar\n\n\n\nabcdefg\n\nabcdefg"; |
| 142 WebURLRequest request; | 145 WebURLRequest request; |
| 143 request.initialize(); | 146 request.initialize(); |
| 144 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 147 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 145 static_cast<uint32>(strlen(ex1))); | 148 static_cast<uint32_t>(strlen(ex1))); |
| 146 EXPECT_TRUE(rv); | 149 EXPECT_TRUE(rv); |
| 147 EXPECT_EQ(GetBodyText(request), "\n\nabcdefg\n\nabcdefg"); | 150 EXPECT_EQ(GetBodyText(request), "\n\nabcdefg\n\nabcdefg"); |
| 148 } | 151 } |
| 149 | 152 |
| 150 TEST(WebPluginImplTest, PostParserErrorNoBody) { | 153 TEST(WebPluginImplTest, PostParserErrorNoBody) { |
| 151 // Test with headers and no body | 154 // Test with headers and no body |
| 152 const char *ex1 = "Foo:bar\n"; | 155 const char *ex1 = "Foo:bar\n"; |
| 153 WebURLRequest request; | 156 WebURLRequest request; |
| 154 request.initialize(); | 157 request.initialize(); |
| 155 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 158 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 156 static_cast<uint32>(strlen(ex1))); | 159 static_cast<uint32_t>(strlen(ex1))); |
| 157 EXPECT_TRUE(rv); | 160 EXPECT_TRUE(rv); |
| 158 } | 161 } |
| 159 | 162 |
| 160 TEST(WebPluginImplTest, PostParserErrorEmpty) { | 163 TEST(WebPluginImplTest, PostParserErrorEmpty) { |
| 161 // Test with an empty string | 164 // Test with an empty string |
| 162 const char *ex1 = ""; | 165 const char *ex1 = ""; |
| 163 WebURLRequest request; | 166 WebURLRequest request; |
| 164 request.initialize(); | 167 request.initialize(); |
| 165 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 168 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 166 static_cast<uint32>(strlen(ex1))); | 169 static_cast<uint32_t>(strlen(ex1))); |
| 167 EXPECT_TRUE(rv); | 170 EXPECT_TRUE(rv); |
| 168 } | 171 } |
| 169 | 172 |
| 170 TEST(WebPluginImplTest, PostParserEmptyName) { | 173 TEST(WebPluginImplTest, PostParserEmptyName) { |
| 171 // Test an error case with an empty header name field | 174 // Test an error case with an empty header name field |
| 172 const char *ex1 = "foo:bar\n:blat\n\nbody"; | 175 const char *ex1 = "foo:bar\n:blat\n\nbody"; |
| 173 WebURLRequest request; | 176 WebURLRequest request; |
| 174 request.initialize(); | 177 request.initialize(); |
| 175 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 178 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 176 static_cast<uint32>(strlen(ex1))); | 179 static_cast<uint32_t>(strlen(ex1))); |
| 177 EXPECT_TRUE(rv); | 180 EXPECT_TRUE(rv); |
| 178 EXPECT_EQ("bar", GetHeader(request, "foo")); | 181 EXPECT_EQ("bar", GetHeader(request, "foo")); |
| 179 EXPECT_EQ("body", GetBodyText(request)); | 182 EXPECT_EQ("body", GetBodyText(request)); |
| 180 } | 183 } |
| 181 | 184 |
| 182 TEST(WebPluginImplTest, PostParserEmptyValue) { | 185 TEST(WebPluginImplTest, PostParserEmptyValue) { |
| 183 // Test an error case with an empty value field | 186 // Test an error case with an empty value field |
| 184 const char *ex1 = "foo:bar\nbar:\n\nbody"; | 187 const char *ex1 = "foo:bar\nbar:\n\nbody"; |
| 185 WebURLRequest request; | 188 WebURLRequest request; |
| 186 request.initialize(); | 189 request.initialize(); |
| 187 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 190 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 188 static_cast<uint32>(strlen(ex1))); | 191 static_cast<uint32_t>(strlen(ex1))); |
| 189 EXPECT_TRUE(rv); | 192 EXPECT_TRUE(rv); |
| 190 EXPECT_EQ("bar", GetHeader(request, "foo")); | 193 EXPECT_EQ("bar", GetHeader(request, "foo")); |
| 191 EXPECT_EQ(0U, GetHeader(request, "bar").length()); | 194 EXPECT_EQ(0U, GetHeader(request, "bar").length()); |
| 192 EXPECT_EQ("body", GetBodyText(request)); | 195 EXPECT_EQ("body", GetBodyText(request)); |
| 193 } | 196 } |
| 194 | 197 |
| 195 TEST(WebPluginImplTest, PostParserCRLF) { | 198 TEST(WebPluginImplTest, PostParserCRLF) { |
| 196 // Test an error case with an empty value field | 199 // Test an error case with an empty value field |
| 197 const char *ex1 = "foo: bar\r\nbar:\r\n\r\nbody\r\n\r\nbody2"; | 200 const char *ex1 = "foo: bar\r\nbar:\r\n\r\nbody\r\n\r\nbody2"; |
| 198 WebURLRequest request; | 201 WebURLRequest request; |
| 199 request.initialize(); | 202 request.initialize(); |
| 200 bool rv = WebPluginImpl::SetPostData(&request, ex1, | 203 bool rv = WebPluginImpl::SetPostData(&request, ex1, |
| 201 static_cast<uint32>(strlen(ex1))); | 204 static_cast<uint32_t>(strlen(ex1))); |
| 202 EXPECT_TRUE(rv); | 205 EXPECT_TRUE(rv); |
| 203 EXPECT_EQ("bar", GetHeader(request, "foo")); | 206 EXPECT_EQ("bar", GetHeader(request, "foo")); |
| 204 EXPECT_EQ(0U, GetHeader(request, "bar").length()); | 207 EXPECT_EQ(0U, GetHeader(request, "bar").length()); |
| 205 EXPECT_EQ("body\r\n\r\nbody2", GetBodyText(request)); | 208 EXPECT_EQ("body\r\n\r\nbody2", GetBodyText(request)); |
| 206 } | 209 } |
| 207 | 210 |
| 208 TEST(WebPluginImplTest, PostParserBodyWithBinaryData) { | 211 TEST(WebPluginImplTest, PostParserBodyWithBinaryData) { |
| 209 // Test a simple case with headers and binary data. | 212 // Test a simple case with headers and binary data. |
| 210 char ex1[33] = "foo: bar\nContent-length: 10\n\n"; | 213 char ex1[33] = "foo: bar\nContent-length: 10\n\n"; |
| 211 unsigned int binary_data = 0xFFFFFFF0; | 214 unsigned int binary_data = 0xFFFFFFF0; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 223 | 226 |
| 224 std::string body = GetBodyText(request); | 227 std::string body = GetBodyText(request); |
| 225 | 228 |
| 226 EXPECT_EQ(0xF0, (unsigned char)body[0]); | 229 EXPECT_EQ(0xF0, (unsigned char)body[0]); |
| 227 EXPECT_EQ(0xFF, (unsigned char)body[1]); | 230 EXPECT_EQ(0xFF, (unsigned char)body[1]); |
| 228 EXPECT_EQ(0xFF, (unsigned char)body[2]); | 231 EXPECT_EQ(0xFF, (unsigned char)body[2]); |
| 229 EXPECT_EQ(0xFF, (unsigned char)body[3]); | 232 EXPECT_EQ(0xFF, (unsigned char)body[3]); |
| 230 } | 233 } |
| 231 | 234 |
| 232 } // namespace content | 235 } // namespace content |
| OLD | NEW |