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/location.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/memory/scoped_vector.h" |
| 10 #include "base/pending_task.h" |
| 11 #include "base/string_util.h" |
7 #include "net/base/cert_test_util.h" | 12 #include "net/base/cert_test_util.h" |
8 #include "net/base/host_cache.h" | 13 #include "net/base/host_cache.h" |
| 14 #include "net/base/io_buffer.h" |
9 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
10 #include "net/base/net_log_unittest.h" | 16 #include "net/base/net_log_unittest.h" |
11 #include "net/base/test_data_directory.h" | 17 #include "net/base/test_data_directory.h" |
| 18 #include "net/base/test_data_stream.h" |
12 #include "net/spdy/spdy_io_buffer.h" | 19 #include "net/spdy/spdy_io_buffer.h" |
13 #include "net/spdy/spdy_session_pool.h" | 20 #include "net/spdy/spdy_session_pool.h" |
| 21 #include "net/spdy/spdy_session_test_util.h" |
14 #include "net/spdy/spdy_stream.h" | 22 #include "net/spdy/spdy_stream.h" |
15 #include "net/spdy/spdy_test_util_spdy3.h" | 23 #include "net/spdy/spdy_test_util_spdy3.h" |
16 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
17 | 25 |
18 using namespace net::test_spdy3; | 26 using namespace net::test_spdy3; |
19 | 27 |
20 namespace net { | 28 namespace net { |
21 | 29 |
22 namespace { | 30 namespace { |
23 | 31 |
(...skipping 1717 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1741 | 1749 |
1742 scoped_refptr<SpdyStream> spdy_stream2; | 1750 scoped_refptr<SpdyStream> spdy_stream2; |
1743 EXPECT_EQ(OK, session->CreateStream(test_url_, MEDIUM, &spdy_stream2, | 1751 EXPECT_EQ(OK, session->CreateStream(test_url_, MEDIUM, &spdy_stream2, |
1744 BoundNetLog(), callback1.callback())); | 1752 BoundNetLog(), callback1.callback())); |
1745 | 1753 |
1746 EXPECT_EQ(spdy_stream2->send_window_size(), window_size); | 1754 EXPECT_EQ(spdy_stream2->send_window_size(), window_size); |
1747 spdy_stream2->Cancel(); | 1755 spdy_stream2->Cancel(); |
1748 spdy_stream2 = NULL; | 1756 spdy_stream2 = NULL; |
1749 } | 1757 } |
1750 | 1758 |
| 1759 TEST_F(SpdySessionSpdy3Test, SyncRead) { |
| 1760 MockConnect connect_data(SYNCHRONOUS, OK); |
| 1761 BufferedSpdyFramer framer(3, false); |
| 1762 |
| 1763 scoped_ptr<SpdyFrame> req1(ConstructSpdyGet(NULL, 0, false, 1, MEDIUM)); |
| 1764 MockWrite writes[] = { |
| 1765 CreateMockWrite(*req1, 0), |
| 1766 }; |
| 1767 |
| 1768 const int kPayloadSize = 1600; |
| 1769 TestDataStream test_stream; |
| 1770 scoped_refptr<net::IOBuffer> payload(new net::IOBuffer(kPayloadSize)); |
| 1771 char* payload_data = payload->data(); |
| 1772 test_stream.GetBytes(payload_data, kPayloadSize); |
| 1773 |
| 1774 scoped_ptr<SpdyFrame> data_frame1( |
| 1775 framer.CreateDataFrame(1, payload_data, kPayloadSize, DATA_FLAG_NONE)); |
| 1776 scoped_ptr<SpdyFrame> data_frame( |
| 1777 framer.CreateDataFrame(1, payload_data, kPayloadSize, DATA_FLAG_FIN)); |
| 1778 |
| 1779 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 1780 |
| 1781 MockRead reads[] = { |
| 1782 CreateMockRead(*resp1, 1), |
| 1783 CreateMockRead(*data_frame1, 2), |
| 1784 CreateMockRead(*data_frame1, 3, SYNCHRONOUS), |
| 1785 CreateMockRead(*data_frame1, 4, SYNCHRONOUS), |
| 1786 CreateMockRead(*data_frame, 5, SYNCHRONOUS), |
| 1787 MockRead(ASYNC, 0, 6) // EOF |
| 1788 }; |
| 1789 |
| 1790 DeterministicSocketData data(reads, arraysize(reads), |
| 1791 writes, arraysize(writes)); |
| 1792 data.set_connect_data(connect_data); |
| 1793 session_deps_.host_resolver->set_synchronous_mode(true); |
| 1794 session_deps_.deterministic_socket_factory->AddSocketDataProvider(&data); |
| 1795 |
| 1796 SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 1797 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl); |
| 1798 |
| 1799 CreateDeterministicNetworkSession(); |
| 1800 |
| 1801 scoped_refptr<SpdySession> session = CreateInitializedSession(); |
| 1802 |
| 1803 scoped_refptr<SpdyStream> spdy_stream1; |
| 1804 TestCompletionCallback callback1; |
| 1805 GURL url1("http://www.google.com"); |
| 1806 EXPECT_EQ(OK, session->CreateStream(url1, MEDIUM, &spdy_stream1, |
| 1807 BoundNetLog(), callback1.callback())); |
| 1808 EXPECT_EQ(0u, spdy_stream1->stream_id()); |
| 1809 |
| 1810 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock); |
| 1811 (*headers)[":method"] = "GET"; |
| 1812 (*headers)[":scheme"] = url1.scheme(); |
| 1813 (*headers)[":host"] = url1.host(); |
| 1814 (*headers)[":path"] = url1.path(); |
| 1815 (*headers)[":version"] = "HTTP/1.1"; |
| 1816 |
| 1817 spdy_stream1->set_spdy_headers(headers.Pass()); |
| 1818 EXPECT_TRUE(spdy_stream1->HasUrl()); |
| 1819 spdy_stream1->SendRequest(false); |
| 1820 |
| 1821 SpdySessionTestTaskObserver observer("spdy_session.cc", "DoRead"); |
| 1822 |
| 1823 // Run until 1st read. |
| 1824 EXPECT_EQ(0u, spdy_stream1->stream_id()); |
| 1825 data.RunFor(2); |
| 1826 EXPECT_EQ(1u, spdy_stream1->stream_id()); |
| 1827 EXPECT_EQ(0u, observer.posted_count()); |
| 1828 |
| 1829 // Do 4 reads. |
| 1830 data.RunFor(4); |
| 1831 EXPECT_EQ(0u, observer.posted_count()); |
| 1832 EXPECT_TRUE(data.at_write_eof()); |
| 1833 EXPECT_TRUE(data.at_read_eof()); |
| 1834 } |
| 1835 |
| 1836 TEST_F(SpdySessionSpdy3Test, ASyncRead) { |
| 1837 MockConnect connect_data(SYNCHRONOUS, OK); |
| 1838 BufferedSpdyFramer framer(3, false); |
| 1839 |
| 1840 scoped_ptr<SpdyFrame> req1(ConstructSpdyGet(NULL, 0, false, 1, MEDIUM)); |
| 1841 MockWrite writes[] = { |
| 1842 CreateMockWrite(*req1, 0), |
| 1843 }; |
| 1844 |
| 1845 // Build buffers of size 8k. |
| 1846 ASSERT_EQ(32 * 1024, kMaxReadBytes); |
| 1847 const int kPayloadSize = kMaxReadBytes / 4 - SpdyDataFrame::size(); |
| 1848 TestDataStream test_stream; |
| 1849 scoped_refptr<net::IOBuffer> payload(new net::IOBuffer(kPayloadSize)); |
| 1850 char* payload_data = payload->data(); |
| 1851 test_stream.GetBytes(payload_data, kPayloadSize); |
| 1852 |
| 1853 scoped_ptr<SpdyFrame> data_frame1( |
| 1854 framer.CreateDataFrame(1, payload_data, kPayloadSize, DATA_FLAG_NONE)); |
| 1855 scoped_ptr<SpdyFrame> data_frame( |
| 1856 framer.CreateDataFrame(1, payload_data, kPayloadSize, DATA_FLAG_FIN)); |
| 1857 |
| 1858 scoped_ptr<SpdyFrame> resp1(ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 1859 |
| 1860 // Send twice as many bytes as kMaxReadBytes. |
| 1861 MockRead reads[] = { |
| 1862 CreateMockRead(*resp1, 1), |
| 1863 CreateMockRead(*data_frame1, 2), |
| 1864 CreateMockRead(*data_frame1, 3, SYNCHRONOUS), |
| 1865 CreateMockRead(*data_frame1, 4, SYNCHRONOUS), |
| 1866 CreateMockRead(*data_frame1, 5, SYNCHRONOUS), |
| 1867 CreateMockRead(*data_frame1, 6, SYNCHRONOUS), |
| 1868 CreateMockRead(*data_frame1, 7, SYNCHRONOUS), |
| 1869 CreateMockRead(*data_frame1, 8, SYNCHRONOUS), |
| 1870 CreateMockRead(*data_frame, 9, SYNCHRONOUS), |
| 1871 MockRead(ASYNC, 0, 10) // EOF |
| 1872 }; |
| 1873 |
| 1874 DeterministicSocketData data(reads, arraysize(reads), |
| 1875 writes, arraysize(writes)); |
| 1876 data.set_connect_data(connect_data); |
| 1877 session_deps_.host_resolver->set_synchronous_mode(true); |
| 1878 session_deps_.deterministic_socket_factory->AddSocketDataProvider(&data); |
| 1879 |
| 1880 SSLSocketDataProvider ssl(SYNCHRONOUS, OK); |
| 1881 session_deps_.deterministic_socket_factory->AddSSLSocketDataProvider(&ssl); |
| 1882 |
| 1883 CreateDeterministicNetworkSession(); |
| 1884 |
| 1885 scoped_refptr<SpdySession> session = CreateInitializedSession(); |
| 1886 |
| 1887 scoped_refptr<SpdyStream> spdy_stream1; |
| 1888 TestCompletionCallback callback1; |
| 1889 GURL url1("http://www.google.com"); |
| 1890 EXPECT_EQ(OK, session->CreateStream(url1, MEDIUM, &spdy_stream1, |
| 1891 BoundNetLog(), callback1.callback())); |
| 1892 EXPECT_EQ(0u, spdy_stream1->stream_id()); |
| 1893 |
| 1894 scoped_ptr<SpdyHeaderBlock> headers(new SpdyHeaderBlock); |
| 1895 (*headers)[":method"] = "GET"; |
| 1896 (*headers)[":scheme"] = url1.scheme(); |
| 1897 (*headers)[":host"] = url1.host(); |
| 1898 (*headers)[":path"] = url1.path(); |
| 1899 (*headers)[":version"] = "HTTP/1.1"; |
| 1900 |
| 1901 spdy_stream1->set_spdy_headers(headers.Pass()); |
| 1902 EXPECT_TRUE(spdy_stream1->HasUrl()); |
| 1903 spdy_stream1->SendRequest(false); |
| 1904 |
| 1905 SpdySessionTestTaskObserver observer("spdy_session.cc", "DoRead"); |
| 1906 |
| 1907 // Run until 1st read. |
| 1908 EXPECT_EQ(0u, spdy_stream1->stream_id()); |
| 1909 data.RunFor(2); |
| 1910 EXPECT_EQ(1u, spdy_stream1->stream_id()); |
| 1911 EXPECT_EQ(0u, observer.posted_count()); |
| 1912 |
| 1913 // Do 8 reads. |
| 1914 data.RunFor(8); |
| 1915 EXPECT_EQ(1u, observer.posted_count()); |
| 1916 EXPECT_TRUE(data.at_write_eof()); |
| 1917 EXPECT_TRUE(data.at_read_eof()); |
| 1918 } |
| 1919 |
1751 } // namespace net | 1920 } // namespace net |
OLD | NEW |