netwerk/test/unit/test_compressappend.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 //
michael@0 2 // Test that data can be appended to a cache entry even when the data is
michael@0 3 // compressed by the cache compression feature - bug 648429.
michael@0 4 //
michael@0 5
michael@0 6 function write_and_check(str, data, len)
michael@0 7 {
michael@0 8 var written = str.write(data, len);
michael@0 9 if (written != len) {
michael@0 10 do_throw("str.write has not written all data!\n" +
michael@0 11 " Expected: " + len + "\n" +
michael@0 12 " Actual: " + written + "\n");
michael@0 13 }
michael@0 14 }
michael@0 15
michael@0 16 function TestAppend(compress, callback)
michael@0 17 {
michael@0 18 this._compress = compress;
michael@0 19 this._callback = callback;
michael@0 20 this.run();
michael@0 21 }
michael@0 22
michael@0 23 TestAppend.prototype = {
michael@0 24 _compress: false,
michael@0 25 _callback: null,
michael@0 26
michael@0 27 run: function() {
michael@0 28 evict_cache_entries();
michael@0 29 asyncOpenCacheEntry("http://data/",
michael@0 30 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
michael@0 31 this.writeData.bind(this));
michael@0 32 },
michael@0 33
michael@0 34 writeData: function(status, entry) {
michael@0 35 do_check_eq(status, Cr.NS_OK);
michael@0 36 if (this._compress)
michael@0 37 entry.setMetaDataElement("uncompressed-len", "0");
michael@0 38 var os = entry.openOutputStream(0);
michael@0 39 write_and_check(os, "12345", 5);
michael@0 40 os.close();
michael@0 41 entry.close();
michael@0 42 asyncOpenCacheEntry("http://data/",
michael@0 43 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null,
michael@0 44 this.appendData.bind(this));
michael@0 45 },
michael@0 46
michael@0 47 appendData: function(status, entry) {
michael@0 48 do_check_eq(status, Cr.NS_OK);
michael@0 49 var os = entry.openOutputStream(entry.storageDataSize);
michael@0 50 write_and_check(os, "abcde", 5);
michael@0 51 os.close();
michael@0 52 entry.close();
michael@0 53
michael@0 54 asyncOpenCacheEntry("http://data/",
michael@0 55 "disk", Ci.nsICacheStorage.OPEN_READONLY, null,
michael@0 56 this.checkData.bind(this));
michael@0 57 },
michael@0 58
michael@0 59 checkData: function(status, entry) {
michael@0 60 do_check_eq(status, Cr.NS_OK);
michael@0 61 var self = this;
michael@0 62 pumpReadStream(entry.openInputStream(0), function(str) {
michael@0 63 do_check_eq(str.length, 10);
michael@0 64 do_check_eq(str, "12345abcde");
michael@0 65 entry.close();
michael@0 66
michael@0 67 do_execute_soon(self._callback);
michael@0 68 });
michael@0 69 }
michael@0 70 };
michael@0 71
michael@0 72 function run_test() {
michael@0 73 do_get_profile();
michael@0 74 new TestAppend(false, run_test2);
michael@0 75 do_test_pending();
michael@0 76 }
michael@0 77
michael@0 78 function run_test2() {
michael@0 79 new TestAppend(true, do_test_finished);
michael@0 80 }

mercurial