Chromium Code Reviews| 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/spdy/spdy_session.h" | 5 #include "net/spdy/spdy_session.h" |
| 6 | 6 |
| 7 #include "base/memory/scoped_ptr.h" | |
| 8 #include "base/memory/scoped_vector.h" | |
| 7 #include "net/base/cert_test_util.h" | 9 #include "net/base/cert_test_util.h" |
| 8 #include "net/base/host_cache.h" | 10 #include "net/base/host_cache.h" |
| 11 #include "net/base/io_buffer.h" | |
| 9 #include "net/base/ip_endpoint.h" | 12 #include "net/base/ip_endpoint.h" |
| 10 #include "net/base/net_log_unittest.h" | 13 #include "net/base/net_log_unittest.h" |
| 11 #include "net/base/test_data_directory.h" | 14 #include "net/base/test_data_directory.h" |
| 15 #include "net/base/test_data_stream.h" | |
| 12 #include "net/spdy/spdy_io_buffer.h" | 16 #include "net/spdy/spdy_io_buffer.h" |
| 13 #include "net/spdy/spdy_session_pool.h" | 17 #include "net/spdy/spdy_session_pool.h" |
| 18 #include "net/spdy/spdy_session_test_util.h" | |
| 14 #include "net/spdy/spdy_stream.h" | 19 #include "net/spdy/spdy_stream.h" |
| 15 #include "net/spdy/spdy_test_util_spdy2.h" | 20 #include "net/spdy/spdy_test_util_spdy2.h" |
| 16 #include "testing/platform_test.h" | 21 #include "testing/platform_test.h" |
| 17 | 22 |
| 18 using namespace net::test_spdy2; | 23 using namespace net::test_spdy2; |
| 19 | 24 |
| 20 namespace net { | 25 namespace net { |
| 21 | 26 |
| 22 namespace { | 27 namespace { |
| 23 | 28 |
| (...skipping 1591 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1615 EXPECT_EQ(OK, session->InitializeWithSocket(connection.release(), true, OK)); | 1620 EXPECT_EQ(OK, session->InitializeWithSocket(connection.release(), true, OK)); |
| 1616 | 1621 |
| 1617 EXPECT_FALSE(session->NeedsCredentials()); | 1622 EXPECT_FALSE(session->NeedsCredentials()); |
| 1618 | 1623 |
| 1619 // Flush the SpdySession::OnReadComplete() task. | 1624 // Flush the SpdySession::OnReadComplete() task. |
| 1620 MessageLoop::current()->RunUntilIdle(); | 1625 MessageLoop::current()->RunUntilIdle(); |
| 1621 | 1626 |
| 1622 spdy_session_pool_->Remove(session); | 1627 spdy_session_pool_->Remove(session); |
| 1623 } | 1628 } |
| 1624 | 1629 |
| 1630 TEST_F(SpdySessionSpdy2Test, SyncRead) { | |
|
Ryan Sleevi
2013/02/06 23:04:46
Nit: You should have a high-level comment for the
ramant (doing other things)
2013/02/07 02:11:07
+1 to the above.
Added comments on what the tests
| |
| 1631 MockConnect connect_data(SYNCHRONOUS, OK); | |
| 1632 BufferedSpdyFramer framer(2, false); | |
| 1633 | |
| 1634 scoped_ptr<SpdyFrame> req1(ConstructSpdyGet(NULL, 0, false, 1, MEDIUM)); | |
| 1635 MockWrite writes[] = { | |
| 1636 CreateMockWrite(*req1, 0), | |
| 1637 }; | |
| 1638 | |
| 1639 const int kPayloadSize = 1600; | |
| 1640 TestDataStream test_stream; | |
| 1641 scoped_refptr<net::IOBuffer> payload(new net::IOBuffer(kPayloadSize)); | |
| 1642 char* payload_data = payload->data(); | |
| 1643 test_stream.GetBytes(payload_data, kPayloadSize); | |
| 1644 | |
| 1645 scoped_ptr<SpdyFrame> data_frame1( | |
| 1646 framer.CreateDataFrame(1, payload_data, kPayloadSize, DATA_FLAG_NONE)); | |
| 1647 scoped_ptr<SpdyFrame> data_frame( | |
| 1648 framer.CreateDataFrame(1, payload_data, kPayloadSize, DATA_FLAG_FIN)); | |
|
Ryan Sleevi
2013/02/06 23:04:46
Is it legal to dupe IOBuffers like this? Just wond
ramant (doing other things)
2013/02/07 02:11:07
SpdyFramer::CreateDataFrame calls SpdyFrameBuilder
| |
| 1649 | |
| 1650 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
| 1651 | |
| 1652 MockRead reads[] = { | |
| 1653 CreateMockRead(*resp1, 1), | |
| 1654 CreateMockRead(*data_frame1, 2), | |
| 1655 CreateMockRead(*data_frame1, 3, SYNCHRONOUS), | |
| 1656 CreateMockRead(*data_frame1, 4, SYNCHRONOUS), | |
| 1657 CreateMockRead(*data_frame, 5, SYNCHRONOUS), | |
| 1658 MockRead(ASYNC, 0, 6) // EOF | |
| 1659 }; | |
| 1660 | |
| 1661 DeterministicSocketData data(reads, arraysize(reads), | |
| 1662 writes, arraysize(writes)); | |
| 1663 data.set_connect_data(connect_data); | |
| 1664 session_deps_.host_resolver->set_synchronous_mode(true); | |
| 1665 session_deps_.deterministic_socket_factory->AddSocketDataProvider(&data); | |
| 1666 | |
| 1667 SSLSocketDataProvider ssl(SYNCHRONOUS, OK); | |
| 1668 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl); | |
| 1669 | |
| 1670 CreateDeterministicNetworkSession(); | |
| 1671 | |
| 1672 scoped_refptr<SpdySession> session = CreateInitializedSession(); | |
| 1673 | |
| 1674 scoped_refptr<SpdyStream> spdy_stream1; | |
| 1675 TestCompletionCallback callback1; | |
| 1676 GURL url1("http://www.google.com"); | |
| 1677 EXPECT_EQ(OK, session->CreateStream(url1, MEDIUM, &spdy_stream1, | |
| 1678 BoundNetLog(), callback1.callback())); | |
| 1679 EXPECT_EQ(0u, spdy_stream1->stream_id()); | |
| 1680 | |
| 1681 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock); | |
| 1682 (*headers)["method"] = "GET"; | |
| 1683 (*headers)["scheme"] = url1.scheme(); | |
| 1684 (*headers)["host"] = url1.host(); | |
| 1685 (*headers)["url"] = url1.path(); | |
| 1686 (*headers)["version"] = "HTTP/1.1"; | |
| 1687 | |
| 1688 spdy_stream1->set_spdy_headers(headers.Pass()); | |
| 1689 EXPECT_TRUE(spdy_stream1->HasUrl()); | |
| 1690 spdy_stream1->SendRequest(false); | |
| 1691 | |
| 1692 SpdySessionTestTaskObserver observer("spdy_session.cc", "DoRead"); | |
| 1693 | |
| 1694 // Run until 1st read. | |
| 1695 EXPECT_EQ(0u, spdy_stream1->stream_id()); | |
| 1696 data.RunFor(2); | |
| 1697 EXPECT_EQ(1u, spdy_stream1->stream_id()); | |
| 1698 EXPECT_EQ(0u, observer.posted_count()); | |
| 1699 | |
| 1700 // Do 4 reads. | |
| 1701 data.RunFor(4); | |
| 1702 EXPECT_EQ(0u, observer.posted_count()); | |
| 1703 EXPECT_TRUE(data.at_write_eof()); | |
| 1704 EXPECT_TRUE(data.at_read_eof()); | |
| 1705 } | |
| 1706 | |
| 1707 TEST_F(SpdySessionSpdy2Test, ASyncRead) { | |
|
Ryan Sleevi
2013/02/06 23:04:46
Naming: AsyncRead if it's asynchronous. It looks l
ramant (doing other things)
2013/02/07 02:11:07
Done.
| |
| 1708 MockConnect connect_data(SYNCHRONOUS, OK); | |
| 1709 BufferedSpdyFramer framer(2, false); | |
| 1710 | |
| 1711 scoped_ptr<SpdyFrame> req1(ConstructSpdyGet(NULL, 0, false, 1, MEDIUM)); | |
| 1712 MockWrite writes[] = { | |
| 1713 CreateMockWrite(*req1, 0), | |
| 1714 }; | |
| 1715 | |
| 1716 // Build buffers of size 8k. | |
| 1717 ASSERT_EQ(32 * 1024, kMaxReadBytes); | |
| 1718 const int kPayloadSize = kMaxReadBytes / 4 - SpdyDataFrame::size(); | |
| 1719 TestDataStream test_stream; | |
| 1720 scoped_refptr<net::IOBuffer> payload(new net::IOBuffer(kPayloadSize)); | |
| 1721 char* payload_data = payload->data(); | |
| 1722 test_stream.GetBytes(payload_data, kPayloadSize); | |
| 1723 | |
| 1724 scoped_ptr<SpdyFrame> data_frame1( | |
| 1725 framer.CreateDataFrame(1, payload_data, kPayloadSize, DATA_FLAG_NONE)); | |
| 1726 scoped_ptr<SpdyFrame> data_frame( | |
| 1727 framer.CreateDataFrame(1, payload_data, kPayloadSize, DATA_FLAG_FIN)); | |
| 1728 | |
| 1729 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); | |
| 1730 | |
| 1731 // Send twice as many bytes as kMaxReadBytes. | |
| 1732 MockRead reads[] = { | |
| 1733 CreateMockRead(*resp1, 1), | |
| 1734 CreateMockRead(*data_frame1, 2), | |
| 1735 CreateMockRead(*data_frame1, 3, SYNCHRONOUS), | |
| 1736 CreateMockRead(*data_frame1, 4, SYNCHRONOUS), | |
| 1737 CreateMockRead(*data_frame1, 5, SYNCHRONOUS), | |
| 1738 CreateMockRead(*data_frame1, 6, SYNCHRONOUS), | |
| 1739 CreateMockRead(*data_frame1, 7, SYNCHRONOUS), | |
| 1740 CreateMockRead(*data_frame1, 8, SYNCHRONOUS), | |
| 1741 CreateMockRead(*data_frame, 9, SYNCHRONOUS), | |
| 1742 MockRead(ASYNC, 0, 10) // EOF | |
| 1743 }; | |
| 1744 | |
| 1745 DeterministicSocketData data(reads, arraysize(reads), | |
| 1746 writes, arraysize(writes)); | |
| 1747 data.set_connect_data(connect_data); | |
| 1748 session_deps_.host_resolver->set_synchronous_mode(true); | |
| 1749 session_deps_.deterministic_socket_factory->AddSocketDataProvider(&data); | |
| 1750 | |
| 1751 SSLSocketDataProvider ssl(SYNCHRONOUS, OK); | |
| 1752 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl); | |
| 1753 | |
| 1754 CreateDeterministicNetworkSession(); | |
| 1755 | |
| 1756 scoped_refptr<SpdySession> session = CreateInitializedSession(); | |
| 1757 | |
| 1758 scoped_refptr<SpdyStream> spdy_stream1; | |
| 1759 TestCompletionCallback callback1; | |
| 1760 GURL url1("http://www.google.com"); | |
| 1761 EXPECT_EQ(OK, session->CreateStream(url1, MEDIUM, &spdy_stream1, | |
| 1762 BoundNetLog(), callback1.callback())); | |
| 1763 EXPECT_EQ(0u, spdy_stream1->stream_id()); | |
| 1764 | |
| 1765 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock); | |
| 1766 (*headers)["method"] = "GET"; | |
| 1767 (*headers)["scheme"] = url1.scheme(); | |
| 1768 (*headers)["host"] = url1.host(); | |
| 1769 (*headers)["url"] = url1.path(); | |
| 1770 (*headers)["version"] = "HTTP/1.1"; | |
| 1771 | |
| 1772 spdy_stream1->set_spdy_headers(headers.Pass()); | |
| 1773 EXPECT_TRUE(spdy_stream1->HasUrl()); | |
| 1774 spdy_stream1->SendRequest(false); | |
| 1775 | |
| 1776 SpdySessionTestTaskObserver observer("spdy_session.cc", "DoRead"); | |
| 1777 | |
| 1778 // Run until 1st read. | |
| 1779 EXPECT_EQ(0u, spdy_stream1->stream_id()); | |
| 1780 data.RunFor(2); | |
| 1781 EXPECT_EQ(1u, spdy_stream1->stream_id()); | |
| 1782 EXPECT_EQ(0u, observer.posted_count()); | |
| 1783 | |
| 1784 // Do 8 reads. | |
| 1785 data.RunFor(8); | |
| 1786 EXPECT_EQ(1u, observer.posted_count()); | |
| 1787 EXPECT_TRUE(data.at_write_eof()); | |
| 1788 EXPECT_TRUE(data.at_read_eof()); | |
| 1789 } | |
| 1790 | |
| 1625 } // namespace net | 1791 } // namespace net |
| OLD | NEW |