Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 package org.chromium.net; | 5 package org.chromium.net; |
| 6 | 6 |
| 7 import android.test.suitebuilder.annotation.SmallTest; | 7 import android.test.suitebuilder.annotation.SmallTest; |
| 8 | 8 |
| 9 import org.chromium.base.test.util.Feature; | 9 import org.chromium.base.test.util.Feature; |
| 10 import org.chromium.net.CronetTestBase.OnlyRunNativeCronet; | 10 import org.chromium.net.CronetTestBase.OnlyRunNativeCronet; |
| 11 import org.json.JSONObject; | 11 import org.json.JSONObject; |
| 12 | 12 |
| 13 import java.nio.ByteBuffer; | |
| 14 | |
| 13 /** | 15 /** |
| 14 * Tests functionality of BidirectionalStream's QUIC implementation. | 16 * Tests functionality of BidirectionalStream's QUIC implementation. |
| 15 */ | 17 */ |
| 16 public class BidirectionalStreamQuicTest extends CronetTestBase { | 18 public class BidirectionalStreamQuicTest extends CronetTestBase { |
| 17 private CronetTestFramework mTestFramework; | 19 private CronetTestFramework mTestFramework; |
| 20 // Whether to skip server shutdown in tearDown(). | |
| 21 private boolean mSkipServerShutdown = false; | |
| 18 private enum QuicBidirectionalStreams { | 22 private enum QuicBidirectionalStreams { |
| 19 ENABLED, | 23 ENABLED, |
| 20 DISABLED, | 24 DISABLED, |
| 21 } | 25 } |
| 22 | 26 |
| 23 private void setUp(QuicBidirectionalStreams enabled) throws Exception { | 27 private void setUp(QuicBidirectionalStreams enabled) throws Exception { |
| 24 // Load library first to create MockCertVerifier. | 28 // Load library first to create MockCertVerifier. |
| 25 System.loadLibrary("cronet_tests"); | 29 System.loadLibrary("cronet_tests"); |
| 26 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); | 30 CronetEngine.Builder builder = new CronetEngine.Builder(getContext()); |
| 27 | 31 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 39 QuicTestServer.getServerPort()); | 43 QuicTestServer.getServerPort()); |
| 40 | 44 |
| 41 builder.setMockCertVerifierForTesting(QuicTestServer.createMockCertVerif ier()); | 45 builder.setMockCertVerifierForTesting(QuicTestServer.createMockCertVerif ier()); |
| 42 | 46 |
| 43 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(n ull, builder); | 47 mTestFramework = startCronetTestFrameworkWithUrlAndCronetEngineBuilder(n ull, builder); |
| 44 registerHostResolver(mTestFramework); | 48 registerHostResolver(mTestFramework); |
| 45 } | 49 } |
| 46 | 50 |
| 47 @Override | 51 @Override |
| 48 protected void tearDown() throws Exception { | 52 protected void tearDown() throws Exception { |
| 49 QuicTestServer.shutdownQuicTestServer(); | 53 if (!mSkipServerShutdown) { |
|
kapishnikov
2016/04/23 00:29:47
To avoid adding test logic to tearDown(), I think
xunjieli
2016/04/23 10:39:00
Done.
| |
| 54 QuicTestServer.shutdownQuicTestServer(); | |
| 55 } | |
| 50 super.tearDown(); | 56 super.tearDown(); |
| 51 } | 57 } |
| 52 | 58 |
| 53 @SmallTest | 59 @SmallTest |
| 54 @Feature({"Cronet"}) | 60 @Feature({"Cronet"}) |
| 55 @OnlyRunNativeCronet | 61 @OnlyRunNativeCronet |
| 56 // Test that QUIC is negotiated. | 62 // Test that QUIC is negotiated. |
| 57 public void testSimpleGet() throws Exception { | 63 public void testSimpleGet() throws Exception { |
| 58 setUp(QuicBidirectionalStreams.ENABLED); | 64 setUp(QuicBidirectionalStreams.ENABLED); |
| 59 String path = "/simple.txt"; | 65 String path = "/simple.txt"; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 85 .Builder(quicURL, callback, callbac k.getExecutor(), | 91 .Builder(quicURL, callback, callbac k.getExecutor(), |
| 86 mTestFramework.mCronetEngin e) | 92 mTestFramework.mCronetEngin e) |
| 87 .setHttpMethod("GET") | 93 .setHttpMethod("GET") |
| 88 .build(); | 94 .build(); |
| 89 stream.start(); | 95 stream.start(); |
| 90 callback.blockForDone(); | 96 callback.blockForDone(); |
| 91 assertTrue(stream.isDone()); | 97 assertTrue(stream.isDone()); |
| 92 assertTrue(callback.mOnErrorCalled); | 98 assertTrue(callback.mOnErrorCalled); |
| 93 assertNull(callback.mResponseInfo); | 99 assertNull(callback.mResponseInfo); |
| 94 } | 100 } |
| 101 | |
| 102 @SmallTest | |
| 103 @Feature({"Cronet"}) | |
| 104 @OnlyRunNativeCronet | |
| 105 // Tests that if the stream failed between the time when we issue a Write() | |
| 106 // and when the Write() is executed in the native stack, there is no crash. | |
| 107 // This test is racy, but it should catch a crash (if there is any) most of | |
| 108 // the time. | |
| 109 public void testStreamFailBeforeWriteIsExecutedOnNetworkThread() throws Exce ption { | |
| 110 setUp(QuicBidirectionalStreams.ENABLED); | |
| 111 String path = "/simple.txt"; | |
| 112 String quicURL = QuicTestServer.getServerURL() + path; | |
| 113 | |
| 114 TestBidirectionalStreamCallback callback = new TestBidirectionalStreamCa llback() { | |
| 115 @Override | |
| 116 public void onWriteCompleted( | |
| 117 BidirectionalStream stream, UrlResponseInfo info, ByteBuffer buffer) { | |
| 118 // Super class will write the next piece of data. | |
| 119 super.onWriteCompleted(stream, info, buffer); | |
| 120 // Only shut down the server exactly once. | |
| 121 if (!mSkipServerShutdown) { | |
| 122 QuicTestServer.shutdownQuicTestServer(); | |
| 123 mSkipServerShutdown = true; | |
| 124 } | |
| 125 } | |
| 126 }; | |
| 127 | |
| 128 callback.addWriteData("Test String".getBytes()); | |
| 129 callback.addWriteData("1234567890".getBytes()); | |
| 130 callback.addWriteData("woot!".getBytes()); | |
| 131 | |
| 132 BidirectionalStream stream = new BidirectionalStream | |
| 133 .Builder(quicURL, callback, callbac k.getExecutor(), | |
| 134 mTestFramework.mCronetEngin e) | |
| 135 .addHeader("foo", "bar") | |
| 136 .addHeader("empty", "") | |
| 137 .addHeader("Content-Type", "zebra") | |
| 138 .build(); | |
| 139 stream.start(); | |
| 140 callback.blockForDone(); | |
| 141 assertTrue(stream.isDone()); | |
| 142 // Server terminated on us, so the stream must fail. | |
| 143 // QUIC reports this as QUIC_PROTOCOL_ERROR = -356. | |
| 144 assertNotNull(callback.mError); | |
| 145 assertEquals(-356, callback.mError.getCronetInternalErrorCode()); | |
|
kapishnikov
2016/04/23 00:29:47
We can replace '-356' with NetError.ERR_QUIC_PROTO
xunjieli
2016/04/23 10:39:00
Done.
Ah, I didn't know that there is a java clas
| |
| 146 } | |
| 95 } | 147 } |
| OLD | NEW |