Test large (>2G, >4G) files.
authorEmil Mikulic <emikulic@gmail.com>
Tue, 3 May 2011 10:43:04 +0000 (20:43 +1000)
committerEmil Mikulic <emikulic@gmail.com>
Sun, 28 Apr 2013 10:57:09 +0000 (20:57 +1000)
devel/test.py

index 6a81ea1..be6e29e 100755 (executable)
@@ -72,6 +72,16 @@ class TestHelper(unittest.TestCase):
             "You requested an invalid URL: %s\n"%path,
             'Generated by darkhttpd')
 
+    def drive_range(self, range_in, range_out, len_out, data_out,
+            status_out = "206 Partial Content"):
+        resp = Conn().get(self.url, req_hdrs = {"Range": "bytes="+range_in})
+        status, hdrs, body = parse(resp)
+        self.assertContains(status, status_out)
+        self.assertEquals(hdrs["Accept-Ranges"], "bytes")
+        self.assertEquals(hdrs["Content-Range"], "bytes "+range_out)
+        self.assertEquals(hdrs["Content-Length"], str(len_out))
+        self.assertEquals(body, data_out)
+
 class TestDirList(TestHelper):
     def setUp(self):
         self.fn = WWWROOT+"/escape#this"
@@ -209,16 +219,6 @@ class TestFileGet(TestHelper):
         self.assertFalse(hdrs.has_key("Content-Length"))
         self.assertFalse(hdrs.has_key("Content-Type"))
 
-    def drive_range(self, range_in, range_out, len_out, data_out,
-            status_out = "206 Partial Content"):
-        resp = Conn().get(self.url, req_hdrs = {"Range": "bytes="+range_in})
-        status, hdrs, body = parse(resp)
-        self.assertContains(status, status_out)
-        self.assertEquals(hdrs["Accept-Ranges"], "bytes")
-        self.assertEquals(hdrs["Content-Range"], "bytes "+range_out)
-        self.assertEquals(hdrs["Content-Length"], str(len_out))
-        self.assertEquals(body, data_out)
-
     def test_range_single(self):
         self.drive_range("5-5", "5-5/%d" % self.datalen,
             1, self.data[5])
@@ -274,6 +274,69 @@ class TestFileGet(TestHelper):
         status, hdrs, body = parse(resp)
         self.assertContains(status, "416 Requested Range Not Satisfiable")
 
+def make_large_file(fn, boundary, data):
+    big = 1<<33
+    assert big == 8589934592L
+    assert str(big) == "8589934592"
+
+    f = open(fn, "w")
+    pos = boundary - len(data)/2
+    f.seek(pos)
+    assert f.tell() == pos
+    assert f.tell() < boundary
+    f.write(data)
+    filesize = f.tell()
+    assert filesize == pos + len(data)
+    assert filesize > boundary
+    f.close()
+    return (pos, filesize)
+
+class TestLargeFile2G(TestHelper):
+    BOUNDARY = 1<<31
+
+    def setUp(self):
+        self.datalen = 4096
+        self.data = "".join(
+            [chr(random.randint(0,255)) for _ in xrange(self.datalen)])
+        self.url = "/big.jpeg"
+        self.fn = WWWROOT + self.url
+        self.filepos, self.filesize = make_large_file(
+            self.fn, self.BOUNDARY, self.data)
+
+    def tearDown(self):
+        os.unlink(self.fn)
+
+    def drive_start(self, ofs):
+        req_start = self.BOUNDARY + ofs
+        req_end = req_start + self.datalen/4 - 1
+        range_in = "%d-%d"%(req_start, req_end)
+        range_out = "%s/%d"%(range_in, self.filesize)
+
+        data_start = req_start - self.filepos
+        data_end = data_start + self.datalen/4
+
+        self.drive_range(range_in, range_out, self.datalen/4,
+            self.data[data_start:data_end])
+
+    def test_largefile_head(self):
+        resp = Conn().get(self.url, method="HEAD")
+        status, hdrs, body = parse(resp)
+        self.assertContains(status, "200 OK")
+        self.assertEquals(hdrs["Accept-Ranges"], "bytes")
+        self.assertEquals(hdrs["Content-Length"], str(self.filesize))
+        self.assertEquals(hdrs["Content-Type"], "image/jpeg")
+
+    def test_largefile__3(self): self.drive_start(-3)
+    def test_largefile__2(self): self.drive_start(-2)
+    def test_largefile__1(self): self.drive_start(-1)
+    def test_largefile_0(self): self.drive_start(0)
+    def test_largefile_1(self): self.drive_start(1)
+    def test_largefile_2(self): self.drive_start(2)
+    def test_largefile_3(self): self.drive_start(3)
+
+class TestLargeFile4G(TestLargeFile2G):
+    BOUNDARY = 1<<32
+
 if __name__ == '__main__':
     setUpModule()
     unittest.main()