OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/web_request/post_data_parser.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 |
| 8 namespace { |
| 9 |
| 10 // Attempts to run the parser corresponding to the |content_type_header| |
| 11 // on the source represented by the concatenation of blocks from |bytes|. |
| 12 // On success, returns true and the parsed |output|, else false. |
| 13 // Parsed |output| has names on even positions (0, 2, ...), values on odd ones. |
| 14 bool RunParser(const std::string& content_type_header, |
| 15 const std::vector<const std::vector<char>* >& bytes, |
| 16 std::vector<std::string>* output) { |
| 17 if (output == NULL) |
| 18 return false; |
| 19 output->clear(); |
| 20 scoped_ptr<extensions::FormDataParser> parser( |
| 21 extensions::FormDataParser::Create(&content_type_header)); |
| 22 if (parser.get() == NULL) |
| 23 return false; |
| 24 extensions::FormDataParser::Result result; |
| 25 for (size_t block = 0; block < bytes.size(); ++block) { |
| 26 if (!parser->SetSource(bytes[block])) |
| 27 return false; |
| 28 while (parser->GetNextNameValue(&result)) { |
| 29 output->push_back(result.name()); |
| 30 output->push_back(result.value()); |
| 31 } |
| 32 } |
| 33 return parser->AllDataReadOK(); |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 38 class ExtensionWebRequestFormDataParserTest : public testing::Test { |
| 39 public: |
| 40 ExtensionWebRequestFormDataParserTest() {} |
| 41 |
| 42 protected: |
| 43 virtual void SetUp() OVERRIDE {} |
| 44 }; |
| 45 |
| 46 TEST_F(ExtensionWebRequestFormDataParserTest, Parsing) { |
| 47 // We verify that POST data parsers cope with various formats of POST data. |
| 48 // Construct the test data. |
| 49 #define kBoundary "THIS_IS_A_BOUNDARY" |
| 50 #define kBlockStr1 "--" kBoundary "\r\n" \ |
| 51 "Content-Disposition: form-data; name=\"text\"\r\n" \ |
| 52 "\r\n" \ |
| 53 "test text\r\n" \ |
| 54 "--" kBoundary "\r\n" \ |
| 55 "Content-Disposition: form-data; name=\"file\"; filename=\"test\"\r\n" \ |
| 56 "Content-Type: application/octet-stream\r\n" \ |
| 57 "\r\n" |
| 58 #define kBlockStr2 "\r\n" \ |
| 59 "--" kBoundary "\r\n" \ |
| 60 "Content-Disposition: form-data; name=\"password\"\r\n" \ |
| 61 "\r\n" \ |
| 62 "test password\r\n" \ |
| 63 "--" kBoundary "\r\n" \ |
| 64 "Content-Disposition: form-data; name=\"radio\"\r\n" \ |
| 65 "\r\n" \ |
| 66 "Yes\r\n" \ |
| 67 "--" kBoundary "\r\n" \ |
| 68 "Content-Disposition: form-data; name=\"check\"\r\n" \ |
| 69 "\r\n" \ |
| 70 "option A\r\n" \ |
| 71 "--" kBoundary "\r\n" \ |
| 72 "Content-Disposition: form-data; name=\"check\"\r\n" \ |
| 73 "\r\n" \ |
| 74 "option B\r\n" \ |
| 75 "--" kBoundary "\r\n" \ |
| 76 "Content-Disposition: form-data; name=\"txtarea\"\r\n" \ |
| 77 "\r\n" \ |
| 78 "Some text.\r\n" \ |
| 79 "Other.\r\n" \ |
| 80 "\r\n" \ |
| 81 "--" kBoundary "\r\n" \ |
| 82 "Content-Disposition: form-data; name=\"select\"\r\n" \ |
| 83 "\r\n" \ |
| 84 "one\r\n" \ |
| 85 "--" kBoundary "--" |
| 86 // POST data input. |
| 87 const char kBigBlock[] = kBlockStr1 kBlockStr2; |
| 88 const char kBlock1[] = kBlockStr1; |
| 89 const char kBlock2[] = kBlockStr2; |
| 90 const char kUrlEncodedBlock[] = "text=test+text&file=test" |
| 91 "&password=test+password&radio=Yes&check=option+A&check=option+B" |
| 92 "&txtarea=Some+text.%0D%0AOther.%0D%0A&select=one"; |
| 93 #define BYTES_FROM_BLOCK(bytes, block) \ |
| 94 const std::vector<char> bytes(block, block + strlen(block)) |
| 95 BYTES_FROM_BLOCK(kMultipartBytes, kBigBlock); |
| 96 BYTES_FROM_BLOCK(kMultipartBytesSplit1, kBlock1); |
| 97 BYTES_FROM_BLOCK(kMultipartBytesSplit2, kBlock2); |
| 98 BYTES_FROM_BLOCK(kUrlEncodedBytes, kUrlEncodedBlock); |
| 99 const std::vector<char> kTextPlainBytes; |
| 100 #undef BYTES_FROM_BLOCK |
| 101 // Headers. |
| 102 const char kUrlEncoded[] = "application/x-www-form-urlencoded"; |
| 103 const char kTextPlain[] = "text/plain"; |
| 104 const char kMultipart[] = "multipart/form-data; boundary=" kBoundary; |
| 105 #undef kBlockStr2 |
| 106 #undef kBlockStr1 |
| 107 #undef kBoundary |
| 108 // Expected output. |
| 109 const char* kPairs[] = { |
| 110 "text", "test text", |
| 111 "file", "test", |
| 112 "password", "test password", |
| 113 "radio", "Yes", |
| 114 "check", "option A", |
| 115 "check", "option B", |
| 116 "txtarea", "Some text.\r\nOther.\r\n", |
| 117 "select", "one" |
| 118 }; |
| 119 const std::vector<std::string> kExpected(kPairs, kPairs + arraysize(kPairs)); |
| 120 |
| 121 std::vector<const std::vector<char>* > input; |
| 122 std::vector<std::string> output; |
| 123 |
| 124 // First test: multipart POST data in one lump. |
| 125 input.push_back(&kMultipartBytes); |
| 126 EXPECT_TRUE(RunParser(kMultipart, input, &output)); |
| 127 EXPECT_EQ(kExpected, output); |
| 128 |
| 129 // Second test: multipart POST data in several lumps. |
| 130 input.clear(); |
| 131 input.push_back(&kMultipartBytesSplit1); |
| 132 input.push_back(&kMultipartBytesSplit2); |
| 133 EXPECT_TRUE(RunParser(kMultipart, input, &output)); |
| 134 EXPECT_EQ(kExpected, output); |
| 135 |
| 136 // Third test: URL-encoded POST data. |
| 137 input.clear(); |
| 138 input.push_back(&kUrlEncodedBytes); |
| 139 EXPECT_TRUE(RunParser(kUrlEncoded, input, &output)); |
| 140 EXPECT_EQ(kExpected, output); |
| 141 |
| 142 // Fourth test: text/plain POST data in one lump. |
| 143 input.clear(); |
| 144 input.push_back(&kTextPlainBytes); |
| 145 // This should fail, text/plain is ambiguous and thus unparseable. |
| 146 EXPECT_FALSE(RunParser(kTextPlain, input, &output)); |
| 147 } |
OLD | NEW |