check for out of order blocks in vhd output

This commit is contained in:
Nicolas Raynaud
2016-08-17 19:19:51 +02:00
parent f89538c6f7
commit 43e0217008
3 changed files with 40 additions and 8 deletions
+1
View File
@@ -35,6 +35,7 @@
"babel-preset-es2015": "^6.13.2",
"babel-preset-stage-0": "^6.5.0",
"chai": "^3.5.0",
"chai-as-promised": "^5.3.0",
"clarify": "^2.0.0",
"dependency-check": "^2.6.0",
"ghooks": "^1.3.2",
+5 -2
View File
@@ -232,8 +232,11 @@ export class ReadableRawVHDStream extends stream.Readable {
} else {
const offset = next.lbaBytes
const buffer = next.grain
const paddingBuffer = new Buffer(offset - this.position)
var paddingLength = offset - this.position
if (paddingLength < 0) {
process.nextTick(() => this.emit('error', 'This VMDK file does not have its blocks in the correct order'))
}
const paddingBuffer = new Buffer(paddingLength)
paddingBuffer.fill(0)
if (paddingBuffer.length !== 0) {
this.push(paddingBuffer)
+34 -6
View File
@@ -1,6 +1,8 @@
'use strict'
import {assert} from 'chai'
import chai, {assert} from 'chai'
import chaiAsPromised from 'chai-as-promised'
chai.use(chaiAsPromised)
import {createWriteStream} from 'fs'
import {describe, it} from 'mocha'
import {exec} from 'child-process-promise'
@@ -36,18 +38,16 @@ describe('VHD writing', () => {
lbaBytes: 100,
grain: new Buffer('azerzaerazeraze', 'ascii')
}, {
offset: 700,
lbaBytes: 700,
grain: new Buffer('gdfslkdfguer', 'ascii')
}]
let index = 0
const mockParser = {
next: async () => {
if (index < data.length) {
const promise = await new Promise((resolve) => {
resolve(data[index])
})
const result = data[index]
index++
return promise
return result
} else {
return null
}
@@ -60,6 +60,34 @@ describe('VHD writing', () => {
pipe.on('error', reject)
})
})
it('ReadableRawVHDStream detects when blocks are out of order', async () => {
const data = [{
lbaBytes: 700,
grain: new Buffer('azerzaerazeraze', 'ascii')
}, {
lbaBytes: 100,
grain: new Buffer('gdfslkdfguer', 'ascii')
}]
let index = 0
const mockParser = {
next: async () => {
if (index < data.length) {
const result = data[index]
index++
return result
} else {
return null
}
}
}
return assert.isRejected(new Promise((resolve, reject) => {
const stream = new ReadableRawVHDStream(100000, mockParser)
stream.on('error', reject)
const pipe = stream.pipe(createWriteStream('outputStream'))
pipe.on('finish', resolve)
pipe.on('error', reject)
}))
})
it('writing a known file is successful', async () => {
const fileName = 'output.vhd'
const rawFilename = 'output.raw'