string-tests

pull/1199/head
Vincent 5 years ago
parent 32015eeab6
commit f4fdd42ef3

@ -1,7 +1,7 @@
import ByteBuffer from 'bytebuffer';
type Encoding = 'base64' | 'hex' | 'binary' | 'utf8';
type BufferType = ByteBuffer | Buffer | ArrayBuffer | Uint8Array;
export type Encoding = 'base64' | 'hex' | 'binary' | 'utf8';
export type BufferType = ByteBuffer | Buffer | ArrayBuffer | Uint8Array;
/**
* Take a string value with the given encoding and converts it to an `ArrayBuffer`.

@ -48,7 +48,7 @@ describe('Promise Utils', () => {
const completionSpy = sandbox.spy();
const task = (_done: any) => undefined;
const promise = PromiseUtils.poll(task, {timeout: 1});
const promise = PromiseUtils.poll(task, { timeout: 1 });
await expect(promise).to.be.rejectedWith('Periodic check timeout');
expect(pollSpy.callCount).to.equal(1);
@ -70,7 +70,7 @@ describe('Promise Utils', () => {
}
};
const promise = PromiseUtils.poll(task, {timeout, interval});
const promise = PromiseUtils.poll(task, { timeout, interval });
await expect(promise).to.be.fulfilled;
expect(pollSpy.callCount).to.equal(1);

@ -1,11 +1,9 @@
import chai from 'chai';
import * as sinon from 'sinon';
import ByteBuffer from 'bytebuffer';
import { PubKey } from '../../../session/types/';
import { SyncMessageUtils } from '../../../session/utils/';
import { SyncMessage } from '../../../session/messages/outgoing';
import { TestUtils } from '../../test-utils';
import { UserUtil } from '../../../util';
// Can't import type as StringUtils.Encoding
import { Encoding } from '../../../session/utils/String';
import { StringUtils } from '../../../session/utils/';
// tslint:disable-next-line: no-require-imports no-var-requires
const chaiAsPromised = require('chai-as-promised');
@ -14,9 +12,104 @@ chai.use(chaiAsPromised);
const { expect } = chai;
describe('String Utils', () => {
describe('encode', () => {
it('', async () => {
//
it('can encode to base64', async () => {
const testString = 'AAAAAAAAAA';
const encoded = StringUtils.encode(testString, 'base64');
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.be.greaterThan(0);
});
it('can encode to hex', async () => {
const testString = 'AAAAAAAAAA';
const encoded = StringUtils.encode(testString, 'hex');
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.be.greaterThan(0);
});
it('wont encode invalid hex', async () => {
const testString = 'ZZZZZZZZZZ';
const encoded = StringUtils.encode(testString, 'hex');
expect(encoded.byteLength).to.equal(0);
});
it('can encode to binary', async () => {
const testString = 'AAAAAAAAAA';
const encoded = StringUtils.encode(testString, 'binary');
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.be.greaterThan(0);
});
it('can encode to utf8', async () => {
const testString = 'AAAAAAAAAA';
const encoded = StringUtils.encode(testString, 'binary');
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.be.greaterThan(0);
});
it('can encode empty string', async () => {
const testString = '';
expect(testString).to.have.length(0);
const allEncodedings = ([
'base64',
'hex',
'binary',
'utf8',
] as Array<Encoding>).map(e => StringUtils.encode(testString, e));
allEncodedings.forEach(encoded => {
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.equal(0);
});
});
it('can encode huge string', async () => {
const testString = Array(Math.pow(2, 16)).fill('0').join('');
const allEncodedings = ([
'base64',
'hex',
'binary',
'utf8',
] as Array<Encoding>).map(e => StringUtils.encode(testString, e));
allEncodedings.forEach(encoded => {
expect(encoded instanceof ArrayBuffer).to.equal(true, 'a buffer was not returned from `encode`');
expect(encoded.byteLength).to.be.greaterThan(0);
});
});
it("won't encode illegal string length in hex", async () => {
const testString = 'A';
const encode = () => StringUtils.encode(testString, 'hex');
// Ensure string is odd length
expect(testString.length % 2).to.equal(1);
expect(encode).to.throw('Illegal str: Length not a multiple of 2');
});
it('can convert obscure string', async () => {
const testString = '↓←¶ᶑᵶ⅑⏕→⅓‎ᵹ⅙ᵰᶎ⅔⅗↔‌ᶈ⅞⸜ᶊᵴᶉ↉¥ᶖᶋᶃᶓ⏦ᵾᶂᶆ↕⸝ᶔᶐ⏔£⏙⅐⅒ᶌ⁁ᶘᶄᶒᶸ⅘‏⅚⅛ᶙᶇᶕᶀ↑ᵿ⏠ᶍᵯ⏖⏗⅜ᶚᶏ⁊‍ᶁᶗᵽᵼ⅝⏘⅖⅕⏡';
// Not valid hex format; try test the others
const encodings = ([
'base64',
'binary',
'utf8',
] as Array<Encoding>);
encodings.forEach(encoding => {
const encoded = StringUtils.encode(testString, encoding);
expect(encoded instanceof ArrayBuffer).to.equal(true, `a buffer was not returned using encoding: '${encoding}'`);
expect(encoded.byteLength).to.be.greaterThan(0);
});
});
});

@ -10,9 +10,7 @@ import {
ClosedGroupChatMessage,
OpenGroupMessage,
} from '../../session/messages/outgoing';
import {
ConversationAttributes,
} from '../../../js/models/conversation';
import { ConversationAttributes } from '../../../js/models/conversation';
import { TestUtils } from '.';
const globalAny: any = global;

Loading…
Cancel
Save