signal-desktop/libtextsecure/test/contacts_parser_test.js

107 lines
3.5 KiB
JavaScript
Raw Normal View History

'use strict';
2018-05-02 16:51:22 +00:00
describe('ContactBuffer', function() {
function getTestBuffer() {
var buffer = new dcodeIO.ByteBuffer();
var avatarBuffer = new dcodeIO.ByteBuffer();
var avatarLen = 255;
2018-05-02 16:51:22 +00:00
for (var i = 0; i < avatarLen; ++i) {
avatarBuffer.writeUint8(i);
}
avatarBuffer.limit = avatarBuffer.offset;
avatarBuffer.offset = 0;
var contactInfo = new textsecure.protobuf.ContactDetails({
2018-05-02 16:51:22 +00:00
name: 'Zero Cool',
number: '+10000000000',
avatar: { contentType: 'image/jpeg', 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.limit = buffer.offset;
buffer.offset = 0;
return buffer.toArrayBuffer();
}
2018-05-02 16:51:22 +00:00
it('parses an array buffer of contacts', function() {
var arrayBuffer = getTestBuffer();
var contactBuffer = new ContactBuffer(arrayBuffer);
var contact = contactBuffer.next();
var count = 0;
while (contact !== undefined) {
count++;
2018-05-02 16:51:22 +00:00
assert.strictEqual(contact.name, 'Zero Cool');
assert.strictEqual(contact.number, '+10000000000');
assert.strictEqual(contact.avatar.contentType, 'image/jpeg');
assert.strictEqual(contact.avatar.length, 255);
assert.strictEqual(contact.avatar.data.byteLength, 255);
var avatarBytes = new Uint8Array(contact.avatar.data);
2018-05-02 16:51:22 +00:00
for (var j = 0; j < 255; ++j) {
assert.strictEqual(avatarBytes[j], j);
}
contact = contactBuffer.next();
}
assert.strictEqual(count, 3);
});
});
2018-05-02 16:51:22 +00:00
describe('GroupBuffer', function() {
function getTestBuffer() {
var buffer = new dcodeIO.ByteBuffer();
var avatarBuffer = new dcodeIO.ByteBuffer();
var avatarLen = 255;
2018-05-02 16:51:22 +00:00
for (var i = 0; i < avatarLen; ++i) {
avatarBuffer.writeUint8(i);
}
avatarBuffer.limit = avatarBuffer.offset;
avatarBuffer.offset = 0;
var groupInfo = new textsecure.protobuf.GroupDetails({
id: new Uint8Array([1, 3, 3, 7]).buffer,
2018-05-02 16:51:22 +00:00
name: 'Hackers',
members: ['cereal', 'burn', 'phreak', 'joey'],
2018-05-02 16:51:22 +00:00
avatar: { contentType: 'image/jpeg', length: avatarLen },
});
var groupInfoBuffer = groupInfo.encode().toArrayBuffer();
for (var i = 0; i < 3; ++i) {
buffer.writeVarint32(groupInfoBuffer.byteLength);
buffer.append(groupInfoBuffer);
buffer.append(avatarBuffer.clone());
}
buffer.limit = buffer.offset;
buffer.offset = 0;
return buffer.toArrayBuffer();
}
2018-05-02 16:51:22 +00:00
it('parses an array buffer of groups', function() {
var arrayBuffer = getTestBuffer();
var groupBuffer = new GroupBuffer(arrayBuffer);
var group = groupBuffer.next();
var count = 0;
while (group !== undefined) {
count++;
2018-05-02 16:51:22 +00:00
assert.strictEqual(group.name, 'Hackers');
assertEqualArrayBuffers(
group.id.toArrayBuffer(),
new Uint8Array([1, 3, 3, 7]).buffer
);
assert.sameMembers(group.members, ['cereal', 'burn', 'phreak', 'joey']);
2018-05-02 16:51:22 +00:00
assert.strictEqual(group.avatar.contentType, 'image/jpeg');
assert.strictEqual(group.avatar.length, 255);
assert.strictEqual(group.avatar.data.byteLength, 255);
var avatarBytes = new Uint8Array(group.avatar.data);
2018-05-02 16:51:22 +00:00
for (var j = 0; j < 255; ++j) {
assert.strictEqual(avatarBytes[j], j);
}
group = groupBuffer.next();
}
assert.strictEqual(count, 3);
});
});