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