From 316838cfe99c0d2eebb0908ace1c3d43a37b02ec Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 3 Jun 2015 12:51:37 -0700 Subject: [PATCH] Add tests and bug fixes for ContactBuffer --- js/libtextsecure.js | 14 ++--- libtextsecure/contacts_parser.js | 14 ++--- libtextsecure/test/contacts_parser_test.js | 61 ++++++++++++++++++++++ libtextsecure/test/index.html | 2 + 4 files changed, 79 insertions(+), 12 deletions(-) create mode 100644 libtextsecure/test/contacts_parser_test.js diff --git a/js/libtextsecure.js b/js/libtextsecure.js index 97c3785e..ff4c8d7e 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -40116,19 +40116,21 @@ window.textsecure.messaging = function() { * vim: ts=4:sw=4:expandtab */ function ContactBuffer(arrayBuffer) { - this.buffer = new dCodeIO.ByteBuffer(arrayBuffer); + this.buffer = new dcodeIO.ByteBuffer(); + this.buffer.append(arrayBuffer); + this.buffer.offset = 0; + this.buffer.limit = arrayBuffer.byteLength; } ContactBuffer.prototype = { constructor: ContactBuffer, readContact: function() { try { var len = this.buffer.readVarint32(); + var contactInfoBuffer = this.buffer.slice(this.buffer.offset, this.buffer.offset+len); + var contactInfo = textsecure.protobuf.ContactDetails.decode(contactInfoBuffer); this.buffer.skip(len); - var contactInfo = textsecure.protobuf.ContactDetails.decode( - this.buffer.slice(this.buffer.offset, len) - ); - var attachmentLen = contactInfo.avatar.length; - contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, attachmentLen).toArrayBuffer(true /* copy? */); + 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; diff --git a/libtextsecure/contacts_parser.js b/libtextsecure/contacts_parser.js index 9790e458..4c954d39 100644 --- a/libtextsecure/contacts_parser.js +++ b/libtextsecure/contacts_parser.js @@ -2,19 +2,21 @@ * vim: ts=4:sw=4:expandtab */ function ContactBuffer(arrayBuffer) { - this.buffer = new dCodeIO.ByteBuffer(arrayBuffer); + this.buffer = new dcodeIO.ByteBuffer(); + this.buffer.append(arrayBuffer); + this.buffer.offset = 0; + this.buffer.limit = arrayBuffer.byteLength; } ContactBuffer.prototype = { constructor: ContactBuffer, readContact: function() { try { var len = this.buffer.readVarint32(); + var contactInfoBuffer = this.buffer.slice(this.buffer.offset, this.buffer.offset+len); + var contactInfo = textsecure.protobuf.ContactDetails.decode(contactInfoBuffer); this.buffer.skip(len); - var contactInfo = textsecure.protobuf.ContactDetails.decode( - this.buffer.slice(this.buffer.offset, len) - ); - var attachmentLen = contactInfo.avatar.length; - contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, attachmentLen).toArrayBuffer(true /* copy? */); + 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; diff --git a/libtextsecure/test/contacts_parser_test.js b/libtextsecure/test/contacts_parser_test.js new file mode 100644 index 00000000..57de4835 --- /dev/null +++ b/libtextsecure/test/contacts_parser_test.js @@ -0,0 +1,61 @@ +/* vim: ts=4:sw=4 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +'use strict'; + +describe("ContactsBuffer", function() { + function getTestBuffer() { + var buffer = new dcodeIO.ByteBuffer(); + var avatarBuffer = new dcodeIO.ByteBuffer(); + var avatarLen = 255; + for (var i=0; i < avatarLen; ++i) { + avatarBuffer.writeUint8(i); + } + avatarBuffer.limit = avatarBuffer.offset; + avatarBuffer.offset = 0; + var contactInfo = new textsecure.protobuf.ContactDetails({ + name: "Zero Cool", + number: "+10000000000", + avatar: { contentType: "image/jpg", length: avatarLen } + }); + var contactInfoBuffer = contactInfo.encode().toArrayBuffer(); + + for (var i = 0; i < 3; ++i) { + buffer.writeVarint32(contactInfoBuffer.byteLength); + buffer.append(contactInfoBuffer); + buffer.append(avatarBuffer.clone()); + } + + 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(); + assert.strictEqual(contact.name, "Zero Cool"); + assert.strictEqual(contact.number, "+10000000000"); + assert.strictEqual(contact.avatar.contentType, "image/jpg"); + var avatarBytes = new Uint8Array(contact.avatar.data); + for (var j=0; j < 255; ++j) { + assert.strictEqual(avatarBytes[j],j); + } + } + }); +}); diff --git a/libtextsecure/test/index.html b/libtextsecure/test/index.html index e740fdae..312d959b 100644 --- a/libtextsecure/test/index.html +++ b/libtextsecure/test/index.html @@ -46,11 +46,13 @@ + +