Bug 1467554 [wpt PR 11382] - Fix the TemporaryFile mode to prevent crashing., a=testonly
authorDavid H <dhdavvie@gmail.com>
Tue, 26 Jun 2018 02:57:08 +0000
changeset 812114 73b0e112fdfcff9d3bb465a9ec65ac257f149817
parent 812113 bdfa1e8cab79e1829a8361c05a8fa27816d99a53
child 812115 d97405360c9af7a51a24881e2664424f6b36b003
push id114453
push userrwood@mozilla.com
push dateThu, 28 Jun 2018 15:10:03 +0000
reviewerstestonly
bugs1467554, 11382
milestone63.0a1
Bug 1467554 [wpt PR 11382] - Fix the TemporaryFile mode to prevent crashing., a=testonly Automatic update from web-platform-testsFix the TemporaryFile mode to prevent crashing. (#11382) Setting the TemporaryFile mode to 'rw+b' in request.py causes an IOError when trying to write to the _buf. Removing the mode, and using the default 'w+b', fixes this issue. Added 2 tests that test for this -- wpt-commits: 13365c7a94a80b19e0c57b8fc76609e73aea2061 wpt-pr: 11382
testing/web-platform/tests/tools/wptserve/tests/functional/test_request.py
testing/web-platform/tests/tools/wptserve/wptserve/request.py
--- a/testing/web-platform/tests/tools/wptserve/tests/functional/test_request.py
+++ b/testing/web-platform/tests/tools/wptserve/tests/functional/test_request.py
@@ -1,14 +1,16 @@
 import unittest
 
 import pytest
 
 wptserve = pytest.importorskip("wptserve")
 from .base import TestUsingServer
+from wptserve.request import InputFile
+from urllib2 import HTTPError
 
 
 class TestInputFile(TestUsingServer):
     def test_seek(self):
         @wptserve.handlers.handler
         def handler(request, response):
             rv = []
             f = request.raw_input
@@ -30,28 +32,74 @@ class TestInputFile(TestUsingServer):
         route = ("POST", "/test/test_seek", handler)
         self.server.router.register(*route)
         resp = self.request(route[1], method="POST", body="12345ab\ncdef")
         self.assertEqual(200, resp.getcode())
         self.assertEqual(["ab", "7", "12345ab\n", "8", "cdef", "12",
                           "12345ab\ncdef", "12345ab\n", "cdef"],
                          resp.read().split(" "))
 
+    def test_seek_input_longer_than_buffer(self):
+
+        @wptserve.handlers.handler
+        def handler(request, response):
+            rv = []
+            f = request.raw_input
+            f.seek(5)
+            rv.append(f.read(2))
+            rv.append(f.tell())
+            f.seek(0)
+            rv.append(f.tell())
+            rv.append(f.tell())
+            return " ".join(str(item) for item in rv)
+
+        route = ("POST", "/test/test_seek", handler)
+        self.server.router.register(*route)
+
+        old_max_buf = InputFile.max_buffer_size
+        InputFile.max_buffer_size = 10
+        try:
+            resp = self.request(route[1], method="POST", body="1"*20)
+            self.assertEqual(200, resp.getcode())
+            self.assertEqual(["11", "7", "0", "0"],
+                            resp.read().split(" "))
+        finally:
+            InputFile.max_buffer_size = old_max_buf
+
     def test_iter(self):
         @wptserve.handlers.handler
         def handler(request, response):
             f = request.raw_input
             return " ".join(line for line in f)
 
         route = ("POST", "/test/test_iter", handler)
         self.server.router.register(*route)
         resp = self.request(route[1], method="POST", body="12345\nabcdef\r\nzyxwv")
         self.assertEqual(200, resp.getcode())
         self.assertEqual(["12345\n", "abcdef\r\n", "zyxwv"], resp.read().split(" "))
 
+    def test_iter_input_longer_than_buffer(self):
+
+        @wptserve.handlers.handler
+        def handler(request, response):
+            f = request.raw_input
+            return " ".join(line for line in f)
+
+        route = ("POST", "/test/test_iter", handler)
+        self.server.router.register(*route)
+
+        old_max_buf = InputFile.max_buffer_size
+        InputFile.max_buffer_size = 10
+        try:
+            resp = self.request(route[1], method="POST", body="12345\nabcdef\r\nzyxwv")
+            self.assertEqual(200, resp.getcode())
+            self.assertEqual(["12345\n", "abcdef\r\n", "zyxwv"], resp.read().split(" "))
+        finally:
+            InputFile.max_buffer_size = old_max_buf
+
 class TestRequest(TestUsingServer):
     def test_body(self):
         @wptserve.handlers.handler
         def handler(request, response):
             request.raw_input.seek(5)
             return request.body
 
         route = ("POST", "/test/test_body", handler)
--- a/testing/web-platform/tests/tools/wptserve/wptserve/request.py
+++ b/testing/web-platform/tests/tools/wptserve/wptserve/request.py
@@ -45,17 +45,17 @@ class InputFile(object):
     def __init__(self, rfile, length):
         """File-like object used to provide a seekable view of request body data"""
         self._file = rfile
         self.length = length
 
         self._file_position = 0
 
         if length > self.max_buffer_size:
-            self._buf = tempfile.TemporaryFile(mode="rw+b")
+            self._buf = tempfile.TemporaryFile()
         else:
             self._buf = StringIO.StringIO()
 
     @property
     def _buf_position(self):
         rv = self._buf.tell()
         assert rv <= self._file_position
         return rv