OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_spdy_stream.h" | 5 #include "net/quic/quic_spdy_stream.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 869 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
880 | 880 |
881 TEST_P(QuicSpdyStreamTest, WritingTrailersSendsAFin) { | 881 TEST_P(QuicSpdyStreamTest, WritingTrailersSendsAFin) { |
882 // Test that writing trailers will send a FIN, as Trailers are the last thing | 882 // Test that writing trailers will send a FIN, as Trailers are the last thing |
883 // to be sent on a stream. | 883 // to be sent on a stream. |
884 Initialize(kShouldProcessData); | 884 Initialize(kShouldProcessData); |
885 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) | 885 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
886 .Times(AnyNumber()) | 886 .Times(AnyNumber()) |
887 .WillRepeatedly(Invoke(MockQuicSession::ConsumeAllData)); | 887 .WillRepeatedly(Invoke(MockQuicSession::ConsumeAllData)); |
888 | 888 |
889 // Write the initial headers, without a FIN. | 889 // Write the initial headers, without a FIN. |
890 EXPECT_CALL(*session_, WriteHeaders(_, _, _, _, _)); | 890 EXPECT_CALL(*session_, WriteHeadersMock(_, _, _, _, _)); |
891 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); | 891 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); |
892 | 892 |
893 // Writing trailers implicitly sends a FIN. | 893 // Writing trailers implicitly sends a FIN. |
894 SpdyHeaderBlock trailers; | 894 SpdyHeaderBlock trailers; |
895 trailers["trailer key"] = "trailer value"; | 895 trailers["trailer key"] = "trailer value"; |
896 EXPECT_CALL(*session_, WriteHeaders(_, _, | 896 EXPECT_CALL(*session_, WriteHeadersMock(_, _, true, _, _)); |
897 /*fin=*/true, _, _)); | |
898 stream_->WriteTrailers(std::move(trailers), nullptr); | 897 stream_->WriteTrailers(std::move(trailers), nullptr); |
899 EXPECT_TRUE(stream_->fin_sent()); | 898 EXPECT_TRUE(stream_->fin_sent()); |
900 } | 899 } |
901 | 900 |
902 TEST_P(QuicSpdyStreamTest, WritingTrailersFinalOffset) { | 901 TEST_P(QuicSpdyStreamTest, WritingTrailersFinalOffset) { |
903 // Test that when writing trailers, the trailers that are actually sent to the | 902 // Test that when writing trailers, the trailers that are actually sent to the |
904 // peer contain the final offset field indicating last byte of data. | 903 // peer contain the final offset field indicating last byte of data. |
905 Initialize(kShouldProcessData); | 904 Initialize(kShouldProcessData); |
906 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) | 905 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
907 .Times(AnyNumber()) | 906 .Times(AnyNumber()) |
908 .WillRepeatedly(Invoke(MockQuicSession::ConsumeAllData)); | 907 .WillRepeatedly(Invoke(MockQuicSession::ConsumeAllData)); |
909 | 908 |
910 // Write the initial headers. | 909 // Write the initial headers. |
911 EXPECT_CALL(*session_, WriteHeaders(_, _, _, _, _)); | 910 EXPECT_CALL(*session_, WriteHeadersMock(_, _, _, _, _)); |
912 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); | 911 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); |
913 | 912 |
914 // Write non-zero body data to force a non-zero final offset. | 913 // Write non-zero body data to force a non-zero final offset. |
915 const int kBodySize = 1 * 1024; // 1 MB | 914 const int kBodySize = 1 * 1024; // 1 MB |
916 stream_->WriteOrBufferData(string(kBodySize, 'x'), false, nullptr); | 915 stream_->WriteOrBufferData(string(kBodySize, 'x'), false, nullptr); |
917 | 916 |
918 // The final offset field in the trailing headers is populated with the | 917 // The final offset field in the trailing headers is populated with the |
919 // number of body bytes written (including queued bytes). | 918 // number of body bytes written (including queued bytes). |
920 SpdyHeaderBlock trailers; | 919 SpdyHeaderBlock trailers; |
921 trailers["trailer key"] = "trailer value"; | 920 trailers["trailer key"] = "trailer value"; |
922 SpdyHeaderBlock trailers_with_offset = trailers; | 921 SpdyHeaderBlock trailers_with_offset(trailers.Clone()); |
923 trailers_with_offset[kFinalOffsetHeaderKey] = base::IntToString(kBodySize); | 922 trailers_with_offset[kFinalOffsetHeaderKey] = base::IntToString(kBodySize); |
924 EXPECT_CALL(*session_, WriteHeaders(_, testing::Eq(trailers_with_offset), | 923 EXPECT_CALL(*session_, WriteHeadersMock(_, _, true, _, _)); |
925 /*fin=*/true, _, _)); | |
926 stream_->WriteTrailers(std::move(trailers), nullptr); | 924 stream_->WriteTrailers(std::move(trailers), nullptr); |
| 925 EXPECT_EQ(trailers_with_offset, session_->GetWriteHeaders()); |
927 } | 926 } |
928 | 927 |
929 TEST_P(QuicSpdyStreamTest, WritingTrailersClosesWriteSide) { | 928 TEST_P(QuicSpdyStreamTest, WritingTrailersClosesWriteSide) { |
930 // Test that if trailers are written after all other data has been written | 929 // Test that if trailers are written after all other data has been written |
931 // (headers and body), that this closes the stream for writing. | 930 // (headers and body), that this closes the stream for writing. |
932 Initialize(kShouldProcessData); | 931 Initialize(kShouldProcessData); |
933 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) | 932 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
934 .Times(AnyNumber()) | 933 .Times(AnyNumber()) |
935 .WillRepeatedly(Invoke(MockQuicSession::ConsumeAllData)); | 934 .WillRepeatedly(Invoke(MockQuicSession::ConsumeAllData)); |
936 | 935 |
937 // Write the initial headers. | 936 // Write the initial headers. |
938 EXPECT_CALL(*session_, WriteHeaders(_, _, _, _, _)); | 937 EXPECT_CALL(*session_, WriteHeadersMock(_, _, _, _, _)); |
939 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); | 938 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); |
940 | 939 |
941 // Write non-zero body data. | 940 // Write non-zero body data. |
942 const int kBodySize = 1 * 1024; // 1 MB | 941 const int kBodySize = 1 * 1024; // 1 MB |
943 stream_->WriteOrBufferData(string(kBodySize, 'x'), false, nullptr); | 942 stream_->WriteOrBufferData(string(kBodySize, 'x'), false, nullptr); |
944 EXPECT_EQ(0u, stream_->queued_data_bytes()); | 943 EXPECT_EQ(0u, stream_->queued_data_bytes()); |
945 | 944 |
946 // Headers and body have been fully written, there is no queued data. Writing | 945 // Headers and body have been fully written, there is no queued data. Writing |
947 // trailers marks the end of this stream, and thus the write side is closed. | 946 // trailers marks the end of this stream, and thus the write side is closed. |
948 EXPECT_CALL(*session_, WriteHeaders(_, _, | 947 EXPECT_CALL(*session_, WriteHeadersMock(_, _, true, _, _)); |
949 /*fin=*/true, _, _)); | |
950 stream_->WriteTrailers(SpdyHeaderBlock(), nullptr); | 948 stream_->WriteTrailers(SpdyHeaderBlock(), nullptr); |
951 EXPECT_TRUE(stream_->write_side_closed()); | 949 EXPECT_TRUE(stream_->write_side_closed()); |
952 } | 950 } |
953 | 951 |
954 TEST_P(QuicSpdyStreamTest, WritingTrailersWithQueuedBytes) { | 952 TEST_P(QuicSpdyStreamTest, WritingTrailersWithQueuedBytes) { |
955 // Test that the stream is not closed for writing when trailers are sent | 953 // Test that the stream is not closed for writing when trailers are sent |
956 // while there are still body bytes queued. | 954 // while there are still body bytes queued. |
957 Initialize(kShouldProcessData); | 955 Initialize(kShouldProcessData); |
958 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) | 956 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
959 .Times(AnyNumber()) | 957 .Times(AnyNumber()) |
960 .WillRepeatedly(Invoke(MockQuicSession::ConsumeAllData)); | 958 .WillRepeatedly(Invoke(MockQuicSession::ConsumeAllData)); |
961 | 959 |
962 // Write the initial headers. | 960 // Write the initial headers. |
963 EXPECT_CALL(*session_, WriteHeaders(_, _, _, _, _)); | 961 EXPECT_CALL(*session_, WriteHeadersMock(_, _, _, _, _)); |
964 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); | 962 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/false, nullptr); |
965 | 963 |
966 // Write non-zero body data, but only consume partially, ensuring queueing. | 964 // Write non-zero body data, but only consume partially, ensuring queueing. |
967 const int kBodySize = 1 * 1024; // 1 MB | 965 const int kBodySize = 1 * 1024; // 1 MB |
968 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) | 966 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
969 .WillOnce(Return(QuicConsumedData(kBodySize - 1, false))); | 967 .WillOnce(Return(QuicConsumedData(kBodySize - 1, false))); |
970 stream_->WriteOrBufferData(string(kBodySize, 'x'), false, nullptr); | 968 stream_->WriteOrBufferData(string(kBodySize, 'x'), false, nullptr); |
971 EXPECT_EQ(1u, stream_->queued_data_bytes()); | 969 EXPECT_EQ(1u, stream_->queued_data_bytes()); |
972 | 970 |
973 // Writing trailers will send a FIN, but not close the write side of the | 971 // Writing trailers will send a FIN, but not close the write side of the |
974 // stream as there are queued bytes. | 972 // stream as there are queued bytes. |
975 EXPECT_CALL(*session_, WriteHeaders(_, _, | 973 EXPECT_CALL(*session_, WriteHeadersMock(_, _, true, _, _)); |
976 /*fin=*/true, _, _)); | |
977 stream_->WriteTrailers(SpdyHeaderBlock(), nullptr); | 974 stream_->WriteTrailers(SpdyHeaderBlock(), nullptr); |
978 EXPECT_TRUE(stream_->fin_sent()); | 975 EXPECT_TRUE(stream_->fin_sent()); |
979 EXPECT_FALSE(stream_->write_side_closed()); | 976 EXPECT_FALSE(stream_->write_side_closed()); |
980 } | 977 } |
981 | 978 |
982 TEST_P(QuicSpdyStreamTest, WritingTrailersAfterFIN) { | 979 TEST_P(QuicSpdyStreamTest, WritingTrailersAfterFIN) { |
983 // Test that it is not possible to write Trailers after a FIN has been sent. | 980 // Test that it is not possible to write Trailers after a FIN has been sent. |
984 Initialize(kShouldProcessData); | 981 Initialize(kShouldProcessData); |
985 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) | 982 EXPECT_CALL(*session_, WritevData(_, _, _, _, _, _)) |
986 .Times(AnyNumber()) | 983 .Times(AnyNumber()) |
987 .WillRepeatedly(Invoke(MockQuicSession::ConsumeAllData)); | 984 .WillRepeatedly(Invoke(MockQuicSession::ConsumeAllData)); |
988 | 985 |
989 // Write the initial headers, with a FIN. | 986 // Write the initial headers, with a FIN. |
990 EXPECT_CALL(*session_, WriteHeaders(_, _, _, _, _)); | 987 EXPECT_CALL(*session_, WriteHeadersMock(_, _, _, _, _)); |
991 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/true, nullptr); | 988 stream_->WriteHeaders(SpdyHeaderBlock(), /*fin=*/true, nullptr); |
992 EXPECT_TRUE(stream_->fin_sent()); | 989 EXPECT_TRUE(stream_->fin_sent()); |
993 | 990 |
994 // Writing Trailers should fail, as the FIN has already been sent. | 991 // Writing Trailers should fail, as the FIN has already been sent. |
995 // populated with the number of body bytes written. | 992 // populated with the number of body bytes written. |
996 EXPECT_DFATAL(stream_->WriteTrailers(SpdyHeaderBlock(), nullptr), | 993 EXPECT_DFATAL(stream_->WriteTrailers(SpdyHeaderBlock(), nullptr), |
997 "Trailers cannot be sent after a FIN"); | 994 "Trailers cannot be sent after a FIN"); |
998 } | 995 } |
999 | 996 |
1000 } // namespace | 997 } // namespace |
1001 } // namespace test | 998 } // namespace test |
1002 } // namespace net | 999 } // namespace net |
OLD | NEW |