OLD | NEW |
1 // Copyright 2014 The Crashpad Authors. All rights reserved. | 1 // Copyright 2014 The Crashpad Authors. All rights reserved. |
2 // | 2 // |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
6 // | 6 // |
7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
8 // | 8 // |
9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
13 // limitations under the License. | 13 // limitations under the License. |
14 | 14 |
15 #include "util/net/http_body.h" | 15 #include "util/net/http_body.h" |
16 | 16 |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "gtest/gtest.h" | 17 #include "gtest/gtest.h" |
19 #include "util/net/http_body_test_util.h" | 18 #include "util/net/http_body_test_util.h" |
20 | 19 |
21 namespace crashpad { | 20 namespace crashpad { |
22 namespace test { | 21 namespace test { |
23 namespace { | 22 namespace { |
24 | 23 |
25 void ExpectBufferSet(const uint8_t* actual, | 24 void ExpectBufferSet(const uint8_t* actual, |
26 uint8_t expected_byte, | 25 uint8_t expected_byte, |
27 size_t num_expected_bytes) { | 26 size_t num_expected_bytes) { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 EXPECT_EQ('b', buf[1]); // Unmodified from last read. | 88 EXPECT_EQ('b', buf[1]); // Unmodified from last read. |
90 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 89 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
91 EXPECT_EQ('c', buf[0]); | 90 EXPECT_EQ('c', buf[0]); |
92 EXPECT_EQ('b', buf[1]); | 91 EXPECT_EQ('b', buf[1]); |
93 } | 92 } |
94 } | 93 } |
95 | 94 |
96 TEST(FileHTTPBodyStream, ReadASCIIFile) { | 95 TEST(FileHTTPBodyStream, ReadASCIIFile) { |
97 // TODO(rsesek): Use a more robust mechanism to locate testdata | 96 // TODO(rsesek): Use a more robust mechanism to locate testdata |
98 // <https://code.google.com/p/crashpad/issues/detail?id=4>. | 97 // <https://code.google.com/p/crashpad/issues/detail?id=4>. |
99 base::FilePath path = base::FilePath("util/net/testdata/ascii_http_body.txt"); | 98 base::FilePath path = base::FilePath( |
| 99 FILE_PATH_LITERAL("util/net/testdata/ascii_http_body.txt")); |
100 FileHTTPBodyStream stream(path); | 100 FileHTTPBodyStream stream(path); |
101 std::string contents = ReadStreamToString(&stream, 32); | 101 std::string contents = ReadStreamToString(&stream, 32); |
102 EXPECT_EQ("This is a test.\n", contents); | 102 EXPECT_EQ("This is a test.\n", contents); |
103 | 103 |
104 // Make sure that the file is not read again after it has been read to | 104 // Make sure that the file is not read again after it has been read to |
105 // completion. | 105 // completion. |
106 uint8_t buf[8]; | 106 uint8_t buf[8]; |
107 memset(buf, '!', sizeof(buf)); | 107 memset(buf, '!', sizeof(buf)); |
108 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 108 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
109 ExpectBufferSet(buf, '!', sizeof(buf)); | 109 ExpectBufferSet(buf, '!', sizeof(buf)); |
110 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 110 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
111 ExpectBufferSet(buf, '!', sizeof(buf)); | 111 ExpectBufferSet(buf, '!', sizeof(buf)); |
112 } | 112 } |
113 | 113 |
114 TEST(FileHTTPBodyStream, ReadBinaryFile) { | 114 TEST(FileHTTPBodyStream, ReadBinaryFile) { |
115 // HEX contents of file: |FEEDFACE A11A15|. | 115 // HEX contents of file: |FEEDFACE A11A15|. |
116 // TODO(rsesek): Use a more robust mechanism to locate testdata | 116 // TODO(rsesek): Use a more robust mechanism to locate testdata |
117 // <https://code.google.com/p/crashpad/issues/detail?id=4>. | 117 // <https://code.google.com/p/crashpad/issues/detail?id=4>. |
118 base::FilePath path = | 118 base::FilePath path = base::FilePath( |
119 base::FilePath("util/net/testdata/binary_http_body.dat"); | 119 FILE_PATH_LITERAL("util/net/testdata/binary_http_body.dat")); |
120 // This buffer size was chosen so that reading the file takes multiple reads. | 120 // This buffer size was chosen so that reading the file takes multiple reads. |
121 uint8_t buf[4]; | 121 uint8_t buf[4]; |
122 | 122 |
123 FileHTTPBodyStream stream(path); | 123 FileHTTPBodyStream stream(path); |
124 | 124 |
125 memset(buf, '!', sizeof(buf)); | 125 memset(buf, '!', sizeof(buf)); |
126 EXPECT_EQ(4, stream.GetBytesBuffer(buf, sizeof(buf))); | 126 EXPECT_EQ(4, stream.GetBytesBuffer(buf, sizeof(buf))); |
127 EXPECT_EQ(0xfe, buf[0]); | 127 EXPECT_EQ(0xfe, buf[0]); |
128 EXPECT_EQ(0xed, buf[1]); | 128 EXPECT_EQ(0xed, buf[1]); |
129 EXPECT_EQ(0xfa, buf[2]); | 129 EXPECT_EQ(0xfa, buf[2]); |
130 EXPECT_EQ(0xce, buf[3]); | 130 EXPECT_EQ(0xce, buf[3]); |
131 | 131 |
132 memset(buf, '!', sizeof(buf)); | 132 memset(buf, '!', sizeof(buf)); |
133 EXPECT_EQ(3, stream.GetBytesBuffer(buf, sizeof(buf))); | 133 EXPECT_EQ(3, stream.GetBytesBuffer(buf, sizeof(buf))); |
134 EXPECT_EQ(0xa1, buf[0]); | 134 EXPECT_EQ(0xa1, buf[0]); |
135 EXPECT_EQ(0x1a, buf[1]); | 135 EXPECT_EQ(0x1a, buf[1]); |
136 EXPECT_EQ(0x15, buf[2]); | 136 EXPECT_EQ(0x15, buf[2]); |
137 EXPECT_EQ('!', buf[3]); | 137 EXPECT_EQ('!', buf[3]); |
138 | 138 |
139 memset(buf, '!', sizeof(buf)); | 139 memset(buf, '!', sizeof(buf)); |
140 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 140 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
141 ExpectBufferSet(buf, '!', sizeof(buf)); | 141 ExpectBufferSet(buf, '!', sizeof(buf)); |
142 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 142 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
143 ExpectBufferSet(buf, '!', sizeof(buf)); | 143 ExpectBufferSet(buf, '!', sizeof(buf)); |
144 } | 144 } |
145 | 145 |
146 TEST(FileHTTPBodyStream, NonExistentFile) { | 146 TEST(FileHTTPBodyStream, NonExistentFile) { |
147 base::FilePath path = | 147 base::FilePath path = base::FilePath( |
148 base::FilePath("/var/empty/crashpad/util/net/http_body/null"); | 148 FILE_PATH_LITERAL("/var/empty/crashpad/util/net/http_body/null")); |
149 FileHTTPBodyStream stream(path); | 149 FileHTTPBodyStream stream(path); |
150 | 150 |
151 uint8_t buf = 0xff; | 151 uint8_t buf = 0xff; |
152 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0); | 152 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0); |
153 EXPECT_EQ(0xff, buf); | 153 EXPECT_EQ(0xff, buf); |
154 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0); | 154 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0); |
155 EXPECT_EQ(0xff, buf); | 155 EXPECT_EQ(0xff, buf); |
156 } | 156 } |
157 | 157 |
158 TEST(CompositeHTTPBodyStream, TwoEmptyStrings) { | 158 TEST(CompositeHTTPBodyStream, TwoEmptyStrings) { |
(...skipping 10 matching lines...) Expand all Loading... |
169 } | 169 } |
170 | 170 |
171 class CompositeHTTPBodyStreamBufferSize | 171 class CompositeHTTPBodyStreamBufferSize |
172 : public testing::TestWithParam<size_t> { | 172 : public testing::TestWithParam<size_t> { |
173 }; | 173 }; |
174 | 174 |
175 TEST_P(CompositeHTTPBodyStreamBufferSize, ThreeStringParts) { | 175 TEST_P(CompositeHTTPBodyStreamBufferSize, ThreeStringParts) { |
176 std::string string1("crashpad"); | 176 std::string string1("crashpad"); |
177 std::string string2("test"); | 177 std::string string2("test"); |
178 std::string string3("foobar"); | 178 std::string string3("foobar"); |
179 const size_t all_strings_length = string1.length() + string2.length() + | 179 const size_t all_strings_length = |
180 string3.length(); | 180 string1.length() + string2.length() + string3.length(); |
181 uint8_t buf[all_strings_length + 3]; | 181 std::string buf(all_strings_length + 3, '!'); |
182 memset(buf, '!', sizeof(buf)); | |
183 | 182 |
184 std::vector<HTTPBodyStream*> parts; | 183 std::vector<HTTPBodyStream*> parts; |
185 parts.push_back(new StringHTTPBodyStream(string1)); | 184 parts.push_back(new StringHTTPBodyStream(string1)); |
186 parts.push_back(new StringHTTPBodyStream(string2)); | 185 parts.push_back(new StringHTTPBodyStream(string2)); |
187 parts.push_back(new StringHTTPBodyStream(string3)); | 186 parts.push_back(new StringHTTPBodyStream(string3)); |
188 | 187 |
189 CompositeHTTPBodyStream stream(parts); | 188 CompositeHTTPBodyStream stream(parts); |
190 | 189 |
191 std::string actual_string = ReadStreamToString(&stream, GetParam()); | 190 std::string actual_string = ReadStreamToString(&stream, GetParam()); |
192 EXPECT_EQ(string1 + string2 + string3, actual_string); | 191 EXPECT_EQ(string1 + string2 + string3, actual_string); |
193 | 192 |
194 ExpectBufferSet(buf + all_strings_length, '!', | 193 ExpectBufferSet(reinterpret_cast<uint8_t*>(&buf[all_strings_length]), '!', 3); |
195 sizeof(buf) - all_strings_length); | |
196 } | 194 } |
197 | 195 |
198 TEST_P(CompositeHTTPBodyStreamBufferSize, StringsAndFile) { | 196 TEST_P(CompositeHTTPBodyStreamBufferSize, StringsAndFile) { |
199 std::string string1("Hello! "); | 197 std::string string1("Hello! "); |
200 std::string string2(" Goodbye :)"); | 198 std::string string2(" Goodbye :)"); |
201 | 199 |
202 std::vector<HTTPBodyStream*> parts; | 200 std::vector<HTTPBodyStream*> parts; |
203 parts.push_back(new StringHTTPBodyStream(string1)); | 201 parts.push_back(new StringHTTPBodyStream(string1)); |
204 parts.push_back(new FileHTTPBodyStream( | 202 parts.push_back(new FileHTTPBodyStream(base::FilePath( |
205 base::FilePath("util/net/testdata/ascii_http_body.txt"))); | 203 FILE_PATH_LITERAL("util/net/testdata/ascii_http_body.txt")))); |
206 parts.push_back(new StringHTTPBodyStream(string2)); | 204 parts.push_back(new StringHTTPBodyStream(string2)); |
207 | 205 |
208 CompositeHTTPBodyStream stream(parts); | 206 CompositeHTTPBodyStream stream(parts); |
209 | 207 |
210 std::string expected_string = string1 + "This is a test.\n" + string2; | 208 std::string expected_string = string1 + "This is a test.\n" + string2; |
211 std::string actual_string = ReadStreamToString(&stream, GetParam()); | 209 std::string actual_string = ReadStreamToString(&stream, GetParam()); |
212 EXPECT_EQ(expected_string, actual_string); | 210 EXPECT_EQ(expected_string, actual_string); |
213 } | 211 } |
214 | 212 |
215 INSTANTIATE_TEST_CASE_P(VariableBufferSize, | 213 INSTANTIATE_TEST_CASE_P(VariableBufferSize, |
216 CompositeHTTPBodyStreamBufferSize, | 214 CompositeHTTPBodyStreamBufferSize, |
217 testing::Values(1, 2, 9, 16, 31, 128, 1024)); | 215 testing::Values(1, 2, 9, 16, 31, 128, 1024)); |
218 | 216 |
219 } // namespace | 217 } // namespace |
220 } // namespace test | 218 } // namespace test |
221 } // namespace crashpad | 219 } // namespace crashpad |
OLD | NEW |