Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_alt-data_too_big.js
blobfc9ca337fa6da4557e9eb4f1e76510c7627a295f
1 /**
2  * Test for handling too big alternative data
3  *
4  *  - first we try to open an output stream for too big alt-data which must fail
5  *    and leave original data intact
6  *
7  *  - then we open the output stream without passing predicted data size which
8  *    succeeds but writing must fail later at the size limit and the original
9  *    data must be kept
10  */
12 "use strict";
14 var data = "data    ";
15 var altData = "alt-data";
17 function run_test() {
18   do_get_profile();
20   // Expand both data to 1MB
21   for (let i = 0; i < 17; i++) {
22     data += data;
23     altData += altData;
24   }
26   // Set the limit so that the data fits but alt-data doesn't.
27   Services.prefs.setIntPref("browser.cache.disk.max_entry_size", 1800);
29   write_data();
31   do_test_pending();
34 function write_data() {
35   asyncOpenCacheEntry(
36     "http://data/",
37     "disk",
38     Ci.nsICacheStorage.OPEN_NORMALLY,
39     null,
40     function (status, entry) {
41       Assert.equal(status, Cr.NS_OK);
43       var os = entry.openOutputStream(0, -1);
44       var written = os.write(data, data.length);
45       Assert.equal(written, data.length);
46       os.close();
48       open_big_altdata_output(entry);
49     }
50   );
53 function open_big_altdata_output(entry) {
54   try {
55     entry.openAlternativeOutputStream("text/binary", altData.length);
56   } catch (e) {
57     Assert.equal(e.result, Cr.NS_ERROR_FILE_TOO_BIG);
58   }
59   entry.close();
61   check_entry(write_big_altdata);
64 function write_big_altdata() {
65   asyncOpenCacheEntry(
66     "http://data/",
67     "disk",
68     Ci.nsICacheStorage.OPEN_NORMALLY,
69     null,
70     function (status, entry) {
71       Assert.equal(status, Cr.NS_OK);
73       var os = entry.openAlternativeOutputStream("text/binary", -1);
74       try {
75         os.write(altData, altData.length);
76       } catch (e) {
77         Assert.equal(e.result, Cr.NS_ERROR_FILE_TOO_BIG);
78       }
79       os.close();
80       entry.close();
82       check_entry(do_test_finished);
83     }
84   );
87 function check_entry(cb) {
88   asyncOpenCacheEntry(
89     "http://data/",
90     "disk",
91     Ci.nsICacheStorage.OPEN_NORMALLY,
92     null,
93     function (status, entry) {
94       Assert.equal(status, Cr.NS_OK);
96       var is = null;
97       try {
98         is = entry.openAlternativeInputStream("text/binary");
99       } catch (e) {
100         Assert.equal(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
101       }
103       is = entry.openInputStream(0);
104       pumpReadStream(is, function (read) {
105         Assert.equal(read.length, data.length);
106         is.close();
107         entry.close();
109         executeSoon(cb);
110       });
111     }
112   );