From fc07084ada3d788f31beb22ffc6eac73415b843d Mon Sep 17 00:00:00 2001 From: Nobuaki Sukegawa Date: Mon, 11 Jan 2016 14:18:06 +0900 Subject: [PATCH] THRIFT-3542 Add length limit support to Java test server This closes #788 --- .../apache/thrift/protocol/TBinaryProtocol.java | 17 +++++++++-------- .../test/org/apache/thrift/test/TestServer.java | 12 ++++++++++-- test/features/known_failures_Linux.json | 4 ---- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java b/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java index 4e936066b..d7f8b83ab 100644 --- a/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java +++ b/lib/java/src/org/apache/thrift/protocol/TBinaryProtocol.java @@ -71,6 +71,10 @@ public class TBinaryProtocol extends TProtocol { this(strictRead, strictWrite, NO_LENGTH_LIMIT, NO_LENGTH_LIMIT); } + public Factory(long stringLengthLimit, long containerLengthLimit) { + this(false, true, stringLengthLimit, containerLengthLimit); + } + public Factory(boolean strictRead, boolean strictWrite, long stringLengthLimit, long containerLengthLimit) { stringLengthLimit_ = stringLengthLimit; containerLengthLimit_ = containerLengthLimit; @@ -94,6 +98,10 @@ public class TBinaryProtocol extends TProtocol { this(trans, NO_LENGTH_LIMIT, NO_LENGTH_LIMIT, strictRead, strictWrite); } + public TBinaryProtocol(TTransport trans, long stringLengthLimit, long containerLengthLimit) { + this(trans, stringLengthLimit, containerLengthLimit, false, true); + } + public TBinaryProtocol(TTransport trans, long stringLengthLimit, long containerLengthLimit, boolean strictRead, boolean strictWrite) { super(trans); stringLengthLimit_ = stringLengthLimit; @@ -350,10 +358,6 @@ public class TBinaryProtocol extends TProtocol { int size = readI32(); checkStringReadLength(size); - if (stringLengthLimit_ > 0 && size > stringLengthLimit_) { - throw new TProtocolException(TProtocolException.SIZE_LIMIT, - "String field exceeded string size limit"); - } if (trans_.getBytesRemainingInBuffer() >= size) { try { @@ -381,10 +385,7 @@ public class TBinaryProtocol extends TProtocol { public ByteBuffer readBinary() throws TException { int size = readI32(); - if (stringLengthLimit_ > 0 && size > stringLengthLimit_) { - throw new TProtocolException(TProtocolException.SIZE_LIMIT, - "Binary field exceeded string size limit"); - } + checkStringReadLength(size); if (trans_.getBytesRemainingInBuffer() >= size) { ByteBuffer bb = ByteBuffer.wrap(trans_.getBuffer(), trans_.getBufferPosition(), size); diff --git a/lib/java/test/org/apache/thrift/test/TestServer.java b/lib/java/test/org/apache/thrift/test/TestServer.java index ee0866e01..14cd2aba0 100644 --- a/lib/java/test/org/apache/thrift/test/TestServer.java +++ b/lib/java/test/org/apache/thrift/test/TestServer.java @@ -111,6 +111,8 @@ public class TestServer { String protocol_type = "binary"; String server_type = "thread-pool"; String domain_socket = ""; + int string_limit = -1; + int container_limit = -1; try { for (int i = 0; i < args.length; i++) { if (args[i].startsWith("--port")) { @@ -128,6 +130,10 @@ public class TestServer { transport_type.trim(); } else if (args[i].equals("--ssl")) { ssl = true; + } else if (args[i].startsWith("--string-limit")) { + string_limit = Integer.valueOf(args[i].split("=")[1]); + } else if (args[i].startsWith("--container-limit")) { + container_limit = Integer.valueOf(args[i].split("=")[1]); } else if (args[i].equals("--help")) { System.out.println("Allowed options:"); System.out.println(" --help\t\t\tProduce help message"); @@ -136,6 +142,8 @@ public class TestServer { System.out.println(" --protocol=arg (=" + protocol_type + ")\tProtocol: binary, json, compact"); System.out.println(" --ssl\t\t\tEncrypted Transport using SSL"); System.out.println(" --server-type=arg (=" + server_type +")\n\t\t\t\tType of server: simple, thread-pool, nonblocking, threaded-selector"); + System.out.println(" --string-limit=arg (=" + string_limit + ")\tString read length limit"); + System.out.println(" --container-limit=arg (=" + container_limit + ")\tContainer read length limit"); System.exit(0); } } @@ -186,9 +194,9 @@ public class TestServer { if (protocol_type.equals("json")) { tProtocolFactory = new TJSONProtocol.Factory(); } else if (protocol_type.equals("compact")) { - tProtocolFactory = new TCompactProtocol.Factory(); + tProtocolFactory = new TCompactProtocol.Factory(string_limit, container_limit); } else { - tProtocolFactory = new TBinaryProtocol.Factory(); + tProtocolFactory = new TBinaryProtocol.Factory(string_limit, container_limit); } TTransportFactory tTransportFactory = null; diff --git a/test/features/known_failures_Linux.json b/test/features/known_failures_Linux.json index 7a510839a..9bf600dff 100644 --- a/test/features/known_failures_Linux.json +++ b/test/features/known_failures_Linux.json @@ -17,10 +17,6 @@ "hs-limit_container_length_compact_buffered-ip", "hs-limit_string_length_binary_buffered-ip", "hs-limit_string_length_compact_buffered-ip", - "java-limit_container_length_binary_buffered-ip", - "java-limit_container_length_compact_buffered-ip", - "java-limit_string_length_binary_buffered-ip", - "java-limit_string_length_compact_buffered-ip", "nodejs-limit_container_length_binary_buffered-ip", "nodejs-limit_container_length_compact_buffered-ip", "nodejs-limit_string_length_binary_buffered-ip",