|
1 var _PSvc; |
|
2 function get_pref_service() { |
|
3 if (_PSvc) |
|
4 return _PSvc; |
|
5 |
|
6 return _PSvc = Cc["@mozilla.org/preferences-service;1"]. |
|
7 getService(Ci.nsIPrefBranch); |
|
8 } |
|
9 |
|
10 function gen_1MiB() |
|
11 { |
|
12 var i; |
|
13 var data="x"; |
|
14 for (i=0 ; i < 20 ; i++) |
|
15 data+=data; |
|
16 return data; |
|
17 } |
|
18 |
|
19 function write_and_check(str, data, len) |
|
20 { |
|
21 var written = str.write(data, len); |
|
22 if (written != len) { |
|
23 do_throw("str.write has not written all data!\n" + |
|
24 " Expected: " + len + "\n" + |
|
25 " Actual: " + written + "\n"); |
|
26 } |
|
27 } |
|
28 |
|
29 function write_datafile(status, entry) |
|
30 { |
|
31 do_check_eq(status, Cr.NS_OK); |
|
32 var os = entry.openOutputStream(0); |
|
33 var data = gen_1MiB(); |
|
34 |
|
35 // write 2MiB |
|
36 var i; |
|
37 for (i=0 ; i<2 ; i++) |
|
38 write_and_check(os, data, data.length); |
|
39 |
|
40 os.close(); |
|
41 entry.close(); |
|
42 |
|
43 // now change max_entry_size so that the existing entry is too big |
|
44 get_pref_service().setIntPref("browser.cache.disk.max_entry_size", 1024); |
|
45 |
|
46 // append to entry |
|
47 asyncOpenCacheEntry("http://data/", |
|
48 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null, |
|
49 append_datafile); |
|
50 } |
|
51 |
|
52 function append_datafile(status, entry) |
|
53 { |
|
54 do_check_eq(status, Cr.NS_OK); |
|
55 var os = entry.openOutputStream(entry.dataSize); |
|
56 var data = gen_1MiB(); |
|
57 |
|
58 // append 1MiB |
|
59 try { |
|
60 write_and_check(os, data, data.length); |
|
61 do_throw(); |
|
62 } |
|
63 catch (ex) { } |
|
64 |
|
65 // closing the ostream should fail in this case |
|
66 try { |
|
67 os.close(); |
|
68 do_throw(); |
|
69 } |
|
70 catch (ex) { } |
|
71 |
|
72 entry.close(); |
|
73 |
|
74 do_test_finished(); |
|
75 } |
|
76 |
|
77 function run_test() { |
|
78 if (newCacheBackEndUsed()) { |
|
79 // Needs limit preferences |
|
80 do_check_true(true, "This test doesn't run with the new cache backend, the test or the cache needs to be fixed"); |
|
81 return; |
|
82 } |
|
83 |
|
84 do_get_profile(); |
|
85 |
|
86 // clear the cache |
|
87 evict_cache_entries(); |
|
88 |
|
89 asyncOpenCacheEntry("http://data/", |
|
90 "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null, |
|
91 write_datafile); |
|
92 |
|
93 do_test_pending(); |
|
94 } |