Initial commit
[yaffs-website] / node_modules / tar / test / extract.js
1 // Set the umask, so that it works the same everywhere.
2 process.umask(parseInt('22', 8))
3
4 var tap = require("tap")
5   , tar = require("../tar.js")
6   , fs = require("fs")
7   , path = require("path")
8   , file = path.resolve(__dirname, "fixtures/c.tar")
9   , target = path.resolve(__dirname, "tmp/extract-test")
10   , index = 0
11   , fstream = require("fstream")
12
13   , ee = 0
14   , expectEntries =
15 [ { path: 'c.txt',
16     mode: '644',
17     type: '0',
18     depth: undefined,
19     size: 513,
20     linkpath: '',
21     nlink: undefined,
22     dev: undefined,
23     ino: undefined },
24   { path: 'cc.txt',
25     mode: '644',
26     type: '0',
27     depth: undefined,
28     size: 513,
29     linkpath: '',
30     nlink: undefined,
31     dev: undefined,
32     ino: undefined },
33   { path: 'r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d/e/r/-/p/a/t/h/cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
34     mode: '644',
35     type: '0',
36     depth: undefined,
37     size: 100,
38     linkpath: '',
39     nlink: undefined,
40     dev: undefined,
41     ino: undefined },
42   { path: 'Ω.txt',
43     mode: '644',
44     type: '0',
45     depth: undefined,
46     size: 2,
47     linkpath: '',
48     nlink: undefined,
49     dev: undefined,
50     ino: undefined },
51   { path: 'Ω.txt',
52     mode: '644',
53     type: '0',
54     depth: undefined,
55     size: 2,
56     linkpath: '',
57     nlink: 1,
58     dev: 234881026,
59     ino: 51693379 },
60   { path: '200ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
61     mode: '644',
62     type: '0',
63     depth: undefined,
64     size: 200,
65     linkpath: '',
66     nlink: 1,
67     dev: 234881026,
68     ino: 51681874 },
69   { path: '200ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
70     mode: '644',
71     type: '0',
72     depth: undefined,
73     size: 201,
74     linkpath: '',
75     nlink: undefined,
76     dev: undefined,
77     ino: undefined },
78   { path: '200LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL',
79     mode: '777',
80     type: '2',
81     depth: undefined,
82     size: 0,
83     linkpath: '200ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
84     nlink: undefined,
85     dev: undefined,
86     ino: undefined },
87   { path: '200-hard',
88     mode: '644',
89     type: '0',
90     depth: undefined,
91     size: 200,
92     linkpath: '',
93     nlink: 2,
94     dev: 234881026,
95     ino: 51681874 },
96   { path: '200ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
97     mode: '644',
98     type: '1',
99     depth: undefined,
100     size: 0,
101     linkpath: path.resolve(target, '200-hard'),
102     nlink: 2,
103     dev: 234881026,
104     ino: 51681874 } ]
105
106   , ef = 0
107   , expectFiles =
108 [ { path: '',
109     mode: '40755',
110     type: 'Directory',
111     depth: 0,
112     linkpath: undefined },
113   { path: '/200-hard',
114     mode: '100644',
115     type: 'File',
116     depth: 1,
117     size: 200,
118     linkpath: undefined,
119     nlink: 2 },
120   { path: '/200LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL',
121     mode: '120777',
122     type: 'SymbolicLink',
123     depth: 1,
124     size: 200,
125     linkpath: '200ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
126     nlink: 1 },
127   { path: '/200ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
128     mode: '100644',
129     type: 'Link',
130     depth: 1,
131     size: 200,
132     linkpath: path.join(target, '200-hard'),
133     nlink: 2 },
134   { path: '/c.txt',
135     mode: '100644',
136     type: 'File',
137     depth: 1,
138     size: 513,
139     linkpath: undefined,
140     nlink: 1 },
141   { path: '/cc.txt',
142     mode: '100644',
143     type: 'File',
144     depth: 1,
145     size: 513,
146     linkpath: undefined,
147     nlink: 1 },
148   { path: '/r',
149     mode: '40755',
150     type: 'Directory',
151     depth: 1,
152     linkpath: undefined },
153   { path: '/r/e',
154     mode: '40755',
155     type: 'Directory',
156     depth: 2,
157     linkpath: undefined },
158   { path: '/r/e/a',
159     mode: '40755',
160     type: 'Directory',
161     depth: 3,
162     linkpath: undefined },
163   { path: '/r/e/a/l',
164     mode: '40755',
165     type: 'Directory',
166     depth: 4,
167     linkpath: undefined },
168   { path: '/r/e/a/l/l',
169     mode: '40755',
170     type: 'Directory',
171     depth: 5,
172     linkpath: undefined },
173   { path: '/r/e/a/l/l/y',
174     mode: '40755',
175     type: 'Directory',
176     depth: 6,
177     linkpath: undefined },
178   { path: '/r/e/a/l/l/y/-',
179     mode: '40755',
180     type: 'Directory',
181     depth: 7,
182     linkpath: undefined },
183   { path: '/r/e/a/l/l/y/-/d',
184     mode: '40755',
185     type: 'Directory',
186     depth: 8,
187     linkpath: undefined },
188   { path: '/r/e/a/l/l/y/-/d/e',
189     mode: '40755',
190     type: 'Directory',
191     depth: 9,
192     linkpath: undefined },
193   { path: '/r/e/a/l/l/y/-/d/e/e',
194     mode: '40755',
195     type: 'Directory',
196     depth: 10,
197     linkpath: undefined },
198   { path: '/r/e/a/l/l/y/-/d/e/e/p',
199     mode: '40755',
200     type: 'Directory',
201     depth: 11,
202     linkpath: undefined },
203   { path: '/r/e/a/l/l/y/-/d/e/e/p/-',
204     mode: '40755',
205     type: 'Directory',
206     depth: 12,
207     linkpath: undefined },
208   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f',
209     mode: '40755',
210     type: 'Directory',
211     depth: 13,
212     linkpath: undefined },
213   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o',
214     mode: '40755',
215     type: 'Directory',
216     depth: 14,
217     linkpath: undefined },
218   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o/l',
219     mode: '40755',
220     type: 'Directory',
221     depth: 15,
222     linkpath: undefined },
223   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d',
224     mode: '40755',
225     type: 'Directory',
226     depth: 16,
227     linkpath: undefined },
228   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d/e',
229     mode: '40755',
230     type: 'Directory',
231     depth: 17,
232     linkpath: undefined },
233   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d/e/r',
234     mode: '40755',
235     type: 'Directory',
236     depth: 18,
237     linkpath: undefined },
238   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d/e/r/-',
239     mode: '40755',
240     type: 'Directory',
241     depth: 19,
242     linkpath: undefined },
243   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d/e/r/-/p',
244     mode: '40755',
245     type: 'Directory',
246     depth: 20,
247     linkpath: undefined },
248   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d/e/r/-/p/a',
249     mode: '40755',
250     type: 'Directory',
251     depth: 21,
252     linkpath: undefined },
253   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d/e/r/-/p/a/t',
254     mode: '40755',
255     type: 'Directory',
256     depth: 22,
257     linkpath: undefined },
258   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d/e/r/-/p/a/t/h',
259     mode: '40755',
260     type: 'Directory',
261     depth: 23,
262     linkpath: undefined },
263   { path: '/r/e/a/l/l/y/-/d/e/e/p/-/f/o/l/d/e/r/-/p/a/t/h/cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc',
264     mode: '100644',
265     type: 'File',
266     depth: 24,
267     size: 100,
268     linkpath: undefined,
269     nlink: 1 },
270   { path: '/Ω.txt',
271     mode: '100644',
272     type: 'File',
273     depth: 1,
274     size: 2,
275     linkpath: undefined,
276     nlink: 1 } ]
277
278
279
280 // The extract class basically just pipes the input
281 // to a Reader, and then to a fstream.DirWriter
282
283 // So, this is as much a test of fstream.Reader and fstream.Writer
284 // as it is of tar.Extract, but it sort of makes sense.
285
286 tap.test("preclean", function (t) {
287   require("rimraf").sync(__dirname + "/tmp/extract-test")
288   t.pass("cleaned!")
289   t.end()
290 })
291
292 tap.test("extract test", function (t) {
293   var extract = tar.Extract(target)
294   var inp = fs.createReadStream(file)
295
296   // give it a weird buffer size to try to break in odd places
297   inp.bufferSize = 1234
298
299   inp.pipe(extract)
300
301   extract.on("end", function () {
302     t.equal(ee, expectEntries.length, "should see "+ee+" entries")
303
304     // should get no more entries after end
305     extract.removeAllListeners("entry")
306     extract.on("entry", function (e) {
307       t.fail("Should not get entries after end!")
308     })
309
310     next()
311   })
312
313   extract.on("entry", function (entry) {
314     var found =
315       { path: entry.path
316       , mode: entry.props.mode.toString(8)
317       , type: entry.props.type
318       , depth: entry.props.depth
319       , size: entry.props.size
320       , linkpath: entry.props.linkpath
321       , nlink: entry.props.nlink
322       , dev: entry.props.dev
323       , ino: entry.props.ino
324       }
325
326     var wanted = expectEntries[ee ++]
327
328     t.equivalent(found, wanted, "tar entry " + ee + " " + wanted.path)
329   })
330
331   function next () {
332     var r = fstream.Reader({ path: target
333                            , type: "Directory"
334                            // this is just to encourage consistency
335                            , sort: "alpha" })
336
337     r.on("ready", function () {
338       foundEntry(r)
339     })
340
341     r.on("end", finish)
342
343     function foundEntry (entry) {
344       var p = entry.path.substr(target.length)
345       var found =
346         { path: p
347         , mode: entry.props.mode.toString(8)
348         , type: entry.props.type
349         , depth: entry.props.depth
350         , size: entry.props.size
351         , linkpath: entry.props.linkpath
352         , nlink: entry.props.nlink
353         }
354
355       var wanted = expectFiles[ef ++]
356
357       t.has(found, wanted, "unpacked file " + ef + " " + wanted.path)
358
359       entry.on("entry", foundEntry)
360     }
361
362     function finish () {
363       t.equal(ef, expectFiles.length, "should have "+ef+" items")
364       t.end()
365     }
366   }
367 })