summaryrefslogtreecommitdiffstats
path: root/modules/libjar/zipwriter/test/unit/test_editexisting.js
blob: 84b20690b9163e3c4eee061a2732f22f4b13e88f (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

// Values taken from using zipinfo to list the test.zip contents
var TESTS = [
  {
    name: "test.txt",
    size: 232,
    crc: 0x0373ac26,
    time: Date.UTC(2007, 4, 1, 20, 44, 55)
  },
  {
    name: "test.png",
    size: 3402,
    crc: 0x504a5c30,
    time: Date.UTC(2007, 4, 1, 20, 49, 39)
  }
];
var BADENTRY = "unknown.txt";

function run_test()
{
  // Copy our test zip to the tmp dir so we can modify it
  var testzip = do_get_file(DATA_DIR + "test.zip");
  testzip.copyTo(tmpDir, tmpFile.leafName);

  do_check_true(tmpFile.exists());

  zipW.open(tmpFile, PR_RDWR);

  for (var i = 0; i < TESTS.length; i++) {
    do_check_true(zipW.hasEntry(TESTS[i].name));
    var entry = zipW.getEntry(TESTS[i].name);
    do_check_true(entry != null);

    do_check_eq(entry.realSize, TESTS[i].size);
    do_check_eq(entry.CRC32, TESTS[i].crc);
    do_check_eq(entry.lastModifiedTime / PR_USEC_PER_MSEC, TESTS[i].time);
  }

  try {
    zipW.removeEntry(BADENTRY, false);
    do_throw("shouldn't be able to remove an entry that doesn't exist");
  }
  catch (e) {
    do_check_eq(e.result, Components.results.NS_ERROR_FILE_NOT_FOUND);
  }

  for (var i = 0; i < TESTS.length; i++) {
    zipW.removeEntry(TESTS[i].name, false);
  }

  zipW.close();

  // Certain platforms cache the file size so get a fresh file to check.
  tmpFile = tmpFile.clone();

  // Empty zip file should just be the end of central directory marker
  do_check_eq(tmpFile.fileSize, ZIP_EOCDR_HEADER_SIZE);
}