[tomcat] branch master updated: Increase coverage of Range header tests

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[tomcat] branch master updated: Increase coverage of Range header tests

markt
This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/master by this push:
     new 3116926  Increase coverage of Range header tests
3116926 is described below

commit 3116926c077fe6561763946b9668444a9806327e
Author: Mark Thomas <[hidden email]>
AuthorDate: Tue Mar 24 15:58:16 2020 +0000

    Increase coverage of Range header tests
---
 .../catalina/servlets/TestDefaultServletPut.java   | 29 +++++---
 .../servlets/TestDefaultServletRangeRequests.java  | 78 +++++++++++++++-------
 2 files changed, 72 insertions(+), 35 deletions(-)

diff --git a/test/org/apache/catalina/servlets/TestDefaultServletPut.java b/test/org/apache/catalina/servlets/TestDefaultServletPut.java
index 915c448..09c30ff 100644
--- a/test/org/apache/catalina/servlets/TestDefaultServletPut.java
+++ b/test/org/apache/catalina/servlets/TestDefaultServletPut.java
@@ -52,27 +52,30 @@ public class TestDefaultServletPut extends TomcatBaseTest {
 
         // Valid partial PUT
         parameterSets.add(new Object[] {
-                "Content-Range: bytes=0-" + PATCH_LEN + "/" + START_LEN + CRLF, Boolean.TRUE, END_TEXT });
+                "Content-Range: bytes=0-" + PATCH_LEN + "/" + START_LEN + CRLF, Boolean.TRUE, END_TEXT, Boolean.TRUE });
         // Full PUT
         parameterSets.add(new Object[] {
-                "", null, PATCH_TEXT });
+                "", null, PATCH_TEXT, Boolean.TRUE });
         // Invalid range
         parameterSets.add(new Object[] {
-                "Content-Range: apples=0-" + PATCH_LEN + "/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT });
+                "Content-Range: apples=0-" + PATCH_LEN + "/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT, Boolean.TRUE });
         parameterSets.add(new Object[] {
-                "Content-Range: bytes00-" + PATCH_LEN + "/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT });
+                "Content-Range: bytes00-" + PATCH_LEN + "/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT, Boolean.TRUE });
         parameterSets.add(new Object[] {
-                "Content-Range: bytes=9-7/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT });
+                "Content-Range: bytes=9-7/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT, Boolean.TRUE });
         parameterSets.add(new Object[] {
-                "Content-Range: bytes=-7/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT });
+                "Content-Range: bytes=-7/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT, Boolean.TRUE });
         parameterSets.add(new Object[] {
-                "Content-Range: bytes=9-/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT });
+                "Content-Range: bytes=9-/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT, Boolean.TRUE });
         parameterSets.add(new Object[] {
-                "Content-Range: bytes=9-X/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT });
+                "Content-Range: bytes=9-X/" + START_LEN + CRLF, Boolean.FALSE, START_TEXT, Boolean.TRUE });
         parameterSets.add(new Object[] {
-                "Content-Range: bytes=0-5/" + CRLF, Boolean.FALSE, START_TEXT });
+                "Content-Range: bytes=0-5/" + CRLF, Boolean.FALSE, START_TEXT, Boolean.TRUE });
         parameterSets.add(new Object[] {
-                "Content-Range: bytes=0-5/0x5" + CRLF, Boolean.FALSE, START_TEXT });
+                "Content-Range: bytes=0-5/0x5" + CRLF, Boolean.FALSE, START_TEXT, Boolean.TRUE });
+        // Valid partial PUT but partial PUT is disabled
+        parameterSets.add(new Object[] {
+                "Content-Range: bytes=0-" + PATCH_LEN + "/" + START_LEN + CRLF, Boolean.TRUE, START_TEXT, Boolean.FALSE });
 
         return parameterSets;
     }
@@ -89,6 +92,9 @@ public class TestDefaultServletPut extends TomcatBaseTest {
     @Parameter(2)
     public String expectedEndText;
 
+    @Parameter(3)
+    public boolean allowPartialPut;
+
     @Override
     public void setUp() throws Exception {
         super.setUp();
@@ -107,6 +113,7 @@ public class TestDefaultServletPut extends TomcatBaseTest {
 
         Wrapper w = Tomcat.addServlet(ctxt, "default", DefaultServlet.class.getName());
         w.addInitParameter("readonly", "false");
+        w.addInitParameter("allowPartialPut", Boolean.toString(allowPartialPut));
         ctxt.addServletMappingDecoded("/", "default");
 
         tomcat.start();
@@ -145,7 +152,7 @@ public class TestDefaultServletPut extends TomcatBaseTest {
         if (contentRangeHeaderValid == null) {
             // Not present (so will do a full PUT, replacing the existing)
             Assert.assertTrue(putClient.isResponse204());
-        } else if (contentRangeHeaderValid.booleanValue()) {
+        } else if (contentRangeHeaderValid.booleanValue() && allowPartialPut) {
             // Valid
             Assert.assertTrue(putClient.isResponse204());
         } else {
diff --git a/test/org/apache/catalina/servlets/TestDefaultServletRangeRequests.java b/test/org/apache/catalina/servlets/TestDefaultServletRangeRequests.java
index c50cc4b..cd69501 100644
--- a/test/org/apache/catalina/servlets/TestDefaultServletRangeRequests.java
+++ b/test/org/apache/catalina/servlets/TestDefaultServletRangeRequests.java
@@ -33,11 +33,12 @@ import org.apache.catalina.Context;
 import org.apache.catalina.startup.Tomcat;
 import org.apache.catalina.startup.TomcatBaseTest;
 import org.apache.tomcat.util.buf.ByteChunk;
+import org.apache.tomcat.util.http.FastHttpDateFormat;
 
 @RunWith(Parameterized.class)
 public class TestDefaultServletRangeRequests extends TomcatBaseTest {
 
-    @Parameterized.Parameters(name = "{index} rangeHeader [{0}]")
+    @Parameterized.Parameters(name = "{index} rangeHeader [{0}], ifRangeHeader [{1}]")
     public static Collection<Object[]> parameters() {
 
         // Get the length of the file used for this test
@@ -45,46 +46,68 @@ public class TestDefaultServletRangeRequests extends TomcatBaseTest {
         File index = new File("test/webapp/index.html");
         long len = index.length();
         String strLen = Long.toString(len);
+        String lastModified = FastHttpDateFormat.formatDate(index.lastModified());
 
         List<Object[]> parameterSets = new ArrayList<>();
 
-        parameterSets.add(new Object[] { "", Integer.valueOf(200), strLen, "" });
+        parameterSets.add(new Object[] { "", null, Integer.valueOf(200), strLen, "" });
         // Invalid
-        parameterSets.add(new Object[] { "bytes", Integer.valueOf(416), "", "*/" + len });
-        parameterSets.add(new Object[] { "bytes=", Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "bytes", null, Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "bytes=", null, Integer.valueOf(416), "", "*/" + len });
         // Invalid with unknown type
-        parameterSets.add(new Object[] { "unknown", Integer.valueOf(416), "", "*/" + len });
-        parameterSets.add(new Object[] { "unknown=", Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "unknown", null, Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "unknown=", null, Integer.valueOf(416), "", "*/" + len });
         // Invalid ranges
-        parameterSets.add(new Object[] { "bytes=-", Integer.valueOf(416), "", "*/" + len });
-        parameterSets.add(new Object[] { "bytes=10-b", Integer.valueOf(416), "", "*/" + len });
-        parameterSets.add(new Object[] { "bytes=b-10", Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "bytes=-", null, Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "bytes=10-b", null, Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "bytes=b-10", null, Integer.valueOf(416), "", "*/" + len });
+        // Invalid ranges (out of range)
+        parameterSets.add(new Object[] { "bytes=1000-2000", null, Integer.valueOf(416), "", "*/" + len });
         // Invalid no equals
-        parameterSets.add(new Object[] { "bytes 1-10", Integer.valueOf(416), "", "*/" + len });
-        parameterSets.add(new Object[] { "bytes1-10", Integer.valueOf(416), "", "*/" + len });
-        parameterSets.add(new Object[] { "bytes10-", Integer.valueOf(416), "", "*/" + len });
-        parameterSets.add(new Object[] { "bytes-10", Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "bytes 1-10", null, Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "bytes1-10", null, Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "bytes10-", null, Integer.valueOf(416), "", "*/" + len });
+        parameterSets.add(new Object[] { "bytes-10", null, Integer.valueOf(416), "", "*/" + len });
         // Unknown types
-        parameterSets.add(new Object[] { "unknown=1-2", Integer.valueOf(200), strLen, "" });
-        parameterSets.add(new Object[] { "bytesX=1-2", Integer.valueOf(200), strLen, "" });
-        parameterSets.add(new Object[] { "Xbytes=1-2", Integer.valueOf(200), strLen, "" });
+        parameterSets.add(new Object[] { "unknown=1-2", null, Integer.valueOf(200), strLen, "" });
+        parameterSets.add(new Object[] { "bytesX=1-2", null, Integer.valueOf(200), strLen, "" });
+        parameterSets.add(new Object[] { "Xbytes=1-2", null, Integer.valueOf(200), strLen, "" });
         // Valid range
-        parameterSets.add(new Object[] { "bytes=0-9", Integer.valueOf(206), "10", "0-9/" + len });
-        parameterSets.add(new Object[] { "bytes=-100", Integer.valueOf(206), "100", (len - 100) + "-" + (len - 1) + "/" + len });
-        parameterSets.add(new Object[] { "bytes=100-", Integer.valueOf(206), "" + (len - 100), "100-" + (len - 1) + "/" + len });
+        parameterSets.add(new Object[] {
+                "bytes=0-9", null, Integer.valueOf(206), "10", "0-9/" + len });
+        parameterSets.add(new Object[] {
+                "bytes=-100", null, Integer.valueOf(206), "100", (len - 100) + "-" + (len - 1) + "/" + len });
+        parameterSets.add(new Object[] {
+                "bytes=100-", null, Integer.valueOf(206), "" + (len - 100), "100-" + (len - 1) + "/" + len });
         // Valid range (too much)
-        parameterSets.add(new Object[] { "bytes=0-1000", Integer.valueOf(206), strLen, "0-" +  (len - 1) + "/" + len });
-        parameterSets.add(new Object[] { "bytes=-1000", Integer.valueOf(206), strLen, "0-" + (len - 1) + "/" + len });
+        parameterSets.add(new Object[] {
+                "bytes=0-1000", null, Integer.valueOf(206), strLen, "0-" +  (len - 1) + "/" + len });
+        parameterSets.add(new Object[] {
+                "bytes=-1000", null, Integer.valueOf(206), strLen, "0-" + (len - 1) + "/" + len });
+
+        /* If-Range tests */
+        // Valid
+        parameterSets.add(new Object[] {
+                "bytes=0-9", lastModified, Integer.valueOf(206), "10", "0-9/" + len });
+        // Nonsense date (return whole entity)
+        parameterSets.add(new Object[] {
+                "bytes=0-9", "a-b-c", Integer.valueOf(200), strLen, ""});
+        // Different date (return whole entity)
+        parameterSets.add(new Object[] {
+                "bytes=0-9", FastHttpDateFormat.formatDate(1000), Integer.valueOf(200), strLen, ""});
+
         return parameterSets;
     }
 
     @Parameter(0)
     public String rangeHeader;
     @Parameter(1)
-    public int responseCodeExpected;
+    public String ifRangeHeader;
     @Parameter(2)
-    public String contentLengthExpected;
+    public int responseCodeExpected;
     @Parameter(3)
+    public String contentLengthExpected;
+    @Parameter(4)
     public String responseRangeExpected;
 
     @Test
@@ -105,7 +128,14 @@ public class TestDefaultServletRangeRequests extends TomcatBaseTest {
         ByteChunk responseBody = new ByteChunk();
         Map<String,List<String>> responseHeaders = new HashMap<>();
 
-        int rc = getUrl(path, responseBody, buildRangeHeader(rangeHeader), responseHeaders);
+        Map<String,List<String>> requestHeaders = buildRangeHeader(rangeHeader);
+        if (ifRangeHeader != null) {
+            List<String> values = new ArrayList<>(1);
+            values.add(ifRangeHeader);
+            requestHeaders.put("If-Range", values);
+        }
+
+        int rc = getUrl(path, responseBody, requestHeaders, responseHeaders);
 
         // Check the result
         Assert.assertEquals(responseCodeExpected, rc);


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]