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, |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
89 EXPECT_EQ('b', buf[1]); // Unmodified from last read. | 89 EXPECT_EQ('b', buf[1]); // Unmodified from last read. |
90 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 90 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
91 EXPECT_EQ('c', buf[0]); | 91 EXPECT_EQ('c', buf[0]); |
92 EXPECT_EQ('b', buf[1]); | 92 EXPECT_EQ('b', buf[1]); |
93 } | 93 } |
94 } | 94 } |
95 | 95 |
96 TEST(FileHTTPBodyStream, ReadASCIIFile) { | 96 TEST(FileHTTPBodyStream, ReadASCIIFile) { |
97 // TODO(rsesek): Use a more robust mechanism to locate testdata | 97 // TODO(rsesek): Use a more robust mechanism to locate testdata |
98 // <https://code.google.com/p/crashpad/issues/detail?id=4>. | 98 // <https://code.google.com/p/crashpad/issues/detail?id=4>. |
99 base::FilePath path = base::FilePath("util/net/testdata/ascii_http_body.txt"); | 99 base::FilePath path = base::FilePath( |
100 FILE_PATH_LITERAL("util/net/testdata/ascii_http_body.txt")); | |
100 FileHTTPBodyStream stream(path); | 101 FileHTTPBodyStream stream(path); |
101 std::string contents = ReadStreamToString(&stream, 32); | 102 std::string contents = ReadStreamToString(&stream, 32); |
102 EXPECT_EQ("This is a test.\n", contents); | 103 EXPECT_EQ("This is a test.\n", contents); |
103 | 104 |
104 // Make sure that the file is not read again after it has been read to | 105 // Make sure that the file is not read again after it has been read to |
105 // completion. | 106 // completion. |
106 uint8_t buf[8]; | 107 uint8_t buf[8]; |
107 memset(buf, '!', sizeof(buf)); | 108 memset(buf, '!', sizeof(buf)); |
108 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 109 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
109 ExpectBufferSet(buf, '!', sizeof(buf)); | 110 ExpectBufferSet(buf, '!', sizeof(buf)); |
110 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 111 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
111 ExpectBufferSet(buf, '!', sizeof(buf)); | 112 ExpectBufferSet(buf, '!', sizeof(buf)); |
112 } | 113 } |
113 | 114 |
114 TEST(FileHTTPBodyStream, ReadBinaryFile) { | 115 TEST(FileHTTPBodyStream, ReadBinaryFile) { |
115 // HEX contents of file: |FEEDFACE A11A15|. | 116 // HEX contents of file: |FEEDFACE A11A15|. |
116 // TODO(rsesek): Use a more robust mechanism to locate testdata | 117 // TODO(rsesek): Use a more robust mechanism to locate testdata |
117 // <https://code.google.com/p/crashpad/issues/detail?id=4>. | 118 // <https://code.google.com/p/crashpad/issues/detail?id=4>. |
118 base::FilePath path = | 119 base::FilePath path = base::FilePath( |
119 base::FilePath("util/net/testdata/binary_http_body.dat"); | 120 FILE_PATH_LITERAL("util/net/testdata/binary_http_body.dat")); |
120 // This buffer size was chosen so that reading the file takes multiple reads. | 121 // This buffer size was chosen so that reading the file takes multiple reads. |
121 uint8_t buf[4]; | 122 uint8_t buf[4]; |
122 | 123 |
123 FileHTTPBodyStream stream(path); | 124 FileHTTPBodyStream stream(path); |
124 | 125 |
125 memset(buf, '!', sizeof(buf)); | 126 memset(buf, '!', sizeof(buf)); |
126 EXPECT_EQ(4, stream.GetBytesBuffer(buf, sizeof(buf))); | 127 EXPECT_EQ(4, stream.GetBytesBuffer(buf, sizeof(buf))); |
127 EXPECT_EQ(0xfe, buf[0]); | 128 EXPECT_EQ(0xfe, buf[0]); |
128 EXPECT_EQ(0xed, buf[1]); | 129 EXPECT_EQ(0xed, buf[1]); |
129 EXPECT_EQ(0xfa, buf[2]); | 130 EXPECT_EQ(0xfa, buf[2]); |
130 EXPECT_EQ(0xce, buf[3]); | 131 EXPECT_EQ(0xce, buf[3]); |
131 | 132 |
132 memset(buf, '!', sizeof(buf)); | 133 memset(buf, '!', sizeof(buf)); |
133 EXPECT_EQ(3, stream.GetBytesBuffer(buf, sizeof(buf))); | 134 EXPECT_EQ(3, stream.GetBytesBuffer(buf, sizeof(buf))); |
134 EXPECT_EQ(0xa1, buf[0]); | 135 EXPECT_EQ(0xa1, buf[0]); |
135 EXPECT_EQ(0x1a, buf[1]); | 136 EXPECT_EQ(0x1a, buf[1]); |
136 EXPECT_EQ(0x15, buf[2]); | 137 EXPECT_EQ(0x15, buf[2]); |
137 EXPECT_EQ('!', buf[3]); | 138 EXPECT_EQ('!', buf[3]); |
138 | 139 |
139 memset(buf, '!', sizeof(buf)); | 140 memset(buf, '!', sizeof(buf)); |
140 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 141 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
141 ExpectBufferSet(buf, '!', sizeof(buf)); | 142 ExpectBufferSet(buf, '!', sizeof(buf)); |
142 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); | 143 EXPECT_EQ(0, stream.GetBytesBuffer(buf, sizeof(buf))); |
143 ExpectBufferSet(buf, '!', sizeof(buf)); | 144 ExpectBufferSet(buf, '!', sizeof(buf)); |
144 } | 145 } |
145 | 146 |
146 TEST(FileHTTPBodyStream, NonExistentFile) { | 147 TEST(FileHTTPBodyStream, NonExistentFile) { |
147 base::FilePath path = | 148 base::FilePath path = base::FilePath( |
148 base::FilePath("/var/empty/crashpad/util/net/http_body/null"); | 149 FILE_PATH_LITERAL("/var/empty/crashpad/util/net/http_body/null")); |
149 FileHTTPBodyStream stream(path); | 150 FileHTTPBodyStream stream(path); |
150 | 151 |
151 uint8_t buf = 0xff; | 152 uint8_t buf = 0xff; |
152 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0); | 153 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0); |
153 EXPECT_EQ(0xff, buf); | 154 EXPECT_EQ(0xff, buf); |
154 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0); | 155 EXPECT_LT(stream.GetBytesBuffer(&buf, 1), 0); |
155 EXPECT_EQ(0xff, buf); | 156 EXPECT_EQ(0xff, buf); |
156 } | 157 } |
157 | 158 |
158 TEST(CompositeHTTPBodyStream, TwoEmptyStrings) { | 159 TEST(CompositeHTTPBodyStream, TwoEmptyStrings) { |
(...skipping 10 matching lines...) Expand all Loading... | |
169 } | 170 } |
170 | 171 |
171 class CompositeHTTPBodyStreamBufferSize | 172 class CompositeHTTPBodyStreamBufferSize |
172 : public testing::TestWithParam<size_t> { | 173 : public testing::TestWithParam<size_t> { |
173 }; | 174 }; |
174 | 175 |
175 TEST_P(CompositeHTTPBodyStreamBufferSize, ThreeStringParts) { | 176 TEST_P(CompositeHTTPBodyStreamBufferSize, ThreeStringParts) { |
176 std::string string1("crashpad"); | 177 std::string string1("crashpad"); |
177 std::string string2("test"); | 178 std::string string2("test"); |
178 std::string string3("foobar"); | 179 std::string string3("foobar"); |
179 const size_t all_strings_length = string1.length() + string2.length() + | 180 const size_t all_strings_length = |
Mark Mentovai
2015/01/07 22:47:27
We could make this constexpr, but then it’d have t
scottmg
2015/01/07 23:01:11
No constexpr support in VS2013. :(
| |
180 string3.length(); | 181 string1.length() + string2.length() + string3.length(); |
181 uint8_t buf[all_strings_length + 3]; | 182 scoped_ptr<uint8_t[]> buf(new uint8_t[all_strings_length + 3]); |
Mark Mentovai
2015/01/07 22:47:27
Maybe it’s more natural to make this std::string b
scottmg
2015/01/07 23:01:11
Done.
| |
182 memset(buf, '!', sizeof(buf)); | 183 memset(buf.get(), '!', all_strings_length + 3); |
183 | 184 |
184 std::vector<HTTPBodyStream*> parts; | 185 std::vector<HTTPBodyStream*> parts; |
185 parts.push_back(new StringHTTPBodyStream(string1)); | 186 parts.push_back(new StringHTTPBodyStream(string1)); |
186 parts.push_back(new StringHTTPBodyStream(string2)); | 187 parts.push_back(new StringHTTPBodyStream(string2)); |
187 parts.push_back(new StringHTTPBodyStream(string3)); | 188 parts.push_back(new StringHTTPBodyStream(string3)); |
188 | 189 |
189 CompositeHTTPBodyStream stream(parts); | 190 CompositeHTTPBodyStream stream(parts); |
190 | 191 |
191 std::string actual_string = ReadStreamToString(&stream, GetParam()); | 192 std::string actual_string = ReadStreamToString(&stream, GetParam()); |
192 EXPECT_EQ(string1 + string2 + string3, actual_string); | 193 EXPECT_EQ(string1 + string2 + string3, actual_string); |
193 | 194 |
194 ExpectBufferSet(buf + all_strings_length, '!', | 195 ExpectBufferSet(buf.get() + all_strings_length, '!', 3); |
195 sizeof(buf) - all_strings_length); | |
196 } | 196 } |
197 | 197 |
198 TEST_P(CompositeHTTPBodyStreamBufferSize, StringsAndFile) { | 198 TEST_P(CompositeHTTPBodyStreamBufferSize, StringsAndFile) { |
199 std::string string1("Hello! "); | 199 std::string string1("Hello! "); |
200 std::string string2(" Goodbye :)"); | 200 std::string string2(" Goodbye :)"); |
201 | 201 |
202 std::vector<HTTPBodyStream*> parts; | 202 std::vector<HTTPBodyStream*> parts; |
203 parts.push_back(new StringHTTPBodyStream(string1)); | 203 parts.push_back(new StringHTTPBodyStream(string1)); |
204 parts.push_back(new FileHTTPBodyStream( | 204 parts.push_back(new FileHTTPBodyStream(base::FilePath( |
205 base::FilePath("util/net/testdata/ascii_http_body.txt"))); | 205 FILE_PATH_LITERAL("util/net/testdata/ascii_http_body.txt")))); |
206 parts.push_back(new StringHTTPBodyStream(string2)); | 206 parts.push_back(new StringHTTPBodyStream(string2)); |
207 | 207 |
208 CompositeHTTPBodyStream stream(parts); | 208 CompositeHTTPBodyStream stream(parts); |
209 | 209 |
210 std::string expected_string = string1 + "This is a test.\n" + string2; | 210 std::string expected_string = string1 + "This is a test.\n" + string2; |
211 std::string actual_string = ReadStreamToString(&stream, GetParam()); | 211 std::string actual_string = ReadStreamToString(&stream, GetParam()); |
212 EXPECT_EQ(expected_string, actual_string); | 212 EXPECT_EQ(expected_string, actual_string); |
213 } | 213 } |
214 | 214 |
215 INSTANTIATE_TEST_CASE_P(VariableBufferSize, | 215 INSTANTIATE_TEST_CASE_P(VariableBufferSize, |
216 CompositeHTTPBodyStreamBufferSize, | 216 CompositeHTTPBodyStreamBufferSize, |
217 testing::Values(1, 2, 9, 16, 31, 128, 1024)); | 217 testing::Values(1, 2, 9, 16, 31, 128, 1024)); |
218 | 218 |
219 } // namespace | 219 } // namespace |
220 } // namespace test | 220 } // namespace test |
221 } // namespace crashpad | 221 } // namespace crashpad |
OLD | NEW |