-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration.test.ts
More file actions
237 lines (211 loc) · 7.29 KB
/
Copy pathintegration.test.ts
File metadata and controls
237 lines (211 loc) · 7.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
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
import type { FileStat, MetadataKeywords } from '#types.js';
import type { VirtualFile, VirtualDirectory } from './types.js';
import { test } from '@fast-check/jest';
import * as testsUtils from './utils/index.js';
import {
Generator,
Parser,
VirtualTarGenerator,
VirtualTarParser,
} from '#index.js';
import * as utils from '#utils.js';
import * as constants from '#constants.js';
describe('integration testing', () => {
test.prop([testsUtils.fileTreeArb()])(
'should archive and unarchive a file tree using generator-parser pair',
(fileTree) => {
const generator = new Generator();
const blocks: Array<Uint8Array> = [];
const encoder = new TextEncoder();
for (const entry of fileTree) {
if (entry.path.length > constants.STANDARD_PATH_SIZE) {
// Push the extended header
const extendedData = utils.encodeExtendedHeader({
path: entry.path,
});
blocks.push(generator.generateExtended(extendedData.byteLength));
// Push each data chunk
for (
let offset = 0;
offset < extendedData.byteLength;
offset += constants.BLOCK_SIZE
) {
const chunk = extendedData.slice(
offset,
offset + constants.BLOCK_SIZE,
);
blocks.push(generator.generateData(chunk));
}
}
const filePath =
entry.path.length <= constants.STANDARD_PATH_SIZE ? entry.path : '';
if (entry.type === 'file') {
blocks.push(generator.generateFile(filePath, entry.stat));
const data = encoder.encode(entry.content);
// Push each data chunk
for (
let offset = 0;
offset < data.byteLength;
offset += constants.BLOCK_SIZE
) {
const chunk = data.slice(offset, offset + constants.BLOCK_SIZE);
blocks.push(generator.generateData(chunk));
}
} else {
blocks.push(generator.generateDirectory(filePath, entry.stat));
}
}
blocks.push(generator.generateEnd());
blocks.push(generator.generateEnd());
// The tar archive should be inside the blocks array now. Each block is
// a single chunk aligned to 512-byte. Now we can parse it and check if
// the parsed virtual file system matches the input.
const parser = new Parser();
const reconstructedTree: Record<
string,
{
data?: Uint8Array;
stat: FileStat;
}
> = {};
let workingPath: string | undefined = undefined;
let workingStat: FileStat | undefined = undefined;
let workingData: Uint8Array = new Uint8Array();
let extendedData: Uint8Array | undefined;
let dataOffset = 0;
for (const chunk of blocks) {
const token = parser.write(chunk);
if (token == null) continue;
switch (token.type) {
case 'header': {
let extendedMetadata:
| Partial<Record<MetadataKeywords, string>>
| undefined;
if (extendedData != null) {
extendedMetadata = utils.decodeExtendedHeader(extendedData);
}
const fullPath = extendedMetadata?.path
? extendedMetadata.path
: token.filePath;
if (workingPath != null && workingStat != null) {
reconstructedTree[workingPath] = {
stat: workingStat,
data: workingData,
};
workingData = new Uint8Array();
workingPath = undefined;
workingStat = undefined;
}
const fileStat: FileStat = {
size: token.fileSize,
mtime: token.fileMtime,
mode: token.fileMode,
uid: token.ownerUid,
gid: token.ownerGid,
uname: token.ownerUserName,
gname: token.ownerGroupName,
};
switch (token.fileType) {
case 'file': {
workingPath = fullPath;
workingStat = fileStat;
break;
}
case 'directory': {
reconstructedTree[fullPath] = { stat: fileStat };
break;
}
case 'extended': {
extendedData = new Uint8Array(token.fileSize);
extendedMetadata = {};
break;
}
default:
throw new Error('Invalid state');
}
// If we were using the extended metadata for this header, reset it
// for the next header.
extendedData = undefined;
dataOffset = 0;
break;
}
case 'data': {
if (extendedData == null) {
workingData = utils.concatUint8Arrays(workingData, token.data);
} else {
extendedData.set(token.data, dataOffset);
dataOffset += token.data.byteLength;
}
break;
}
case 'end': {
// Finalise adding the last file into the tree
if (workingPath != null && workingStat != null) {
reconstructedTree[workingPath] = {
stat: workingStat,
data: workingData,
};
workingData = new Uint8Array();
workingPath = undefined;
workingStat = undefined;
}
}
}
}
for (const entry of fileTree) {
expect(entry.stat).toMatchObject(reconstructedTree[entry.path].stat);
if (entry.type === 'file') {
const content = encoder.encode(entry.content);
expect(reconstructedTree[entry.path].data).toEqual(content);
} else {
expect(reconstructedTree[entry.path].data).toBeUndefined();
}
}
},
);
test.prop([testsUtils.fileTreeArb()])(
'should archive and unarchive a file tree using virtualtar',
async (fileTree) => {
const generator = new VirtualTarGenerator();
for (const entry of fileTree) {
if (entry.type === 'file') {
generator.addFile(entry.path, entry.stat, entry.content);
} else {
generator.addDirectory(entry.path, entry.stat);
}
}
generator.finalize();
const archive = generator.yieldChunks();
const entries: Array<VirtualFile | VirtualDirectory> = [];
const parser = new VirtualTarParser({
onFile: async (header, data) => {
const content: Array<Uint8Array> = [];
for await (const chunk of data()) {
content.push(chunk);
}
const fileContent = Buffer.concat(content).toString();
entries.push({
type: 'file',
path: header.path,
stat: header.stat,
content: fileContent,
});
},
onDirectory: async (header) => {
entries.push({
type: 'directory',
path: header.path,
stat: header.stat,
});
},
});
for await (const chunk of archive) {
await parser.write(chunk);
}
await parser.settled();
expect(testsUtils.deepSort(entries)).toContainAllValues(
testsUtils.deepSort(fileTree),
);
},
);
});