Add tests for msg/broadcast with address v2

This commit is contained in:
Kagami Hiiragi 2015-01-30 15:53:03 +03:00
parent a42ec4b8f7
commit 4ef94e91f7
2 changed files with 64 additions and 2 deletions

View File

@ -587,7 +587,7 @@ var msg = exports.msg = {
// Ripe, encoding. // Ripe, encoding.
assert(rest.length >= 20, "Bad msg object payload length"); assert(rest.length >= 20, "Bad msg object payload length");
decoded.ripe = rest.slice(0, 20); decoded.ripe = rest.slice(0, 20);
// TODO(Kagami): Also check against the calculatedRipe (see // TODO(Kagami): Also check against the calculated ripe (see
// GH-6)? // GH-6)?
assert( assert(
bufferEqual(decoded.ripe, decInfo.addr.ripe), bufferEqual(decoded.ripe, decInfo.addr.ripe),

64
test.js
View File

@ -501,7 +501,8 @@ describe("Object types", function() {
var signPublicKey = bmcrypto.getPublic(signPrivateKey); var signPublicKey = bmcrypto.getPublic(signPrivateKey);
var encPrivateKey = Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex"); var encPrivateKey = Buffer("9f9969c93c2d186787a7653f70e49be34c03c4a853e6ad0c867db0946bc433c6", "hex");
var encPublicKey = bmcrypto.getPublic(encPrivateKey); var encPublicKey = bmcrypto.getPublic(encPrivateKey);
var from = Address({ var fromV2 = Address({
version: 2,
signPrivateKey: signPrivateKey, signPrivateKey: signPrivateKey,
encPrivateKey: encPrivateKey, encPrivateKey: encPrivateKey,
}); });
@ -510,6 +511,11 @@ describe("Object types", function() {
signPrivateKey: signPrivateKey, signPrivateKey: signPrivateKey,
encPrivateKey: encPrivateKey, encPrivateKey: encPrivateKey,
}); });
var fromV4 = Address({
signPrivateKey: signPrivateKey,
encPrivateKey: encPrivateKey,
});
var from = fromV4;
it("should get type of the encoded object message", function() { it("should get type of the encoded object message", function() {
var encoded = object.encode({ var encoded = object.encode({
@ -665,6 +671,35 @@ describe("Object types", function() {
}); });
}); });
it("should encode and decode msg for address v2", function() {
return msg.encodeAsync({
ttl: 111,
from: fromV2,
to: fromV2,
message: "test",
}).then(function(buf) {
expect(message.decode(buf).command).to.equal("object");
return msg.decodeAsync(buf, {identities: [fromV2]});
}).then(function(res) {
expect(res.ttl).to.be.at.most(111);
expect(res.type).to.equal(object.MSG);
expect(res.version).to.equal(1);
expect(res.stream).to.equal(1);
expect(res.senderVersion).to.equal(2);
expect(res.senderStream).to.equal(1);
expect(res.behavior.get(PubkeyBitfield.DOES_ACK)).to.be.true;
expect(bufferEqual(res.signPublicKey, signPublicKey)).to.be.true;
expect(bufferEqual(res.encPublicKey, encPublicKey)).to.be.true;
expect(res).to.not.have.property("nonceTrialsPerByte");
expect(res).to.not.have.property("payloadLengthExtraBytes");
expect(bufferEqual(res.ripe, fromV2.ripe)).to.be.true;
expect(res.encoding).to.equal(msg.TRIVIAL);
expect(res.message).to.equal("test");
expect(res).to.not.have.property("subject");
expect(Buffer.isBuffer(res.signature)).to.be.true;
});
});
it("shouldn't decode msg without identities", function(done) { it("shouldn't decode msg without identities", function(done) {
return msg.encodeAsync({ return msg.encodeAsync({
ttl: 111, ttl: 111,
@ -724,6 +759,33 @@ describe("Object types", function() {
}); });
}); });
it("should encode and decode broadcast v4 for address v2", function() {
return broadcast.encodeAsync({
ttl: 999,
from: fromV2,
message: "test",
}).then(function(buf) {
expect(message.decode(buf).command).to.equal("object");
return broadcast.decodeAsync(buf, {subscriptions: fromV2});
}).then(function(res) {
expect(res.ttl).to.be.at.most(999);
expect(res.type).to.equal(object.BROADCAST);
expect(res.version).to.equal(4);
expect(res.stream).to.equal(1);
expect(res.senderVersion).to.equal(2);
expect(res.senderStream).to.equal(1);
expect(res.behavior.get(PubkeyBitfield.DOES_ACK)).to.be.true;
expect(bufferEqual(res.signPublicKey, signPublicKey)).to.be.true;
expect(bufferEqual(res.encPublicKey, encPublicKey)).to.be.true;
expect(res).to.not.have.property("nonceTrialsPerByte");
expect(res).to.not.have.property("payloadLengthExtraBytes");
expect(res.encoding).to.equal(msg.TRIVIAL);
expect(res.message).to.equal("test");
expect(res).to.not.have.property("subject");
expect(Buffer.isBuffer(res.signature)).to.be.true;
});
});
it("should encode and decode broadcast v5", function() { it("should encode and decode broadcast v5", function() {
return broadcast.encodeAsync({ return broadcast.encodeAsync({
ttl: 101, ttl: 101,