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