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

Side by Side Diff: blimp/net/compressed_packet_unittest.cc

Issue 1962393004: Added a debug info UI for Blimp (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Changed implementation to receive synchronous calls Created 4 years, 7 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
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/rand_util.h" 6 #include "base/rand_util.h"
7 #include "base/run_loop.h"
7 #include "base/sys_byteorder.h" 8 #include "base/sys_byteorder.h"
9 #include "blimp/net/blimp_connection_statistics.h"
8 #include "blimp/net/common.h" 10 #include "blimp/net/common.h"
9 #include "blimp/net/compressed_packet_reader.h" 11 #include "blimp/net/compressed_packet_reader.h"
10 #include "blimp/net/compressed_packet_writer.h" 12 #include "blimp/net/compressed_packet_writer.h"
11 #include "blimp/net/test_common.h" 13 #include "blimp/net/test_common.h"
12 #include "net/base/test_completion_callback.h" 14 #include "net/base/test_completion_callback.h"
13 #include "testing/gmock/include/gmock/gmock.h" 15 #include "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
15 17
16 using testing::_; 18 using testing::_;
17 using testing::DoAll; 19 using testing::DoAll;
(...skipping 22 matching lines...) Expand all
40 ASSERT_TRUE(src_buf); 42 ASSERT_TRUE(src_buf);
41 ASSERT_EQ(0, dest_buf->offset()); 43 ASSERT_EQ(0, dest_buf->offset());
42 src_buf->SetOffset(0); 44 src_buf->SetOffset(0);
43 dest_buf->SetCapacity(src_buf->BytesRemaining()); 45 dest_buf->SetCapacity(src_buf->BytesRemaining());
44 memcpy(dest_buf->data(), src_buf->data(), src_buf->BytesRemaining()); 46 memcpy(dest_buf->data(), src_buf->data(), src_buf->BytesRemaining());
45 } 47 }
46 48
47 class CompressedPacketTest : public testing::Test { 49 class CompressedPacketTest : public testing::Test {
48 public: 50 public:
49 CompressedPacketTest() 51 CompressedPacketTest()
50 : mock_reader_(new MockPacketReader), 52 : blimp_connection_statistics_(new BlimpConnectionStatistics),
53 mock_reader_(new MockPacketReader),
51 mock_writer_(new MockPacketWriter), 54 mock_writer_(new MockPacketWriter),
52 compressed_reader_( 55 compressed_reader_(
53 new CompressedPacketReader(base::WrapUnique(mock_reader_))), 56 new CompressedPacketReader(base::WrapUnique(mock_reader_))),
54 compressed_writer_( 57 compressed_writer_(
55 new CompressedPacketWriter(base::WrapUnique(mock_writer_))) {} 58 new CompressedPacketWriter(base::WrapUnique(mock_writer_))) {
59 compressed_reader_->set_blimp_connection_statistics(
60 blimp_connection_statistics_.get());
61 compressed_writer_->set_blimp_connection_statistics(
62 blimp_connection_statistics_.get());
63 }
56 ~CompressedPacketTest() override {} 64 ~CompressedPacketTest() override {}
57 65
58 protected: 66 protected:
59 // Returns the compressed result of |content|. 67 // Returns the compressed result of |content|.
60 std::string Compress(const std::string& content) { 68 std::string Compress(const std::string& content) {
61 scoped_refptr<net::StringIOBuffer> content_str_buf( 69 scoped_refptr<net::StringIOBuffer> content_str_buf(
62 new net::StringIOBuffer(content)); 70 new net::StringIOBuffer(content));
63 scoped_refptr<net::DrainableIOBuffer> content_buf( 71 scoped_refptr<net::DrainableIOBuffer> content_buf(
64 new net::DrainableIOBuffer(content_str_buf.get(), 72 new net::DrainableIOBuffer(content_str_buf.get(),
65 content_str_buf->size())); 73 content_str_buf->size()));
(...skipping 25 matching lines...) Expand all
91 compressed_reader_->ReadPacket(decompressed_buf, 99 compressed_reader_->ReadPacket(decompressed_buf,
92 completion_cb_2.callback()); 100 completion_cb_2.callback());
93 int size = completion_cb_2.WaitForResult(); 101 int size = completion_cb_2.WaitForResult();
94 return std::string(decompressed_buf->data(), size); 102 return std::string(decompressed_buf->data(), size);
95 } 103 }
96 104
97 bool CheckRoundTrip(const std::string& content) { 105 bool CheckRoundTrip(const std::string& content) {
98 return Decompress(Compress(content)) == content; 106 return Decompress(Compress(content)) == content;
99 } 107 }
100 108
109 std::unique_ptr<BlimpConnectionStatistics> blimp_connection_statistics_;
Kevin M 2016/05/24 01:02:01 suggestion: make it a regular (non-unique_ptr) fie
shaktisahu 2016/05/24 21:02:45 Removed from this file
101 MockPacketReader* mock_reader_; 110 MockPacketReader* mock_reader_;
102 MockPacketWriter* mock_writer_; 111 MockPacketWriter* mock_writer_;
103 std::unique_ptr<CompressedPacketReader> compressed_reader_; 112 std::unique_ptr<CompressedPacketReader> compressed_reader_;
104 std::unique_ptr<CompressedPacketWriter> compressed_writer_; 113 std::unique_ptr<CompressedPacketWriter> compressed_writer_;
105 testing::InSequence s; 114 testing::InSequence s;
106 }; 115 };
107 116
108 TEST_F(CompressedPacketTest, Empty) { 117 TEST_F(CompressedPacketTest, Empty) {
109 EXPECT_TRUE(CheckRoundTrip("1234")); 118 EXPECT_TRUE(CheckRoundTrip("1234"));
119 EXPECT_TRUE(CheckRoundTrip("1234"));
Kevin M 2016/05/24 01:02:02 Why duplicate this line?
shaktisahu 2016/05/24 21:02:45 Done.
110 EXPECT_TRUE(CheckRoundTrip("")); 120 EXPECT_TRUE(CheckRoundTrip(""));
111 EXPECT_TRUE(CheckRoundTrip("1234")); 121 EXPECT_TRUE(CheckRoundTrip("1234"));
112 } 122 }
113 123
124 TEST_F(CompressedPacketTest, StatisticsCollection) {
125 std::string source = "AAAAAAAAAAAAAAAAAAAAAAAAA";
126 std::string compressed = Compress(source);
127 std::string decompressed = Decompress(compressed);
128 EXPECT_EQ((int)compressed.size(),
Kevin M 2016/05/24 01:02:01 nit: use static_cast<> instead of C-style cast.
shaktisahu 2016/05/24 21:02:45 Done.
129 blimp_connection_statistics_->Get(
130 BlimpConnectionStatistics::BYTES_RECEIVED));
131 EXPECT_EQ((int)compressed.size(), blimp_connection_statistics_->Get(
132 BlimpConnectionStatistics::BYTES_SENT));
Kevin M 2016/05/24 01:02:01 This formatting looks a little off, run git cl for
shaktisahu 2016/05/24 21:02:45 Done.
133 }
134
114 TEST_F(CompressedPacketTest, Simple) { 135 TEST_F(CompressedPacketTest, Simple) {
115 std::string source = "AAAAAAAAAAAAAAAAAAAAAAAAA"; 136 std::string source = "AAAAAAAAAAAAAAAAAAAAAAAAA";
116 std::string compressed = Compress(source); 137 std::string compressed = Compress(source);
117 std::string decompressed = Decompress(compressed); 138 std::string decompressed = Decompress(compressed);
118 EXPECT_GT(source.size(), compressed.size()); 139 EXPECT_GT(source.size(), compressed.size());
119 EXPECT_EQ(source, decompressed); 140 EXPECT_EQ(source, decompressed);
120 } 141 }
121 142
122 TEST_F(CompressedPacketTest, DisjointSequences) { 143 TEST_F(CompressedPacketTest, DisjointSequences) {
123 EXPECT_TRUE(CheckRoundTrip("1234")); 144 EXPECT_TRUE(CheckRoundTrip("1234"));
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 DoAll(CopyBuffer<0>(compressed_buf), 239 DoAll(CopyBuffer<0>(compressed_buf),
219 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining()))); 240 InvokeCompletionCallback<1>(compressed_buf->BytesRemaining())));
220 net::TestCompletionCallback completion_cb_2; 241 net::TestCompletionCallback completion_cb_2;
221 compressed_reader_->ReadPacket(make_scoped_refptr(new net::GrowableIOBuffer), 242 compressed_reader_->ReadPacket(make_scoped_refptr(new net::GrowableIOBuffer),
222 completion_cb_2.callback()); 243 completion_cb_2.callback());
223 EXPECT_EQ(net::ERR_UNEXPECTED, completion_cb_2.WaitForResult()); 244 EXPECT_EQ(net::ERR_UNEXPECTED, completion_cb_2.WaitForResult());
224 } 245 }
225 246
226 } // namespace 247 } // namespace
227 } // namespace blimp 248 } // namespace blimp
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698