2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2015-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
describe('Helpers', () => {
|
|
|
|
describe('ArrayBuffer->String conversion', () => {
|
|
|
|
it('works', () => {
|
|
|
|
const b = new ArrayBuffer(3);
|
|
|
|
const a = new Uint8Array(b);
|
2018-05-02 16:51:22 +00:00
|
|
|
a[0] = 0;
|
|
|
|
a[1] = 255;
|
|
|
|
a[2] = 128;
|
2020-04-13 17:37:29 +00:00
|
|
|
assert.equal(window.textsecure.utils.getString(b), '\x00\xff\x80');
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2014-11-06 22:16:54 +00:00
|
|
|
});
|
2014-11-20 07:23:43 +00:00
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
describe('stringToArrayBuffer', () => {
|
|
|
|
it('returns ArrayBuffer when passed string', () => {
|
|
|
|
const anArrayBuffer = new ArrayBuffer(1);
|
|
|
|
const typedArray = new Uint8Array(anArrayBuffer);
|
2018-05-02 16:51:22 +00:00
|
|
|
typedArray[0] = 'a'.charCodeAt(0);
|
2020-04-13 17:37:29 +00:00
|
|
|
assertEqualArrayBuffers(
|
|
|
|
window.textsecure.utils.stringToArrayBuffer('a'),
|
|
|
|
anArrayBuffer
|
|
|
|
);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
it('throws an error when passed a non string', () => {
|
|
|
|
const notStringable = [{}, undefined, null, new ArrayBuffer()];
|
|
|
|
notStringable.forEach(notString => {
|
|
|
|
assert.throw(() => {
|
2020-04-13 17:37:29 +00:00
|
|
|
window.textsecure.utils.stringToArrayBuffer(notString);
|
2018-05-02 16:51:22 +00:00
|
|
|
}, Error);
|
2014-11-20 07:23:43 +00:00
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2014-11-20 07:23:43 +00:00
|
|
|
});
|
2014-11-06 22:16:54 +00:00
|
|
|
});
|