Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(792)

Side by Side Diff: util/net/http_body_test.cc

Issue 669153006: Add HTTPBodyStream interface, three concrete implementations, and their tests. (Closed) Base URL: https://chromium.googlesource.com/crashpad/crashpad@master
Patch Set: Close and read error logging Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Crashpad Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "util/net/http_body.h"
16
17 #include "base/memory/scoped_ptr.h"
18 #include "gtest/gtest.h"
19
20 namespace crashpad {
21 namespace test {
22 namespace {
23
24 void ExpectBufferSet(const uint8_t* actual,
25 uint8_t expected_byte,
26 size_t num_expected_bytes) {
27 for (size_t i = 0; i < num_expected_bytes; ++i) {
28 EXPECT_EQ(expected_byte, actual[i]) << i;
29 }
30 }
31
32 TEST(StringHTTPBodyStream, EmptyString) {
33 uint8_t buf[32];
34 memset(buf, '!', sizeof(buf));
35
36 std::string empty_string;
37 StringHTTPBodyStream stream(empty_string);
38 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
39 ExpectBufferSet(buf, '!', sizeof(buf));
40 }
41
42 TEST(StringHTTPBodyStream, SmallString) {
43 uint8_t buf[32];
44 memset(buf, '!', sizeof(buf));
45
46 std::string string("Hello, world");
47 StringHTTPBodyStream stream(string);
48 EXPECT_EQ(static_cast<ssize_t>(string.length()),
49 stream.GetBytesBuffer(buf, sizeof(buf)));
50
51 std::string actual(reinterpret_cast<const char*>(buf), string.length());
52 EXPECT_EQ(string, actual);
53 ExpectBufferSet(buf + string.length(), '!', sizeof(buf) - string.length());
54
55 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
56 }
57
58 TEST(StringHTTPBodyStream, MultipleReads) {
59 uint8_t buf[2];
60 memset(buf, '!', sizeof(buf));
61
62 {
63 std::string string("test");
64 SCOPED_TRACE("aligned buffer boundary");
65
66 StringHTTPBodyStream stream(string);
67 EXPECT_EQ(2, stream.GetBytesBuffer(buf, sizeof(buf)));
68 EXPECT_EQ('t', buf[0]);
69 EXPECT_EQ('e', buf[1]);
70 EXPECT_EQ(2, stream.GetBytesBuffer(buf, sizeof(buf)));
71 EXPECT_EQ('s', buf[0]);
72 EXPECT_EQ('t', buf[1]);
73 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
74 EXPECT_EQ('s', buf[0]);
75 EXPECT_EQ('t', buf[1]);
76 }
77
78 {
79 std::string string("abc");
80 SCOPED_TRACE("unaligned buffer boundary");
81
82 StringHTTPBodyStream stream(string);
83 EXPECT_EQ(2, stream.GetBytesBuffer(buf, sizeof(buf)));
84 EXPECT_EQ('a', buf[0]);
85 EXPECT_EQ('b', buf[1]);
86 EXPECT_EQ(1, stream.GetBytesBuffer(buf, sizeof(buf)));
87 EXPECT_EQ('c', buf[0]);
88 EXPECT_EQ('b', buf[1]); // Unmodified from last read.
89 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
90 EXPECT_EQ('c', buf[0]);
91 EXPECT_EQ('b', buf[1]);
92 }
93 }
94
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 while (true) {
Mark Mentovai 2014/10/24 17:46:58 Optional, but this could have been ssize_t byte
Robert Sesek 2014/10/24 18:45:31 Done.
100 ssize_t bytes_read = stream->GetBytesBuffer(buf.get(), buffer_size);
101 if (bytes_read == 0) {
102 break;
103 } else if (bytes_read < 0) {
104 ADD_FAILURE() << "Failed to read from stream: " << bytes_read;
105 return std::string();
106 }
107
108 result.append(reinterpret_cast<char*>(buf.get()), bytes_read);
109 }
110
111 return result;
112 }
113
114 TEST(FileHTTPBodyStream, ReadASCIIFile) {
115 // TODO(rsesek): Use a more robust mechanism to locate testdata
116 // <https://code.google.com/p/crashpad/issues/detail?id=4>.
117 base::FilePath path = base::FilePath("util/net/testdata/ascii_http_body.txt");
118 FileHTTPBodyStream stream(path);
119 std::string contents = ReadStreamToString(&stream, 32);
120 EXPECT_EQ("This is a test.\n", contents);
121
122 // Make sure that the file is not read again after it has been read to
123 // completion.
124 uint8_t buf[8];
125 memset(buf, '!', sizeof(buf));
126 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
Mark Mentovai 2014/10/24 17:46:58 Do this twice to make sure that it actually return
Robert Sesek 2014/10/24 18:45:31 Done.
127 ExpectBufferSet(buf, '!', sizeof(buf));
128 }
129
130 TEST(FileHTTPBodyStream, ReadBinaryFile) {
131 // HEX contents of file: |FEEDFACE A11A15|.
132 // TODO(rsesek): Use a more robust mechanism to locate testdata
133 // <https://code.google.com/p/crashpad/issues/detail?id=4>.
134 base::FilePath path =
135 base::FilePath("util/net/testdata/binary_http_body.dat");
136 // This buffer size was chosen so that reading the file takes multiple reads.
137 uint8_t buf[4];
138
139 FileHTTPBodyStream stream(path);
140
141 memset(buf, '!', sizeof(buf));
142 EXPECT_EQ(4, stream.GetBytesBuffer(buf, sizeof(buf)));
143 EXPECT_EQ(0xfe, buf[0]);
144 EXPECT_EQ(0xed, buf[1]);
145 EXPECT_EQ(0xfa, buf[2]);
146 EXPECT_EQ(0xce, buf[3]);
147
148 memset(buf, '!', sizeof(buf));
149 EXPECT_EQ(3, stream.GetBytesBuffer(buf, sizeof(buf)));
150 EXPECT_EQ(0xa1, buf[0]);
151 EXPECT_EQ(0x1a, buf[1]);
152 EXPECT_EQ(0x15, buf[2]);
153 EXPECT_EQ('!', buf[3]);
154
155 memset(buf, '!', sizeof(buf));
156 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
157 ExpectBufferSet(buf, '!', sizeof(buf));
158 }
159
160 TEST(FileHTTPBodyStream, NonExistentFile) {
161 base::FilePath path =
162 base::FilePath("/var/empty/crashpad/util/net/http_body/null");
163 FileHTTPBodyStream stream(path);
164
165 uint8_t buf = 0xff;
166 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0);
167 EXPECT_EQ(0xff, buf);
168 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0);
169 EXPECT_EQ(0xff, buf);
170 }
171
172 TEST(CompositeHTTPBodyStream, TwoEmptyStrings) {
173 std::vector<HTTPBodyStream*> parts;
174 parts.push_back(new StringHTTPBodyStream(std::string()));
175 parts.push_back(new StringHTTPBodyStream(std::string()));
176
177 CompositeHTTPBodyStream stream(parts);
178
179 uint8_t buf[5];
180 memset(buf, '!', sizeof(buf));
181 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
182 ExpectBufferSet(buf, '!', sizeof(buf));
183 }
184
185 class CompositeHTTPBodyStreamBufferSize
186 : public testing::TestWithParam<size_t> {
187 };
188
189 TEST_P(CompositeHTTPBodyStreamBufferSize, ThreeStringParts) {
190 std::string string1("crashpad");
191 std::string string2("test");
192 std::string string3("foobar");
193 const size_t all_strings_length = string1.length() + string2.length() +
194 string3.length();
195 uint8_t buf[all_strings_length + 3];
196 memset(buf, '!', sizeof(buf));
197
198 std::vector<HTTPBodyStream*> parts;
199 parts.push_back(new StringHTTPBodyStream(string1));
200 parts.push_back(new StringHTTPBodyStream(string2));
201 parts.push_back(new StringHTTPBodyStream(string3));
202
203 CompositeHTTPBodyStream stream(parts);
204
205 std::string actual_string = ReadStreamToString(&stream, GetParam());
206 EXPECT_EQ(string1 + string2 + string3, actual_string);
207
208 ExpectBufferSet(buf + all_strings_length, '!',
209 sizeof(buf) - all_strings_length);
210 }
211
212 TEST_P(CompositeHTTPBodyStreamBufferSize, StringsAndFile) {
213 std::string string1("Hello! ");
214 std::string string2(" Goodbye :)");
215
216 std::vector<HTTPBodyStream*> parts;
217 parts.push_back(new StringHTTPBodyStream(string1));
218 parts.push_back(new FileHTTPBodyStream(
219 base::FilePath("util/net/testdata/ascii_http_body.txt")));
220 parts.push_back(new StringHTTPBodyStream(string2));
221
222 CompositeHTTPBodyStream stream(parts);
223
224 std::string expected_string = string1 + "This is a test.\n" + string2;
225 std::string actual_string = ReadStreamToString(&stream, GetParam());
226 EXPECT_EQ(expected_string, actual_string);
227 }
228
229 INSTANTIATE_TEST_CASE_P(VariableBufferSize,
230 CompositeHTTPBodyStreamBufferSize,
231 testing::Values(1, 2, 9, 16, 31, 128));
Mark Mentovai 2014/10/24 17:46:58 Awesome.
232
233 } // namespace
234 } // namespace test
235 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698