forked from keichi/binary-parser
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes encoding array readUntil function and add encodeUntil option
- Loading branch information
Showing
4 changed files
with
170 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
var assert = require("assert"); | ||
var Parser = require("../lib/binary_parser").Parser; | ||
|
||
describe("Specific bugs testing", function() { | ||
describe("Array encoder with readUntil", function() { | ||
it("should limit to array length even if readUntil is never true", function() { | ||
var parser = Parser.start() | ||
.uint16("len") | ||
.array("payloads", { | ||
type: new Parser().uint8("cmd").array("params", { | ||
type: new Parser().uint8("param"), | ||
readUntil: function(item, buffer) { | ||
return buffer.length == 2; // Stop when 2 bytes left in parsed buffer | ||
} | ||
}), | ||
lengthInBytes: function() { | ||
return this.len - 4; | ||
} | ||
}) | ||
.uint16("crc"); | ||
|
||
var buffer = Buffer.from("0008AAB1B2B3FFFF", "hex"); | ||
var decoded = parser.parse(buffer); | ||
|
||
assert.deepEqual(decoded, { | ||
len: 8, | ||
payloads: [ | ||
{ | ||
cmd: 170, | ||
params: [ | ||
{ | ||
param: 177 | ||
}, | ||
{ | ||
param: 178 | ||
}, | ||
{ | ||
param: 179 | ||
} | ||
] | ||
} | ||
], | ||
crc: 65535 | ||
}); | ||
|
||
var encoded; | ||
// Although readUntil is never true here, the encoding will be good | ||
assert.doesNotThrow(function() { | ||
encoded = parser.encode(decoded); | ||
}); | ||
assert.deepEqual(encoded, buffer); | ||
}); | ||
|
||
it("is not the reverse of parsing when readUntil gives false information", function() { | ||
var parser = Parser.start() | ||
.uint16("len") | ||
.array("payloads", { | ||
type: new Parser().uint8("cmd").array("params", { | ||
type: new Parser().uint8("param"), | ||
readUntil: function(item, buffer) { | ||
return buffer.length <= 2; // Stop when 2 bytes left in buffer | ||
} | ||
}), | ||
lengthInBytes: function() { | ||
return this.len - 4; | ||
} | ||
}) | ||
.uint16("crc"); | ||
|
||
var buffer = Buffer.from("0008AAB1B2B3FFFF", "hex"); | ||
var decoded = parser.parse(buffer); | ||
|
||
assert.deepEqual(decoded, { | ||
len: 8, | ||
payloads: [ | ||
{ | ||
cmd: 170, | ||
params: [ | ||
{ | ||
param: 177 | ||
}, | ||
{ | ||
param: 178 | ||
}, | ||
{ | ||
param: 179 | ||
} | ||
] | ||
} | ||
], | ||
crc: 0xffff | ||
}); | ||
|
||
var encoded = parser.encode(decoded); | ||
// Missing parms 178 and 179 as readUntil will be true at first run | ||
assert.deepEqual(encoded, Buffer.from("0008AAB1FFFF", "hex")); | ||
}); | ||
|
||
it("should ignore readUntil when encodeUntil is provided", function() { | ||
var parser = Parser.start() | ||
.uint16("len") | ||
.array("payloads", { | ||
type: new Parser().uint8("cmd").array("params", { | ||
type: new Parser().uint8("param"), | ||
readUntil: function(item, buffer) { | ||
return buffer.length == 2; // Stop when 2 bytes left in buffer | ||
}, | ||
encodeUntil: function(item, obj) { | ||
return item.param === 178; // Stop encoding when value 178 is reached | ||
} | ||
}), | ||
lengthInBytes: function() { | ||
return this.len - 4; | ||
} | ||
}) | ||
.uint16("crc"); | ||
|
||
var buffer = Buffer.from("0008AAB1B2B3FFFF", "hex"); | ||
var decoded = parser.parse(buffer); | ||
|
||
assert.deepEqual(decoded, { | ||
len: 8, | ||
payloads: [ | ||
{ | ||
cmd: 170, | ||
params: [ | ||
{ | ||
param: 177 | ||
}, | ||
{ | ||
param: 178 | ||
}, | ||
{ | ||
param: 179 | ||
} | ||
] | ||
} | ||
], | ||
crc: 0xffff | ||
}); | ||
|
||
var encoded = parser.encode(decoded); | ||
// Missing parms 179 as encodeUntil stops at 178 | ||
assert.deepEqual(encoded, Buffer.from("0008AAB1B2FFFF", "hex")); | ||
}); | ||
}); | ||
}); |