2022-12-06 15:04:04 +00:00
|
|
|
const test = require('brittle')
|
|
|
|
const concat = require('concat-stream')
|
|
|
|
const fs = require('fs')
|
|
|
|
const b4a = require('b4a')
|
|
|
|
const { Writable } = require('streamx')
|
|
|
|
const tar = require('..')
|
|
|
|
const fixtures = require('./fixtures')
|
2013-12-21 00:03:48 +00:00
|
|
|
|
2015-11-06 23:30:21 +00:00
|
|
|
test('one-file', function (t) {
|
2014-11-05 14:05:22 +00:00
|
|
|
t.plan(2)
|
2013-12-21 00:03:48 +00:00
|
|
|
|
2022-12-06 15:04:04 +00:00
|
|
|
const pack = tar.pack()
|
2013-12-21 00:03:48 +00:00
|
|
|
|
2014-11-05 14:05:22 +00:00
|
|
|
pack.entry({
|
2015-11-06 23:30:21 +00:00
|
|
|
name: 'test.txt',
|
|
|
|
mtime: new Date(1387580181000),
|
2022-12-06 15:04:04 +00:00
|
|
|
mode: 0o644,
|
2015-11-06 23:30:21 +00:00
|
|
|
uname: 'maf',
|
|
|
|
gname: 'staff',
|
|
|
|
uid: 501,
|
|
|
|
gid: 20
|
2014-11-05 14:05:22 +00:00
|
|
|
}, 'hello world\n')
|
2013-12-21 00:03:48 +00:00
|
|
|
|
2014-11-05 14:05:22 +00:00
|
|
|
pack.finalize()
|
2013-12-21 00:03:48 +00:00
|
|
|
|
2015-11-06 23:30:21 +00:00
|
|
|
pack.pipe(concat(function (data) {
|
2022-12-06 15:04:04 +00:00
|
|
|
t.is(data.length & 511, 0)
|
|
|
|
t.alike(data, fs.readFileSync(fixtures.ONE_FILE_TAR))
|
2014-11-05 14:05:22 +00:00
|
|
|
}))
|
|
|
|
})
|
2013-12-21 01:04:02 +00:00
|
|
|
|
2015-11-06 23:30:21 +00:00
|
|
|
test('multi-file', function (t) {
|
2014-11-05 14:05:22 +00:00
|
|
|
t.plan(2)
|
|
|
|
|
2022-12-06 15:04:04 +00:00
|
|
|
const pack = tar.pack()
|
2014-11-05 14:05:22 +00:00
|
|
|
|
|
|
|
pack.entry({
|
2015-11-06 23:30:21 +00:00
|
|
|
name: 'file-1.txt',
|
|
|
|
mtime: new Date(1387580181000),
|
2022-12-06 15:04:04 +00:00
|
|
|
mode: 0o644,
|
2015-11-06 23:30:21 +00:00
|
|
|
uname: 'maf',
|
|
|
|
gname: 'staff',
|
|
|
|
uid: 501,
|
|
|
|
gid: 20
|
2014-11-05 14:05:22 +00:00
|
|
|
}, 'i am file-1\n')
|
|
|
|
|
|
|
|
pack.entry({
|
2015-11-06 23:30:21 +00:00
|
|
|
name: 'file-2.txt',
|
|
|
|
mtime: new Date(1387580181000),
|
2022-12-06 15:04:04 +00:00
|
|
|
mode: 0o644,
|
2015-11-06 23:30:21 +00:00
|
|
|
size: 12,
|
|
|
|
uname: 'maf',
|
|
|
|
gname: 'staff',
|
|
|
|
uid: 501,
|
|
|
|
gid: 20
|
2014-11-05 14:05:22 +00:00
|
|
|
}).end('i am file-2\n')
|
|
|
|
|
|
|
|
pack.finalize()
|
|
|
|
|
2015-11-06 23:30:21 +00:00
|
|
|
pack.pipe(concat(function (data) {
|
2022-12-06 15:04:04 +00:00
|
|
|
t.is(data.length & 511, 0)
|
|
|
|
t.alike(data, fs.readFileSync(fixtures.MULTI_FILE_TAR))
|
2014-11-05 14:05:22 +00:00
|
|
|
}))
|
|
|
|
})
|
2013-12-21 01:04:02 +00:00
|
|
|
|
2016-04-05 14:40:00 +00:00
|
|
|
test('pax', function (t) {
|
|
|
|
t.plan(2)
|
|
|
|
|
2022-12-06 15:04:04 +00:00
|
|
|
const pack = tar.pack()
|
2016-04-05 14:40:00 +00:00
|
|
|
|
|
|
|
pack.entry({
|
|
|
|
name: 'pax.txt',
|
|
|
|
mtime: new Date(1387580181000),
|
2022-12-06 15:04:04 +00:00
|
|
|
mode: 0o644,
|
2016-04-05 14:40:00 +00:00
|
|
|
uname: 'maf',
|
|
|
|
gname: 'staff',
|
|
|
|
uid: 501,
|
|
|
|
gid: 20,
|
2019-02-04 09:46:57 +00:00
|
|
|
pax: { special: 'sauce' }
|
2016-04-05 14:40:00 +00:00
|
|
|
}, 'hello world\n')
|
|
|
|
|
|
|
|
pack.finalize()
|
|
|
|
|
|
|
|
pack.pipe(concat(function (data) {
|
2022-12-06 15:04:04 +00:00
|
|
|
t.is(data.length & 511, 0)
|
|
|
|
t.alike(data, fs.readFileSync(fixtures.PAX_TAR))
|
2016-04-05 14:40:00 +00:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2015-11-06 23:30:21 +00:00
|
|
|
test('types', function (t) {
|
2014-11-05 14:05:22 +00:00
|
|
|
t.plan(2)
|
2022-12-06 15:04:04 +00:00
|
|
|
|
|
|
|
const pack = tar.pack()
|
2014-11-05 14:05:22 +00:00
|
|
|
|
|
|
|
pack.entry({
|
2015-11-06 23:30:21 +00:00
|
|
|
name: 'directory',
|
|
|
|
mtime: new Date(1387580181000),
|
|
|
|
type: 'directory',
|
2022-12-06 15:04:04 +00:00
|
|
|
mode: 0o755,
|
2015-11-06 23:30:21 +00:00
|
|
|
uname: 'maf',
|
|
|
|
gname: 'staff',
|
|
|
|
uid: 501,
|
|
|
|
gid: 20
|
2014-11-05 14:05:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
pack.entry({
|
2015-11-06 23:30:21 +00:00
|
|
|
name: 'directory-link',
|
|
|
|
mtime: new Date(1387580181000),
|
|
|
|
type: 'symlink',
|
2014-11-05 14:05:22 +00:00
|
|
|
linkname: 'directory',
|
2022-12-06 15:04:04 +00:00
|
|
|
mode: 0o755,
|
2015-11-06 23:30:21 +00:00
|
|
|
uname: 'maf',
|
|
|
|
gname: 'staff',
|
|
|
|
uid: 501,
|
2016-04-19 12:52:43 +00:00
|
|
|
gid: 20,
|
2018-04-26 14:57:03 +00:00
|
|
|
size: 9 // Should convert to zero
|
2014-11-05 14:05:22 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
pack.finalize()
|
|
|
|
|
2015-11-06 23:30:21 +00:00
|
|
|
pack.pipe(concat(function (data) {
|
2022-12-06 15:04:04 +00:00
|
|
|
t.is(data.length & 511, 0)
|
|
|
|
t.alike(data, fs.readFileSync(fixtures.TYPES_TAR))
|
2014-11-05 14:05:22 +00:00
|
|
|
}))
|
|
|
|
})
|
2013-12-21 01:22:03 +00:00
|
|
|
|
2015-11-06 23:30:21 +00:00
|
|
|
test('long-name', function (t) {
|
2014-11-05 14:05:22 +00:00
|
|
|
t.plan(2)
|
2022-12-06 15:04:04 +00:00
|
|
|
|
|
|
|
const pack = tar.pack()
|
2014-11-05 14:05:22 +00:00
|
|
|
|
|
|
|
pack.entry({
|
2015-11-06 23:30:21 +00:00
|
|
|
name: 'my/file/is/longer/than/100/characters/and/should/use/the/prefix/header/foobarbaz/foobarbaz/foobarbaz/foobarbaz/foobarbaz/foobarbaz/filename.txt',
|
|
|
|
mtime: new Date(1387580181000),
|
|
|
|
type: 'file',
|
2022-12-06 15:04:04 +00:00
|
|
|
mode: 0o644,
|
2015-11-06 23:30:21 +00:00
|
|
|
uname: 'maf',
|
|
|
|
gname: 'staff',
|
|
|
|
uid: 501,
|
|
|
|
gid: 20
|
2014-11-05 14:05:22 +00:00
|
|
|
}, 'hello long name\n')
|
|
|
|
|
|
|
|
pack.finalize()
|
|
|
|
|
2015-11-06 23:30:21 +00:00
|
|
|
pack.pipe(concat(function (data) {
|
2022-12-06 15:04:04 +00:00
|
|
|
t.is(data.length & 511, 0)
|
|
|
|
t.alike(data, fs.readFileSync(fixtures.LONG_NAME_TAR))
|
2014-11-05 14:05:22 +00:00
|
|
|
}))
|
|
|
|
})
|
2013-12-21 01:32:52 +00:00
|
|
|
|
2016-04-21 05:48:51 +00:00
|
|
|
test('large-uid-gid', function (t) {
|
|
|
|
t.plan(2)
|
2022-12-06 15:04:04 +00:00
|
|
|
|
|
|
|
const pack = tar.pack()
|
2016-04-21 05:48:51 +00:00
|
|
|
|
|
|
|
pack.entry({
|
|
|
|
name: 'test.txt',
|
|
|
|
mtime: new Date(1387580181000),
|
2022-12-06 15:04:04 +00:00
|
|
|
mode: 0o644,
|
2016-04-21 05:48:51 +00:00
|
|
|
uname: 'maf',
|
|
|
|
gname: 'staff',
|
|
|
|
uid: 1000000001,
|
|
|
|
gid: 1000000002
|
|
|
|
}, 'hello world\n')
|
|
|
|
|
|
|
|
pack.finalize()
|
|
|
|
|
|
|
|
pack.pipe(concat(function (data) {
|
2022-12-06 15:04:04 +00:00
|
|
|
t.is(data.length & 511, 0)
|
|
|
|
t.alike(data, fs.readFileSync(fixtures.LARGE_UID_GID))
|
2016-04-21 05:48:51 +00:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
|
2015-11-06 23:30:21 +00:00
|
|
|
test('unicode', function (t) {
|
2014-11-05 14:05:22 +00:00
|
|
|
t.plan(2)
|
2022-12-06 15:04:04 +00:00
|
|
|
|
|
|
|
const pack = tar.pack()
|
2014-11-05 14:05:22 +00:00
|
|
|
|
|
|
|
pack.entry({
|
2015-11-06 23:30:21 +00:00
|
|
|
name: 'høstål.txt',
|
|
|
|
mtime: new Date(1387580181000),
|
|
|
|
type: 'file',
|
2022-12-06 15:04:04 +00:00
|
|
|
mode: 0o644,
|
2015-11-06 23:30:21 +00:00
|
|
|
uname: 'maf',
|
|
|
|
gname: 'staff',
|
|
|
|
uid: 501,
|
|
|
|
gid: 20
|
2014-11-05 14:05:22 +00:00
|
|
|
}, 'høllø\n')
|
|
|
|
|
|
|
|
pack.finalize()
|
|
|
|
|
2015-11-06 23:30:21 +00:00
|
|
|
pack.pipe(concat(function (data) {
|
2022-12-06 15:04:04 +00:00
|
|
|
t.is(data.length & 511, 0)
|
|
|
|
t.alike(data, fs.readFileSync(fixtures.UNICODE_TAR))
|
2014-11-05 14:05:22 +00:00
|
|
|
}))
|
2015-10-15 15:56:44 +00:00
|
|
|
})
|
2020-03-10 08:44:58 +00:00
|
|
|
|
2022-12-06 15:04:04 +00:00
|
|
|
test('backpressure', async function (t) {
|
|
|
|
const end = t.test('end')
|
|
|
|
end.plan(1)
|
2020-03-10 08:44:58 +00:00
|
|
|
|
2022-12-06 15:04:04 +00:00
|
|
|
const slowStream = new Writable({
|
|
|
|
highWaterMark: 1,
|
2020-03-10 08:44:58 +00:00
|
|
|
|
2022-12-06 15:04:04 +00:00
|
|
|
write (data, cb) {
|
|
|
|
setImmediate(cb)
|
|
|
|
}
|
2020-03-10 08:44:58 +00:00
|
|
|
})
|
|
|
|
|
2022-12-06 15:04:04 +00:00
|
|
|
slowStream.on('finish', () => end.pass())
|
|
|
|
|
|
|
|
const pack = tar.pack()
|
|
|
|
|
|
|
|
let later = false
|
2020-03-10 08:44:58 +00:00
|
|
|
|
2022-12-06 15:04:04 +00:00
|
|
|
setImmediate(() => { later = true })
|
2020-03-10 08:44:58 +00:00
|
|
|
|
2022-12-06 15:04:04 +00:00
|
|
|
pack
|
|
|
|
.on('end', () => t.ok(later))
|
|
|
|
.pipe(slowStream)
|
|
|
|
|
|
|
|
let i = 0
|
|
|
|
const next = () => {
|
2020-03-10 08:44:58 +00:00
|
|
|
if (++i < 25) {
|
2022-12-06 15:04:04 +00:00
|
|
|
const header = {
|
2020-03-10 08:44:58 +00:00
|
|
|
name: `file${i}.txt`,
|
|
|
|
mtime: new Date(1387580181000),
|
2022-12-06 15:04:04 +00:00
|
|
|
mode: 0o644,
|
2020-03-10 08:44:58 +00:00
|
|
|
uname: 'maf',
|
|
|
|
gname: 'staff',
|
|
|
|
uid: 501,
|
|
|
|
gid: 20
|
|
|
|
}
|
|
|
|
|
2022-12-06 15:04:04 +00:00
|
|
|
const buffer = b4a.alloc(1024)
|
2020-03-10 08:44:58 +00:00
|
|
|
|
|
|
|
pack.entry(header, buffer, next)
|
|
|
|
} else {
|
|
|
|
pack.finalize()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
2022-12-06 15:04:04 +00:00
|
|
|
|
|
|
|
await end
|
2020-03-10 08:44:58 +00:00
|
|
|
})
|