Add tests and bug fixes for ContactBuffer
This commit is contained in:
parent
290283f810
commit
316838cfe9
4 changed files with 79 additions and 12 deletions
|
@ -40116,19 +40116,21 @@ window.textsecure.messaging = function() {
|
||||||
* vim: ts=4:sw=4:expandtab
|
* vim: ts=4:sw=4:expandtab
|
||||||
*/
|
*/
|
||||||
function ContactBuffer(arrayBuffer) {
|
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 = {
|
ContactBuffer.prototype = {
|
||||||
constructor: ContactBuffer,
|
constructor: ContactBuffer,
|
||||||
readContact: function() {
|
readContact: function() {
|
||||||
try {
|
try {
|
||||||
var len = this.buffer.readVarint32();
|
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);
|
this.buffer.skip(len);
|
||||||
var contactInfo = textsecure.protobuf.ContactDetails.decode(
|
var attachmentLen = contactInfo.avatar.length.toNumber();
|
||||||
this.buffer.slice(this.buffer.offset, len)
|
contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, this.buffer.offset + attachmentLen).toArrayBuffer(true);
|
||||||
);
|
|
||||||
var attachmentLen = contactInfo.avatar.length;
|
|
||||||
contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, attachmentLen).toArrayBuffer(true /* copy? */);
|
|
||||||
this.buffer.skip(attachmentLen);
|
this.buffer.skip(attachmentLen);
|
||||||
|
|
||||||
return contactInfo;
|
return contactInfo;
|
||||||
|
|
|
@ -2,19 +2,21 @@
|
||||||
* vim: ts=4:sw=4:expandtab
|
* vim: ts=4:sw=4:expandtab
|
||||||
*/
|
*/
|
||||||
function ContactBuffer(arrayBuffer) {
|
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 = {
|
ContactBuffer.prototype = {
|
||||||
constructor: ContactBuffer,
|
constructor: ContactBuffer,
|
||||||
readContact: function() {
|
readContact: function() {
|
||||||
try {
|
try {
|
||||||
var len = this.buffer.readVarint32();
|
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);
|
this.buffer.skip(len);
|
||||||
var contactInfo = textsecure.protobuf.ContactDetails.decode(
|
var attachmentLen = contactInfo.avatar.length.toNumber();
|
||||||
this.buffer.slice(this.buffer.offset, len)
|
contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, this.buffer.offset + attachmentLen).toArrayBuffer(true);
|
||||||
);
|
|
||||||
var attachmentLen = contactInfo.avatar.length;
|
|
||||||
contactInfo.avatar.data = this.buffer.slice(this.buffer.offset, attachmentLen).toArrayBuffer(true /* copy? */);
|
|
||||||
this.buffer.skip(attachmentLen);
|
this.buffer.skip(attachmentLen);
|
||||||
|
|
||||||
return contactInfo;
|
return contactInfo;
|
||||||
|
|
61
libtextsecure/test/contacts_parser_test.js
Normal file
61
libtextsecure/test/contacts_parser_test.js
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
'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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
|
@ -46,11 +46,13 @@
|
||||||
<script type="text/javascript" src="../api.js"></script>
|
<script type="text/javascript" src="../api.js"></script>
|
||||||
<script type="text/javascript" src="../sendmessage.js" data-cover></script>
|
<script type="text/javascript" src="../sendmessage.js" data-cover></script>
|
||||||
<script type="text/javascript" src="../account_manager.js" data-></script>
|
<script type="text/javascript" src="../account_manager.js" data-></script>
|
||||||
|
<script type="text/javascript" src="../contacts_parser.js"></script>
|
||||||
|
|
||||||
<script type="text/javascript" src="fake_api.js"></script>
|
<script type="text/javascript" src="fake_api.js"></script>
|
||||||
<script type="text/javascript" src="helpers_test.js"></script>
|
<script type="text/javascript" src="helpers_test.js"></script>
|
||||||
<script type="text/javascript" src="websocket-resources_test.js"></script>
|
<script type="text/javascript" src="websocket-resources_test.js"></script>
|
||||||
<script type="text/javascript" src="storage_test.js"></script>
|
<script type="text/javascript" src="storage_test.js"></script>
|
||||||
|
<script type="text/javascript" src="contacts_parser_test.js"></script>
|
||||||
<script type="text/javascript" src="generate_keys_test.js"></script>
|
<script type="text/javascript" src="generate_keys_test.js"></script>
|
||||||
<script type="text/javascript" src="websocket_test.js"></script>
|
<script type="text/javascript" src="websocket_test.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|
Loading…
Reference in a new issue