-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathunixfs-format.spec.ts
434 lines (362 loc) · 12.1 KB
/
unixfs-format.spec.ts
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/* eslint-env mocha */
import { expect } from 'aegir/chai'
import loadFixture from 'aegir/fixtures'
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
import { type Mtime, UnixFS } from '../src/index.js'
import * as Pb from '../src/unixfs.js'
const PBData = Pb.Data
const raw = loadFixture('test/fixtures/raw.unixfs')
const directory = loadFixture('test/fixtures/directory.unixfs')
const file = loadFixture('test/fixtures/file.txt.unixfs')
const symlink = loadFixture('test/fixtures/symlink.txt.unixfs')
describe('unixfs-format', () => {
it('defaults to file', () => {
const data = new UnixFS()
expect(data.type).to.equal('file')
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(data.type).to.equal(unmarshaled.type)
expect(data.data).to.deep.equal(unmarshaled.data)
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
})
it('raw', () => {
const data = new UnixFS({
type: 'raw',
data: uint8ArrayFromString('bananas')
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(data.type).to.equal(unmarshaled.type)
expect(data.data).to.deep.equal(unmarshaled.data)
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
})
it('directory', () => {
const data = new UnixFS({
type: 'directory'
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(data.type).to.equal(unmarshaled.type)
expect(data.data).to.deep.equal(unmarshaled.data)
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
})
it('hamt-sharded-directory', () => {
const data = new UnixFS({
type: 'hamt-sharded-directory'
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(data.type).to.equal(unmarshaled.type)
expect(data.data).to.deep.equal(unmarshaled.data)
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
})
it('file', () => {
const data = new UnixFS({
type: 'file',
data: uint8ArrayFromString('batata')
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(data.type).to.equal(unmarshaled.type)
expect(data.data).to.deep.equal(unmarshaled.data)
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
})
it('file add blocksize', () => {
const data = new UnixFS({
type: 'file'
})
data.addBlockSize(256n)
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(data.type).to.equal(unmarshaled.type)
expect(data.data).to.deep.equal(unmarshaled.data)
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
})
it('file add and remove blocksize', () => {
const data = new UnixFS({
type: 'file'
})
data.addBlockSize(256n)
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(data.type).to.equal(unmarshaled.type)
expect(data.data).to.deep.equal(unmarshaled.data)
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
unmarshaled.removeBlockSize(0)
expect(data.blockSizes).to.not.deep.equal(unmarshaled.blockSizes)
})
it('mode', () => {
const mode = parseInt('0555', 8)
const data = new UnixFS({
type: 'file'
})
data.mode = mode
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', mode)
})
it('default mode for files', () => {
const data = new UnixFS({
type: 'file'
})
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', parseInt('0644', 8))
})
it('default mode for directories', () => {
const data = new UnixFS({
type: 'directory'
})
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', parseInt('0755', 8))
})
it('default mode for hamt shards', () => {
const data = new UnixFS({
type: 'hamt-sharded-directory'
})
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', parseInt('0755', 8))
})
it('sets mode to 0', () => {
const mode = 0
const data = new UnixFS({
type: 'file'
})
data.mode = mode
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', mode)
})
it('mode as string', () => {
const data = new UnixFS({
type: 'file',
mode: 0o555
})
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', parseInt('0555', 8))
})
it('mode as string set outside constructor', () => {
const data = new UnixFS({
type: 'file'
})
data.mode = 0o555
expect(UnixFS.unmarshal(data.marshal())).to.have.property('mode', parseInt('0555', 8))
})
it('mtime', () => {
const mtime = {
secs: 5n,
nsecs: undefined
}
const data = new UnixFS({
type: 'file',
mtime
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(unmarshaled).to.have.deep.property('mtime', mtime)
})
it('default mtime', () => {
const data = new UnixFS({
type: 'file'
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(unmarshaled).to.have.property('mtime').that.is.undefined()
})
it('sets mtime to 0', () => {
const mtime = {
secs: 0n,
nsecs: 0
}
const data = new UnixFS({
type: 'file',
mtime
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(unmarshaled).to.have.deep.property('mtime', mtime)
})
it.skip('sets mtime to 0 as BigInt', () => {
const mtime = {
secs: 0,
nsecs: 0
}
const data = new UnixFS({
type: 'file'
// TODO: https://github.com/ipfs/aegir/issues/487
// mtime: BigInt(0)
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(unmarshaled).to.have.deep.property('mtime', mtime)
})
it.skip('sets mtime to 0 as BigInt literal', () => {
const mtime = {
secs: 0,
nsecs: 0
}
const data = new UnixFS({
type: 'file'
// TODO: https://github.com/ipfs/aegir/issues/487
// mtime: 0n
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(unmarshaled).to.have.deep.property('mtime', mtime)
})
it('sets mtime to 0 as Date', () => {
const mtime: Mtime = {
secs: 0n,
nsecs: undefined
}
const data = new UnixFS({
type: 'file',
mtime: {
secs: 0n
}
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(unmarshaled).to.have.deep.property('mtime', mtime)
})
it('survives undefined mtime', () => {
const entry = new UnixFS({
type: 'file',
mtime: undefined
})
const marshaled = entry.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(unmarshaled).to.have.property('mtime').that.is.undefined()
})
it('does not overwrite unknown mode bits', () => {
const mode = 0xFFFFFFF // larger than currently defined mode bits
const buf = PBData.encode({
Type: PBData.DataType.File,
mode
})
const unmarshaled = UnixFS.unmarshal(buf)
const marshaled = unmarshaled.marshal()
const entry = PBData.decode(marshaled)
expect(entry).to.have.property('mode', mode)
})
it('omits default file mode from protobuf', () => {
const entry = new UnixFS({
type: 'file',
mode: 0o644
})
const marshaled = entry.marshal()
const object = PBData.decode(marshaled)
expect(object).not.to.have.property('mode')
})
it('omits default directory mode from protobuf', () => {
const entry = new UnixFS({
type: 'directory',
mode: 0o755
})
const marshaled = entry.marshal()
const object = PBData.decode(marshaled)
expect(object).not.to.have.property('mode')
})
it('respects high bits in mode read from buffer', () => {
const mode = 0o0100644 // similar to output from fs.stat
const buf = PBData.encode({
Type: PBData.DataType.File,
mode
})
const entry = UnixFS.unmarshal(buf)
// should have truncated mode to bits in the version of the spec this module supports
expect(entry).to.have.property('mode', 0o644)
const marshaled = entry.marshal()
const protobuf = PBData.decode(marshaled)
expect(protobuf).to.have.property('mode', mode)
})
it('ignores high bits in mode passed to constructor', () => {
const mode = 0o0100644 // similar to output from fs.stat
const entry = new UnixFS({
type: 'file',
mode
})
// should have truncated mode to bits in the version of the spec this module supports
expect(entry).to.have.property('mode', 0o644)
const marshaled = entry.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(unmarshaled).to.have.property('mode', 0o644)
const object = PBData.decode(marshaled)
expect(object).not.to.have.property('mode')
})
// figuring out what is this metadata for https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182336526
it('metadata', () => {
const entry = new UnixFS({
type: 'metadata'
})
const marshaled = entry.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(unmarshaled).to.have.property('type', 'metadata')
})
it('symlink', () => {
const data = new UnixFS({
type: 'symlink'
})
const marshaled = data.marshal()
const unmarshaled = UnixFS.unmarshal(marshaled)
expect(data.type).to.equal(unmarshaled.type)
expect(data.data).to.deep.equal(unmarshaled.data)
expect(data.blockSizes).to.deep.equal(unmarshaled.blockSizes)
expect(data.fileSize()).to.deep.equal(unmarshaled.fileSize())
})
it('invalid type', (done) => {
try {
// eslint-disable-next-line no-new
new UnixFS({
type: 'bananas'
})
} catch (err: any) {
expect(err).to.have.property('code', 'ERR_INVALID_TYPE')
done()
}
})
it('invalid type set outside constructor', (done) => {
const entry = new UnixFS()
entry.type = 'bananas'
try {
entry.marshal()
} catch (err: any) {
expect(err).to.have.property('code', 'ERR_INVALID_TYPE')
done()
}
})
describe('interop', () => {
it('raw', () => {
const unmarshaled = UnixFS.unmarshal(raw)
expect(unmarshaled.data).to.eql(uint8ArrayFromString('Hello UnixFS\n'))
expect(unmarshaled.type).to.equal('file')
expect(unmarshaled.marshal()).to.deep.equal(raw)
})
it('directory', () => {
const unmarshaled = UnixFS.unmarshal(directory)
expect(unmarshaled.data).to.deep.equal(undefined)
expect(unmarshaled.type).to.equal('directory')
expect(unmarshaled.marshal()).to.deep.equal(directory)
})
it('file', () => {
const unmarshaled = UnixFS.unmarshal(file)
expect(unmarshaled.data).to.deep.equal(uint8ArrayFromString('Hello UnixFS\n'))
expect(unmarshaled.type).to.equal('file')
expect(unmarshaled.marshal()).to.deep.equal(file)
})
it.skip('metadata', () => {
})
it('symlink', () => {
const unmarshaled = UnixFS.unmarshal(symlink)
expect(unmarshaled.data).to.deep.equal(uint8ArrayFromString('file.txt'))
expect(unmarshaled.type).to.equal('symlink')
// TODO: waiting on https://github.com/ipfs/js-ipfs-data-importing/issues/3#issuecomment-182440079
// expect(unmarshaled.marshal()).to.deep.equal(symlink)
})
})
it('empty', () => {
const data = new UnixFS({
type: 'file'
})
const marshaled = data.marshal()
expect(marshaled).to.deep.equal(Uint8Array.from([0x08, 0x02, 0x18, 0x00]))
})
})