summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_doomentry.js
blob: 2ad0ee5525aa996586f799b65705d300baa8fffd (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
/**
 * Test for nsICacheStorage.asyncDoomURI().
 * It tests dooming
 *   - an existent inactive entry
 *   - a non-existent inactive entry
 *   - an existent active entry
 */

"use strict";

function doom(url, callback) {
  Services.cache2
    .diskCacheStorage(Services.loadContextInfo.default)
    .asyncDoomURI(createURI(url), "", {
      onCacheEntryDoomed(result) {
        callback(result);
      },
    });
}

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 write_entry() {
  asyncOpenCacheEntry(
    "http://testentry/",
    "disk",
    Ci.nsICacheStorage.OPEN_TRUNCATE,
    null,
    function (status, entry) {
      write_entry_cont(entry, entry.openOutputStream(0, -1));
    }
  );
}

function write_entry_cont(entry, ostream) {
  var data = "testdata";
  write_and_check(ostream, data, data.length);
  ostream.close();
  entry.close();
  doom("http://testentry/", check_doom1);
}

function check_doom1(status) {
  Assert.equal(status, Cr.NS_OK);
  doom("http://nonexistententry/", check_doom2);
}

function check_doom2(status) {
  Assert.equal(status, Cr.NS_ERROR_NOT_AVAILABLE);
  asyncOpenCacheEntry(
    "http://testentry/",
    "disk",
    Ci.nsICacheStorage.OPEN_TRUNCATE,
    null,
    function (status, entry) {
      write_entry2(entry, entry.openOutputStream(0, -1));
    }
  );
}

var gEntry;
var gOstream;
function write_entry2(entry, ostream) {
  // write some data and doom the entry while it is active
  var data = "testdata";
  write_and_check(ostream, data, data.length);
  gEntry = entry;
  gOstream = ostream;
  doom("http://testentry/", check_doom3);
}

function check_doom3(status) {
  Assert.equal(status, Cr.NS_OK);
  // entry was doomed but writing should still succeed
  var data = "testdata";
  write_and_check(gOstream, data, data.length);
  gOstream.close();
  gEntry.close();
  // dooming the same entry again should fail
  doom("http://testentry/", check_doom4);
}

function check_doom4(status) {
  Assert.equal(status, Cr.NS_ERROR_NOT_AVAILABLE);
  do_test_finished();
}

function run_test() {
  do_get_profile();

  // clear the cache
  evict_cache_entries();
  write_entry();
  do_test_pending();
}