forked from kevva/decompress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
131 lines (105 loc) · 4.29 KB
/
Copy pathtest.js
File metadata and controls
131 lines (105 loc) · 4.29 KB
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
import fs from 'fs';
import path from 'path';
import isJpg from 'is-jpg';
import pathExists from 'path-exists';
import pify from 'pify';
import test from 'ava';
import m from '.';
const fsP = pify(fs);
test('extract file', async t => {
const tarFiles = await m(path.join(__dirname, 'fixtures', 'file.tar'));
const tarbzFiles = await m(path.join(__dirname, 'fixtures', 'file.tar.bz2'));
const targzFiles = await m(path.join(__dirname, 'fixtures', 'file.tar.gz'));
const zipFiles = await m(path.join(__dirname, 'fixtures', 'file.zip'));
t.is(tarFiles[0].path, 'test.jpg');
t.true(isJpg(tarFiles[0].data));
t.is(tarbzFiles[0].path, 'test.jpg');
t.true(isJpg(tarbzFiles[0].data));
t.is(targzFiles[0].path, 'test.jpg');
t.true(isJpg(targzFiles[0].data));
t.is(zipFiles[0].path, 'test.jpg');
t.true(isJpg(zipFiles[0].data));
});
test('extract file using buffer', async t => {
const tarBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.tar'));
const tarFiles = await m(tarBuf);
const tarbzBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.tar.bz2'));
const tarbzFiles = await m(tarbzBuf);
const targzBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.tar.gz'));
const targzFiles = await m(targzBuf);
const zipBuf = await fsP.readFile(path.join(__dirname, 'fixtures', 'file.zip'));
const zipFiles = await m(zipBuf);
t.is(tarFiles[0].path, 'test.jpg');
t.is(tarbzFiles[0].path, 'test.jpg');
t.is(targzFiles[0].path, 'test.jpg');
t.is(zipFiles[0].path, 'test.jpg');
});
test.serial('extract file to directory', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), __dirname);
t.is(files[0].path, 'test.jpg');
t.true(isJpg(files[0].data));
t.true(await pathExists(path.join(__dirname, 'test.jpg')));
await fsP.unlink(path.join(__dirname, 'test.jpg'));
});
test('extract symlink', async t => {
await m(path.join(__dirname, 'fixtures', 'symlink.tar'), __dirname, {strip: 1});
t.is(await fsP.realpath(path.join(__dirname, 'symlink')), path.join(__dirname, 'file.txt'));
await fsP.unlink(path.join(__dirname, 'symlink'));
await fsP.unlink(path.join(__dirname, 'file.txt'));
});
test('extract directory', async t => {
await m(path.join(__dirname, 'fixtures', 'directory.tar'), __dirname);
t.true(await pathExists(path.join(__dirname, 'directory')));
await fsP.rmdir(path.join(__dirname, 'directory'));
});
test('strip option', async t => {
const zipFiles = await m(path.join(__dirname, 'fixtures', 'strip.zip'), {strip: 1});
const tarFiles = await m(path.join(__dirname, 'fixtures', 'strip.tar'), {strip: 1});
t.is(zipFiles[0].path, 'test-strip.jpg');
t.true(isJpg(zipFiles[0].data));
t.is(tarFiles[0].path, 'test-strip.jpg');
t.true(isJpg(tarFiles[0].data));
});
test('filter option', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), {
filter: x => x.path !== 'test.jpg'
});
t.is(files.length, 0);
});
test('filter unsafe parent relative paths', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'slip.tar.gz'));
t.is(files.length, 0);
});
test('filter unsafe parent symlink traversal paths', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'ln-slip.tgz'));
t.is(files.length, 1);
});
test('do not follow symlinks', async t => {
const thru = path.join(__dirname, 'thru');
const victim = path.join(__dirname, 'real');
await fsP.mkdir(victim);
await fsP.symlink(path.relative(__dirname, victim), thru);
await t.throwsAsync(() => m(path.join(__dirname, 'fixtures', 'ln-no-follow.tgz'), __dirname));
t.false(fs.existsSync(path.join(victim, 'drive.txt')));
await fsP.unlink(thru);
await fsP.rmdir(victim);
});
test('map option', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), {
map: x => {
x.path = `unicorn-${x.path}`;
return x;
}
});
t.is(files[0].path, 'unicorn-test.jpg');
});
test.serial('set mtime', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), __dirname);
const stat = await fsP.stat(path.join(__dirname, 'test.jpg'));
t.deepEqual(files[0].mtime, stat.mtime);
await fsP.unlink(path.join(__dirname, 'test.jpg'));
});
test('return empty array if no plugins are set', async t => {
const files = await m(path.join(__dirname, 'fixtures', 'file.tar'), {plugins: []});
t.is(files.length, 0);
});