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

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: 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 "gtest/gtest.h"
18
19 namespace crashpad {
20 namespace test {
21 namespace {
22
23 std::string ReadStreamToString(HTTPBodyStream* stream) {
Mark Mentovai 2014/10/23 19:31:04 I would move this down to above ReadASCIIFile, sin
Robert Sesek 2014/10/24 16:53:34 Done.
24 uint8_t buf[32];
25 std::string result;
26
27 while (stream->HasBytesRemaining()) {
28 ssize_t bytes_read = stream->GetBytesBuffer(buf, sizeof(buf));
29 if (bytes_read == 0) {
30 break;
31 } else if (bytes_read < 0) {
32 ADD_FAILURE() << "Failed to read from stream: " << bytes_read;
33 return std::string();
34 }
35
36 result.append(reinterpret_cast<char*>(buf), bytes_read);
37 }
38
39 return result;
40 }
41
42 void ExpectStringBuffer(const std::string& expected,
Mark Mentovai 2014/10/23 19:31:04 Ditto, above SmallString.
Robert Sesek 2014/10/24 16:53:33 Done.
43 const uint8_t* actual,
44 size_t actual_len) {
45 for (size_t i = 0; i < actual_len; ++i) {
Mark Mentovai 2014/10/23 19:31:04 You could just do: std::string actual_string(re
Robert Sesek 2014/10/24 16:53:33 Done.
46 ASSERT_LT(i, expected.length());
47 EXPECT_EQ(expected[i], actual[i]);
48 }
49 }
50
51 TEST(StringHTTPBodyStreamTest, EmptyString) {
Mark Mentovai 2014/10/23 19:31:04 Except for death tests, the test class name never
Robert Sesek 2014/10/24 16:53:34 Done.
52 uint8_t buf[32];
53 memset(buf, '!', sizeof(buf));
Mark Mentovai 2014/10/23 19:31:04 #include <string.h>
Robert Sesek 2014/10/24 16:53:34 Done.
54
55 std::string empty_string;
56 StringHTTPBodyStream stream(empty_string);
57 EXPECT_FALSE(stream.HasBytesRemaining());
58 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
59 EXPECT_EQ('!', buf[0]);
Mark Mentovai 2014/10/23 19:31:04 Check the whole (remainder) of the buffer. Maybe y
Robert Sesek 2014/10/24 16:53:33 Done.
60 EXPECT_FALSE(stream.HasBytesRemaining());
61 }
62
63 TEST(StringHTTPBodyStreamTest, SmallString) {
64 uint8_t buf[32];
65 memset(buf, '!', sizeof(buf));
66
67 std::string string("Hello, world");
68 StringHTTPBodyStream stream(string);
69 EXPECT_TRUE(stream.HasBytesRemaining());
70 EXPECT_EQ(static_cast<ssize_t>(string.length()),
71 stream.GetBytesBuffer(buf, sizeof(buf)));
72
73 EXPECT_EQ('!', buf[string.length()]);
74 ExpectStringBuffer(string, buf, string.length());
75
76 EXPECT_FALSE(stream.HasBytesRemaining());
77 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
78 }
79
80 TEST(StringHTTPBodyStreamTest, MultipleReads) {
81 uint8_t buf[2];
82 memset(buf, '!', sizeof(buf));
83
84 {
85 std::string string("test");
86 SCOPED_TRACE("aligned buffer boundary");
87
88 StringHTTPBodyStream stream(string);
89 EXPECT_TRUE(stream.HasBytesRemaining());
90 EXPECT_EQ(2, stream.GetBytesBuffer(buf, sizeof(buf)));
91 EXPECT_EQ('t', buf[0]);
92 EXPECT_EQ('e', buf[1]);
93 EXPECT_TRUE(stream.HasBytesRemaining());
94 EXPECT_EQ(2, stream.GetBytesBuffer(buf, sizeof(buf)));
95 EXPECT_EQ('s', buf[0]);
96 EXPECT_EQ('t', buf[1]);
97 EXPECT_FALSE(stream.HasBytesRemaining());
98 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
99 EXPECT_EQ('s', buf[0]);
100 EXPECT_EQ('t', buf[1]);
101 }
102
103 {
104 std::string string("abc");
105 SCOPED_TRACE("unaligned buffer boundary");
106
107 StringHTTPBodyStream stream(string);
108 EXPECT_TRUE(stream.HasBytesRemaining());
109 EXPECT_EQ(2, stream.GetBytesBuffer(buf, sizeof(buf)));
110 EXPECT_EQ('a', buf[0]);
111 EXPECT_EQ('b', buf[1]);
112 EXPECT_TRUE(stream.HasBytesRemaining());
113 EXPECT_EQ(1, stream.GetBytesBuffer(buf, sizeof(buf)));
114 EXPECT_EQ('c', buf[0]);
115 EXPECT_EQ('b', buf[1]); // Unmodified from last read.
116 EXPECT_FALSE(stream.HasBytesRemaining());
117 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
118 EXPECT_EQ('c', buf[0]);
119 EXPECT_EQ('b', buf[1]);
120 }
121 }
122
123 TEST(FileHTTPBodyStreamTest, ReadASCIIFile) {
124 base::FilePath path = base::FilePath("util/net/testdata/ascii_http_body.txt");
Mark Mentovai 2014/10/23 19:31:04 This is fine for now, but I filed bug 4 to do some
Robert Sesek 2014/10/24 16:53:34 Done.
125 FileHTTPBodyStream stream(path);
126 EXPECT_TRUE(stream.HasBytesRemaining());
127 std::string contents = ReadStreamToString(&stream);
128 EXPECT_EQ("This is a test.\n", contents);
129 EXPECT_FALSE(stream.HasBytesRemaining());
130 }
131
132 TEST(FileHTTPBodyStreamTest, ReadBinaryFile) {
133 // HEX contents of file: |FEEDFACE A11A15|.
134 base::FilePath path =
135 base::FilePath("util/net/testdata/binary_http_body.dat");
136 uint8_t buf[4];
Mark Mentovai 2014/10/23 19:31:04 Comment that the buffer size was intentionally cho
Robert Sesek 2014/10/24 16:53:34 Done.
137
138 FileHTTPBodyStream stream(path);
139
140 EXPECT_TRUE(stream.HasBytesRemaining());
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 EXPECT_TRUE(stream.HasBytesRemaining());
148
149 memset(buf, '!', sizeof(buf));
150 EXPECT_EQ(3, stream.GetBytesBuffer(buf, sizeof(buf)));
151 EXPECT_EQ(0xa1, buf[0]);
152 EXPECT_EQ(0x1a, buf[1]);
153 EXPECT_EQ(0x15, buf[2]);
154 EXPECT_EQ('!', buf[3]);
155 EXPECT_TRUE(stream.HasBytesRemaining());
156
157 memset(buf, '!', sizeof(buf));
158 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
159 EXPECT_FALSE(stream.HasBytesRemaining());
160 for (size_t i = 0; i < sizeof(buf); ++i)
Mark Mentovai 2014/10/23 19:31:04 If you had a !-untouched verifier helper, you coul
Robert Sesek 2014/10/24 16:53:34 Done.
161 EXPECT_EQ('!', buf[i]) << i;
162 }
163
164 TEST(FileHTTPBodyStreamTest, NonExistentFile) {
165 base::FilePath path = base::FilePath("/tmp/crashpad/util/net/http_body/null");
Mark Mentovai 2014/10/23 19:31:04 /var/empty, not /tmp
Robert Sesek 2014/10/24 16:53:34 Done.
166 FileHTTPBodyStream stream(path);
167 EXPECT_TRUE(stream.HasBytesRemaining());
168
169 uint8_t buf = 0xff;
170 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0);
171 EXPECT_EQ(0xff, buf);
172 EXPECT_FALSE(stream.HasBytesRemaining());
173 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0);
174 EXPECT_EQ(0xff, buf);
175 EXPECT_FALSE(stream.HasBytesRemaining());
176 }
177
178 TEST(CompositeHTTPBodyStreamTest, TwoEmptyStrings) {
179 std::vector<HTTPBodyStream*> parts;
180 parts.push_back(new StringHTTPBodyStream(std::string()));
181 parts.push_back(new StringHTTPBodyStream(std::string()));
182
183 CompositeHTTPBodyStream stream(parts);
184 EXPECT_FALSE(stream.HasBytesRemaining());
Mark Mentovai 2014/10/23 19:31:04 All of your tests call HasBytesRemaining() before
Robert Sesek 2014/10/24 16:53:34 HasBytesRemaining() is now gone.
185
186 uint8_t buf[5];
187 memset(buf, '!', sizeof(buf));
188 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf)));
189 for (size_t i = 0; i < sizeof(buf); ++i)
190 EXPECT_EQ('!', buf[i]) << i;
191
192 EXPECT_FALSE(stream.HasBytesRemaining());
193 }
194
195 TEST(CompositeHTTPBodyStreamTest, ThreeStringParts) {
196 std::string string1("crashpad");
197 std::string string2("test");
198 std::string string3("foobar");
199 const size_t all_strings_length = string1.length() + string2.length() +
200 string3.length();
201 uint8_t buf[all_strings_length + 3];
202 memset(buf, '!', sizeof(buf));
203
204 std::vector<HTTPBodyStream*> parts;
205 parts.push_back(new StringHTTPBodyStream(string1));
206 parts.push_back(new StringHTTPBodyStream(string2));
207 parts.push_back(new StringHTTPBodyStream(string3));
208
209 CompositeHTTPBodyStream stream(parts);
210 EXPECT_TRUE(stream.HasBytesRemaining());
211
212 std::string actual_string = ReadStreamToString(&stream);
213 EXPECT_EQ(string1 + string2 + string3, actual_string);
214
215 EXPECT_EQ('!', buf[all_strings_length]);
216 EXPECT_EQ('!', buf[all_strings_length + 1]);
217 EXPECT_EQ('!', buf[all_strings_length + 2]);
218
219 EXPECT_FALSE(stream.HasBytesRemaining());
220 }
221
222 TEST(CompositeHTTPBodyStreamTest, StringsAndFile) {
223 std::string string1("Hello! ");
224 std::string string2(" Goodbye :)");
225
226 std::vector<HTTPBodyStream*> parts;
227 parts.push_back(new StringHTTPBodyStream(string1));
228 parts.push_back(new FileHTTPBodyStream(
229 base::FilePath("util/net/testdata/ascii_http_body.txt")));
230 parts.push_back(new StringHTTPBodyStream(string2));
231
232 CompositeHTTPBodyStream stream(parts);
233 EXPECT_TRUE(stream.HasBytesRemaining());
234
235 std::string expected_string = string1 + "This is a test.\n" + string2;
236 std::string actual_string = ReadStreamToString(&stream);
Mark Mentovai 2014/10/23 19:31:04 ReadStreamToString will get the whole thing in thr
Robert Sesek 2014/10/24 16:53:34 Done. Now have a parameterized test for various bu
237 EXPECT_EQ(expected_string, actual_string);
238
239 EXPECT_FALSE(stream.HasBytesRemaining());
240 }
241
242 } // namespace
243 } // namespace test
244 } // namespace crashpad
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698