UNPKG

8.9 kBJavaScriptView Raw
1'use strict';
2
3var _promise = require('babel-runtime/core-js/promise');
4
5var _promise2 = _interopRequireDefault(_promise);
6
7var _regenerator = require('babel-runtime/regenerator');
8
9var _regenerator2 = _interopRequireDefault(_regenerator);
10
11var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');
12
13var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);
14
15var _chai = require('chai');
16
17var _chai2 = _interopRequireDefault(_chai);
18
19var _chaiAsPromised = require('chai-as-promised');
20
21var _chaiAsPromised2 = _interopRequireDefault(_chaiAsPromised);
22
23var _fs = require('fs');
24
25var _mocha = require('mocha');
26
27var _childProcessPromise = require('child-process-promise');
28
29var _fsPromise = require('fs-promise');
30
31var _vhdWrite = require('./vhd-write');
32
33function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
34
35_chai2.default.use(_chaiAsPromised2.default);
36
37
38(0, _mocha.describe)('VHD writing', function () {
39 (0, _mocha.it)('computeChecksum() is correct against some reference values', function () {
40 // those values were taken from a file generated by qemu
41 var testValue1 = '636F6E6563746978000000020001000000000000000002001F34DB9F71656D75000500035769326B0000000000019800000000000001980000030411000000030000000033B3A5E17F94433498376740246E5660';
42 var expectedChecksum1 = 0xFFFFEFB2;
43 var testValue2 = '6378737061727365FFFFFFFFFFFFFFFF0000000000000600000100000000000100200000';
44 var expectedChecksum2 = 0xFFFFF476;
45 _chai.assert.equal((0, _vhdWrite.computeChecksum)(new Buffer(testValue1, 'hex')), expectedChecksum1);
46 _chai.assert.equal((0, _vhdWrite.computeChecksum)(new Buffer(testValue2, 'hex')), expectedChecksum2);
47 });
48 (0, _mocha.it)('createFooter() does not crash', function () {
49 (0, _vhdWrite.createFooter)(104448, Math.floor(Date.now() / 1000), { cylinders: 3, heads: 4, sectorsPerTrack: 17 });
50 });
51 (0, _mocha.it)('createDynamicDiskHeader() does not crash', function () {
52 (0, _vhdWrite.createDynamicDiskHeader)(1, 0x00200000);
53 });
54 (0, _mocha.it)('ReadableRawVHDStream does not crash', (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee2() {
55 var data, index, mockParser, stream, pipe;
56 return _regenerator2.default.wrap(function _callee2$(_context2) {
57 while (1) {
58 switch (_context2.prev = _context2.next) {
59 case 0:
60 data = [{
61 lbaBytes: 100,
62 grain: new Buffer('azerzaerazeraze', 'ascii')
63 }, {
64 lbaBytes: 700,
65 grain: new Buffer('gdfslkdfguer', 'ascii')
66 }];
67 index = 0;
68 mockParser = {
69 next: function () {
70 var _ref2 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee() {
71 var result;
72 return _regenerator2.default.wrap(function _callee$(_context) {
73 while (1) {
74 switch (_context.prev = _context.next) {
75 case 0:
76 if (!(index < data.length)) {
77 _context.next = 6;
78 break;
79 }
80
81 result = data[index];
82
83 index++;
84 return _context.abrupt('return', result);
85
86 case 6:
87 return _context.abrupt('return', null);
88
89 case 7:
90 case 'end':
91 return _context.stop();
92 }
93 }
94 }, _callee, undefined);
95 }));
96
97 function next() {
98 return _ref2.apply(this, arguments);
99 }
100
101 return next;
102 }()
103 };
104 stream = new _vhdWrite.ReadableRawVHDStream(100000, mockParser);
105 pipe = stream.pipe((0, _fs.createWriteStream)('outputStream'));
106 _context2.next = 7;
107 return new _promise2.default(function (resolve, reject) {
108 pipe.on('finish', resolve);
109 pipe.on('error', reject);
110 });
111
112 case 7:
113 case 'end':
114 return _context2.stop();
115 }
116 }
117 }, _callee2, undefined);
118 })));
119 (0, _mocha.it)('ReadableRawVHDStream detects when blocks are out of order', (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee4() {
120 var data, index, mockParser;
121 return _regenerator2.default.wrap(function _callee4$(_context4) {
122 while (1) {
123 switch (_context4.prev = _context4.next) {
124 case 0:
125 data = [{
126 lbaBytes: 700,
127 grain: new Buffer('azerzaerazeraze', 'ascii')
128 }, {
129 lbaBytes: 100,
130 grain: new Buffer('gdfslkdfguer', 'ascii')
131 }];
132 index = 0;
133 mockParser = {
134 next: function () {
135 var _ref4 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee3() {
136 var result;
137 return _regenerator2.default.wrap(function _callee3$(_context3) {
138 while (1) {
139 switch (_context3.prev = _context3.next) {
140 case 0:
141 if (!(index < data.length)) {
142 _context3.next = 6;
143 break;
144 }
145
146 result = data[index];
147
148 index++;
149 return _context3.abrupt('return', result);
150
151 case 6:
152 return _context3.abrupt('return', null);
153
154 case 7:
155 case 'end':
156 return _context3.stop();
157 }
158 }
159 }, _callee3, undefined);
160 }));
161
162 function next() {
163 return _ref4.apply(this, arguments);
164 }
165
166 return next;
167 }()
168 };
169 return _context4.abrupt('return', _chai.assert.isRejected(new _promise2.default(function (resolve, reject) {
170 var stream = new _vhdWrite.ReadableRawVHDStream(100000, mockParser);
171 stream.on('error', reject);
172 var pipe = stream.pipe((0, _fs.createWriteStream)('outputStream'));
173 pipe.on('finish', resolve);
174 pipe.on('error', reject);
175 })));
176
177 case 4:
178 case 'end':
179 return _context4.stop();
180 }
181 }
182 }, _callee4, undefined);
183 })));
184 (0, _mocha.it)('writing a known file with VHDFile is successful', (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee5() {
185 var fileName, rawFilename, randomFileName, geometry, dataSize, buffer, f, splitPoint, fileContent, i;
186 return _regenerator2.default.wrap(function _callee5$(_context5) {
187 while (1) {
188 switch (_context5.prev = _context5.next) {
189 case 0:
190 fileName = 'output.vhd';
191 rawFilename = 'output.raw';
192 randomFileName = 'random.raw';
193 geometry = (0, _vhdWrite.computeGeometryForSize)(1024 * 1024 * 8);
194 dataSize = geometry.actualSize;
195 _context5.next = 7;
196 return (0, _childProcessPromise.exec)('base64 /dev/urandom | head -c ' + dataSize + ' > ' + randomFileName);
197
198 case 7:
199 _context5.next = 9;
200 return (0, _fsPromise.readFile)(randomFileName);
201
202 case 9:
203 buffer = _context5.sent;
204 f = new _vhdWrite.VHDFile(buffer.length, 523557791);
205 splitPoint = Math.floor(Math.random() * buffer.length);
206
207 f.writeBuffer(buffer.slice(splitPoint), splitPoint);
208 f.writeBuffer(buffer.slice(0, splitPoint), 0);
209 f.writeBuffer(buffer.slice(splitPoint), splitPoint);
210 _context5.next = 17;
211 return f.writeFile(fileName);
212
213 case 17:
214 _context5.next = 19;
215 return (0, _childProcessPromise.exec)('qemu-img convert -fvpc -Oraw ' + fileName + ' ' + rawFilename);
216
217 case 19:
218 _context5.next = 21;
219 return (0, _fsPromise.readFile)(rawFilename);
220
221 case 21:
222 fileContent = _context5.sent;
223
224 _chai.assert.equal(fileContent.length, dataSize);
225 for (i = 0; i < fileContent.length; i++) {
226 if (fileContent[i] !== buffer[i]) {
227 _chai.assert.fail(fileContent[i], 0);
228 }
229 }
230
231 case 24:
232 case 'end':
233 return _context5.stop();
234 }
235 }
236 }, _callee5, undefined);
237 })));
238});
239//# sourceMappingURL=vhd-write.spec.js.map
\No newline at end of file