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

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: Address comments Created 6 years, 1 month 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 ssize_t bytes_read;
100 while ((bytes_read = stream->GetBytesBuffer(buf.get(), buffer_size)) != 0) {
101 if (bytes_read == 0) {
Mark Mentovai 2014/10/24 18:58:21 Get rid of this now.
Robert Sesek 2014/10/24 19:02:47 I did, but I forgot to save before uploading. Done
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)));
127 ExpectBufferSet(buf, '!', sizeof(buf));
128 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
129 ExpectBufferSet(buf, '!', sizeof(buf));
130 }
131
132 TEST(FileHTTPBodyStream, ReadBinaryFile) {
133 // HEX contents of file: |FEEDFACE A11A15|.
134 // TODO(rsesek): Use a more robust mechanism to locate testdata
135 // <https://code.google.com/p/crashpad/issues/detail?id=4>.
136 base::FilePath path =
137 base::FilePath("util/net/testdata/binary_http_body.dat");
138 // This buffer size was chosen so that reading the file takes multiple reads.
139 uint8_t buf[4];
140
141 FileHTTPBodyStream stream(path);
142
143 memset(buf, '!', sizeof(buf));
144 EXPECT_EQ(4, stream.GetBytesBuffer(buf, sizeof(buf)));
145 EXPECT_EQ(0xfe, buf[0]);
146 EXPECT_EQ(0xed, buf[1]);
147 EXPECT_EQ(0xfa, buf[2]);
148 EXPECT_EQ(0xce, buf[3]);
149
150 memset(buf, '!', sizeof(buf));
151 EXPECT_EQ(3, stream.GetBytesBuffer(buf, sizeof(buf)));
152 EXPECT_EQ(0xa1, buf[0]);
153 EXPECT_EQ(0x1a, buf[1]);
154 EXPECT_EQ(0x15, buf[2]);
155 EXPECT_EQ('!', buf[3]);
156
157 memset(buf, '!', sizeof(buf));
158 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
159 ExpectBufferSet(buf, '!', sizeof(buf));
160 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
161 ExpectBufferSet(buf, '!', sizeof(buf));
162 }
163
164 TEST(FileHTTPBodyStream, NonExistentFile) {
165 base::FilePath path =
166 base::FilePath("/var/empty/crashpad/util/net/http_body/null");
167 FileHTTPBodyStream stream(path);
168
169 uint8_t buf = 0xff;
170 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0);
171 EXPECT_EQ(0xff, buf);
172 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0);
173 EXPECT_EQ(0xff, buf);
174 }
175
176 TEST(CompositeHTTPBodyStream, TwoEmptyStrings) {
177 std::vector<HTTPBodyStream*> parts;
178 parts.push_back(new StringHTTPBodyStream(std::string()));
179 parts.push_back(new StringHTTPBodyStream(std::string()));
180
181 CompositeHTTPBodyStream stream(parts);
182
183 uint8_t buf[5];
184 memset(buf, '!', sizeof(buf));
185 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
186 ExpectBufferSet(buf, '!', sizeof(buf));
187 }
188
189 class CompositeHTTPBodyStreamBufferSize
190 : public testing::TestWithParam<size_t> {
191 };
192
193 TEST_P(CompositeHTTPBodyStreamBufferSize, ThreeStringParts) {
194 std::string string1("crashpad");
195 std::string string2("test");
196 std::string string3("foobar");
197 const size_t all_strings_length = string1.length() + string2.length() +
198 string3.length();
199 uint8_t buf[all_strings_length + 3];
200 memset(buf, '!', sizeof(buf));
201
202 std::vector<HTTPBodyStream*> parts;
203 parts.push_back(new StringHTTPBodyStream(string1));
204 parts.push_back(new StringHTTPBodyStream(string2));
205 parts.push_back(new StringHTTPBodyStream(string3));
206
207 CompositeHTTPBodyStream stream(parts);
208
209 std::string actual_string = ReadStreamToString(&stream, GetParam());
210 EXPECT_EQ(string1 + string2 + string3, actual_string);
211
212 ExpectBufferSet(buf + all_strings_length, '!',
213 sizeof(buf) - all_strings_length);
214 }
215
216 TEST_P(CompositeHTTPBodyStreamBufferSize, StringsAndFile) {
217 std::string string1("Hello! ");
218 std::string string2(" Goodbye :)");
219
220 std::vector<HTTPBodyStream*> parts;
221 parts.push_back(new StringHTTPBodyStream(string1));
222 parts.push_back(new FileHTTPBodyStream(
223 base::FilePath("util/net/testdata/ascii_http_body.txt")));
224 parts.push_back(new StringHTTPBodyStream(string2));
225
226 CompositeHTTPBodyStream stream(parts);
227
228 std::string expected_string = string1 + "This is a test.\n" + string2;
229 std::string actual_string = ReadStreamToString(&stream, GetParam());
230 EXPECT_EQ(expected_string, actual_string);
231 }
232
233 INSTANTIATE_TEST_CASE_P(VariableBufferSize,
234 CompositeHTTPBodyStreamBufferSize,
235 testing::Values(1, 2, 9, 16, 31, 128));
236
237 } // namespace
238 } // namespace test
239 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698