Skip to content

Commit

Permalink
Merge pull request square#2996 from square/jwilson.1121.fix_some_auto…
Browse files Browse the repository at this point in the history
…bahn_tests

Fix some Autobahn tests.
  • Loading branch information
swankjesse authored Nov 21, 2016
2 parents 850d468 + c8dbcce commit a72ed5e
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion okhttp-tests/fuzzingserver-config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"url": "ws://127.0.0.1:9001",
"url": "ws://127.0.0.1:9099",
"outdir": "./target/fuzzingserver-report",
"cases": ["*"],
"exclude-cases": [
Expand Down
6 changes: 3 additions & 3 deletions okhttp-tests/fuzzingserver-expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -257,15 +257,15 @@
"4.2.1 OK"
"4.2.2 OK"
"4.2.3 NON-STRICT"
"4.2.4 NON-STRICT"
"4.2.4 OK"
"4.2.5 OK"
"5.1 OK"
"5.10 OK"
"5.11 OK"
"5.12 OK"
"5.13 OK"
"5.14 OK"
"5.15 NON-STRICT"
"5.15 OK"
"5.16 OK"
"5.17 OK"
"5.18 OK"
Expand All @@ -283,7 +283,7 @@
"7.1.2 OK"
"7.1.3 OK"
"7.1.4 OK"
"7.1.5 FAILED"
"7.1.5 OK"
"7.1.6 INFORMATIONAL"
"7.13.1 INFORMATIONAL"
"7.13.2 INFORMATIONAL"
Expand Down
4 changes: 2 additions & 2 deletions okhttp-tests/src/main/java/okhttp3/AutobahnTester.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* href="http://autobahn.ws/testsuite/">Autobahn Testsuite</a>.
*/
public final class AutobahnTester {
private static final String HOST = "ws://localhost:9001";
private static final String HOST = "ws://localhost:9099";

public static void main(String... args) throws IOException {
new AutobahnTester().run();
Expand Down Expand Up @@ -133,7 +133,7 @@ private void updateReports() {
final CountDownLatch latch = new CountDownLatch(1);
newWebSocket("/updateReports?agent=" + Version.userAgent(), new WebSocketListener() {
@Override public void onClosing(WebSocket webSocket, int code, String reason) {
webSocket.close(code, null);
webSocket.close(1000, null);
latch.countDown();
}

Expand Down
4 changes: 3 additions & 1 deletion okhttp/src/main/java/okhttp3/internal/ws/RealWebSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import static okhttp3.internal.ws.WebSocketProtocol.CLOSE_CLIENT_GOING_AWAY;
import static okhttp3.internal.ws.WebSocketProtocol.OPCODE_BINARY;
import static okhttp3.internal.ws.WebSocketProtocol.OPCODE_TEXT;
import static okhttp3.internal.ws.WebSocketProtocol.validateCloseCode;

public final class RealWebSocket implements WebSocket, WebSocketReader.FrameCallback {
private static final List<Protocol> ONLY_HTTP1 = Collections.singletonList(Protocol.HTTP_1_1);
Expand All @@ -56,7 +57,7 @@ public final class RealWebSocket implements WebSocket, WebSocketReader.FrameCall
* The maximum number of bytes to enqueue. Rather than enqueueing beyond this limit we tear down
* the web socket! It's possible that we're writing faster than the peer can read.
*/
private static final long MAX_QUEUE_SIZE = 1024 * 1024; // 1 MiB.
private static final long MAX_QUEUE_SIZE = 16 * 1024 * 1024; // 16 MiB.

/** A shared executor for all web sockets. */
private static final ExecutorService executor = new ThreadPoolExecutor(0,
Expand Down Expand Up @@ -340,6 +341,7 @@ public synchronized boolean pong(ByteString payload) {
}

@Override public synchronized boolean close(final int code, final String reason) {
validateCloseCode(code);
// TODO(jwilson): confirm reason is well-formed. (<=123 bytes, etc.)

if (failed || enqueuedClose) return false;
Expand Down
20 changes: 10 additions & 10 deletions okhttp/src/main/java/okhttp3/internal/ws/WebSocketProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ static void toggleMask(byte[] buffer, long byteCount, byte[] key, long frameByte
}
}

static void validateCloseCode(int code, boolean argument) throws ProtocolException {
String message = null;
static String closeCodeExceptionMessage(int code) {
if (code < 1000 || code >= 5000) {
message = "Code must be in range [1000,5000): " + code;
return "Code must be in range [1000,5000): " + code;
} else if ((code >= 1004 && code <= 1006) || (code >= 1012 && code <= 2999)) {
message = "Code " + code + " is reserved and may not be used.";
}
if (message != null) {
if (argument) {
throw new IllegalArgumentException(message);
}
throw new ProtocolException(message);
return "Code " + code + " is reserved and may not be used.";
} else {
return null;
}
}

static void validateCloseCode(int code) {
String message = closeCodeExceptionMessage(code);
if (message != null) throw new IllegalArgumentException(message);
}

public static String acceptHeader(String key) {
return ByteString.encodeUtf8(key + WebSocketProtocol.ACCEPT_MAGIC).sha1().base64();
}
Expand Down
4 changes: 2 additions & 2 deletions okhttp/src/main/java/okhttp3/internal/ws/WebSocketReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
import static okhttp3.internal.ws.WebSocketProtocol.PAYLOAD_LONG;
import static okhttp3.internal.ws.WebSocketProtocol.PAYLOAD_SHORT;
import static okhttp3.internal.ws.WebSocketProtocol.toggleMask;
import static okhttp3.internal.ws.WebSocketProtocol.validateCloseCode;

/**
* An <a href="http://tools.ietf.org/html/rfc6455">RFC 6455</a>-compatible WebSocket frame reader.
Expand Down Expand Up @@ -190,7 +189,8 @@ private void readControlFrame() throws IOException {
} else if (bufferSize != 0) {
code = buffer.readShort();
reason = buffer.readUtf8();
validateCloseCode(code, false);
String codeExceptionMessage = WebSocketProtocol.closeCodeExceptionMessage(code);
if (codeExceptionMessage != null) throw new ProtocolException(codeExceptionMessage);
}
frameCallback.onReadClose(code, reason);
closed = true;
Expand Down
4 changes: 2 additions & 2 deletions okhttp/src/main/java/okhttp3/internal/ws/WebSocketWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import static okhttp3.internal.ws.WebSocketProtocol.PAYLOAD_LONG;
import static okhttp3.internal.ws.WebSocketProtocol.PAYLOAD_SHORT;
import static okhttp3.internal.ws.WebSocketProtocol.PAYLOAD_SHORT_MAX;
import static okhttp3.internal.ws.WebSocketProtocol.toggleMask;
import static okhttp3.internal.ws.WebSocketProtocol.validateCloseCode;
import static okhttp3.internal.ws.WebSocketProtocol.toggleMask;

/**
* An <a href="http://tools.ietf.org/html/rfc6455">RFC 6455</a>-compatible WebSocket frame writer.
Expand Down Expand Up @@ -98,7 +98,7 @@ void writeClose(int code, String reason) throws IOException {
ByteString payload = ByteString.EMPTY;
if (code != 0 || reason != null) {
if (code != 0) {
validateCloseCode(code, true);
validateCloseCode(code);
}
Buffer buffer = new Buffer();
buffer.writeShort(code);
Expand Down

0 comments on commit a72ed5e

Please sign in to comment.