Index: util/net/http_multipart_builder_test.cc |
diff --git a/util/net/http_multipart_builder_test.cc b/util/net/http_multipart_builder_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..022960c8a4a12b763873e6a2096782de08a77485 |
--- /dev/null |
+++ b/util/net/http_multipart_builder_test.cc |
@@ -0,0 +1,146 @@ |
+// Copyright 2014 The Crashpad Authors. All rights reserved. |
+// |
+// Licensed under the Apache License, Version 2.0 (the "License"); |
+// you may not use this file except in compliance with the License. |
+// You may obtain a copy of the License at |
+// |
+// http://www.apache.org/licenses/LICENSE-2.0 |
+// |
+// Unless required by applicable law or agreed to in writing, software |
+// distributed under the License is distributed on an "AS IS" BASIS, |
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
+// See the License for the specific language governing permissions and |
+// limitations under the License. |
+ |
+#include "util/net/http_multipart_builder.h" |
+ |
+#include <vector> |
+ |
+#include "gtest/gtest.h" |
+#include "util/net/http_body.h" |
+#include "util/net/http_body_test_util.h" |
+ |
+namespace crashpad { |
+namespace test { |
+namespace { |
+ |
+std::vector<std::string> SplitCRLF(const std::string& string) { |
+ std::vector<std::string> lines; |
+ size_t last_line = 0; |
+ for (size_t i = 0; i < string.length(); ++i) { |
+ if (string[i] == '\r' && i+1 < string.length() && string[i+1] == '\n') { |
+ lines.push_back(string.substr(last_line, i - last_line)); |
+ last_line = i + 2; |
Mark Mentovai
2014/10/28 22:34:28
You should also ++i again since you don’t want to
Robert Sesek
2014/10/29 19:52:01
Done.
|
+ } |
+ } |
+ return lines; |
Mark Mentovai
2014/10/28 22:34:28
This should signal somehow if the string didn’t en
Robert Sesek
2014/10/29 19:52:01
I don't think it's an error to not end in CRLF. Th
|
+} |
+ |
+// In the tests below, the form data pairs don’t appear in the order they were |
+// added. The current implementation uses a std::map which sorts keys, so the |
+// entires appear in alphabetical order. However, this is an implementation |
+// detail, and it’s OK if the writer stops sorting in this order. Testing for |
+// a specific order is just the easiest way to write this test while the writer |
+// will output things in a known order. |
+ |
+TEST(HTTPMultipartBuilder, ThreeStringFields) { |
+ HTTPMultipartBuilder builder; |
+ |
+ const char kKey1[] = "key1"; |
+ const char kValue1[] = "test"; |
+ builder.SetFormData(kKey1, kValue1); |
+ |
+ const char kKey2[] = "key2"; |
+ const char kValue2[] = "This is another test."; |
+ builder.SetFormData(kKey2, kValue2); |
+ |
+ const char kKey3[] = "key-three"; |
+ const char kValue3[] = "More tests"; |
+ builder.SetFormData(kKey3, kValue3); |
+ |
+ scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); |
+ ASSERT_TRUE(body.get()); |
+ std::string contents = ReadStreamToString(body.get()); |
+ auto lines = SplitCRLF(contents); |
+ auto lines_it = lines.begin(); |
+ |
+ // The first line is the boundary. All subsequent boundaries must match this. |
+ const std::string& boundary = *lines_it++; |
+ EXPECT_GT(boundary.length(), 1u); |
Mark Mentovai
2014/10/28 22:34:28
EXPECT_GE, the standard allows a boundary of one c
Robert Sesek
2014/10/29 19:52:01
Done.
|
+ EXPECT_LE(boundary.length(), 70u); |
+ |
+ EXPECT_EQ("Content-Disposition: form-data; name=\"key-three\"", *lines_it++); |
+ EXPECT_EQ("", *lines_it++); |
+ EXPECT_EQ(kValue3, *lines_it++); |
+ |
+ EXPECT_EQ(boundary, *lines_it++); |
+ EXPECT_EQ("Content-Disposition: form-data; name=\"key1\"", *lines_it++); |
+ EXPECT_EQ("", *lines_it++); |
+ EXPECT_EQ(kValue1, *lines_it++); |
+ |
+ EXPECT_EQ(boundary, *lines_it++); |
+ EXPECT_EQ("Content-Disposition: form-data; name=\"key2\"", *lines_it++); |
+ EXPECT_EQ("", *lines_it++); |
+ EXPECT_EQ(kValue2, *lines_it++); |
+ |
+ EXPECT_EQ(lines.end(), lines_it); |
+} |
+ |
+TEST(HTTPMultipartBuilder, OverwriteFormDataWithEscapedKey) { |
+ HTTPMultipartBuilder builder; |
+ const char kKey[] = "a 100% \"silly\"\r\ntest"; |
+ builder.SetFormData(kKey, "some dummy value"); |
+ builder.SetFormData(kKey, "overwrite"); |
+ scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); |
+ ASSERT_TRUE(body.get()); |
+ std::string contents = ReadStreamToString(body.get()); |
+ auto lines = SplitCRLF(contents); |
+ auto lines_it = lines.begin(); |
+ |
+ const std::string& boundary = *lines_it++; |
+ EXPECT_GT(boundary.length(), 1u); |
+ EXPECT_LE(boundary.length(), 70u); |
+ |
+ EXPECT_EQ( |
+ "Content-Disposition: form-data; name=\"a 100%25 %22silly%22%0d%0atest\"", |
+ *lines_it++); |
+ EXPECT_EQ("", *lines_it++); |
+ EXPECT_EQ("overwrite", *lines_it++); |
+ EXPECT_EQ(lines.end(), lines_it); |
+} |
+ |
+TEST(HTTPMultipartBuilder, OverwriteFileAttachment) { |
+ HTTPMultipartBuilder builder; |
+ const char kValue[] = "1 2 3 test"; |
+ builder.SetFormData("a key", kValue); |
+ // TODO(rsesek): Use a more robust mechanism to locate testdata |
+ // <https://code.google.com/p/crashpad/issues/detail?id=4>. |
+ builder.SetFileAttachment("minidump", |
+ base::FilePath("util/net/testdata/binary_http_body.dat")); |
+ builder.SetFileAttachment("minidump", |
+ base::FilePath("util/net/testdata/ascii_http_body.txt")); |
+ scoped_ptr<HTTPBodyStream> body(builder.GetBodyStream()); |
+ ASSERT_TRUE(body.get()); |
+ std::string contents = ReadStreamToString(body.get()); |
+ auto lines = SplitCRLF(contents); |
+ auto lines_it = lines.begin(); |
+ |
+ const std::string& boundary = *lines_it++; |
+ EXPECT_GT(boundary.length(), 1u); |
+ EXPECT_LE(boundary.length(), 70u); |
+ |
+ EXPECT_EQ("Content-Disposition: form-data; name=\"a key\"", *lines_it++); |
+ EXPECT_EQ("", *lines_it++); |
+ EXPECT_EQ(kValue, *lines_it++); |
+ |
+ EXPECT_EQ(boundary, *lines_it++); |
+ EXPECT_EQ("Content-Disposition: form-data; name=\"minidump\"", *lines_it++); |
+ EXPECT_EQ("", *lines_it++); |
+ EXPECT_EQ("This is a test.\n", *lines_it++); |
+ |
+ EXPECT_EQ(lines.end(), lines_it); |
+} |
+ |
+} // namespace |
+} // namespace test |
+} // namespace crashpad |