summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_alt-data_too_big.js
blob: d928394272df419642f29bc3c6d2aa17699bbedb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/**
 * Test for handling too big alternative data
 *
 *  - first we try to open an output stream for too big alt-data which must fail
 *    and leave original data intact
 *
 *  - then we open the output stream without passing predicted data size which
 *    succeeds but writing must fail later at the size limit and the original
 *    data must be kept
 */

"use strict";

var data = "data    ";
var altData = "alt-data";

function run_test() {
  do_get_profile();

  // Expand both data to 1MB
  for (let i = 0; i < 17; i++) {
    data += data;
    altData += altData;
  }

  // Set the limit so that the data fits but alt-data doesn't.
  Services.prefs.setIntPref("browser.cache.disk.max_entry_size", 1800);

  write_data();

  do_test_pending();
}

function write_data() {
  asyncOpenCacheEntry(
    "http://data/",
    "disk",
    Ci.nsICacheStorage.OPEN_NORMALLY,
    null,
    function(status, entry) {
      Assert.equal(status, Cr.NS_OK);

      var os = entry.openOutputStream(0, -1);
      var written = os.write(data, data.length);
      Assert.equal(written, data.length);
      os.close();

      open_big_altdata_output(entry);
    }
  );
}

function open_big_altdata_output(entry) {
  try {
    entry.openAlternativeOutputStream("text/binary", altData.length);
  } catch (e) {
    Assert.equal(e.result, Cr.NS_ERROR_FILE_TOO_BIG);
  }
  entry.close();

  check_entry(write_big_altdata);
}

function write_big_altdata() {
  asyncOpenCacheEntry(
    "http://data/",
    "disk",
    Ci.nsICacheStorage.OPEN_NORMALLY,
    null,
    function(status, entry) {
      Assert.equal(status, Cr.NS_OK);

      var os = entry.openAlternativeOutputStream("text/binary", -1);
      try {
        os.write(altData, altData.length);
      } catch (e) {
        Assert.equal(e.result, Cr.NS_ERROR_FILE_TOO_BIG);
      }
      os.close();
      entry.close();

      check_entry(do_test_finished);
    }
  );
}

function check_entry(cb) {
  asyncOpenCacheEntry(
    "http://data/",
    "disk",
    Ci.nsICacheStorage.OPEN_NORMALLY,
    null,
    function(status, entry) {
      Assert.equal(status, Cr.NS_OK);

      var is = null;
      try {
        is = entry.openAlternativeInputStream("text/binary");
      } catch (e) {
        Assert.equal(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
      }

      is = entry.openInputStream(0);
      pumpReadStream(is, function(read) {
        Assert.equal(read.length, data.length);
        is.close();
        entry.close();

        executeSoon(cb);
      });
    }
  );
}