summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug654926_doom_and_read.js
blob: 641b1d233e9e069535d23e7ef0e51eaa2911747c (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
"use strict";

function gen_1MiB() {
  var i;
  var data = "x";
  for (i = 0; i < 20; i++) {
    data += data;
  }
  return data;
}

function write_and_check(str, data, len) {
  var written = str.write(data, len);
  if (written != len) {
    do_throw(
      "str.write has not written all data!\n" +
        "  Expected: " +
        len +
        "\n" +
        "  Actual: " +
        written +
        "\n"
    );
  }
}

function make_input_stream_scriptable(input) {
  var wrapper = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
    Ci.nsIScriptableInputStream
  );
  wrapper.init(input);
  return wrapper;
}

function write_datafile(status, entry) {
  Assert.equal(status, Cr.NS_OK);
  var data = gen_1MiB();
  var os = entry.openOutputStream(0, data.length);

  write_and_check(os, data, data.length);

  os.close();
  entry.close();

  // open, doom, append, read
  asyncOpenCacheEntry(
    "http://data/",
    "disk",
    Ci.nsICacheStorage.OPEN_NORMALLY,
    null,
    test_read_after_doom
  );
}

function test_read_after_doom(status, entry) {
  Assert.equal(status, Cr.NS_OK);
  var data = gen_1MiB();
  var os = entry.openOutputStream(entry.dataSize, data.length);

  entry.asyncDoom(null);
  write_and_check(os, data, data.length);

  os.close();

  var is = entry.openInputStream(0);
  pumpReadStream(is, function(read) {
    Assert.equal(read.length, 2 * 1024 * 1024);
    is.close();

    entry.close();
    do_test_finished();
  });
}

function run_test() {
  do_get_profile();

  // clear the cache
  evict_cache_entries();

  asyncOpenCacheEntry(
    "http://data/",
    "disk",
    Ci.nsICacheStorage.OPEN_NORMALLY,
    null,
    write_datafile
  );

  do_test_pending();
}