Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/ptr_util.h" | 5 #include "base/memory/ptr_util.h" |
| 6 #include "base/strings/string_number_conversions.h" | |
| 6 #include "base/sys_byteorder.h" | 7 #include "base/sys_byteorder.h" |
| 7 #include "blimp/net/common.h" | 8 #include "blimp/net/common.h" |
| 8 #include "blimp/net/compressed_packet_reader.h" | 9 #include "blimp/net/compressed_packet_reader.h" |
| 9 #include "blimp/net/compressed_packet_writer.h" | 10 #include "blimp/net/compressed_packet_writer.h" |
| 10 #include "blimp/net/test_common.h" | 11 #include "blimp/net/test_common.h" |
| 11 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
| 12 #include "testing/gmock/include/gmock/gmock.h" | 13 #include "testing/gmock/include/gmock/gmock.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 15 |
| 15 using testing::_; | 16 using testing::_; |
| 16 using testing::DoAll; | 17 using testing::DoAll; |
| 17 using testing::InvokeArgument; | 18 using testing::InvokeArgument; |
| 18 using testing::SaveArg; | 19 using testing::SaveArg; |
| 19 | 20 |
| 20 namespace blimp { | 21 namespace blimp { |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 24 // A payload containing a large volume of high-entropy/incompressible data. | |
| 25 const char kLargeIncompressiblePayload[] = | |
|
Wez
2016/04/12 23:54:34
Would it work for us to replace this with an equiv
Kevin M
2016/04/13 00:53:34
Done.
| |
| 26 "083F1009823F931208011214290A516B88E88CF38C31E8CDEE11D838D27D51501AF8115249" | |
| 27 "4646F008000057454250565038580A00000010000000B100003B0000414C50486B03000001" | |
| 28 "8740906D3383CD9FF7778888A4B503C4469214493D8C3BD3FEBBFBF7D5300E44F47F02BAC7" | |
| 29 "4C60EAF248BA29D7DED7DEFBE3FFCFDEB3D744F28E4712A72E0EC41E3B3259DE34D261D3D5" | |
| 30 "A280F12C0A87A595DC5D85AD91D97C653F83A593DC25D60C2F30EFE824B3F40BFE864A7D83" | |
| 31 "6F75118EE446F27754D623CCDDC36269217F91759FE16A5758DAE981A6549EE16C15AE64D3" | |
| 32 "0B5B69BE73AC160B4F7821B0F27D87AB4D6169A2172A7220CE4639D2CF58E6151D93702595" | |
| 33 "94BA72FE67216343C388D030249C2C360B276991F1454A878E1FA22A69068D855F78A3301A" | |
| 34 "32C4C91175C1D28B577023BD31904384754F74B0ADB75998E991834CA20D7DAE06C66A8D85" | |
| 35 "9D1E498C56A20E71F4545DC42B58F44A871251C69AA7ECE263FC84673EE4121161FB99ABD4" | |
| 36 "19BF895E898CAE3F0BE2E0A8625B27B1B0D233156A7F1A561D0D6CE87C8249EF2C28FD89D8" | |
| 37 "F2132E56543AE31FBD13183DF4F340ECA7337C4823317EE343059ABF0656BC54C6BBCA1664" | |
| 38 "32CBBA3F26547E156C3AE98C1FD2688C77B253FE71A1F08B2E743D8476589834CAC50EBD94" | |
| 39 "18FDE8DF0571D6EBBFF76171258DC3C2FCD280FA7F151B7AFA8354C4273C74A0F45FC48EBB" | |
| 40 "4A2EB8BF1319BD047E10275F2793138ECF3468221DEB9E6E27D46A3FB3A18A24EC73740239" | |
| 41 "E2F24860382274218E7EF8364F27BC51A18FE0893547CCD311F7371674377CB0E58AA7C5EA" | |
| 42 "028E365D97E842A6C11577BD2FD0116C1B522EECB52AD1CFDC8EE446AD9B888A80CB03D3CD" | |
| 43 "3221A276319E5A8588680B6EF077DC5C2B2A028E3A8DFE26010F7789FD162B1A82AE32E9F7" | |
| 44 "1470F2D61D4DB370B1A392FF0957B0BD7D8EAE194D8C93057501575F913D27B32468267404" | |
| 45 "37B86AAE86191D6CDB14010F570B5B59DC05C76E626C435BC0C95160BC90380A38995541B6" | |
| 46 "4992CF511104197D82669604DD86A6809B9F896D526C82CF8C2EF619852BB8C1CDC5BA4612" | |
| 47 "70345B18071BEA029E5E32E349838EA099354131A223E0EC6460975487609925C1B02A92CF" | |
| 48 "C9C1A64E1170B0A28B7D56B405DC5C44C6AB0E5D41355B1807AB24B9C1431344A525586655" | |
| 49 "50AC680A7879F8B08F94ABE09A45C1340B57C0D92E303EB482808B157DD831A32E39765590" | |
| 50 "B5E8134CB3867132A323E06EB6B04BEA4D70CDA2A0D915C98D56175B7A51C0C98A3676EC68" | |
| 51 "0B7851C64521E3518FB230868C2BC48C878C078D98A50400565038205E050000B024009D01" | |
| 52 "2AB2003C003E75329247A4A321A13DF71800900E89690015E37FD8FF87F749FD77F45B9A9F" | |
| 53 "D2FF355FC00FC40E007F58BF05F921F92DCF3ED5FFE1BF9BEFDE724FE2FFABBE2E1FAEFE00" | |
| 54 "7B81F44FF9DFA90FE75FEE7C803FC778A67917B007F07FE75FE77EED7E907F49FF4BFD4BF2" | |
| 55 "47D8BFE29FCEBFF07F9CF804FE29FCDBFE47F7FFF1BEE01EA03F693D82FF5EBFEFA8E402C7" | |
| 56 "68AF5EF341EB3FA3DCE9F7868278FFDA1DE84775141A8BC1D7CF7F595F7586FE4B3E5C4B29" | |
| 57 "6819ABF8615E8F2C5765F38001840AB09B8B3906B749AF4466D0D1FF8219A21292B7FADE4A" | |
| 58 "1E7C883D403B17FDFDF0F439E73EC907E33DE328E22C595082826C1C5E4A1B37BE52C44083" | |
| 59 "4B93C1B29FADD72AF7F33521EB3F845156D854D5AFD41B5C5E2CA24C5E65D12317D7419992" | |
| 60 "0000FECB60EDF4C2FB91DE2D415123E0114D081CFAD2A379F91BC5C36E3A6A81016A3D2374" | |
| 61 "734E7820478CE92F44CE91DE8B8F2F278E1A69CDF8F3A0F8EA28674011715F27B23E6CED65" | |
| 62 "32C34B38292A3E3256070D611FDC27664F7341DA07B7E08430B1AD0CF0B90F062676D5C561" | |
| 63 "4F4EA33200C6BDEACDA0509BE17D91479B9DB75E4561D39EFCA1C5C3C447AB37B0084DB3D2" | |
| 64 "0AB40FFD4B563E572DF093FFF1CFFB8446F3F9A80F5399C4260DAB3F416844694A54F0191C" | |
| 65 "EBBFD12FDB1F89AE633B2F050470CF1D9BE8B6613000E48E8786B387D9AD1B8A9E518FDF35" | |
| 66 "97A6D9950413E16E635924086AC20A24371B652A6F097998EBAD8552FED0834F7619A04D44" | |
| 67 "4CFEAA4B8934606144AF1A55CAEC8AAB15647304A7348432DC170A87F844DA416E24EE8D74" | |
| 68 "BFF17F7EF5B68232F000D1D6A413FF03D859C5F63721C042FFA41C80934CBFBFBB14DE254D" | |
| 69 "4F370FC2400613882E68F3A6E5412968167DC7ED2F169514CE98887126ECC3E362C58CDA29" | |
| 70 "53FCE87BCC58BA3243DBD02FDF8EE1EE1EF70CE022CB15EF3788A735C1440D0B06C7444215" | |
| 71 "819B009315A1C9C8D3752B20F7AD7F0B37C56585F70C0A0629787CD1FB2F1D049181BB81C4" | |
| 72 "48859C7B6CB71B41E3F0887B95277F9E88152B44A495D7F013B3D91A36344818F86CB7CF50" | |
| 73 "77FE1C866DB8B70F410CC3E1C6B1F06DDF5FBCBE8626AC6AA8FF12B250B054720ED0C7A0E3" | |
| 74 "0E5C9AC588C10F9C832F9C4CC9F284B584CFC1A9E790DFA8E6FC8954551E7E478AF1C19DF1" | |
| 75 "59E1DEAD8E594EE3880F736EFAFDE5F43135627EF89DBD1CEA436A6BA7F2A2C61E97F5B19C" | |
| 76 "E6A3051F7E56D93ECAC3FA3D421DE597D0F10BF18F198AEAB22589A008B3A39D14A84B088E" | |
| 77 "468F802D11AE9D31AB03769F6C7B042200DA84432CC28767B9A4277C7042CD1BA365D7BB49" | |
| 78 "E4522A9AC6F4C1C39682CF5ADC02CA2DCB69851B01E3285750E9DD711B57B73521EF302F99" | |
| 79 "748DFD7AF8E07E00729D38EFC5CDF564AB43D03CE29E4DCDB0DBF1F655C1EE42D11442BB89" | |
| 80 "F1B8DE2B4C69C122399873CCC6E079BEC4013EBB653B8E2018F4A770D6A0929C0BDC815983" | |
| 81 "F297F630238397604288AB009F83C753177B75DC66295859059CCD4A0000CE4E1BB111DAFC" | |
| 82 "7EA1DF2A12A9D8FA175C358694DDD099A14F167FEA47D3B432F6A695A0946C8765316A5B74" | |
| 83 "4CEDA7D47D655AAA8974BBBD2F034A959066B07ABA12209DE149D7C3701D5DEB37702BF460" | |
| 84 "53DFABAB2DC61F3738A0F124F0F97C16B67C4115EABADAC770D2E88C133BA3C7B13AE8E85F" | |
| 85 "04E0239939918344B534EE0F3712849F82CF66C7A15F07C96DD41AE693FDFE1EF1BB62D0E0" | |
| 86 "42D3A2C9694BC2288B11AEF65EB7BC1312C33414CDB8CA0A8C3DBCCFC1CD262D88764FF006" | |
| 87 "4812023CC07EE0E6E8576F7225E1D3631C1E0C19AD4B41774DEC1707B78E776C107548E40C" | |
| 88 "5E55A54321A5D0EDFED3550DC7206E33011E819EE830D560ACA14D978639AD904593D0000" | |
| 89 "0"; | |
| 90 | |
| 23 // Calls the CompletionCallback (indicated by the 0-based index cb_idx) | 91 // Calls the CompletionCallback (indicated by the 0-based index cb_idx) |
| 24 // with the value |result|. | 92 // with the value |result|. |
| 25 ACTION_TEMPLATE(InvokeCompletionCallback, | 93 ACTION_TEMPLATE(InvokeCompletionCallback, |
| 26 HAS_1_TEMPLATE_PARAMS(int, cb_idx), | 94 HAS_1_TEMPLATE_PARAMS(int, cb_idx), |
| 27 AND_1_VALUE_PARAMS(result)) { | 95 AND_1_VALUE_PARAMS(result)) { |
| 28 testing::get<cb_idx>(args).Run(result); | 96 testing::get<cb_idx>(args).Run(result); |
| 29 } | 97 } |
| 30 | 98 |
| 31 // Copies a DrainableIOBuffer to a GrowableIOBuffer. | 99 // Copies a DrainableIOBuffer to a GrowableIOBuffer. |
| 32 // |dest_buf_idx|: The 0-based index of a GrowableIOBuffer. | 100 // |dest_buf_idx|: The 0-based index of a GrowableIOBuffer. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 62 scoped_refptr<net::DrainableIOBuffer> content_buf( | 130 scoped_refptr<net::DrainableIOBuffer> content_buf( |
| 63 new net::DrainableIOBuffer(content_str_buf.get(), | 131 new net::DrainableIOBuffer(content_str_buf.get(), |
| 64 content_str_buf->size())); | 132 content_str_buf->size())); |
| 65 scoped_refptr<net::DrainableIOBuffer> compressed_buf; | 133 scoped_refptr<net::DrainableIOBuffer> compressed_buf; |
| 66 EXPECT_CALL(*mock_writer_, WritePacket(_, _)) | 134 EXPECT_CALL(*mock_writer_, WritePacket(_, _)) |
| 67 .WillOnce(DoAll(SaveArg<0>(&compressed_buf), | 135 .WillOnce(DoAll(SaveArg<0>(&compressed_buf), |
| 68 InvokeCompletionCallback<1>(net::OK))); | 136 InvokeCompletionCallback<1>(net::OK))); |
| 69 net::TestCompletionCallback completion_cb_1; | 137 net::TestCompletionCallback completion_cb_1; |
| 70 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); | 138 compressed_writer_->WritePacket(content_buf, completion_cb_1.callback()); |
| 71 EXPECT_EQ(net::OK, completion_cb_1.WaitForResult()); | 139 EXPECT_EQ(net::OK, completion_cb_1.WaitForResult()); |
| 140 LOG(ERROR) << "Compressed size: " << compressed_buf->BytesRemaining(); | |
|
Wez
2016/04/12 23:54:34
Ick! Nasty, noisy test! Shoo! Shoo!
Kevin M
2016/04/13 00:53:34
Done.
| |
| 72 return std::string(compressed_buf->data(), | 141 return std::string(compressed_buf->data(), |
| 73 compressed_buf->BytesRemaining()); | 142 compressed_buf->BytesRemaining()); |
| 74 } | 143 } |
| 75 | 144 |
| 76 // Returns the decompressed result of |compressed|. | 145 // Returns the decompressed result of |compressed|. |
| 77 std::string Decompress(const std::string& compressed) { | 146 std::string Decompress(const std::string& compressed) { |
| 78 scoped_refptr<net::StringIOBuffer> compressed_str_buf( | 147 scoped_refptr<net::StringIOBuffer> compressed_str_buf( |
| 79 new net::StringIOBuffer(compressed)); | 148 new net::StringIOBuffer(compressed)); |
| 80 scoped_refptr<net::DrainableIOBuffer> compressed_buf( | 149 scoped_refptr<net::DrainableIOBuffer> compressed_buf( |
| 81 new net::DrainableIOBuffer(compressed_str_buf.get(), | 150 new net::DrainableIOBuffer(compressed_str_buf.get(), |
| 82 compressed_str_buf->size())); | 151 compressed_str_buf->size())); |
| 83 scoped_refptr<net::GrowableIOBuffer> decompressed_buf( | 152 scoped_refptr<net::GrowableIOBuffer> decompressed_buf( |
| 84 new net::GrowableIOBuffer); | 153 new net::GrowableIOBuffer); |
| 85 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) | 154 EXPECT_CALL(*mock_reader_, ReadPacket(_, _)) |
| 86 .WillOnce(DoAll( | 155 .WillOnce(DoAll( |
| 87 CopyBuffer<0>(compressed_buf), | 156 CopyBuffer<0>(compressed_buf), |
| 88 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); | 157 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); |
| 89 net::TestCompletionCallback completion_cb_2; | 158 net::TestCompletionCallback completion_cb_2; |
| 90 compressed_reader_->ReadPacket(decompressed_buf, | 159 compressed_reader_->ReadPacket(decompressed_buf, |
| 91 completion_cb_2.callback()); | 160 completion_cb_2.callback()); |
| 92 int size = completion_cb_2.WaitForResult(); | 161 int size = completion_cb_2.WaitForResult(); |
| 162 LOG(ERROR) << "Decompressed size: " << size; | |
|
Wez
2016/04/12 23:54:34
Out, damned LOG(ERROR)! Out, I tell you!
Kevin M
2016/04/13 00:53:34
Done.
| |
| 93 return std::string(decompressed_buf->data(), size); | 163 return std::string(decompressed_buf->data(), size); |
| 94 } | 164 } |
| 95 | 165 |
| 96 bool CheckRoundTrip(const std::string& content) { | 166 bool CheckRoundTrip(const std::string& content) { |
| 97 return Decompress(Compress(content)) == content; | 167 return Decompress(Compress(content)) == content; |
| 98 } | 168 } |
| 99 | 169 |
| 100 MockPacketReader* mock_reader_; | 170 MockPacketReader* mock_reader_; |
| 101 MockPacketWriter* mock_writer_; | 171 MockPacketWriter* mock_writer_; |
| 102 std::unique_ptr<CompressedPacketReader> compressed_reader_; | 172 std::unique_ptr<CompressedPacketReader> compressed_reader_; |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 146 int size_1 = Compress("1234").size(); | 216 int size_1 = Compress("1234").size(); |
| 147 int size_2 = Compress("1234").size(); | 217 int size_2 = Compress("1234").size(); |
| 148 EXPECT_GT(size_1, size_2); | 218 EXPECT_GT(size_1, size_2); |
| 149 } | 219 } |
| 150 | 220 |
| 151 TEST_F(CompressedPacketTest, LargeInput) { | 221 TEST_F(CompressedPacketTest, LargeInput) { |
| 152 std::string big_str(kMaxPacketPayloadSizeBytes, 'A'); // 3MB of A's. | 222 std::string big_str(kMaxPacketPayloadSizeBytes, 'A'); // 3MB of A's. |
| 153 EXPECT_TRUE(CheckRoundTrip(big_str)); | 223 EXPECT_TRUE(CheckRoundTrip(big_str)); |
| 154 } | 224 } |
| 155 | 225 |
| 226 TEST_F(CompressedPacketTest, LowCompressionRatio) { | |
| 227 std::vector<uint8_t> decoded; | |
| 228 ASSERT_TRUE(base::HexStringToBytes(std::string(kRepro, arraysize(kRepro) - 1), | |
| 229 &decoded)); | |
| 230 EXPECT_TRUE(CheckRoundTrip(std::string(decoded.begin(), decoded.end()))); | |
| 231 } | |
| 232 | |
| 156 TEST_F(CompressedPacketTest, DecompressIllegallyLargePayload) { | 233 TEST_F(CompressedPacketTest, DecompressIllegallyLargePayload) { |
| 157 // We can't use the compressor to compress an illegally sized payload, however | 234 // We can't use the compressor to compress an illegally sized payload, however |
| 158 // we can concatenate the output of smaller payloads to form an uber-payload. | 235 // we can concatenate the output of smaller payloads to form an uber-payload. |
| 159 std::string huge_block = | 236 std::string huge_block = |
| 160 Compress(std::string(kMaxPacketPayloadSizeBytes, 'A')) + | 237 Compress(std::string(kMaxPacketPayloadSizeBytes, 'A')) + |
| 161 Compress("1337 payl0ad 0verfl0w 'spl0it"); | 238 Compress("1337 payl0ad 0verfl0w 'spl0it"); |
| 162 | 239 |
| 163 scoped_refptr<net::StringIOBuffer> compressed_str_buf( | 240 scoped_refptr<net::StringIOBuffer> compressed_str_buf( |
| 164 new net::StringIOBuffer(huge_block)); | 241 new net::StringIOBuffer(huge_block)); |
| 165 scoped_refptr<net::DrainableIOBuffer> compressed_buf( | 242 scoped_refptr<net::DrainableIOBuffer> compressed_buf( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 208 DoAll(CopyBuffer<0>(compressed_buf), | 285 DoAll(CopyBuffer<0>(compressed_buf), |
| 209 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); | 286 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); |
| 210 net::TestCompletionCallback completion_cb_2; | 287 net::TestCompletionCallback completion_cb_2; |
| 211 compressed_reader_->ReadPacket(make_scoped_refptr(new net::GrowableIOBuffer), | 288 compressed_reader_->ReadPacket(make_scoped_refptr(new net::GrowableIOBuffer), |
| 212 completion_cb_2.callback()); | 289 completion_cb_2.callback()); |
| 213 EXPECT_EQ(net::ERR_UNEXPECTED, completion_cb_2.WaitForResult()); | 290 EXPECT_EQ(net::ERR_UNEXPECTED, completion_cb_2.WaitForResult()); |
| 214 } | 291 } |
| 215 | 292 |
| 216 } // namespace | 293 } // namespace |
| 217 } // namespace blimp | 294 } // namespace blimp |
| OLD | NEW |