| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/quic/quic_connection.h" | 5 #include "net/quic/quic_connection.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 DISALLOW_COPY_AND_ASSIGN(TestReceiveAlgorithm); | 83 DISALLOW_COPY_AND_ASSIGN(TestReceiveAlgorithm); |
| 84 }; | 84 }; |
| 85 | 85 |
| 86 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message. | 86 // TaggingEncrypter appends kTagSize bytes of |tag| to the end of each message. |
| 87 class TaggingEncrypter : public QuicEncrypter { | 87 class TaggingEncrypter : public QuicEncrypter { |
| 88 public: | 88 public: |
| 89 explicit TaggingEncrypter(uint8 tag) | 89 explicit TaggingEncrypter(uint8 tag) |
| 90 : tag_(tag) { | 90 : tag_(tag) { |
| 91 } | 91 } |
| 92 | 92 |
| 93 virtual ~TaggingEncrypter() {} | 93 ~TaggingEncrypter() override {} |
| 94 | 94 |
| 95 // QuicEncrypter interface. | 95 // QuicEncrypter interface. |
| 96 virtual bool SetKey(StringPiece key) override { return true; } | 96 bool SetKey(StringPiece key) override { return true; } |
| 97 virtual bool SetNoncePrefix(StringPiece nonce_prefix) override { | 97 bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; } |
| 98 return true; | |
| 99 } | |
| 100 | 98 |
| 101 virtual bool Encrypt(StringPiece nonce, | 99 bool Encrypt(StringPiece nonce, |
| 102 StringPiece associated_data, | 100 StringPiece associated_data, |
| 103 StringPiece plaintext, | 101 StringPiece plaintext, |
| 104 unsigned char* output) override { | 102 unsigned char* output) override { |
| 105 memcpy(output, plaintext.data(), plaintext.size()); | 103 memcpy(output, plaintext.data(), plaintext.size()); |
| 106 output += plaintext.size(); | 104 output += plaintext.size(); |
| 107 memset(output, tag_, kTagSize); | 105 memset(output, tag_, kTagSize); |
| 108 return true; | 106 return true; |
| 109 } | 107 } |
| 110 | 108 |
| 111 virtual QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number, | 109 QuicData* EncryptPacket(QuicPacketSequenceNumber sequence_number, |
| 112 StringPiece associated_data, | 110 StringPiece associated_data, |
| 113 StringPiece plaintext) override { | 111 StringPiece plaintext) override { |
| 114 const size_t len = plaintext.size() + kTagSize; | 112 const size_t len = plaintext.size() + kTagSize; |
| 115 uint8* buffer = new uint8[len]; | 113 uint8* buffer = new uint8[len]; |
| 116 Encrypt(StringPiece(), associated_data, plaintext, buffer); | 114 Encrypt(StringPiece(), associated_data, plaintext, buffer); |
| 117 return new QuicData(reinterpret_cast<char*>(buffer), len, true); | 115 return new QuicData(reinterpret_cast<char*>(buffer), len, true); |
| 118 } | 116 } |
| 119 | 117 |
| 120 virtual size_t GetKeySize() const override { return 0; } | 118 size_t GetKeySize() const override { return 0; } |
| 121 virtual size_t GetNoncePrefixSize() const override { return 0; } | 119 size_t GetNoncePrefixSize() const override { return 0; } |
| 122 | 120 |
| 123 virtual size_t GetMaxPlaintextSize(size_t ciphertext_size) const override { | 121 size_t GetMaxPlaintextSize(size_t ciphertext_size) const override { |
| 124 return ciphertext_size - kTagSize; | 122 return ciphertext_size - kTagSize; |
| 125 } | 123 } |
| 126 | 124 |
| 127 virtual size_t GetCiphertextSize(size_t plaintext_size) const override { | 125 size_t GetCiphertextSize(size_t plaintext_size) const override { |
| 128 return plaintext_size + kTagSize; | 126 return plaintext_size + kTagSize; |
| 129 } | 127 } |
| 130 | 128 |
| 131 virtual StringPiece GetKey() const override { | 129 StringPiece GetKey() const override { return StringPiece(); } |
| 132 return StringPiece(); | |
| 133 } | |
| 134 | 130 |
| 135 virtual StringPiece GetNoncePrefix() const override { | 131 StringPiece GetNoncePrefix() const override { return StringPiece(); } |
| 136 return StringPiece(); | |
| 137 } | |
| 138 | 132 |
| 139 private: | 133 private: |
| 140 enum { | 134 enum { |
| 141 kTagSize = 12, | 135 kTagSize = 12, |
| 142 }; | 136 }; |
| 143 | 137 |
| 144 const uint8 tag_; | 138 const uint8 tag_; |
| 145 | 139 |
| 146 DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter); | 140 DISALLOW_COPY_AND_ASSIGN(TaggingEncrypter); |
| 147 }; | 141 }; |
| 148 | 142 |
| 149 // TaggingDecrypter ensures that the final kTagSize bytes of the message all | 143 // TaggingDecrypter ensures that the final kTagSize bytes of the message all |
| 150 // have the same value and then removes them. | 144 // have the same value and then removes them. |
| 151 class TaggingDecrypter : public QuicDecrypter { | 145 class TaggingDecrypter : public QuicDecrypter { |
| 152 public: | 146 public: |
| 153 virtual ~TaggingDecrypter() {} | 147 ~TaggingDecrypter() override {} |
| 154 | 148 |
| 155 // QuicDecrypter interface | 149 // QuicDecrypter interface |
| 156 virtual bool SetKey(StringPiece key) override { return true; } | 150 bool SetKey(StringPiece key) override { return true; } |
| 157 virtual bool SetNoncePrefix(StringPiece nonce_prefix) override { | 151 bool SetNoncePrefix(StringPiece nonce_prefix) override { return true; } |
| 158 return true; | |
| 159 } | |
| 160 | 152 |
| 161 virtual bool Decrypt(StringPiece nonce, | 153 bool Decrypt(StringPiece nonce, |
| 162 StringPiece associated_data, | 154 StringPiece associated_data, |
| 163 StringPiece ciphertext, | 155 StringPiece ciphertext, |
| 164 unsigned char* output, | 156 unsigned char* output, |
| 165 size_t* output_length) override { | 157 size_t* output_length) override { |
| 166 if (ciphertext.size() < kTagSize) { | 158 if (ciphertext.size() < kTagSize) { |
| 167 return false; | 159 return false; |
| 168 } | 160 } |
| 169 if (!CheckTag(ciphertext, GetTag(ciphertext))) { | 161 if (!CheckTag(ciphertext, GetTag(ciphertext))) { |
| 170 return false; | 162 return false; |
| 171 } | 163 } |
| 172 *output_length = ciphertext.size() - kTagSize; | 164 *output_length = ciphertext.size() - kTagSize; |
| 173 memcpy(output, ciphertext.data(), *output_length); | 165 memcpy(output, ciphertext.data(), *output_length); |
| 174 return true; | 166 return true; |
| 175 } | 167 } |
| 176 | 168 |
| 177 virtual QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number, | 169 QuicData* DecryptPacket(QuicPacketSequenceNumber sequence_number, |
| 178 StringPiece associated_data, | 170 StringPiece associated_data, |
| 179 StringPiece ciphertext) override { | 171 StringPiece ciphertext) override { |
| 180 if (ciphertext.size() < kTagSize) { | 172 if (ciphertext.size() < kTagSize) { |
| 181 return nullptr; | 173 return nullptr; |
| 182 } | 174 } |
| 183 if (!CheckTag(ciphertext, GetTag(ciphertext))) { | 175 if (!CheckTag(ciphertext, GetTag(ciphertext))) { |
| 184 return nullptr; | 176 return nullptr; |
| 185 } | 177 } |
| 186 const size_t len = ciphertext.size() - kTagSize; | 178 const size_t len = ciphertext.size() - kTagSize; |
| 187 uint8* buf = new uint8[len]; | 179 uint8* buf = new uint8[len]; |
| 188 memcpy(buf, ciphertext.data(), len); | 180 memcpy(buf, ciphertext.data(), len); |
| 189 return new QuicData(reinterpret_cast<char*>(buf), len, | 181 return new QuicData(reinterpret_cast<char*>(buf), len, |
| 190 true /* owns buffer */); | 182 true /* owns buffer */); |
| 191 } | 183 } |
| 192 | 184 |
| 193 virtual StringPiece GetKey() const override { return StringPiece(); } | 185 StringPiece GetKey() const override { return StringPiece(); } |
| 194 virtual StringPiece GetNoncePrefix() const override { return StringPiece(); } | 186 StringPiece GetNoncePrefix() const override { return StringPiece(); } |
| 195 | 187 |
| 196 protected: | 188 protected: |
| 197 virtual uint8 GetTag(StringPiece ciphertext) { | 189 virtual uint8 GetTag(StringPiece ciphertext) { |
| 198 return ciphertext.data()[ciphertext.size()-1]; | 190 return ciphertext.data()[ciphertext.size()-1]; |
| 199 } | 191 } |
| 200 | 192 |
| 201 private: | 193 private: |
| 202 enum { | 194 enum { |
| 203 kTagSize = 12, | 195 kTagSize = 12, |
| 204 }; | 196 }; |
| 205 | 197 |
| 206 bool CheckTag(StringPiece ciphertext, uint8 tag) { | 198 bool CheckTag(StringPiece ciphertext, uint8 tag) { |
| 207 for (size_t i = ciphertext.size() - kTagSize; i < ciphertext.size(); i++) { | 199 for (size_t i = ciphertext.size() - kTagSize; i < ciphertext.size(); i++) { |
| 208 if (ciphertext.data()[i] != tag) { | 200 if (ciphertext.data()[i] != tag) { |
| 209 return false; | 201 return false; |
| 210 } | 202 } |
| 211 } | 203 } |
| 212 | 204 |
| 213 return true; | 205 return true; |
| 214 } | 206 } |
| 215 }; | 207 }; |
| 216 | 208 |
| 217 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message | 209 // StringTaggingDecrypter ensures that the final kTagSize bytes of the message |
| 218 // match the expected value. | 210 // match the expected value. |
| 219 class StrictTaggingDecrypter : public TaggingDecrypter { | 211 class StrictTaggingDecrypter : public TaggingDecrypter { |
| 220 public: | 212 public: |
| 221 explicit StrictTaggingDecrypter(uint8 tag) : tag_(tag) {} | 213 explicit StrictTaggingDecrypter(uint8 tag) : tag_(tag) {} |
| 222 virtual ~StrictTaggingDecrypter() {} | 214 ~StrictTaggingDecrypter() override {} |
| 223 | 215 |
| 224 // TaggingQuicDecrypter | 216 // TaggingQuicDecrypter |
| 225 virtual uint8 GetTag(StringPiece ciphertext) override { | 217 uint8 GetTag(StringPiece ciphertext) override { return tag_; } |
| 226 return tag_; | |
| 227 } | |
| 228 | 218 |
| 229 private: | 219 private: |
| 230 const uint8 tag_; | 220 const uint8 tag_; |
| 231 }; | 221 }; |
| 232 | 222 |
| 233 class TestConnectionHelper : public QuicConnectionHelperInterface { | 223 class TestConnectionHelper : public QuicConnectionHelperInterface { |
| 234 public: | 224 public: |
| 235 class TestAlarm : public QuicAlarm { | 225 class TestAlarm : public QuicAlarm { |
| 236 public: | 226 public: |
| 237 explicit TestAlarm(QuicAlarm::Delegate* delegate) | 227 explicit TestAlarm(QuicAlarm::Delegate* delegate) |
| 238 : QuicAlarm(delegate) { | 228 : QuicAlarm(delegate) { |
| 239 } | 229 } |
| 240 | 230 |
| 241 virtual void SetImpl() override {} | 231 void SetImpl() override {} |
| 242 virtual void CancelImpl() override {} | 232 void CancelImpl() override {} |
| 243 using QuicAlarm::Fire; | 233 using QuicAlarm::Fire; |
| 244 }; | 234 }; |
| 245 | 235 |
| 246 TestConnectionHelper(MockClock* clock, MockRandom* random_generator) | 236 TestConnectionHelper(MockClock* clock, MockRandom* random_generator) |
| 247 : clock_(clock), | 237 : clock_(clock), |
| 248 random_generator_(random_generator) { | 238 random_generator_(random_generator) { |
| 249 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); | 239 clock_->AdvanceTime(QuicTime::Delta::FromSeconds(1)); |
| 250 } | 240 } |
| 251 | 241 |
| 252 // QuicConnectionHelperInterface | 242 // QuicConnectionHelperInterface |
| 253 virtual const QuicClock* GetClock() const override { | 243 const QuicClock* GetClock() const override { return clock_; } |
| 254 return clock_; | |
| 255 } | |
| 256 | 244 |
| 257 virtual QuicRandom* GetRandomGenerator() override { | 245 QuicRandom* GetRandomGenerator() override { return random_generator_; } |
| 258 return random_generator_; | |
| 259 } | |
| 260 | 246 |
| 261 virtual QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) override { | 247 QuicAlarm* CreateAlarm(QuicAlarm::Delegate* delegate) override { |
| 262 return new TestAlarm(delegate); | 248 return new TestAlarm(delegate); |
| 263 } | 249 } |
| 264 | 250 |
| 265 private: | 251 private: |
| 266 MockClock* clock_; | 252 MockClock* clock_; |
| 267 MockRandom* random_generator_; | 253 MockRandom* random_generator_; |
| 268 | 254 |
| 269 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper); | 255 DISALLOW_COPY_AND_ASSIGN(TestConnectionHelper); |
| 270 }; | 256 }; |
| 271 | 257 |
| 272 class TestPacketWriter : public QuicPacketWriter { | 258 class TestPacketWriter : public QuicPacketWriter { |
| 273 public: | 259 public: |
| 274 explicit TestPacketWriter(QuicVersion version) | 260 explicit TestPacketWriter(QuicVersion version) |
| 275 : version_(version), | 261 : version_(version), |
| 276 framer_(SupportedVersions(version_)), | 262 framer_(SupportedVersions(version_)), |
| 277 last_packet_size_(0), | 263 last_packet_size_(0), |
| 278 write_blocked_(false), | 264 write_blocked_(false), |
| 279 block_on_next_write_(false), | 265 block_on_next_write_(false), |
| 280 is_write_blocked_data_buffered_(false), | 266 is_write_blocked_data_buffered_(false), |
| 281 final_bytes_of_last_packet_(0), | 267 final_bytes_of_last_packet_(0), |
| 282 final_bytes_of_previous_packet_(0), | 268 final_bytes_of_previous_packet_(0), |
| 283 use_tagging_decrypter_(false), | 269 use_tagging_decrypter_(false), |
| 284 packets_write_attempts_(0) { | 270 packets_write_attempts_(0) { |
| 285 } | 271 } |
| 286 | 272 |
| 287 // QuicPacketWriter interface | 273 // QuicPacketWriter interface |
| 288 virtual WriteResult WritePacket( | 274 WriteResult WritePacket(const char* buffer, |
| 289 const char* buffer, size_t buf_len, | 275 size_t buf_len, |
| 290 const IPAddressNumber& self_address, | 276 const IPAddressNumber& self_address, |
| 291 const IPEndPoint& peer_address) override { | 277 const IPEndPoint& peer_address) override { |
| 292 QuicEncryptedPacket packet(buffer, buf_len); | 278 QuicEncryptedPacket packet(buffer, buf_len); |
| 293 ++packets_write_attempts_; | 279 ++packets_write_attempts_; |
| 294 | 280 |
| 295 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { | 281 if (packet.length() >= sizeof(final_bytes_of_last_packet_)) { |
| 296 final_bytes_of_previous_packet_ = final_bytes_of_last_packet_; | 282 final_bytes_of_previous_packet_ = final_bytes_of_last_packet_; |
| 297 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, | 283 memcpy(&final_bytes_of_last_packet_, packet.data() + packet.length() - 4, |
| 298 sizeof(final_bytes_of_last_packet_)); | 284 sizeof(final_bytes_of_last_packet_)); |
| 299 } | 285 } |
| 300 | 286 |
| 301 if (use_tagging_decrypter_) { | 287 if (use_tagging_decrypter_) { |
| 302 framer_.framer()->SetDecrypter(new TaggingDecrypter, ENCRYPTION_NONE); | 288 framer_.framer()->SetDecrypter(new TaggingDecrypter, ENCRYPTION_NONE); |
| 303 } | 289 } |
| 304 EXPECT_TRUE(framer_.ProcessPacket(packet)); | 290 EXPECT_TRUE(framer_.ProcessPacket(packet)); |
| 305 if (block_on_next_write_) { | 291 if (block_on_next_write_) { |
| 306 write_blocked_ = true; | 292 write_blocked_ = true; |
| 307 block_on_next_write_ = false; | 293 block_on_next_write_ = false; |
| 308 } | 294 } |
| 309 if (IsWriteBlocked()) { | 295 if (IsWriteBlocked()) { |
| 310 return WriteResult(WRITE_STATUS_BLOCKED, -1); | 296 return WriteResult(WRITE_STATUS_BLOCKED, -1); |
| 311 } | 297 } |
| 312 last_packet_size_ = packet.length(); | 298 last_packet_size_ = packet.length(); |
| 313 return WriteResult(WRITE_STATUS_OK, last_packet_size_); | 299 return WriteResult(WRITE_STATUS_OK, last_packet_size_); |
| 314 } | 300 } |
| 315 | 301 |
| 316 virtual bool IsWriteBlockedDataBuffered() const override { | 302 bool IsWriteBlockedDataBuffered() const override { |
| 317 return is_write_blocked_data_buffered_; | 303 return is_write_blocked_data_buffered_; |
| 318 } | 304 } |
| 319 | 305 |
| 320 virtual bool IsWriteBlocked() const override { return write_blocked_; } | 306 bool IsWriteBlocked() const override { return write_blocked_; } |
| 321 | 307 |
| 322 virtual void SetWritable() override { write_blocked_ = false; } | 308 void SetWritable() override { write_blocked_ = false; } |
| 323 | 309 |
| 324 void BlockOnNextWrite() { block_on_next_write_ = true; } | 310 void BlockOnNextWrite() { block_on_next_write_ = true; } |
| 325 | 311 |
| 326 const QuicPacketHeader& header() { return framer_.header(); } | 312 const QuicPacketHeader& header() { return framer_.header(); } |
| 327 | 313 |
| 328 size_t frame_count() const { return framer_.num_frames(); } | 314 size_t frame_count() const { return framer_.num_frames(); } |
| 329 | 315 |
| 330 const vector<QuicAckFrame>& ack_frames() const { | 316 const vector<QuicAckFrame>& ack_frames() const { |
| 331 return framer_.ack_frames(); | 317 return framer_.ack_frames(); |
| 332 } | 318 } |
| (...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 return static_cast<TestPacketWriter*>(QuicConnection::writer()); | 569 return static_cast<TestPacketWriter*>(QuicConnection::writer()); |
| 584 } | 570 } |
| 585 | 571 |
| 586 DISALLOW_COPY_AND_ASSIGN(TestConnection); | 572 DISALLOW_COPY_AND_ASSIGN(TestConnection); |
| 587 }; | 573 }; |
| 588 | 574 |
| 589 // Used for testing packets revived from FEC packets. | 575 // Used for testing packets revived from FEC packets. |
| 590 class FecQuicConnectionDebugVisitor | 576 class FecQuicConnectionDebugVisitor |
| 591 : public QuicConnectionDebugVisitor { | 577 : public QuicConnectionDebugVisitor { |
| 592 public: | 578 public: |
| 593 virtual void OnRevivedPacket(const QuicPacketHeader& header, | 579 void OnRevivedPacket(const QuicPacketHeader& header, |
| 594 StringPiece data) override { | 580 StringPiece data) override { |
| 595 revived_header_ = header; | 581 revived_header_ = header; |
| 596 } | 582 } |
| 597 | 583 |
| 598 // Public accessor method. | 584 // Public accessor method. |
| 599 QuicPacketHeader revived_header() const { | 585 QuicPacketHeader revived_header() const { |
| 600 return revived_header_; | 586 return revived_header_; |
| 601 } | 587 } |
| 602 | 588 |
| 603 private: | 589 private: |
| 604 QuicPacketHeader revived_header_; | 590 QuicPacketHeader revived_header_; |
| (...skipping 3416 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4021 QuicBlockedFrame blocked; | 4007 QuicBlockedFrame blocked; |
| 4022 blocked.stream_id = 3; | 4008 blocked.stream_id = 3; |
| 4023 EXPECT_CALL(visitor_, OnBlockedFrames(_)); | 4009 EXPECT_CALL(visitor_, OnBlockedFrames(_)); |
| 4024 ProcessFramePacket(QuicFrame(&blocked)); | 4010 ProcessFramePacket(QuicFrame(&blocked)); |
| 4025 EXPECT_TRUE(ack_alarm->IsSet()); | 4011 EXPECT_TRUE(ack_alarm->IsSet()); |
| 4026 } | 4012 } |
| 4027 | 4013 |
| 4028 } // namespace | 4014 } // namespace |
| 4029 } // namespace test | 4015 } // namespace test |
| 4030 } // namespace net | 4016 } // namespace net |
| OLD | NEW |