summaryrefslogtreecommitdiffstats
path: root/modules/libjar/test/unit/test_jarchannel.js
blob: 46eda869140c7d4badb9206482a1043c55abb67f (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 */

/**
 * Tests some basic jar channel functionality
 */


const {classes: Cc,
       interfaces: Ci,
       results: Cr,
       utils: Cu,
       Constructor: ctor
       } = Components;

Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/NetUtil.jsm");

const ios = Cc["@mozilla.org/network/io-service;1"].
                getService(Ci.nsIIOService);
const dirSvc = Cc["@mozilla.org/file/directory_service;1"].
                getService(Ci.nsIProperties);
const obs = Cc["@mozilla.org/observer-service;1"].
                getService(Ci.nsIObserverService);

const nsIBinaryInputStream = ctor("@mozilla.org/binaryinputstream;1",
                               "nsIBinaryInputStream",
                               "setInputStream"
                               );

const fileBase = "test_bug637286.zip";
const file = do_get_file("data/" + fileBase);
const jarBase = "jar:" + ios.newFileURI(file).spec + "!";
const tmpDir = dirSvc.get("TmpD", Ci.nsIFile);

function Listener(callback) {
    this._callback = callback;
}
Listener.prototype = {
    gotStartRequest: false,
    available: -1,
    gotStopRequest: false,
    QueryInterface: function(iid) {
        if (iid.equals(Ci.nsISupports) ||
            iid.equals(Ci.nsIRequestObserver))
            return this;
        throw Cr.NS_ERROR_NO_INTERFACE;
    },
    onDataAvailable: function(request, ctx, stream, offset, count) {
        try {
            this.available = stream.available();
            do_check_eq(this.available, count);
            // Need to consume stream to avoid assertion
            new nsIBinaryInputStream(stream).readBytes(count);
        }
        catch (ex) {
            do_throw(ex);
        }
    },
    onStartRequest: function(request, ctx) {
        this.gotStartRequest = true;
    },
    onStopRequest: function(request, ctx, status) {
        this.gotStopRequest = true;
        do_check_eq(status, 0);
        if (this._callback) {
            this._callback.call(null, this);
        }
    }
};

/**
 * Basic reading test for asynchronously opened jar channel
 */
function testAsync() {
    var uri = jarBase + "/inner40.zip";
    var chan = NetUtil.newChannel({uri: uri, loadUsingSystemPrincipal: true});
    do_check_true(chan.contentLength < 0);
    chan.asyncOpen2(new Listener(function(l) {
        do_check_true(chan.contentLength > 0);
        do_check_true(l.gotStartRequest);
        do_check_true(l.gotStopRequest);
        do_check_eq(l.available, chan.contentLength);

        run_next_test();
    }));
}

add_test(testAsync);
// Run same test again so we test the codepath for a zipcache hit
add_test(testAsync);

/**
 * Basic test for nsIZipReader.
 */
function testZipEntry() {
    var uri = jarBase + "/inner40.zip";
    var chan = NetUtil.newChannel({uri: uri, loadUsingSystemPrincipal: true})
                      .QueryInterface(Ci.nsIJARChannel);
    var entry = chan.zipEntry;
    do_check_true(entry.CRC32 == 0x8b635486);
    do_check_true(entry.realSize == 184);
    run_next_test();
}

add_test(testZipEntry);


/**
 * Basic reading test for synchronously opened jar channels
 */
add_test(function testSync() {
    var uri = jarBase + "/inner40.zip";
    var chan = NetUtil.newChannel({uri: uri, loadUsingSystemPrincipal: true});
    var stream = chan.open2();
    do_check_true(chan.contentLength > 0);
    do_check_eq(stream.available(), chan.contentLength);
    stream.close();
    stream.close(); // should still not throw

    run_next_test();
});


/**
 * Basic reading test for synchronously opened, nested jar channels
 */
add_test(function testSyncNested() {
    var uri = "jar:" + jarBase + "/inner40.zip!/foo";
    var chan = NetUtil.newChannel({uri: uri, loadUsingSystemPrincipal: true});
    var stream = chan.open2();
    do_check_true(chan.contentLength > 0);
    do_check_eq(stream.available(), chan.contentLength);
    stream.close();
    stream.close(); // should still not throw

    run_next_test();
});

/**
 * Basic reading test for asynchronously opened, nested jar channels
 */
add_test(function testAsyncNested(next) {
    var uri = "jar:" + jarBase + "/inner40.zip!/foo";
    var chan = NetUtil.newChannel({uri: uri, loadUsingSystemPrincipal: true});
    chan.asyncOpen2(new Listener(function(l) {
        do_check_true(chan.contentLength > 0);
        do_check_true(l.gotStartRequest);
        do_check_true(l.gotStopRequest);
        do_check_eq(l.available, chan.contentLength);

        run_next_test();
    }));
});

/**
 * Verify that file locks are released when closing a synchronously
 * opened jar channel stream
 */
add_test(function testSyncCloseUnlocks() {
    var copy = tmpDir.clone();
    copy.append(fileBase);
    file.copyTo(copy.parent, copy.leafName);
    var uri = "jar:" + ios.newFileURI(copy).spec + "!/inner40.zip";
    var chan = NetUtil.newChannel({uri: uri, loadUsingSystemPrincipal: true});
    var stream = chan.open2();
    do_check_true(chan.contentLength > 0);
    stream.close();

    // Drop any jar caches
    obs.notifyObservers(null, "chrome-flush-caches", null);

    try {
        copy.remove(false);
    }
    catch (ex) {
        do_throw(ex);
    }

    run_next_test();
});

/**
 * Verify that file locks are released when closing an asynchronously
 * opened jar channel stream
 */
add_test(function testAsyncCloseUnlocks() {
    var copy = tmpDir.clone();
    copy.append(fileBase);
    file.copyTo(copy.parent, copy.leafName);

    var uri = "jar:" + ios.newFileURI(copy).spec + "!/inner40.zip";
    var chan = NetUtil.newChannel({uri: uri, loadUsingSystemPrincipal: true});

    chan.asyncOpen2(new Listener(function (l) {
        do_check_true(chan.contentLength > 0);

        // Drop any jar caches
        obs.notifyObservers(null, "chrome-flush-caches", null);

        try {
            copy.remove(false);
        }
        catch (ex) {
            do_throw(ex);
        }

        run_next_test();
    }));
});


function run_test() {
  return run_next_test();
}