Commit c25e3845 authored by Cédric Moreau's avatar Cédric Moreau

Keychange + Keyblock unit tests pass

parent f57ec03b
......@@ -166,25 +166,9 @@ module.exports = new function() {
for(var i = 0; i < json.membersChanges.length; i++){
raw += json.membersChanges[i] + "\n";
}
raw += "PublicKeys:\n";
for(var i = 0; i < json.publicKeys.length; i++){
var packets = json.publicKeys[i].packets;
raw += KEYBLOCK_PUBK_PREFIX + json.publicKeys[i].fingerprint + KEYBLOCK_PUBK_SUFFIX + '\n';
raw += packets;
if (!packets.match(/\n$/))
raw += '\n';
}
raw += "Memberships:\n";
for(var i = 0; i < json.memberships.length; i++){
raw += json.memberships[i] + "\n";
}
raw += "MembershipsSignatures:\n";
for(var i = 0; i < json.membershipsSigs.length; i++){
var packets = json.membershipsSigs[i].packets;
raw += KEYBLOCK_PUBK_PREFIX + json.membershipsSigs[i].fingerprint + KEYBLOCK_PUBK_SUFFIX + '\n';
raw += packets;
if (!packets.match(/\n$/))
raw += '\n';
raw += "KeysChanges:\n";
for(var i = 0; i < json.keysChanges.length; i++){
raw += this.getKeychange(json.keysChanges[i]);
}
return unix2dos(raw);
};
......
......@@ -36,6 +36,11 @@ function GenericParser (captures, multipleLinesFields, rawerFunc, onError) {
doJob(str, done);
};
// Sync way
this.syncWrite = function (str) {
return doJob(str);
};
function doJob (str, done) {
var error;
var obj = {};
......@@ -49,7 +54,10 @@ function GenericParser (captures, multipleLinesFields, rawerFunc, onError) {
if (sha1(str) != sha1(raw))
error = 'Document has unkown fields or wrong line ending format';
}
done(error, obj);
if (typeof done == 'function')
done(error, obj);
else
return obj;
}
this._clean = function (obj) {
......
......@@ -21,10 +21,8 @@ function KeyblockParser (onError) {
{prop: "previousIssuer", regexp: /PreviousIssuer: (.*)/},
{prop: "membersCount", regexp: /MembersCount: (.*)/},
{prop: "membersRoot", regexp: /MembersRoot: (.*)/},
{prop: "membersChanges", regexp: /MembersChanges:\n([\s\S]*)PublicKeys/, parser: split("\n")},
{prop: "publicKeys", regexp: /PublicKeys:\n([\s\S]*)Memberships/, parser: extractFingerprintSeparatedPackets},
{prop: "memberships", regexp: /Memberships:\n([\s\S]*)MembershipsSignatures/, parser: split("\n")},
{prop: "membershipsSigs", regexp: /MembershipsSignatures:\n([\s\S]*)/, parser: extractFingerprintSeparatedPackets},
{prop: "membersChanges", regexp: /MembersChanges:\n([\s\S]*)KeysChanges/, parser: split("\n")},
{prop: "keysChanges", regexp: /KeysChanges:\n([\s\S]*)/, parser: extractKeyChanges}
];
var multilineFields = [];
GenericParser.call(this, captures, multilineFields, rawer.getKeyblock, onError);
......@@ -109,25 +107,31 @@ function KeyblockParser (onError) {
};
}
function extractFingerprintSeparatedPackets(raw) {
var packetsByFPR = [];
function extractKeyChanges(raw) {
var rawKeychanges = [];
var keychanges = [];
var currentKC;
var lines = raw.split(/\n/);
var nbKeys = 0;
lines.forEach(function(line){
if (line.match(/^#####-----[A-Z0-9]{40}-----#####$/)) {
if (line.match(/^#####----(F|N|U|L|B):[A-Z0-9]{40}----#####$/)) {
// New key block
packetsByFPR.push({
number: nbKeys++,
fingerprint: line.substring(10, 50),
packets: ""
});
} else if (line.match(/^[A-Za-z0-9\/+=]{1,64}$/) && nbKeys > 0) {
packetsByFPR[nbKeys-1].packets += line + '\n';
if (currentKC)
rawKeychanges.push(currentKC);
currentKC = line + '\n';
} else {
// Adding to current
currentKC += line + '\n';
}
});
return _(packetsByFPR).sortBy(function (pubk) {
return pubk.number;
if (currentKC)
rawKeychanges.push(currentKC);
rawKeychanges.forEach(function(kc){
var parsers = require('./.');
var obj = parsers.parseKeychange().syncWrite(kc);
keychanges.push(obj);
});
return keychanges;
}
util.inherits(KeyblockParser, GenericParser);
......@@ -21,6 +21,8 @@ function KeychangeParser (onError) {
GenericParser.call(this, captures, multilineFields, rawer.getKeychange, onError);
this._clean = function (obj) {
if (obj.keypackets == undefined) obj.keypackets = '';
if (obj.certpackets == undefined) obj.certpackets = '';
};
this._verify = function(obj){
......
......@@ -6,6 +6,7 @@ var openpgp = require('openpgp');
var base64 = require('../../app/lib/base64');
var jpgp = require('../../app/lib/jpgp');
var mongoose = require('mongoose');
var parsers = require('../../app/lib/streams/parsers/doc');
var Keyblock = mongoose.model('Keyblock', require('../../app/models/keyblock'));
var catPubkeyPackets = "" +
......@@ -32,6 +33,124 @@ var catPubkeyPackets = "" +
"hMUN3UJwGzk7HdO7wo0F3e5onOinit7RTpg/tAZX+r3VIj8TzZnl4QpCS15A\n" +
"9r9tAcdC0An1ji4sVQ==\n";
var rawKeyblock = "" +
"Version: 1\r\n" +
"Type: KeyBlock\r\n" +
"Currency: beta_brousouf\r\n" +
"Nonce: 55\r\n" +
"Number: 0\r\n" +
"Timestamp: 1408264410\r\n" +
"MembersCount: 1\r\n" +
"MembersRoot: C73882B64B7E72237A2F460CE9CAB76D19A8651E\r\n" +
"MembersChanges:\r\n" +
"+C73882B64B7E72237A2F460CE9CAB76D19A8651E\r\n" +
"KeysChanges:\r\n" +
"#####----F:C73882B64B7E72237A2F460CE9CAB76D19A8651E----#####\r\n" +
"KeyPackets:\r\n" +
"xsBNBFHHC/EBCADWTLSN7EGP+n30snndS3ZNcB02foL+0opcS6LK2coPDJLg\r\n" +
"2nookeJRHZxF3THmZQrKwZOjiuDBinOc5DWlzIS/gD/RaXwntgPFlGKBlBU+\r\n" +
"g255fr28ziSb5Y1lW4N//nUFdPZzoMmPgRj0b17T0UPCoMR8ZZ/Smk5LINbQ\r\n" +
"wt+A+LEoxEdEVcq+Tyc0OlEabqO6RFqiKDRiPhGPiCwVQA3yPjb6iCp5gTch\r\n" +
"ObCxCnDbxA0Mfj9FmHrGbepNHGXxStO4xT0woCb7y02S1E8K08kOc5Bq9e1Y\r\n" +
"j5I/mdaw4Hn/Wp28lZl1mnO1u1z9ZU/rcglhEyaEOTwasheb44QcdGSfABEB\r\n" +
"AAHNTUxvTCBDYXQgKHVkaWQyO2M7Q0FUO0xPTDsyMDAwLTA0LTE5O2UrNDMu\r\n" +
"NzAtMDc5LjQyOzA7KSA8Y2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ReL6KeOIsrpolTBeJGMsU1/+9l8zh5fEgDcJqSkw=\r\n" +
"=3Iri\r\n";
describe('Reading a root keyblock', function(){
var block = parsers.parseKeyblock().syncWrite(rawKeyblock);
it('should give root basic informations', function(){
assert.equal(block.version, 1);
assert.equal(block.type, 'KeyBlock');
assert.equal(block.currency, 'beta_brousouf');
assert.equal(block.nonce, 55);
assert.equal(block.number, 0);
assert.equal(block.timestamp, 1408264410);
should.not.exist(block.previousHash);
should.not.exist(block.previousIssuer);
assert.equal(block.membersCount, 1);
assert.equal(block.membersRoot, 'C73882B64B7E72237A2F460CE9CAB76D19A8651E');
assert.deepEqual(block.membersChanges, ['+C73882B64B7E72237A2F460CE9CAB76D19A8651E']);
});
describe('should have 1 keychange', function(){
it('', function(){
assert.equal(block.keysChanges.length, 1);
});
it('with FOUNDER packt type', function(){
assert.equal(block.keysChanges[0].type, 'F');
});
it('with LoL Cat\' fingerprint', function(){
assert.equal(block.keysChanges[0].fingerprint, 'C73882B64B7E72237A2F460CE9CAB76D19A8651E');
});
it('with 4 key packets', function(){
var pubkeyPackets = block.keysChanges[0].keypackets;
var packetList = new openpgp.packet.List();
packetList.read(base64.decode(pubkeyPackets));
var pubkey = new openpgp.key.Key(packetList);
assert.equal(packetList.length, 7);
assert.equal(pubkey.primaryKey.getFingerprint().toUpperCase(), 'C73882B64B7E72237A2F460CE9CAB76D19A8651E');
});
it('with empty certification packets', function(){
assert.equal(block.keysChanges[0].certpackets, '');
});
});
});
describe('Extracting pubkeys of a keyblock', function(){
var block = new Keyblock({ publicKeys: [
......
......@@ -80,12 +80,24 @@ describe('Founder packet (Cat)', function(){
});
});
it('should be FOUNDER packt type', function(){
assert.equal(keychange.type, 'F');
});
it('should be LoL Cat\' fingerprint', function(){
assert.equal(keychange.fingerprint, 'C73882B64B7E72237A2F460CE9CAB76D19A8651E');
});
it('should have 4 key packets', function(){
var pubkeyPackets = keychange.keypackets;
var packetList = new openpgp.packet.List();
packetList.read(base64.decode(pubkeyPackets));
var pubkey = new openpgp.key.Key(packetList);
assert.equal(packetList.length, 4);
assert.equal(pubkey.getFingerprint().toUpperCase(), 'C73882B64B7E72237A2F460CE9CAB76D19A8651E');
assert.equal(packetList.length, 7);
assert.equal(pubkey.primaryKey.getFingerprint().toUpperCase(), 'C73882B64B7E72237A2F460CE9CAB76D19A8651E');
});
it('should have empty certification packets', function(){
assert.equal(keychange.certpackets, '');
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment