From 8dc4e34aaf21d9a0785ade30e30d9728ad2a68f7 Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 17 Jun 2015 15:59:14 -0700 Subject: [PATCH] Bug and test fixes for contact sync Closes #135 // FREEBIE --- js/background.js | 6 ++++-- js/libtextsecure.js | 13 +++++++++---- libtextsecure/contacts_parser.js | 13 +++++++++---- libtextsecure/test/contacts_parser_test.js | 10 +++++++--- 4 files changed, 29 insertions(+), 13 deletions(-) diff --git a/js/background.js b/js/background.js index da7c1e22..a55ceb92 100644 --- a/js/background.js +++ b/js/background.js @@ -53,7 +53,8 @@ messageReceiver = new textsecure.MessageReceiver(window); } - function onContactReceived(contactInfo) { + function onContactReceived(ev) { + var contactInfo = ev.contactInfo; new Whisper.Conversation({ name: contactInfo.name, id: contactInfo.number, @@ -63,7 +64,8 @@ }).save(); } - function onGroupReceived(group) { + function onGroupReceived(ev) { + var group = ev.group; new Whisper.Conversation({ members: group.members, name: group.name, diff --git a/js/libtextsecure.js b/js/libtextsecure.js index c34b0425..f4190494 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -40134,13 +40134,18 @@ ContactBuffer.prototype = { constructor: ContactBuffer, readContact: function() { try { - var len = this.buffer.readVarint32(); + if (this.buffer.limit === this.buffer.offset) { + return undefined; // eof + } + var len = this.buffer.readVarint64().toNumber(); var contactInfoBuffer = this.buffer.slice(this.buffer.offset, this.buffer.offset+len); var contactInfo = textsecure.protobuf.ContactDetails.decode(contactInfoBuffer); this.buffer.skip(len); - var attachmentLen = contactInfo.avatar.length.toNumber(); - contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, this.buffer.offset + attachmentLen).toArrayBuffer(true); - this.buffer.skip(attachmentLen); + if (contactInfo.avatar) { + var attachmentLen = contactInfo.avatar.length.toNumber(); + contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, this.buffer.offset + attachmentLen).toArrayBuffer(true); + this.buffer.skip(attachmentLen); + } return contactInfo; } catch(e) { diff --git a/libtextsecure/contacts_parser.js b/libtextsecure/contacts_parser.js index 4c954d39..983f1408 100644 --- a/libtextsecure/contacts_parser.js +++ b/libtextsecure/contacts_parser.js @@ -11,13 +11,18 @@ ContactBuffer.prototype = { constructor: ContactBuffer, readContact: function() { try { - var len = this.buffer.readVarint32(); + if (this.buffer.limit === this.buffer.offset) { + return undefined; // eof + } + var len = this.buffer.readVarint64().toNumber(); var contactInfoBuffer = this.buffer.slice(this.buffer.offset, this.buffer.offset+len); var contactInfo = textsecure.protobuf.ContactDetails.decode(contactInfoBuffer); this.buffer.skip(len); - var attachmentLen = contactInfo.avatar.length.toNumber(); - contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, this.buffer.offset + attachmentLen).toArrayBuffer(true); - this.buffer.skip(attachmentLen); + if (contactInfo.avatar) { + var attachmentLen = contactInfo.avatar.length.toNumber(); + contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, this.buffer.offset + attachmentLen).toArrayBuffer(true); + this.buffer.skip(attachmentLen); + } return contactInfo; } catch(e) { diff --git a/libtextsecure/test/contacts_parser_test.js b/libtextsecure/test/contacts_parser_test.js index 57de4835..43e29dd1 100644 --- a/libtextsecure/test/contacts_parser_test.js +++ b/libtextsecure/test/contacts_parser_test.js @@ -39,16 +39,18 @@ describe("ContactsBuffer", function() { buffer.append(avatarBuffer.clone()); } + buffer.limit = buffer.offset; buffer.offset = 0; - buffer.limit = buffer.buffer.byteLength; return buffer.toArrayBuffer(); } it("parses an array buffer of contacts", function() { var arrayBuffer = getTestBuffer(); var contactBuffer = new ContactBuffer(arrayBuffer); - for (var i=0; i < 3; ++i) { - var contact = contactBuffer.readContact(); + var contact = contactBuffer.readContact(); + var count = 0; + while (contact !== undefined) { + count++; assert.strictEqual(contact.name, "Zero Cool"); assert.strictEqual(contact.number, "+10000000000"); assert.strictEqual(contact.avatar.contentType, "image/jpg"); @@ -56,6 +58,8 @@ describe("ContactsBuffer", function() { for (var j=0; j < 255; ++j) { assert.strictEqual(avatarBytes[j],j); } + contact = contactBuffer.readContact(); } + assert.strictEqual(count, 3); }); });