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" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "gtest/gtest.h" | 18 #include "gtest/gtest.h" |
| 19 #include "util/net/http_body_test_util.h" |
19 | 20 |
20 namespace crashpad { | 21 namespace crashpad { |
21 namespace test { | 22 namespace test { |
22 namespace { | 23 namespace { |
23 | 24 |
24 void ExpectBufferSet(const uint8_t* actual, | 25 void ExpectBufferSet(const uint8_t* actual, |
25 uint8_t expected_byte, | 26 uint8_t expected_byte, |
26 size_t num_expected_bytes) { | 27 size_t num_expected_bytes) { |
27 for (size_t i = 0; i < num_expected_bytes; ++i) { | 28 for (size_t i = 0; i < num_expected_bytes; ++i) { |
28 EXPECT_EQ(expected_byte, actual[i]) << i; | 29 EXPECT_EQ(expected_byte, actual[i]) << i; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 EXPECT_EQ('b', buf[1]); | 86 EXPECT_EQ('b', buf[1]); |
86 EXPECT_EQ(1, stream.GetBytesBuffer(buf, sizeof(buf))); | 87 EXPECT_EQ(1, stream.GetBytesBuffer(buf, sizeof(buf))); |
87 EXPECT_EQ('c', buf[0]); | 88 EXPECT_EQ('c', buf[0]); |
88 EXPECT_EQ('b', buf[1]); // Unmodified from last read. | 89 EXPECT_EQ('b', buf[1]); // Unmodified from last read. |
89 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 90 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
90 EXPECT_EQ('c', buf[0]); | 91 EXPECT_EQ('c', buf[0]); |
91 EXPECT_EQ('b', buf[1]); | 92 EXPECT_EQ('b', buf[1]); |
92 } | 93 } |
93 } | 94 } |
94 | 95 |
95 std::string ReadStreamToString(HTTPBodyStream* stream, size_t buffer_size) { | |
96 scoped_ptr<uint8_t[]> buf(new uint8_t[buffer_size]); | |
97 std::string result; | |
98 | |
99 ssize_t bytes_read; | |
100 while ((bytes_read = stream->GetBytesBuffer(buf.get(), buffer_size)) != 0) { | |
101 if (bytes_read < 0) { | |
102 ADD_FAILURE() << "Failed to read from stream: " << bytes_read; | |
103 return std::string(); | |
104 } | |
105 | |
106 result.append(reinterpret_cast<char*>(buf.get()), bytes_read); | |
107 } | |
108 | |
109 return result; | |
110 } | |
111 | |
112 TEST(FileHTTPBodyStream, ReadASCIIFile) { | 96 TEST(FileHTTPBodyStream, ReadASCIIFile) { |
113 // TODO(rsesek): Use a more robust mechanism to locate testdata | 97 // TODO(rsesek): Use a more robust mechanism to locate testdata |
114 // <https://code.google.com/p/crashpad/issues/detail?id=4>. | 98 // <https://code.google.com/p/crashpad/issues/detail?id=4>. |
115 base::FilePath path = base::FilePath("util/net/testdata/ascii_http_body.txt"); | 99 base::FilePath path = base::FilePath("util/net/testdata/ascii_http_body.txt"); |
116 FileHTTPBodyStream stream(path); | 100 FileHTTPBodyStream stream(path); |
117 std::string contents = ReadStreamToString(&stream, 32); | 101 std::string contents = ReadStreamToString(&stream, 32); |
118 EXPECT_EQ("This is a test.\n", contents); | 102 EXPECT_EQ("This is a test.\n", contents); |
119 | 103 |
120 // 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 |
121 // completion. | 105 // completion. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 EXPECT_EQ(expected_string, actual_string); | 212 EXPECT_EQ(expected_string, actual_string); |
229 } | 213 } |
230 | 214 |
231 INSTANTIATE_TEST_CASE_P(VariableBufferSize, | 215 INSTANTIATE_TEST_CASE_P(VariableBufferSize, |
232 CompositeHTTPBodyStreamBufferSize, | 216 CompositeHTTPBodyStreamBufferSize, |
233 testing::Values(1, 2, 9, 16, 31, 128)); | 217 testing::Values(1, 2, 9, 16, 31, 128)); |
234 | 218 |
235 } // namespace | 219 } // namespace |
236 } // namespace test | 220 } // namespace test |
237 } // namespace crashpad | 221 } // namespace crashpad |
OLD | NEW |