103 lines
3.1 KiB
Plaintext
103 lines
3.1 KiB
Plaintext
var fd = use("fd")
|
|
var miniz = use("miniz")
|
|
var utf8 = use("utf8")
|
|
|
|
function safe_unlink(p) { fd.unlink(p) } disruption {}
|
|
|
|
return {
|
|
create_and_read_zip: function() {
|
|
var ZIP_PATH = "miniz_test.zip"
|
|
var SOURCE_PATH = "miniz_source.txt"
|
|
var ENTRY_PATH = "sample/hello.txt"
|
|
var PAYLOAD = "Miniz integration test payload."
|
|
|
|
function write_text_file(path, text) {
|
|
var handle = fd.open(path, "w")
|
|
fd.write(handle, text)
|
|
fd.close(handle)
|
|
}
|
|
|
|
var error_msg = null
|
|
var do_test = function() {
|
|
write_text_file(SOURCE_PATH, PAYLOAD)
|
|
var source_blob = fd.slurp(SOURCE_PATH)
|
|
var writer = miniz.write(ZIP_PATH)
|
|
writer.add_file(ENTRY_PATH, source_blob)
|
|
writer = null
|
|
|
|
var zip_blob = fd.slurp(ZIP_PATH)
|
|
var reader = miniz.read(zip_blob)
|
|
|
|
if (!reader.exists(ENTRY_PATH))
|
|
error_msg = "entry missing in archive"
|
|
|
|
if (!error_msg) {
|
|
var extracted_blob = reader.slurp(ENTRY_PATH)
|
|
var extracted_text = utf8.decode(extracted_blob)
|
|
|
|
if (extracted_text != PAYLOAD)
|
|
error_msg = "extracted text mismatch"
|
|
}
|
|
} disruption {
|
|
if (!error_msg) error_msg = "test disrupted"
|
|
}
|
|
do_test()
|
|
safe_unlink(ZIP_PATH)
|
|
safe_unlink(SOURCE_PATH)
|
|
if (error_msg) return error_msg
|
|
},
|
|
|
|
list_and_count: function() {
|
|
var ZIP_PATH = "miniz_list_test.zip"
|
|
var ENTRY1 = "file1.txt"
|
|
var ENTRY2 = "dir/file2.txt"
|
|
|
|
var error_msg = null
|
|
var do_test = function() {
|
|
var writer = miniz.write(ZIP_PATH)
|
|
writer.add_file(ENTRY1, utf8.encode("content1"))
|
|
writer.add_file(ENTRY2, utf8.encode("content2"))
|
|
writer = null
|
|
|
|
var zip_blob = fd.slurp(ZIP_PATH)
|
|
var reader = miniz.read(zip_blob)
|
|
|
|
var listed = reader.list()
|
|
if (length(listed) != reader.count())
|
|
error_msg = "list/count mismatch"
|
|
if (!error_msg && length(listed) != 2)
|
|
error_msg = "unexpected entry count"
|
|
} disruption {
|
|
if (!error_msg) error_msg = "test disrupted"
|
|
}
|
|
do_test()
|
|
safe_unlink(ZIP_PATH)
|
|
if (error_msg) return error_msg
|
|
},
|
|
|
|
exists_check: function() {
|
|
var ZIP_PATH = "miniz_exists_test.zip"
|
|
var ENTRY_PATH = "existing.txt"
|
|
|
|
var error_msg = null
|
|
var do_test = function() {
|
|
var writer = miniz.write(ZIP_PATH)
|
|
writer.add_file(ENTRY_PATH, utf8.encode("data"))
|
|
writer = null
|
|
|
|
var zip_blob = fd.slurp(ZIP_PATH)
|
|
var reader = miniz.read(zip_blob)
|
|
|
|
if (!reader.exists(ENTRY_PATH))
|
|
error_msg = "existing entry not found"
|
|
if (!error_msg && reader.exists("nonexistent.txt"))
|
|
error_msg = "nonexistent entry reported as existing"
|
|
} disruption {
|
|
if (!error_msg) error_msg = "test disrupted"
|
|
}
|
|
do_test()
|
|
safe_unlink(ZIP_PATH)
|
|
if (error_msg) return error_msg
|
|
}
|
|
}
|